You are here

public function GroupOwnsMenuContentAccessChecker::access in Group Content Menu 8

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.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Access/GroupOwnsMenuContentAccessChecker.php, line 55

Class

GroupOwnsMenuContentAccessChecker
Determines access to routes.

Namespace

Drupal\group_content_menu\Access

Code

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

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

  /** @var \Drupal\group\Entity\GroupContentInterface[] $group_contents */
  $group_contents = $this->entityTypeManager
    ->getStorage('group_content')
    ->loadByEntity($parameters
    ->get('group_content_menu'));
  $group_content = reset($group_contents);
  if (!$group_content) {
    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();
  }

  // Don't interfere if the group content isn't a real group content entity.
  if (!$group_content instanceof GroupContentInterface) {
    return AccessResult::neutral();
  }

  // If we have a group and group content, see if the owner matches.
  $group_owns_content = $group_content
    ->getGroup()
    ->id() === $group
    ->id();

  // Only allow access if the group content is owned by the group and
  // _group_menu_owns_content is set to TRUE or the other way around.
  return AccessResult::allowedIf($group_owns_content xor !$must_own_content);
}