DomainAccessContent.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Drupal\domain_access\Plugin\views\access;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Drupal\views\Plugin\views\access\AccessPluginBase;
  7. use Symfony\Component\DependencyInjection\ContainerInterface;
  8. use Symfony\Component\Routing\Route;
  9. use Drupal\domain\DomainStorageInterface;
  10. use Drupal\domain_access\DomainAccessManagerInterface;
  11. use Drupal\user\UserStorageInterface;
  12. /**
  13. * Access plugin that provides domain-editing access control.
  14. *
  15. * @ViewsAccess(
  16. * id = "domain_access_editor",
  17. * title = @Translation("Domain Access: Edit domain content"),
  18. * help = @Translation("Access will be granted to domains on which the user may edit content.")
  19. * )
  20. */
  21. class DomainAccessContent extends AccessPluginBase implements CacheableDependencyInterface {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected $usesOptions = FALSE;
  26. /**
  27. * Sets the permission to use when checking access.
  28. *
  29. * @var string
  30. */
  31. protected $permission = 'publish to any assigned domain';
  32. /**
  33. * Sets the permission to use when checking all access.
  34. *
  35. * @var string
  36. */
  37. protected $allPermission = 'publish to any domain';
  38. /**
  39. * Domain storage.
  40. *
  41. * @var \Drupal\domain\DomainStorageInterface
  42. */
  43. protected $domainStorage;
  44. /**
  45. * User storage.
  46. *
  47. * @var \Drupal\user\UserStorageInterface
  48. */
  49. protected $userStorage;
  50. /**
  51. * Domain Access manager.
  52. *
  53. * @var \Drupal\domain_access\DomainAccessManagerInterface
  54. */
  55. protected $manager;
  56. /**
  57. * Constructs the access object.
  58. *
  59. * @param array $configuration
  60. * A configuration array containing information about the plugin instance.
  61. * @param string $plugin_id
  62. * The plugin_id for the plugin instance.
  63. * @param mixed $plugin_definition
  64. * The plugin implementation definition.
  65. * @param \Drupal\domain\DomainStorageInterface $domain_storage
  66. * The domain storage service.
  67. * @param \Drupal\user\UserStorageInterface $user_storage
  68. * The user storage service.
  69. * @param \Drupal\domain_access\DomainAccessManagerInterface $manager
  70. * The domain access manager service.
  71. */
  72. public function __construct(array $configuration, $plugin_id, $plugin_definition, DomainStorageInterface $domain_storage, UserStorageInterface $user_storage, DomainAccessManagerInterface $manager) {
  73. parent::__construct($configuration, $plugin_id, $plugin_definition);
  74. $this->domainStorage = $domain_storage;
  75. $this->userStorage = $user_storage;
  76. $this->manager = $manager;
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  82. return new static(
  83. $configuration,
  84. $plugin_id,
  85. $plugin_definition,
  86. $container->get('entity_type.manager')->getStorage('domain'),
  87. $container->get('entity_type.manager')->getStorage('user'),
  88. $container->get('domain_access.manager')
  89. );
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function summaryTitle() {
  95. return $this->t('Domain editor');
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function access(AccountInterface $account) {
  101. // Users with this permission can see any domain content lists, and it is
  102. // required to view all affiliates.
  103. if ($account->hasPermission($this->allPermission)) {
  104. return TRUE;
  105. }
  106. // The routine below determines what domain (if any) was passed to the View.
  107. if (isset($this->view->element['#arguments'])) {
  108. foreach ($this->view->element['#arguments'] as $value) {
  109. if ($domain = $this->domainStorage->load($value)) {
  110. break;
  111. }
  112. }
  113. }
  114. // Domain found, check user permissions.
  115. if (!empty($domain)) {
  116. return $this->manager->hasDomainPermissions($account, $domain, [$this->permission]);
  117. }
  118. return FALSE;
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function alterRouteDefinition(Route $route) {
  124. if ($domains = $this->domainStorage->loadMultiple()) {
  125. $list = array_keys($domains);
  126. }
  127. $list[] = 'all_affiliates';
  128. $route->setRequirement('_domain_access_views', (string) implode('+', $list));
  129. $route->setDefault('domain_permission', $this->permission);
  130. $route->setDefault('domain_all_permission', $this->allPermission);
  131. }
  132. /**
  133. * {@inheritdoc}
  134. */
  135. public function getCacheMaxAge() {
  136. return Cache::PERMANENT;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function getCacheContexts() {
  142. return ['user'];
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getCacheTags() {
  148. return [];
  149. }
  150. }