EntityDeleteMultipleAccessCheck.php 2.8 KB

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