8.4+ How to remove guest permissions for page?

Permalink
I need to set permissions to view a page only by a certain group. I guess I have to remove guest permissions from that page and add group permissions:
$page = Page::getByPath('/account/my_page');
if (is_object($page) && !$page->isError()) {
    $key = PermissionKey::getByHandle('view_page');
    $key->setPermissionObject($page);
    $access = $key->getPermissionAccessObject();
    if ($access) {
        $entity = GroupEntity::getOrCreate(Group::getByID(GUEST_GROUP_ID));
        $access->removeListItem($entity);
    }
    $page->assignPermissions($group, ['view_page']);
}

But this code removes guest permissions for ALL pages, not just my_page. What's wrong here?

Thank you.

linuxoid
 
linuxoid replied on at Permalink Reply
linuxoid
Who would have thought I'd find the info in Documentation for v5.6 while documentation for v8 doesn't works (not complete)?!

Here it is:
$page = Page::getByPath('/account/my_page');
if (is_object($page) && !$page->isError()) {
    $page->setPermissionsToManualOverride();
    $page->setPermissionsInheritanceToOverride();
    $key = PermissionKey::getByHandle('view_page');
    $key->setPermissionObject($page);
    $access_obj = Access::getByID($key->getPermissionAccessID(), $key);
    $access = $access_obj->duplicate();
    $entity = GroupEntity::getOrCreate($group);
    $access->addListItem($entity, false, 10);
    $entity = GroupEntity::getOrCreate(Group::getByID(GUEST_GROUP_ID));
    $access->removeListItem($entity);
    $access->save(array('paID' => $access->getPermissionAccessID()));
    $assignment = $key->getPermissionAssignmentObject();
    $assignment->assignPermissionAccess($access);