group-8.x-1.x-dev/src/Access/GroupMemberAccessCheck.php
src/Access/GroupMemberAccessCheck.php
<?php
namespace Drupal\group\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Entity\GroupInterface;
use Symfony\Component\Routing\Route;
/**
* Determines access to routes based on whether a user is a member of a group.
*/
class GroupMemberAccessCheck implements AccessInterface {
/**
* Checks access.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check against.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The parametrized route.
* @param \Drupal\Core\Session\AccountInterface $account
* The account to check access for.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
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);
}
}
