DomainAccessCheck.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Drupal\domain\Access;
  3. use Drupal\Core\Access\AccessCheckInterface;
  4. use Drupal\Core\Access\AccessResult;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Drupal\Core\Path\PathMatcherInterface;
  7. use Drupal\Core\Session\AccountInterface;
  8. use Drupal\domain\DomainNegotiatorInterface;
  9. use Symfony\Component\Routing\Route;
  10. /**
  11. * Provides a global access check to ensure inactive domains are restricted.
  12. */
  13. class DomainAccessCheck implements AccessCheckInterface {
  14. /**
  15. * The Domain negotiator.
  16. *
  17. * @var \Drupal\domain\DomainNegotiatorInterface
  18. */
  19. protected $domainNegotiator;
  20. /**
  21. * The config factory.
  22. *
  23. * @var \Drupal\Core\Config\ConfigFactoryInterface
  24. */
  25. protected $configFactory;
  26. /**
  27. * The path matcher service.
  28. *
  29. * @var \Drupal\Core\Path\PathMatcherInterface
  30. */
  31. protected $pathMatcher;
  32. /**
  33. * Constructs the object.
  34. *
  35. * @param \Drupal\domain\DomainNegotiatorInterface $negotiator
  36. * The domain negotiation service.
  37. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  38. * The config factory.
  39. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
  40. * The path matcher service.
  41. */
  42. public function __construct(DomainNegotiatorInterface $negotiator, ConfigFactoryInterface $config_factory, PathMatcherInterface $path_matcher) {
  43. $this->domainNegotiator = $negotiator;
  44. $this->configFactory = $config_factory;
  45. $this->pathMatcher = $path_matcher;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function applies(Route $route) {
  51. return $this->checkPath($route->getPath());
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function checkPath($path) {
  57. $allowed_paths = $this->configFactory->get('domain.settings')->get('login_paths');
  58. return !$this->pathMatcher->matchPath($path, $allowed_paths);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function access(AccountInterface $account) {
  64. $domain = $this->domainNegotiator->getActiveDomain();
  65. // Is the domain allowed?
  66. // No domain, let it pass.
  67. if (empty($domain)) {
  68. return AccessResult::allowed()->addCacheTags(['url.site']);
  69. }
  70. // Active domain, let it pass.
  71. if ($domain->status()) {
  72. return AccessResult::allowed()->addCacheTags(['url.site']);
  73. }
  74. // Inactive domain, require permissions.
  75. else {
  76. $permissions = ['administer domains', 'access inactive domains'];
  77. $operator = 'OR';
  78. return AccessResult::allowedIfHasPermissions($account, $permissions, $operator)->addCacheTags(['url.site']);
  79. }
  80. }
  81. }