3 namespace Drupal\menu_link_content;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Access\AccessManagerInterface;
7 use Drupal\Core\Entity\EntityAccessControlHandler;
8 use Drupal\Core\Entity\EntityHandlerInterface;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\Core\Entity\EntityTypeInterface;
11 use Drupal\Core\Session\AccountInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
15 * Defines the access control handler for the user entity type.
17 class MenuLinkContentAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
20 * The access manager to check routes by name.
22 * @var \Drupal\Core\Access\AccessManagerInterface
24 protected $accessManager;
27 * Creates a new MenuLinkContentAccessControlHandler.
29 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
30 * The entity type definition.
31 * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
32 * The access manager to check routes by name.
34 public function __construct(EntityTypeInterface $entity_type, AccessManagerInterface $access_manager) {
35 parent::__construct($entity_type);
37 $this->accessManager = $access_manager;
43 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44 return new static($entity_type, $container->get('access_manager'));
50 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
53 // There is no direct viewing of a menu link, but still for purposes of
54 // content_translation we need a generic way to check access.
55 return AccessResult::allowedIfHasPermission($account, 'administer menu');
58 if (!$account->hasPermission('administer menu')) {
59 return AccessResult::neutral("The 'administer menu' permission is required.")->cachePerPermissions();
62 // Assume that access is allowed.
63 $access = AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
64 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
65 // If the link is routed determine whether the user has access unless
66 // they have the 'link to any page' permission.
67 if (!$account->hasPermission('link to any page') && ($url_object = $entity->getUrlObject()) && $url_object->isRouted()) {
68 $link_access = $this->accessManager->checkNamedRoute($url_object->getRouteName(), $url_object->getRouteParameters(), $account, TRUE);
69 $access = $access->andIf($link_access);
75 return AccessResult::allowedIf(!$entity->isNew() && $account->hasPermission('administer menu'))->cachePerPermissions()->addCacheableDependency($entity);