You are here

abstract class GroupAccessResult in Group 8

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

Extends the AccessResult class with group permission checks.

Hierarchy

Expanded class hierarchy of GroupAccessResult

6 files declare their use of GroupAccessResult
GroupAccessControlHandler.php in src/Entity/Access/GroupAccessControlHandler.php
GroupContentAccessControlHandler.php in src/Plugin/GroupContentAccessControlHandler.php
GroupContentEnablerBase.php in src/Plugin/GroupContentEnablerBase.php
GroupLatestRevisionCheck.php in src/Entity/Access/GroupLatestRevisionCheck.php
GroupMembership.php in src/Plugin/GroupContentEnabler/GroupMembership.php

... See full list

File

src/Access/GroupAccessResult.php, line 12

Namespace

Drupal\group\Access
View source
abstract class GroupAccessResult extends AccessResult {

  /**
   * Allows access if the permission is present, neutral otherwise.
   *
   * @todo Keep an eye on the following with regard to using the current user:
   * - https://www.drupal.org/node/2628870
   * - https://www.drupal.org/node/2667018
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group for which to check a permission.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account for which to check a permission.
   * @param string $permission
   *   The permission to check for.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   If the account has the permission, isAllowed() will be TRUE, otherwise
   *   isNeutral() will be TRUE.
   */
  public static function allowedIfHasGroupPermission(GroupInterface $group, AccountInterface $account, $permission) {
    return static::allowedIf($group
      ->hasPermission($permission, $account))
      ->addCacheContexts([
      'user.group_permissions',
    ]);
  }

  /**
   * Allows access if the permissions are present, neutral otherwise.
   *
   * @todo Keep an eye on the following with regard to using the current user:
   * - https://www.drupal.org/node/2628870
   * - https://www.drupal.org/node/2667018
   *
   * @param \Drupal\group\Entity\GroupInterface $group
   *   The group for which to check permissions.
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The account for which to check permissions.
   * @param array $permissions
   *   The permissions to check.
   * @param string $conjunction
   *   (optional) 'AND' if all permissions are required, 'OR' in case just one.
   *   Defaults to 'AND'.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   If the account has the permissions, isAllowed() will be TRUE, otherwise
   *   isNeutral() will be TRUE.
   */
  public static function allowedIfHasGroupPermissions(GroupInterface $group, AccountInterface $account, array $permissions, $conjunction = 'AND') {
    $access = FALSE;
    if ($conjunction == 'AND' && !empty($permissions)) {
      $access = TRUE;
      foreach ($permissions as $permission) {
        if (!$group
          ->hasPermission($permission, $account)) {
          $access = FALSE;
          break;
        }
      }
    }
    else {
      foreach ($permissions as $permission) {
        if ($group
          ->hasPermission($permission, $account)) {
          $access = TRUE;
          break;
        }
      }
    }
    return static::allowedIf($access)
      ->addCacheContexts([
      'user.group_permissions',
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AccessResult::allowed public static function Creates an AccessResultInterface object with isAllowed() === TRUE.
AccessResult::allowedIf public static function Creates an allowed or neutral access result.
AccessResult::allowedIfHasPermission public static function Creates an allowed access result if the permission is present, neutral otherwise.
AccessResult::allowedIfHasPermissions public static function Creates an allowed access result if the permissions are present, neutral otherwise.
AccessResult::andIf public function Combine this access result with another using AND. Overrides AccessResultInterface::andIf
AccessResult::cachePerPermissions public function Convenience method, adds the "user.permissions" cache context.
AccessResult::cachePerUser public function Convenience method, adds the "user" cache context.
AccessResult::cacheUntilConfigurationChanges Deprecated public function Convenience method, adds the configuration object's cache tag.
AccessResult::cacheUntilEntityChanges Deprecated public function Convenience method, adds the entity's cache tag.
AccessResult::forbidden public static function Creates an AccessResultInterface object with isForbidden() === TRUE.
AccessResult::forbiddenIf public static function Creates a forbidden or neutral access result.
AccessResult::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyTrait::getCacheContexts
AccessResult::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyTrait::getCacheMaxAge
AccessResult::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyTrait::getCacheTags
AccessResult::inheritCacheability public function Inherits the cacheability of the other access result, if any.
AccessResult::isAllowed public function Overrides AccessResultInterface::isAllowed 1
AccessResult::isForbidden public function Overrides AccessResultInterface::isForbidden 1
AccessResult::isNeutral public function Overrides AccessResultInterface::isNeutral 1
AccessResult::neutral public static function Creates an AccessResultInterface object with isNeutral() === TRUE.
AccessResult::orIf public function Combine this access result with another using OR. Overrides AccessResultInterface::orIf
AccessResult::resetCacheContexts public function Resets cache contexts (to the empty array).
AccessResult::resetCacheTags public function Resets cache tags (to the empty array).
AccessResult::setCacheMaxAge public function Sets the maximum age for which this access result may be cached.
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
GroupAccessResult::allowedIfHasGroupPermission public static function Allows access if the permission is present, neutral otherwise.
GroupAccessResult::allowedIfHasGroupPermissions public static function Allows access if the permissions are present, neutral otherwise.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function