entity_agree-2.0.x-dev/src/Plugin/entity_agree/AgreementHandler/RestrictPages.php
src/Plugin/entity_agree/AgreementHandler/RestrictPages.php
<?php namespace Drupal\entity_agree\Plugin\entity_agree\AgreementHandler; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; use Drupal\Core\Url; use Drupal\entity_agree\AgreementHandlerPluginBase; use Drupal\entity_agree\AgreementHandlerRestrictPagesInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Plugin to restrict a user's access to pages. * * @AgreementHandler( * id = "entity_agree_restrict_pages", * label = @Translation("Restrict pages"), * category = @Translation("Restrict"), * ) */ class RestrictPages extends AgreementHandlerPluginBase implements AgreementHandlerRestrictPagesInterface, ContainerFactoryPluginInterface { /** * An alias manager to find the alias for the current system path. * * @var \Drupal\path_alias\AliasManagerInterface */ protected $aliasManager; /** * Config factory. * * @var \Drupal\Core\Config\ConfigFactoryInterface */ protected $configFactory; /** * The path matcher. * * @var \Drupal\Core\Path\PathMatcherInterface */ protected $pathMatcher; /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { $plugin = new static($configuration, $plugin_id, $plugin_definition); $plugin->aliasManager = $container->get('path_alias.manager'); $plugin->configFactory = $container->get('config.factory'); $plugin->pathMatcher = $container->get('path.matcher'); return $plugin; } /** * {@inheritdoc} */ public function defaultConfiguration() { return parent::defaultConfiguration() + [ 'pages' => '/user/*', 'negate' => TRUE, ]; } /** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['pages'] = [ '#type' => 'textarea', '#title' => $this->t('Pages'), '#default_value' => $this->configuration['pages'], '#description' => $this->t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. An example path is %user-wildcard for every user page. %front is the front page.", [ '%user-wildcard' => '/user/*', '%front' => '<front>', ]), ]; $form['negate'] = [ '#type' => 'select', '#title' => $this->t('Allow/restrict'), '#options' => [ 1 => $this->t('Allow access to the specified pages'), 0 => $this->t('Restrict access to the specified pages'), ], '#default_value' => $this->configuration['negate'], ]; return parent::buildConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function allow(RouteMatchInterface $route_match) { // Create a URL from the route being checked and get the path. $url = Url::fromRouteMatch($route_match); $path = '/' . $url->getInternalPath(); if ($path == '/') { $path = $this->configFactory->get('system.site')->get('page.front'); } else { $path = rtrim($path, '/'); } // Attempt to match on internal path. if ($this->pathMatcher->matchPath($path, $this->configuration['pages'])) { return (bool) $this->configuration['negate']; } // Attempt to match on alias. $path_alias = mb_strtolower($this->aliasManager->getAliasByPath($path)); return $this->pathMatcher->matchPath($path_alias, $this->configuration['pages']) == $this->configuration['negate']; } }