Domain301RedirectController.php in Domain 301 Redirect 8
File
src/Controller/Domain301RedirectController.php
View source
<?php
namespace Drupal\domain_301_redirect\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\domain_301_redirect\Domain301RedirectManagerInterface;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
class Domain301RedirectController extends ControllerBase {
protected $redirectManager;
protected $request;
protected $config;
public function __construct(Domain301RedirectManagerInterface $redirectManager, RequestStack $request_stack, ConfigFactoryInterface $config_factory) {
$this->redirectManager = $redirectManager;
$this->request = $request_stack
->getCurrentRequest();
$this->config = $config_factory
->get('domain_301_redirect.settings');
}
public static function create(ContainerInterface $container) {
return new static($container
->get('domain_301_redirect.manager'), $container
->get('request_stack'), $container
->get('config.factory'));
}
public function check() {
$response = new CacheableJsonResponse([
'enabled' => $this->config
->get('enabled'),
]);
return $response
->addCacheableDependency(CacheableMetadata::createFromRenderArray([
'#cache' => [
'max-age' => 0,
'contexts' => [
'url.query_args:token',
],
'tags' => [
'config:domain_301_redirect.settings',
],
],
]));
}
public function access(AccountInterface $account) {
$token = $this->redirectManager
->getRedirectCheckToken();
if ($this->request->query
->get('token') == $token) {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
}