Pull merge.
[yaffs-website] / web / core / lib / Drupal / Core / Entity / EntityDeleteMultipleAccessCheck.php
1 <?php
2
3 namespace Drupal\Core\Entity;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Routing\Access\AccessInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\TempStore\PrivateTempStoreFactory;
9 use Symfony\Component\HttpFoundation\RequestStack;
10
11 /**
12  * Checks if the current user has delete access to the items of the tempstore.
13  */
14 class EntityDeleteMultipleAccessCheck implements AccessInterface {
15
16   /**
17    * The entity type manager.
18    *
19    * @var \Drupal\Core\Entity\EntityManagerInterface
20    */
21   protected $entityTypeManager;
22
23   /**
24    * The tempstore service.
25    *
26    * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
27    */
28   protected $tempStore;
29
30   /**
31    * Request stack service.
32    *
33    * @var \Symfony\Component\HttpFoundation\RequestStack
34    */
35   protected $requestStack;
36
37   /**
38    * Constructs a new EntityDeleteMultipleAccessCheck.
39    *
40    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
41    *   The entity type manager.
42    * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
43    *   The tempstore service.
44    * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
45    *   The request stack service.
46    */
47   public function __construct(EntityTypeManagerInterface $entity_type_manager, PrivateTempStoreFactory $temp_store_factory, RequestStack $request_stack) {
48     $this->entityTypeManager = $entity_type_manager;
49     $this->tempStore = $temp_store_factory->get('entity_delete_multiple_confirm');
50     $this->requestStack = $request_stack;
51   }
52
53   /**
54    * Checks if the user has delete access for at least one item of the store.
55    *
56    * @param \Drupal\Core\Session\AccountInterface $account
57    *   Run access checks for this account.
58    * @param string $entity_type_id
59    *   Entity type ID.
60    *
61    * @return \Drupal\Core\Access\AccessResult
62    *   Allowed or forbidden, neutral if tempstore is empty.
63    */
64   public function access(AccountInterface $account, $entity_type_id) {
65     if (!$this->requestStack->getCurrentRequest()->getSession()) {
66       return AccessResult::neutral();
67     }
68     $selection = $this->tempStore->get($account->id() . ':' . $entity_type_id);
69     if (empty($selection) || !is_array($selection)) {
70       return AccessResult::neutral();
71     }
72
73     $entities = $this->entityTypeManager->getStorage($entity_type_id)->loadMultiple(array_keys($selection));
74     foreach ($entities as $entity) {
75       // As long as the user has access to delete one entity allow access to the
76       // delete form. Access will be checked again in
77       // Drupal\Core\Entity\Form\DeleteMultipleForm::submit() in case it has
78       // changed in the meantime.
79       if ($entity->access('delete', $account)) {
80         return AccessResult::allowed();
81       }
82     }
83     return AccessResult::forbidden();
84   }
85
86 }