Programmatically adding page permissions
Permalink
I am trying to set page permissions programmatically in a package controller and have based this on the docs here https://documentation.concrete5.org/developers/permissions-access-se... . The docs are giving an example for a file object so I'm not sure if it's different for a page? I almost have it but for some reason this code is overwriting every page in the sitemap with the new permissions rather than just the page I need. Not sure what I'm doing wrong?
This is what I have:
This is what I have:
use Concrete\Core\Permission\Key\Key as PermissionKey; use Concrete\Core\Permission\Access\Entity\GroupEntity as GroupPermissionAccessEntity; use Concrete\Core\User\Group\Group as UserGroup; // get my page object $page = Page::getByID(684); // override permissions to Manual $page->setPermissionsToManualOverride(); // remove Guest access $pk = PermissionKey::getByHandle('view_page'); $pk->setPermissionObject($page); $pa = $pk->getPermissionAccessObject(); $pe = GroupPermissionAccessEntity::getOrCreate(UserGroup::getByID(GUEST_GROUP_ID)); $pa->removeListItem($pe); // enable access by a group $g = UserGroup::getByName('My Custom Group Name');
Viewing 15 lines of 21 lines. View entire code block.
Thanks, that works. A bit different than the offical docs. In case anyone else needs this. You have to use the following as well:
and make sure you get your group object too:
and make sure you get your group object too:
$group = Group::getByName('My Group Name'); $entity = GroupEntity::getOrCreate($group);
Here's a working code for removing guest access to an Account sub-page and setting permissions for it: