This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

I needed a way to discover whether a given user or group was able to view certain pages. I could not find a solution online. It appears that a lot of the code within the core appears to be hard coded to view permissions only for the current logged in user. This was no use as I was always going to be logged in as Administrator. Anyway, I eventually found this solution and I thought I should share it for others who need to do the same.

I defined this method on a subclass of Page however it can work standalone:

public function canGuestView() {
  // We the view page permission...
  $pk = PagePermissionKey::getByHandle("view_page");
  // Regarding the current page (this)...
  // Attempt to get the PermissionAccessObject
  $pa = $pk->getPermissionAccessObject();
  // If it doesn't exist, assume restrictions haven't been defined and grant access (black list approach rather than white list)
  if ($pa == NULL) return true;
  // Get the access levels defined for a particular user (including it's groups)...
  //$accessEntities = $u->getUserAccessEntityObjects(); // Commented as wanted group rather than user
  // Get the access levels defined only for the "Guest" group, basically anonymous
  $accessEntities = [GroupPermissionAccessEntity::getOrCreate(Group::getByID(GUEST_GROUP_ID))];
  // Test against these permissions
  return $pa->validateAccessEntities($accessEntities);

To make this work standalone just modify $this to take a parameter instead. Also see commented line if you want to test for a user (and their groups) instead of a just a group.

Loading Conversation