You are here

class BadgeAccessCheck in User Badges 8

Defines the access control handler for the badge listing.

Hierarchy

Expanded class hierarchy of BadgeAccessCheck

1 string reference to 'BadgeAccessCheck'
user_badges.services.yml in ./user_badges.services.yml
user_badges.services.yml
1 service uses BadgeAccessCheck
user_badges.service in ./user_badges.services.yml
\Drupal\user_badges\Access\BadgeAccessCheck

File

src/Access/BadgeAccessCheck.php, line 13

Namespace

Drupal\user_badges\Access
View source
class BadgeAccessCheck implements AccessInterface {

  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * Constructs the access check.
   *
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   */
  public function __construct(RouteMatchInterface $route_match) {
    $this->routeMatch = $route_match;
  }

  /**
   * Checks access to the badge listing pages.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return \Drupal\Core\Access\AccessResultInterface
   *   The access result.
   */
  public function access(AccountInterface $account) {
    $badge_admin = $account
      ->hasPermission('administer badge entities');
    if ($badge_admin) {
      return AccessResult::allowed();
    }
    else {
      $user_from_url = $this->routeMatch
        ->getParameter('user');

      // GetParameter returns the entity and sometimes returns a string.
      if (!is_string($user_from_url)) {
        $user_from_url = $user_from_url
          ->id();
      }
      return AccessResult::allowedIf($account
        ->id() && $account
        ->id() == $user_from_url && $account
        ->hasPermission('re-order badges'))
        ->cachePerPermissions()
        ->cachePerUser();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BadgeAccessCheck::$routeMatch protected property The current route match.
BadgeAccessCheck::access public function Checks access to the badge listing pages.
BadgeAccessCheck::__construct public function Constructs the access check.