DomainAccessControlHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Drupal\domain;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Entity\EntityAccessControlHandler;
  5. use Drupal\Core\Entity\EntityHandlerInterface;
  6. use Drupal\Core\Entity\EntityInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\Core\Entity\EntityTypeManagerInterface;
  9. use Drupal\Core\Session\AccountInterface;
  10. use Drupal\user\UserStorageInterface;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Defines the access controller for the domain entity type.
  14. *
  15. * Note that this is not a node access check.
  16. */
  17. class DomainAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
  18. /**
  19. * The entity type manager.
  20. *
  21. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  22. */
  23. protected $entityTypeManager;
  24. /**
  25. * The domain field element manager.
  26. *
  27. * @var \Drupal\domain\DomainElementManagerInterface
  28. */
  29. protected $domainElementManager;
  30. /**
  31. * The user storage manager.
  32. *
  33. * @var \Drupal\user\UserStorageInterface
  34. */
  35. protected $userStorage;
  36. /**
  37. * Constructs an access control handler instance.
  38. *
  39. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  40. * The entity type definition.
  41. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  42. * The entity type manager.
  43. * @param \Drupal\domain\DomainElementManagerInterface $domain_element_manager
  44. * The domain field element manager.
  45. * @param \Drupal\user\UserStorageInterface $user_storage
  46. * The user storage manager.
  47. */
  48. public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entity_type_manager, DomainElementManagerInterface $domain_element_manager, UserStorageInterface $user_storage) {
  49. parent::__construct($entity_type);
  50. $this->entityTypeManager = $entity_type_manager;
  51. $this->domainElementManager = $domain_element_manager;
  52. $this->userStorage = $user_storage;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  58. return new static(
  59. $entity_type,
  60. $container->get('entity_type.manager'),
  61. $container->get('domain.element_manager'),
  62. $container->get('entity_type.manager')->getStorage('user')
  63. );
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function checkAccess(EntityInterface $entity, $operation, AccountInterface $account = NULL) {
  69. $account = $this->prepareUser($account);
  70. // Check the global permission.
  71. if ($account->hasPermission('administer domains')) {
  72. return AccessResult::allowed();
  73. }
  74. // @TODO: This may not be relevant.
  75. if ($operation == 'create' && $account->hasPermission('create domains')) {
  76. return AccessResult::allowed();
  77. }
  78. // For view, we allow admins unless the domain is inactive.
  79. $is_admin = $this->isDomainAdmin($entity, $account);
  80. if ($operation == 'view' && ($entity->status() || $account->hasPermission('access inactive domains')) && ($is_admin || $account->hasPermission('view domain list'))) {
  81. return AccessResult::allowed();
  82. }
  83. // For other operations, check that the user is a domain admin.
  84. if ($operation == 'update' && $account->hasPermission('edit assigned domains') && $is_admin) {
  85. return AccessResult::allowed();
  86. }
  87. if ($operation == 'delete' && $account->hasPermission('delete assigned domains') && $is_admin) {
  88. return AccessResult::allowed();
  89. }
  90. return AccessResult::forbidden();
  91. }
  92. /**
  93. * Checks if a user can administer a specific domain.
  94. *
  95. * @param \Drupal\Core\Entity\EntityInterface $entity
  96. * The entity to retrieve field data from.
  97. * @param \Drupal\Core\Session\AccountInterface $account
  98. * The user account.
  99. *
  100. * @return bool
  101. * TRUE if a user can administer a specific domain, or FALSE.
  102. */
  103. public function isDomainAdmin(EntityInterface $entity, AccountInterface $account) {
  104. $user = $this->userStorage->load($account->id());
  105. $user_domains = $this->domainElementManager->getFieldValues($user, DOMAIN_ADMIN_FIELD);
  106. return isset($user_domains[$entity->id()]);
  107. }
  108. }