3 namespace Drupal\redirect;
5 use Drupal\Core\Access\AccessManager;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\State\StateInterface;
9 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
10 use Symfony\Cmf\Component\Routing\RouteProviderInterface;
11 use Symfony\Component\HttpFoundation\Request;
14 * Redirect checker class.
16 class RedirectChecker {
19 * @var \Drupal\Core\Config\Config
24 * @var \Drupal\Core\State\StateInterface
29 * @var \Drupal\Core\Access\AccessManager
31 protected $accessManager;
34 * @var \Drupal\Core\Session\AccountInterface
39 * @var \Drupal\Core\Routing\RouteProviderInterface
41 protected $routeProvider;
43 public function __construct(ConfigFactoryInterface $config, StateInterface $state, AccessManager $access_manager, AccountInterface $account, RouteProviderInterface $route_provider) {
44 $this->config = $config->get('redirect.settings');
45 $this->accessManager = $access_manager;
46 $this->state = $state;
47 $this->account = $account;
48 $this->routeProvider = $route_provider;
52 * Determines if redirect may be performed.
54 * @param Request $request
55 * The current request object.
56 * @param string $route_name
57 * The current route name.
60 * TRUE if redirect may be performed.
62 public function canRedirect(Request $request, $route_name = NULL) {
64 if (isset($route_name)) {
65 $route = $this->routeProvider->getRouteByName($route_name);
66 if ($this->config->get('access_check')) {
67 // Do not redirect if is a protected page.
68 $can_redirect = $this->accessManager->checkNamedRoute($route_name, [], $this->account);
72 $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
75 if (!preg_match('/index\.php$/', $request->getScriptName())) {
76 // Do not redirect if the root script is not /index.php.
77 $can_redirect = FALSE;
79 elseif (!($request->isMethod('GET') || $request->isMethod('HEAD'))) {
80 // Do not redirect if this is other than GET request.
81 $can_redirect = FALSE;
83 elseif ($this->state->get('system.maintenance_mode') || defined('MAINTENANCE_MODE')) {
84 // Do not redirect in offline or maintenance mode.
85 $can_redirect = FALSE;
87 elseif ($request->query->has('destination')) {
88 $can_redirect = FALSE;
90 elseif ($this->config->get('ignore_admin_path') && isset($route)) {
91 // Do not redirect on admin paths.
92 $can_redirect &= !(bool) $route->getOption('_admin_route');