_custom_access
Text
hello_world.content:
path: '/hello-world-permissions'
defaults:
_controller: '\Drupal\hello_world_permissions\Controller\HelloWorldPermissionsController::content'
_title: 'Hello Permissions'
requirements:
_permission: 'access hello world page'
Anger vilken rättighet som ska styra tillgång till vår route.
access hello world page:
title: 'Access hello world page'
Definera egena permissions
hello_world_permissions.permissions.yml
Anger vilken roll som ska få tillgång till ens route.
hello_world.content:
path: '/hello-world-permissions'
defaults:
_controller: '\Drupal\hello_world_permissions\Controller\HelloWorldPermissionsController::content'
_title: 'Hello Permissions'
requirements:
_role: 'administrator'
Används vid mer komplexa scenario
hello_world.content:
path: '/hello-world-permissions'
defaults:
_controller: '\Drupal\hello_world_permissions\Controller\HWPController::content'
_title: 'Hello Permissions'
requirements:
_custom_access: '\Drupal\hello_world_permissions\Controller\HWPController::access'
Används vid mer komplexa scenario
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
/**
* Check access for page.
*
* @return array
*/
public function access(AccountInterface $account) {
if ($account->getAccountName() == 'root') {
return AccessResult::allowed();
}
else {
return AccessResult::forbidden();
}
}