You are here

public function GroupMemberAccessCheck::access in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Access/GroupMemberAccessCheck.php \Drupal\group\Access\GroupMemberAccessCheck::access()

Checks access.

Parameters

\Symfony\Component\Routing\Route $route: The route to check against.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The account to check access for.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Access/GroupMemberAccessCheck.php, line 30

Class

GroupMemberAccessCheck
Determines access to routes based on whether a user is a member of a group.

Namespace

Drupal\group\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
  $member_only = $route
    ->getRequirement('_group_member') === 'TRUE';

  // Don't interfere if no group was specified.
  $parameters = $route_match
    ->getParameters();
  if (!$parameters
    ->has('group')) {
    return AccessResult::neutral();
  }

  // Don't interfere if the group isn't a real group.
  $group = $parameters
    ->get('group');
  if (!$group instanceof GroupInterface) {
    return AccessResult::neutral();
  }

  // Only allow access if the user is a member of the group and _group_member
  // is set to TRUE or the other way around.
  return AccessResult::allowedIf($group
    ->getMember($account) xor !$member_only);
}