You are here

public function OgMenuInstanceController::addLinkAccess in Organic Groups Menu (OG Menu) 8

Access callback for the "add link" route.

Parameters

\Drupal\og_menu\Entity\OgMenuInstance $ogmenu_instance: The OG Menu instance for which to determine access.

\Drupal\Core\Session\AccountInterface $account: The user for which to determine access.

Return value

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

1 string reference to 'OgMenuInstanceController::addLinkAccess'
og_menu.routing.yml in ./og_menu.routing.yml
og_menu.routing.yml

File

src/Controller/OgMenuInstanceController.php, line 141
Contains Drupal\og_menu\Controller\OgMenuInstanceController.

Class

OgMenuInstanceController
Class OgMenuInstanceController.

Namespace

Drupal\og_menu\Controller

Code

public function addLinkAccess(OgMenuInstance $ogmenu_instance, AccountInterface $account) {

  // @todo Add per-bundle permissions. You might want to give users access to
  //   add links to a particular OG Menu, but not all of them.
  $permission = 'add new links to og menu instance entities';

  // If the user has the global permission, allow access immediately.
  if ($account
    ->hasPermission($permission)) {
    return AccessResult::allowed();
  }

  // Retrieve the associated group from the menu instance.
  $og_groups = $this->membershipManager
    ->getGroups($ogmenu_instance);

  // A menu should only be associated with a single group.
  $group_entity_type = key($og_groups);
  $og_group = reset($og_groups[$group_entity_type]);

  // If the group could not be found, access could not be determined.
  if (empty($og_group)) {
    return AccessResult::neutral();
  }
  $membership = $this->membershipManager
    ->getMembership($og_group, $account
    ->id());

  // If the membership can not be found, access can not be determined.
  if (empty($membership)) {
    return AccessResult::neutral();
  }
  return AccessResult::allowedIf($membership
    ->hasPermission($permission));
}