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