Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / menu_link_content / src / MenuLinkContentAccessControlHandler.php
1 <?php
2
3 namespace Drupal\menu_link_content;
4
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;
13
14 /**
15  * Defines the access control handler for the user entity type.
16  */
17 class MenuLinkContentAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
18
19   /**
20    * The access manager to check routes by name.
21    *
22    * @var \Drupal\Core\Access\AccessManagerInterface
23    */
24   protected $accessManager;
25
26   /**
27    * Creates a new MenuLinkContentAccessControlHandler.
28    *
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.
33    */
34   public function __construct(EntityTypeInterface $entity_type, AccessManagerInterface $access_manager) {
35     parent::__construct($entity_type);
36
37     $this->accessManager = $access_manager;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44     return new static($entity_type, $container->get('access_manager'));
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
51     switch ($operation) {
52       case 'view':
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');
56
57       case 'update':
58         if (!$account->hasPermission('administer menu')) {
59           return AccessResult::neutral("The 'administer menu' permission is required.")->cachePerPermissions();
60         }
61         else {
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);
70           }
71           return $access;
72         }
73
74       case 'delete':
75         return AccessResult::allowedIf(!$entity->isNew() && $account->hasPermission('administer menu'))->cachePerPermissions()->addCacheableDependency($entity);
76     }
77   }
78
79 }