Domain.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Drupal\domain\Plugin\views\access;
  3. use Drupal\Core\Cache\Cache;
  4. use Drupal\Core\Cache\CacheableDependencyInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. use Drupal\domain\DomainNegotiatorInterface;
  8. use Drupal\domain\DomainStorageInterface;
  9. use Drupal\views\Plugin\views\access\AccessPluginBase;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Symfony\Component\Routing\Route;
  12. /**
  13. * Access plugin that provides domain-based access control.
  14. *
  15. * @ViewsAccess(
  16. * id = "domain",
  17. * title = @Translation("Domain"),
  18. * help = @Translation("Access will be granted when accessed from an allowed domain.")
  19. * )
  20. */
  21. class Domain extends AccessPluginBase implements CacheableDependencyInterface {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. protected $usesOptions = TRUE;
  26. /**
  27. * Domain storage.
  28. *
  29. * @var \Drupal\domain\DomainStorageInterface
  30. */
  31. protected $domainStorage;
  32. /**
  33. * Domain negotiation.
  34. *
  35. * @var \Drupal\domain\DomainNegotiator
  36. */
  37. protected $domainNegotiator;
  38. /**
  39. * Constructs a Role object.
  40. *
  41. * @param array $configuration
  42. * A configuration array containing information about the plugin instance.
  43. * @param string $plugin_id
  44. * The plugin_id for the plugin instance.
  45. * @param mixed $plugin_definition
  46. * The plugin implementation definition.
  47. * @param \Drupal\domain\DomainStorageInterface $domain_storage
  48. * The domain storage loader.
  49. * @param \Drupal\domain\DomainNegotiatorInterface $domain_negotiator
  50. * The domain negotiator.
  51. */
  52. public function __construct(array $configuration, $plugin_id, $plugin_definition, DomainStorageInterface $domain_storage, DomainNegotiatorInterface $domain_negotiator) {
  53. parent::__construct($configuration, $plugin_id, $plugin_definition);
  54. $this->domainStorage = $domain_storage;
  55. $this->domainNegotiator = $domain_negotiator;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  61. return new static(
  62. $configuration,
  63. $plugin_id,
  64. $plugin_definition,
  65. $container->get('entity_type.manager')->getStorage('domain'),
  66. $container->get('domain.negotiator')
  67. );
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function access(AccountInterface $account) {
  73. $id = $this->domainNegotiator->getActiveId();
  74. $options = array_filter($this->options['domain']);
  75. return isset($options[$id]);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function alterRouteDefinition(Route $route) {
  81. if ($this->options['domain']) {
  82. $route->setRequirement('_domain', (string) implode('+', $this->options['domain']));
  83. }
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function summaryTitle() {
  89. $count = count($this->options['domain']);
  90. if ($count < 1) {
  91. return $this->t('No domain(s) selected');
  92. }
  93. elseif ($count > 1) {
  94. return $this->t('Multiple domains');
  95. }
  96. else {
  97. $domains = $this->domainStorage->loadOptionsList();
  98. $domain = reset($this->options['domain']);
  99. return $domains[$domain];
  100. }
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. protected function defineOptions() {
  106. $options = parent::defineOptions();
  107. $options['domain'] = ['default' => []];
  108. return $options;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  114. parent::buildOptionsForm($form, $form_state);
  115. $form['domain'] = [
  116. '#type' => 'checkboxes',
  117. '#title' => $this->t('Domain'),
  118. '#default_value' => $this->options['domain'],
  119. '#options' => $this->domainStorage->loadOptionsList(),
  120. '#description' => $this->t('Only the checked domain(s) will be able to access this display.'),
  121. ];
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function validateOptionsForm(&$form, FormStateInterface $form_state) {
  127. $domain = $form_state->getValue(['access_options', 'domain']);
  128. $domain = array_filter($domain);
  129. if (!$domain) {
  130. $form_state->setError($form['domain'], $this->t('You must select at least one domain if type is "by domain"'));
  131. }
  132. $form_state->setValue(['access_options', 'domain'], $domain);
  133. }
  134. /**
  135. * {@inheritdoc}
  136. */
  137. public function calculateDependencies() {
  138. $dependencies = parent::calculateDependencies();
  139. foreach (array_keys($this->options['domain']) as $id) {
  140. if ($domain = $this->domainStorage->load($id)) {
  141. $dependencies[$domain->getConfigDependencyKey()][] = $domain->getConfigDependencyName();
  142. }
  143. }
  144. return $dependencies;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function getCacheMaxAge() {
  150. return Cache::PERMANENT;
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function getCacheContexts() {
  156. return ['url.site'];
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getCacheTags() {
  162. return [];
  163. }
  164. }