This is great Remo. Thanks for putting this together. Very nice.
Thanks Chad! Glad you like it
Is this website run on WordPress or Concrete5? Looks like WP.
Despite the fact that I’m writing a lot about concrete5, I’m using WordPress for this site. If it’s just about blogging, WordPress feels still more natural to work with. At least to me 😉
As always, thank you for very useful tutorials Remo! Those more advanced topics are priceless when you need them.
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.
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 $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);
8 Comments
This is great Remo. Thanks for putting this together. Very nice.
Thanks Chad! Glad you like it
Is this website run on WordPress or Concrete5? Looks like WP.
Despite the fact that I’m writing a lot about concrete5, I’m using WordPress for this site. If it’s just about blogging, WordPress feels still more natural to work with. At least to me 😉
As always, thank you for very useful tutorials Remo! Those more advanced topics are priceless when you need them.
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?
I think you should be able to duplicate the permission assignment object as shown here https://legacy-documentation.concrete5.org/tutorials/programmatically-setting-advanced-permissions
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;
}