You are here

class RoleAccessCheck in Examples for Developers 8

Same name and namespace in other branches
  1. 3.x modules/menu_example/src/Access/RoleAccessCheck.php \Drupal\menu_example\Access\RoleAccessCheck

Determines access to routes based on roles.

To achieve this, we implement a class with AccessInterface and use that to check access.

Our module is called menu_example, this file will be placed under menu_example/src/Access/CustomAccessCheck.php.

The menu_example_services.yml contains entry for this service class.

Hierarchy

Expanded class hierarchy of RoleAccessCheck

See also

https://www.drupal.org/docs/8/api/routing-system/access-checking-on-routes

1 string reference to 'RoleAccessCheck'
menu_example.services.yml in menu_example/menu_example.services.yml
menu_example/menu_example.services.yml
1 service uses RoleAccessCheck
menu_example.access_check.role in menu_example/menu_example.services.yml
Drupal\menu_example\Access\RoleAccessCheck

File

menu_example/src/Access/RoleAccessCheck.php, line 23

Namespace

Drupal\menu_example\Access
View source
class RoleAccessCheck implements AccessInterface {

  /**
   * Checks access.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   The currently logged in account.
   *
   * @return string
   *   A \Drupal\Core\Access\AccessInterface constant value.
   */
  public function access(AccountInterface $account) {

    // If the user is authenticated, return TRUE.
    return AccessResult::allowedIf($account
      ->isAuthenticated());
  }

}

Members