Version 1
[yaffs-website] / web / modules / contrib / redirect / src / RedirectChecker.php
1 <?php
2
3 namespace Drupal\redirect;
4
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;
12
13 /**
14  * Redirect checker class.
15  */
16 class RedirectChecker {
17
18   /**
19    * @var \Drupal\Core\Config\Config
20    */
21   protected $config;
22
23   /**
24    * @var \Drupal\Core\State\StateInterface
25    */
26   protected $state;
27
28   /**
29    * @var \Drupal\Core\Access\AccessManager
30    */
31   protected $accessManager;
32
33   /**
34    * @var \Drupal\Core\Session\AccountInterface
35    */
36   protected $account;
37
38   /**
39    * @var \Drupal\Core\Routing\RouteProviderInterface
40    */
41   protected $routeProvider;
42
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;
49   }
50
51   /**
52    * Determines if redirect may be performed.
53    *
54    * @param Request $request
55    *   The current request object.
56    * @param string $route_name
57    *   The current route name.
58    *
59    * @return bool
60    *   TRUE if redirect may be performed.
61    */
62   public function canRedirect(Request $request, $route_name = NULL) {
63     $can_redirect = TRUE;
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);
69       }
70     }
71     else {
72       $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
73     }
74
75     if (!preg_match('/index\.php$/', $request->getScriptName())) {
76       // Do not redirect if the root script is not /index.php.
77       $can_redirect = FALSE;
78     }
79     elseif (!($request->isMethod('GET') || $request->isMethod('HEAD'))) {
80       // Do not redirect if this is other than GET request.
81       $can_redirect = FALSE;
82     }
83     elseif ($this->state->get('system.maintenance_mode') || defined('MAINTENANCE_MODE')) {
84       // Do not redirect in offline or maintenance mode.
85       $can_redirect = FALSE;
86     }
87     elseif ($request->query->has('destination')) {
88       $can_redirect = FALSE;
89     }
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');
93     }
94
95     return $can_redirect;
96   }
97
98 }