EntityAccessControlHandlerBase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Drupal\entity;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Entity\EntityAccessControlHandler as CoreEntityAccessControlHandler;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. use Drupal\user\EntityOwnerInterface;
  8. /**
  9. * @internal
  10. */
  11. class EntityAccessControlHandlerBase extends CoreEntityAccessControlHandler {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  16. $account = $this->prepareUser($account);
  17. /** @var \Drupal\Core\Access\AccessResult $result */
  18. $result = parent::checkAccess($entity, $operation, $account);
  19. if ($result->isNeutral()) {
  20. if ($entity instanceof EntityOwnerInterface) {
  21. $result = $this->checkEntityOwnerPermissions($entity, $operation, $account);
  22. }
  23. else {
  24. $result = $this->checkEntityPermissions($entity, $operation, $account);
  25. }
  26. }
  27. // Ensure that access is evaluated again when the entity changes.
  28. return $result->addCacheableDependency($entity);
  29. }
  30. /**
  31. * Checks the entity operation and bundle permissions.
  32. *
  33. * @param \Drupal\Core\Entity\EntityInterface $entity
  34. * The entity for which to check access.
  35. * @param string $operation
  36. * The entity operation. Usually one of 'view', 'view label', 'update' or
  37. * 'delete'.
  38. * @param \Drupal\Core\Session\AccountInterface $account
  39. * The user for which to check access.
  40. *
  41. * @return \Drupal\Core\Access\AccessResultInterface
  42. * The access result.
  43. */
  44. protected function checkEntityPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
  45. $permissions = [
  46. "$operation {$entity->getEntityTypeId()}",
  47. "$operation {$entity->bundle()} {$entity->getEntityTypeId()}",
  48. ];
  49. return AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
  50. }
  51. /**
  52. * Checks the entity operation and bundle permissions, with owners.
  53. *
  54. * @param \Drupal\Core\Entity\EntityInterface $entity
  55. * The entity for which to check access.
  56. * @param string $operation
  57. * The entity operation. Usually one of 'view', 'view label', 'update' or
  58. * 'delete'.
  59. * @param \Drupal\Core\Session\AccountInterface $account
  60. * The user for which to check access.
  61. *
  62. * @return \Drupal\Core\Access\AccessResultInterface
  63. * The access result.
  64. */
  65. protected function checkEntityOwnerPermissions(EntityInterface $entity, $operation, AccountInterface $account) {
  66. /** @var \Drupal\user\EntityOwnerInterface $entity */
  67. if ($account->id() == $entity->getOwnerId()) {
  68. $permissions = [
  69. "$operation own {$entity->getEntityTypeId()}",
  70. "$operation any {$entity->getEntityTypeId()}",
  71. "$operation own {$entity->bundle()} {$entity->getEntityTypeId()}",
  72. "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
  73. ];
  74. }
  75. else {
  76. $permissions = [
  77. "$operation any {$entity->getEntityTypeId()}",
  78. "$operation any {$entity->bundle()} {$entity->getEntityTypeId()}",
  79. ];
  80. }
  81. $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR')->cachePerUser();
  82. return $result;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  88. $result = parent::checkCreateAccess($account, $context, $entity_bundle);
  89. if ($result->isNeutral()) {
  90. $permissions = [
  91. 'administer ' . $this->entityTypeId,
  92. 'create ' . $this->entityTypeId,
  93. ];
  94. if ($entity_bundle) {
  95. $permissions[] = 'create ' . $entity_bundle . ' ' . $this->entityTypeId;
  96. }
  97. $result = AccessResult::allowedIfHasPermissions($account, $permissions, 'OR');
  98. }
  99. return $result;
  100. }
  101. }