You are here

class SynchronizedGroupPermissionCalculator in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Access/SynchronizedGroupPermissionCalculator.php \Drupal\group\Access\SynchronizedGroupPermissionCalculator

Calculates group permissions for an account.

Hierarchy

Expanded class hierarchy of SynchronizedGroupPermissionCalculator

1 string reference to 'SynchronizedGroupPermissionCalculator'
group.services.yml in ./group.services.yml
group.services.yml
1 service uses SynchronizedGroupPermissionCalculator
group_permission.synchronized_calculator in ./group.services.yml
Drupal\group\Access\SynchronizedGroupPermissionCalculator

File

src/Access/SynchronizedGroupPermissionCalculator.php, line 12

Namespace

Drupal\group\Access
View source
class SynchronizedGroupPermissionCalculator extends GroupPermissionCalculatorBase {

  /**
   * The synchronized roles depend on which user roles you have, so we need to
   * vary the calculated permissions by the user.roles cache context.
   */
  const OUTSIDER_CACHE_CONTEXTS = [
    'user.roles',
  ];

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The group role synchronizer service.
   *
   * @var \Drupal\group\GroupRoleSynchronizerInterface
   */
  protected $groupRoleSynchronizer;

  /**
   * Constructs a SynchronizedGroupPermissionCalculator object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\group\GroupRoleSynchronizerInterface $group_role_synchronizer
   *   The group role synchronizer service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, GroupRoleSynchronizerInterface $group_role_synchronizer) {
    $this->entityTypeManager = $entity_type_manager;
    $this->groupRoleSynchronizer = $group_role_synchronizer;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GroupPermissionCalculatorBase::calculateAnonymousPermissions public function Calculates the anonymous group permissions. Overrides GroupPermissionCalculatorInterface::calculateAnonymousPermissions 1
GroupPermissionCalculatorBase::calculateMemberPermissions public function Calculates the member group permissions for an account. Overrides GroupPermissionCalculatorInterface::calculateMemberPermissions 1
GroupPermissionCalculatorInterface::ANONYMOUS_CACHE_CONTEXTS constant The cache contexts that should always be present on anonymous permissions.
GroupPermissionCalculatorInterface::MEMBER_CACHE_CONTEXTS constant The cache contexts that should always be present on member permissions. 1
SynchronizedGroupPermissionCalculator::$entityTypeManager protected property The entity type manager.
SynchronizedGroupPermissionCalculator::$groupRoleSynchronizer protected property The group role synchronizer service.
SynchronizedGroupPermissionCalculator::calculateOutsiderPermissions public function Calculates the outsider group permissions for an account. Overrides GroupPermissionCalculatorBase::calculateOutsiderPermissions
SynchronizedGroupPermissionCalculator::OUTSIDER_CACHE_CONTEXTS constant The synchronized roles depend on which user roles you have, so we need to vary the calculated permissions by the user.roles cache context. Overrides GroupPermissionCalculatorInterface::OUTSIDER_CACHE_CONTEXTS
SynchronizedGroupPermissionCalculator::__construct public function Constructs a SynchronizedGroupPermissionCalculator object.