(English) concrete5 – Working with custom Permission Keys

Leider ist der Eintrag nur auf English verfügbar.




8 Comments

This example was very helpful to me, but there is one thing I’m having an issue with.
The assignment sequence, lines 33 to 40 seems to replace any currently assigned groups.
I can add multiple groups by repeating line 38 for each group object. So, If i could get a list of currently assigned groups, I could add them back in.

However I do not know how to retrieve a list of such groups. I see how i might do that from the database, but I’d rather stick to the API.

Any suggestions?

Thanks Remo. This seems to be on the right track but still having issues. In my adaptation of the code examples you give and cite. $pk->getPermissionAccessID() returns null, causing $paGlobal = PermissionAccess::getByID($accessId, $pk) also to return null.

Here is my full example

/**
* @param $permissionKey string handle for permission
* @param $roleName string handle for group
* @param bool $delete revoke if true, assign if false, default false
* @return bool
*/
private function assignPermissionGroup( $permissionKey, $roleName,$delete=false)
{
$group = Group::getByName($roleName);
if (empty($group)) {
return false;
}

/**
* @var $pkObject \Concrete\Core\Permission\Key\Key
*/
$pkObject = \Concrete\Core\Permission\Key\Key::getByHandle($permissionKey);

/**
* @var $permissionAssignment \Concrete\Core\Permission\Assignment\Assignment
*/
$permissionAssignment = $pkObject->getPermissionAssignmentObject();

// Error!! $accessId assigned null;
$accessId = $pkObject->getPermissionAccessID();

/**
* @var $paGlobal PermissionAccess
*
* Warning, $paGlobal must be duplicated per the statement below. Any other methods applied will affect all Task permissions.
*/
$paGlobal = PermissionAccess::getByID($accessId, $pkObject);

/**
* @var $permissionAccess PermissionAccess
*/
$permissionAccess = $paGlobal->duplicate();

/**
* @var $groupEntity GroupPermissionAccessEntity
*/
$groupEntity = GroupPermissionAccessEntity::getOrCreate($group);
if ($groupEntity === null) {
return false;
}

if ($delete) {
$permissionAccess->removeListItem($groupEntity);
} else {
$permissionAccess->addListItem($groupEntity);
}

$permissionAssignment->assignPermissionAccess($permissionAccess);
return true;
}

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *