You are here

class MenuAdminPerMenuAccess in Menu Admin per Menu 8

Checks access for displaying administer menu pages.

Hierarchy

Expanded class hierarchy of MenuAdminPerMenuAccess

2 files declare their use of MenuAdminPerMenuAccess
MenuAdminPerMenuController.php in src/Controller/MenuAdminPerMenuController.php
MenuSelection.php in src/Plugin/EntityReferenceSelection/MenuSelection.php
1 string reference to 'MenuAdminPerMenuAccess'
menu_admin_per_menu.services.yml in ./menu_admin_per_menu.services.yml
menu_admin_per_menu.services.yml
1 service uses MenuAdminPerMenuAccess
menu_admin_per_menu.allowed_menus in ./menu_admin_per_menu.services.yml
Drupal\menu_admin_per_menu\Access\MenuAdminPerMenuAccess

File

src/Access/MenuAdminPerMenuAccess.php, line 15

Namespace

Drupal\menu_admin_per_menu\Access
View source
class MenuAdminPerMenuAccess implements MenuAdminPerMenuAccessInterface {

  /**
   * {@inheritdoc}
   */
  public function getPerMenuPermissions(AccountInterface $account) {
    $perms_menu =& drupal_static(__FUNCTION__, []);
    if (!isset($perms_menu[$account
      ->id()])) {
      $menus = menu_ui_get_menus();
      foreach ($menus as $name => $title) {
        $permission = 'administer ' . $name . ' menu items';
        if ($account
          ->hasPermission($permission)) {
          $perms_menu[$account
            ->id()][$permission] = $name;
        }
      }
      $user_perms_menu = $perms_menu[$account
        ->id()] ?? [];
      \Drupal::moduleHandler()
        ->alter('menu_admin_per_menu_get_permissions', $user_perms_menu, $account);
      $perms_menu[$account
        ->id()] = $user_perms_menu;
    }
    return $perms_menu[$account
      ->id()] ?? [];
  }

  /**
   * {@inheritdoc}
   */
  public function menusOverviewAccess(AccountInterface $account) {
    if ($account
      ->hasPermission('administer menu')) {
      return AccessResult::allowed();
    }
    $permissions = $this::getPerMenuPermissions($account);
    if ($account
      ->hasPermission('administer menu') || $permissions) {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  public function menuAccess(AccountInterface $account, Menu $menu) {
    $permission = 'administer ' . $menu
      ->get('id') . ' menu items';
    $permissions = $this::getPerMenuPermissions($account);
    if ($account
      ->hasPermission('administer menu') || isset($permissions[$permission])) {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  public function menuItemAccess(AccountInterface $account, MenuLinkContent $menu_link_content = NULL) {
    if (!$menu_link_content instanceof MenuLinkContent) {
      return AccessResult::neutral();
    }
    $permission = 'administer ' . $menu_link_content
      ->getMenuName() . ' menu items';
    $permissions = $this::getPerMenuPermissions($account);
    if ($account
      ->hasPermission('administer menu') || isset($permissions[$permission])) {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

  /**
   * {@inheritdoc}
   */
  public function menuLinkAccess(AccountInterface $account, MenuLinkInterface $menu_link_plugin = NULL) {
    if (!$menu_link_plugin instanceof MenuLinkInterface) {
      return AccessResult::neutral();
    }
    $permission = 'administer ' . $menu_link_plugin
      ->getMenuName() . ' menu items';
    $permissions = $this::getPerMenuPermissions($account);
    if ($account
      ->hasPermission('administer menu') || isset($permissions[$permission])) {
      return AccessResult::allowed();
    }
    return AccessResult::neutral();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MenuAdminPerMenuAccess::getPerMenuPermissions public function Return array of all specific menu permissions. Overrides MenuAdminPerMenuAccessInterface::getPerMenuPermissions
MenuAdminPerMenuAccess::menuAccess public function A custom access check for menu page and add link page. Overrides MenuAdminPerMenuAccessInterface::menuAccess
MenuAdminPerMenuAccess::menuItemAccess public function A custom access check for menu items page. Overrides MenuAdminPerMenuAccessInterface::menuItemAccess
MenuAdminPerMenuAccess::menuLinkAccess public function A custom access check for menu link page. Overrides MenuAdminPerMenuAccessInterface::menuLinkAccess
MenuAdminPerMenuAccess::menusOverviewAccess public function A custom access check for menu overview page. Overrides MenuAdminPerMenuAccessInterface::menusOverviewAccess