Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / shortcut / src / ShortcutAccessControlHandler.php
1 <?php
2
3 namespace Drupal\shortcut;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityHandlerInterface;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Entity\EntityTypeInterface;
10 use Drupal\Core\Session\AccountInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Defines the access control handler for the shortcut entity type.
15  *
16  * @see \Drupal\shortcut\Entity\Shortcut
17  */
18 class ShortcutAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
19
20   /**
21    * The shortcut_set storage.
22    *
23    * @var \Drupal\shortcut\ShortcutSetStorageInterface
24    */
25   protected $shortcutSetStorage;
26
27   /**
28    * Constructs a ShortcutAccessControlHandler object.
29    *
30    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
31    *   The entity type definition.
32    * @param \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_set_storage
33    *   The shortcut_set storage.
34    */
35   public function __construct(EntityTypeInterface $entity_type, ShortcutSetStorageInterface $shortcut_set_storage) {
36     parent::__construct($entity_type);
37     $this->shortcutSetStorage = $shortcut_set_storage;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
44     return new static(
45       $entity_type,
46       $container->get('entity.manager')->getStorage('shortcut_set')
47     );
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
54     if ($shortcut_set = $this->shortcutSetStorage->load($entity->bundle())) {
55       return shortcut_set_edit_access($shortcut_set, $account);
56     }
57     // @todo Fix this bizarre code: how can a shortcut exist without a shortcut
58     // set? The above if-test is unnecessary. See https://www.drupal.org/node/2339903.
59     return AccessResult::neutral()->addCacheableDependency($entity);
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
66     if ($shortcut_set = $this->shortcutSetStorage->load($entity_bundle)) {
67       return shortcut_set_edit_access($shortcut_set, $account);
68     }
69     // @todo Fix this bizarre code: how can a shortcut exist without a shortcut
70     // set? The above if-test is unnecessary. See https://www.drupal.org/node/2339903.
71     return AccessResult::neutral();
72   }
73
74 }