Skip to content

Commit

Permalink
Merge pull request #58 from heyday/fixes/56-permissions
Browse files Browse the repository at this point in the history
fix: allow extensions to manage menu permissions correctly (56)
  • Loading branch information
wilr authored Jul 10, 2023
2 parents 29f8134 + 800a0e0 commit 7a20281
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
30 changes: 25 additions & 5 deletions src/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,9 @@ class MenuItem extends DataObject implements PermissionProvider
* @var array
*/
private static $db = [
// If you want to customise the MenuTitle use this field - leaving blank will use MenuTitle of associated Page
'MenuTitle' => 'Varchar(255)',
// This field is used for external links (picking a page from the dropdown will overwrite this link)
'Link' => 'Text',
// Sort order
'Sort' => 'Int',
// Can be used as a check for adding target="_blank"
'IsNewWindow' => 'Boolean'
];

Expand Down Expand Up @@ -84,7 +80,13 @@ public function providePermissions()
*/
public function canCreate($member = null, $context = [])
{
return Permission::check('MANAGE_MENU_ITEMS');
$extended = $this->extendedCan(__FUNCTION__, $member);

if ($extended !== null) {
return $extended;
}

return Permission::checkMember($member, 'MANAGE_MENU_ITEMS');
}

/**
Expand All @@ -93,6 +95,12 @@ public function canCreate($member = null, $context = [])
*/
public function canDelete($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);

if ($extended !== null) {
return $extended;
}

return Permission::check('MANAGE_MENU_ITEMS');
}

Expand All @@ -102,6 +110,12 @@ public function canDelete($member = null)
*/
public function canEdit($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);

if ($extended !== null) {
return $extended;
}

return Permission::check('MANAGE_MENU_ITEMS');
}

Expand All @@ -111,6 +125,12 @@ public function canEdit($member = null)
*/
public function canView($member = null)
{
$extended = $this->extendedCan(__FUNCTION__, $member);

if ($extended !== null) {
return $extended;
}

return Permission::check('MANAGE_MENU_ITEMS');
}

Expand Down
32 changes: 17 additions & 15 deletions src/MenuSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ public function validate()
*/
public function canCreate($member = null, $context = [])
{
if (Permission::check('MANAGE_MENU_SETS')) {
return true;
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}

return parent::canCreate($member, $context);
return Permission::check('MANAGE_MENU_SETS');
}

/**
Expand All @@ -104,19 +105,17 @@ public function canCreate($member = null, $context = [])
*/
public function canDelete($member = null)
{
$canDelete = parent::canDelete($member);

// Backwards compatibility for duplicate default sets
$existing = MenuManagerTemplateProvider::MenuSet($this->Name);
$isDuplicate = $existing && $existing->ID !== $this->ID;

if ($this->isDefaultSet() && !$isDuplicate) {
// Default menu's cannot be deleted
$canDelete = false;
return false;
}

if ($canDelete !== null) {
return $canDelete;
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}

return Permission::check('MANAGE_MENU_SETS');
Expand All @@ -128,11 +127,12 @@ public function canDelete($member = null)
*/
public function canEdit($member = null)
{
if (Permission::check('MANAGE_MENU_SETS') || Permission::check('MANAGE_MENU_ITEMS')) {
return true;
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}

return parent::canEdit($member);
return (Permission::check('MANAGE_MENU_SETS') || Permission::check('MANAGE_MENU_ITEMS'));
}

/**
Expand All @@ -141,11 +141,13 @@ public function canEdit($member = null)
*/
public function canView($member = null)
{
if (Permission::check('MANAGE_MENU_SETS') || Permission::check('MANAGE_MENU_ITEMS')) {
return true;
$extended = $this->extendedCan(__FUNCTION__, $member);
if ($extended !== null) {
return $extended;
}

return parent::canView($member);

return (Permission::check('MANAGE_MENU_SETS') || Permission::check('MANAGE_MENU_ITEMS'));
}


Expand Down

0 comments on commit 7a20281

Please sign in to comment.