Redirect.php in Entity Legal 3.0.x
File
src/Plugin/EntityLegal/Redirect.php
View source
<?php
namespace Drupal\entity_legal\Plugin\EntityLegal;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RedirectDestinationTrait;
use Drupal\Core\Routing\ResettableStackedRouteMatchInterface;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\entity_legal\EntityLegalDocumentInterface;
use Drupal\entity_legal\EntityLegalPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class Redirect extends EntityLegalPluginBase implements ContainerFactoryPluginInterface {
use MessengerTrait;
use RedirectDestinationTrait;
use StringTranslationTrait;
protected $routeMatch;
protected $currentUser;
protected $tempStore;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ResettableStackedRouteMatchInterface $route_match, AccountProxyInterface $current_user, PrivateTempStoreFactory $private_temp_store_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->routeMatch = $route_match;
$this->currentUser = $current_user;
$this->tempStore = $private_temp_store_factory
->get('entity_legal');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('current_route_match'), $container
->get('current_user'), $container
->get('tempstore.private'));
}
public function execute(array &$context = []) {
foreach ($this->documents as $document) {
$event = $context['event'];
$request = $event
->getRequest();
if ($request
->getRequestFormat() !== 'html') {
return FALSE;
}
if (!$request
->isMethodSafe()) {
return FALSE;
}
if (!($route_name = $this->routeMatch
->getRouteName())) {
return FALSE;
}
if ($this
->isExcludedRoute($route_name, $document)) {
return FALSE;
}
if ($this
->isPasswordReset($event
->getRequest())) {
return FALSE;
}
if ($messages = $this
->messenger()
->all()) {
$this->tempStore
->set('postponed_messages', $messages);
$this
->messenger()
->deleteAll();
}
$this
->messenger()
->addWarning($this
->t('You must accept this agreement before continuing.'));
$entity_url = $document
->toUrl()
->setOption('query', $this
->getDestinationArray())
->setAbsolute(TRUE)
->toString();
$event
->setResponse(new TrustedRedirectResponse($entity_url));
$request->query
->remove('destination');
$request->request
->remove('destination');
}
}
protected function isExcludedRoute($route_name, EntityLegalDocumentInterface $document) {
$excluded_routes = [
'system.csrftoken',
'user.logout',
$document
->toUrl()
->getRouteName(),
];
return in_array($route_name, $excluded_routes);
}
protected function isPasswordReset(Request $request) {
if ($this->routeMatch
->getRouteName() !== 'entity.user.edit_form' && $this->routeMatch
->getRawParameter('user') != $this->currentUser
->id()) {
return FALSE;
}
if (!($pass_reset_token = $request
->get('pass-reset-token'))) {
return FALSE;
}
$session_key = "pass_reset_{$this->currentUser->id()}";
if (!isset($_SESSION[$session_key]) || !hash_equals($_SESSION[$session_key], $pass_reset_token)) {
return FALSE;
}
return TRUE;
}
}
Classes
Name |
Description |
Redirect |
Method class for redirecting existing users to accept a legal document. |