3 namespace Drupal\Core\Entity;
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;
12 * Checks if the current user has delete access to the items of the tempstore.
14 class EntityDeleteMultipleAccessCheck implements AccessInterface {
17 * The entity type manager.
19 * @var \Drupal\Core\Entity\EntityManagerInterface
21 protected $entityTypeManager;
24 * The tempstore service.
26 * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
31 * Request stack service.
33 * @var \Symfony\Component\HttpFoundation\RequestStack
35 protected $requestStack;
38 * Constructs a new EntityDeleteMultipleAccessCheck.
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.
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;
54 * Checks if the user has delete access for at least one item of the store.
56 * @param \Drupal\Core\Session\AccountInterface $account
57 * Run access checks for this account.
58 * @param string $entity_type_id
61 * @return \Drupal\Core\Access\AccessResult
62 * Allowed or forbidden, neutral if tempstore is empty.
64 public function access(AccountInterface $account, $entity_type_id) {
65 if (!$this->requestStack->getCurrentRequest()->getSession()) {
66 return AccessResult::neutral();
68 $selection = $this->tempStore->get($account->id() . ':' . $entity_type_id);
69 if (empty($selection) || !is_array($selection)) {
70 return AccessResult::neutral();
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();
83 return AccessResult::forbidden();