You are here

public function SynchronizedGroupPermissionCalculator::calculateOutsiderPermissions in Group 2.0.x

Same name and namespace in other branches
  1. 8 src/Access/SynchronizedGroupPermissionCalculator.php \Drupal\group\Access\SynchronizedGroupPermissionCalculator::calculateOutsiderPermissions()

Calculates the outsider group permissions for an account.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user account for which to calculate the outsider permissions.

Return value

\Drupal\group\Access\CalculatedGroupPermissionsInterface An object representing the outsider group permissions.

Overrides GroupPermissionCalculatorBase::calculateOutsiderPermissions

File

src/Access/SynchronizedGroupPermissionCalculator.php, line 50

Class

SynchronizedGroupPermissionCalculator
Calculates group permissions for an account.

Namespace

Drupal\group\Access

Code

public function calculateOutsiderPermissions(AccountInterface $account) {
  $calculated_permissions = new RefinableCalculatedGroupPermissions();
  $group_type_storage = $this->entityTypeManager
    ->getStorage('group_type');
  $group_role_storage = $this->entityTypeManager
    ->getStorage('group_role');
  $roles = $account
    ->getRoles(TRUE);
  foreach ($group_type_storage
    ->getQuery()
    ->execute() as $group_type_id) {
    $permission_sets = [];
    $group_role_ids = [];
    foreach ($roles as $role_id) {
      $group_role_ids[] = $this->groupRoleSynchronizer
        ->getGroupRoleId($group_type_id, $role_id);
    }
    if (!empty($group_role_ids)) {

      /** @var \Drupal\group\Entity\GroupRoleInterface $group_role */
      foreach ($group_role_storage
        ->loadMultiple($group_role_ids) as $group_role) {
        $permission_sets[] = $group_role
          ->getPermissions();
        $calculated_permissions
          ->addCacheableDependency($group_role);
      }
    }
    $permissions = $permission_sets ? array_merge(...$permission_sets) : [];
    $item = new CalculatedGroupPermissionsItem(CalculatedGroupPermissionsItemInterface::SCOPE_GROUP_TYPE, $group_type_id, $permissions);
    $calculated_permissions
      ->addItem($item);
  }
  return $calculated_permissions;
}