Get array of user group IDs with view access to a page

I want to get a list of user group IDs that have view access to a page.

I DON’T want to check if the current user has view access to the page.

Should be fairly simple, right? But the permissions related classes don’t seem to provide an obvious way to do this.

Any pointers gladly received!
Thanks.

I’m not sure if this is the best way or not, but by slightly modifying the examples in the documentation on “Checking Permissions Against Other Users or Groups”, this seems to work:

function getGroupIDsWhichCanViewPage() {

  $groups_can_access_page = [];

  $page = \Page::getCurrentPage();
  
  $key = \Concrete\Core\Permission\Key\Key::getByHandle('view_page');
  $key->setPermissionObject($page);
  $access = $key->getPermissionAccessObject();

  if ($access) {
    $groups_list = new \Concrete\Core\User\Group\GroupList();
    $groups_list->includeAllGroups();
    $groups_results = $groups_list->getResults();

    foreach ($groups_results as $group) {
      $group_entity = \Concrete\Core\Permission\Access\Entity\GroupEntity::getOrCreate($group);
      if ($access->validateAccessEntities([$group_entity])) {
        $groups_can_access_page[] = $group->getGroupId();
      }
    }

  }
  return $groups_can_access_page;

}
2 Likes

Hey Myq, that’s awesome, works great.

Needed some class referencing:

use Concrete\Core\Permission\Key\Key;
use Concrete\Core\Permission\Access\Entity\GroupEntity;

Thanks so much!

@simonchilton You should mark @Myq answer as a solution for future reference,

2 Likes