DomainAccessViewsAccess.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace Drupal\domain_access\Access;
  3. use Drupal\Core\Access\AccessCheckInterface;
  4. use Drupal\Core\Access\AccessResult;
  5. use Drupal\Core\Entity\EntityTypeManagerInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. use Symfony\Component\Routing\Route;
  8. use Drupal\domain_access\DomainAccessManagerInterface;
  9. /**
  10. * Class DomainAccessViewsAccess.
  11. *
  12. * @package Drupal\domain_access\Access
  13. */
  14. class DomainAccessViewsAccess implements AccessCheckInterface {
  15. /**
  16. * The key used by the routing requirement.
  17. *
  18. * @var string
  19. */
  20. protected $requirementsKey = '_domain_access_views';
  21. /**
  22. * The entity type manager.
  23. *
  24. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  25. */
  26. protected $entityTypeManager;
  27. /**
  28. * The Domain storage handler.
  29. *
  30. * @var \Drupal\domain\DomainStorageInterface
  31. */
  32. protected $domainStorage;
  33. /**
  34. * User storage.
  35. *
  36. * @var \Drupal\user\UserStorageInterface
  37. */
  38. protected $userStorage;
  39. /**
  40. * The Domain access manager.
  41. *
  42. * @var \Drupal\domain_access\DomainAccessManagerInterface
  43. */
  44. protected $manager;
  45. /**
  46. * Constructs a DomainToken object.
  47. *
  48. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  49. * The entity type manager.
  50. * @param \Drupal\domain_access\DomainAccessManagerInterface $manager
  51. * The domain access manager.
  52. */
  53. public function __construct(EntityTypeManagerInterface $entity_type_manager, DomainAccessManagerInterface $manager) {
  54. $this->entityTypeManager = $entity_type_manager;
  55. $this->domainStorage = $this->entityTypeManager->getStorage('domain');
  56. $this->userStorage = $this->entityTypeManager->getStorage('user');
  57. $this->manager = $manager;
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function access(Route $route, AccountInterface $account, $arg_0 = NULL) {
  63. // Permissions are stored on the route defaults.
  64. $permission = $route->getDefault('domain_permission');
  65. $allPermission = $route->getDefault('domain_all_permission');
  66. // Users with this permission can see any domain content lists, and it is
  67. // required to view all affiliates.
  68. if ($account->hasPermission($allPermission)) {
  69. return AccessResult::allowed();
  70. }
  71. // Load the domain from the passed argument. In testing, this passed NULL
  72. // in some instances.
  73. if (!is_null($arg_0)) {
  74. $domain = $this->domainStorage->load($arg_0);
  75. }
  76. // Domain found, check user permissions.
  77. if (!empty($domain)) {
  78. if ($this->manager->hasDomainPermissions($account, $domain, [$permission])) {
  79. return AccessResult::allowed();
  80. }
  81. }
  82. return AccessResult::forbidden();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function applies(Route $route) {
  88. return $route->hasRequirement($this->requirementsKey);
  89. }
  90. }