Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\menu_link_content;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Access\AccessResult;
|
Chris@0
|
6 use Drupal\Core\Access\AccessManagerInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityAccessControlHandler;
|
Chris@0
|
8 use Drupal\Core\Entity\EntityHandlerInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
10 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@0
|
11 use Drupal\Core\Session\AccountInterface;
|
Chris@0
|
12 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * Defines the access control handler for the user entity type.
|
Chris@0
|
16 */
|
Chris@0
|
17 class MenuLinkContentAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The access manager to check routes by name.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Core\Access\AccessManagerInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $accessManager;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * Creates a new MenuLinkContentAccessControlHandler.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
30 * The entity type definition.
|
Chris@0
|
31 * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
|
Chris@0
|
32 * The access manager to check routes by name.
|
Chris@0
|
33 */
|
Chris@0
|
34 public function __construct(EntityTypeInterface $entity_type, AccessManagerInterface $access_manager) {
|
Chris@0
|
35 parent::__construct($entity_type);
|
Chris@0
|
36
|
Chris@0
|
37 $this->accessManager = $access_manager;
|
Chris@0
|
38 }
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * {@inheritdoc}
|
Chris@0
|
42 */
|
Chris@0
|
43 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
Chris@0
|
44 return new static($entity_type, $container->get('access_manager'));
|
Chris@0
|
45 }
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * {@inheritdoc}
|
Chris@0
|
49 */
|
Chris@0
|
50 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
|
Chris@0
|
51 switch ($operation) {
|
Chris@0
|
52 case 'view':
|
Chris@0
|
53 // There is no direct viewing of a menu link, but still for purposes of
|
Chris@0
|
54 // content_translation we need a generic way to check access.
|
Chris@0
|
55 return AccessResult::allowedIfHasPermission($account, 'administer menu');
|
Chris@0
|
56
|
Chris@0
|
57 case 'update':
|
Chris@0
|
58 if (!$account->hasPermission('administer menu')) {
|
Chris@0
|
59 return AccessResult::neutral("The 'administer menu' permission is required.")->cachePerPermissions();
|
Chris@0
|
60 }
|
Chris@0
|
61 else {
|
Chris@0
|
62 // Assume that access is allowed.
|
Chris@0
|
63 $access = AccessResult::allowed()->cachePerPermissions()->addCacheableDependency($entity);
|
Chris@0
|
64 /** @var \Drupal\menu_link_content\MenuLinkContentInterface $entity */
|
Chris@0
|
65 // If the link is routed determine whether the user has access unless
|
Chris@0
|
66 // they have the 'link to any page' permission.
|
Chris@0
|
67 if (!$account->hasPermission('link to any page') && ($url_object = $entity->getUrlObject()) && $url_object->isRouted()) {
|
Chris@0
|
68 $link_access = $this->accessManager->checkNamedRoute($url_object->getRouteName(), $url_object->getRouteParameters(), $account, TRUE);
|
Chris@0
|
69 $access = $access->andIf($link_access);
|
Chris@0
|
70 }
|
Chris@0
|
71 return $access;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 case 'delete':
|
Chris@17
|
75 return AccessResult::allowedIfHasPermission($account, 'administer menu')
|
Chris@17
|
76 ->andIf(AccessResult::allowedIf(!$entity->isNew())->addCacheableDependency($entity));
|
Chris@0
|
77 }
|
Chris@0
|
78 }
|
Chris@0
|
79
|
Chris@0
|
80 }
|