You are here

public function DefaultGroupPermissionCalculator::calculateMemberPermissions in Group 2.0.x

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

Calculates the member group permissions for an account.

Parameters

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

Return value

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

Overrides GroupPermissionCalculatorBase::calculateMemberPermissions

File

src/Access/DefaultGroupPermissionCalculator.php, line 111

Class

DefaultGroupPermissionCalculator
Calculates group permissions for an account.

Namespace

Drupal\group\Access

Code

public function calculateMemberPermissions(AccountInterface $account) {
  $calculated_permissions = new RefinableCalculatedGroupPermissions();

  // The member permissions need to be recalculated whenever the user is added
  // to or removed from a group.
  $calculated_permissions
    ->addCacheTags([
    'group_content_list:plugin:group_membership:entity:' . $account
      ->id(),
  ]);
  foreach ($this->membershipLoader
    ->loadByUser($account) as $group_membership) {

    // If the member's roles change, so do the permissions.
    $calculated_permissions
      ->addCacheableDependency($group_membership);
    $permission_sets = [];
    foreach ($group_membership
      ->getRoles() 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, $group_membership
      ->getGroup()
      ->id(), $permissions);
    $calculated_permissions
      ->addItem($item);
  }
  return $calculated_permissions;
}