Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\user\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\Routing\RouteMatch;
|
||||
use Drupal\Core\Routing\UrlGeneratorTrait;
|
||||
use Drupal\Core\Routing\UrlGeneratorInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
/**
|
||||
* Redirects users when access is denied.
|
||||
*
|
||||
* Anonymous users are taken to the login page when attempting to access the
|
||||
* user profile pages. Authenticated users are redirected from the login form to
|
||||
* their profile page and from the user registration form to their profile edit
|
||||
* form.
|
||||
*/
|
||||
class AccessDeniedSubscriber implements EventSubscriberInterface {
|
||||
|
||||
use UrlGeneratorTrait;
|
||||
|
||||
/**
|
||||
* The current user.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
/**
|
||||
* Constructs a new redirect subscriber.
|
||||
*
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The current user.
|
||||
* @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
|
||||
* The URL generator.
|
||||
*/
|
||||
public function __construct(AccountInterface $account, UrlGeneratorInterface $url_generator) {
|
||||
$this->account = $account;
|
||||
$this->setUrlGenerator($url_generator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirects users when access is denied.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function onException(GetResponseForExceptionEvent $event) {
|
||||
$exception = $event->getException();
|
||||
if ($exception instanceof AccessDeniedHttpException) {
|
||||
$route_name = RouteMatch::createFromRequest($event->getRequest())->getRouteName();
|
||||
if ($this->account->isAuthenticated()) {
|
||||
switch ($route_name) {
|
||||
case 'user.login';
|
||||
// Redirect an authenticated user to the profile page.
|
||||
$event->setResponse($this->redirect('entity.user.canonical', ['user' => $this->account->id()]));
|
||||
break;
|
||||
|
||||
case 'user.register';
|
||||
// Redirect an authenticated user to the profile form.
|
||||
$event->setResponse($this->redirect('entity.user.edit_form', ['user' => $this->account->id()]));
|
||||
break;
|
||||
}
|
||||
}
|
||||
elseif ($route_name === 'user.page') {
|
||||
$event->setResponse($this->redirect('user.login'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
// Use a higher priority than
|
||||
// \Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber, because there's
|
||||
// no need to log the exception if we can redirect.
|
||||
$events[KernelEvents::EXCEPTION][] = ['onException', 75];
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\user\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Routing\RouteMatch;
|
||||
use Drupal\Core\Routing\UrlGeneratorTrait;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\Site\MaintenanceModeInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
/**
|
||||
* Maintenance mode subscriber to log out users.
|
||||
*/
|
||||
class MaintenanceModeSubscriber implements EventSubscriberInterface {
|
||||
|
||||
use UrlGeneratorTrait;
|
||||
|
||||
/**
|
||||
* The maintenance mode.
|
||||
*
|
||||
* @var \Drupal\Core\Site\MaintenanceMode
|
||||
*/
|
||||
protected $maintenanceMode;
|
||||
|
||||
/**
|
||||
* The current account.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
/**
|
||||
* Constructs a new MaintenanceModeSubscriber.
|
||||
*
|
||||
* @param \Drupal\Core\Site\MaintenanceModeInterface $maintenance_mode
|
||||
* The maintenance mode.
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The current user.
|
||||
*/
|
||||
public function __construct(MaintenanceModeInterface $maintenance_mode, AccountInterface $account) {
|
||||
$this->maintenanceMode = $maintenance_mode;
|
||||
$this->account = $account;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout users if site is in maintenance mode.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function onKernelRequestMaintenance(GetResponseEvent $event) {
|
||||
$request = $event->getRequest();
|
||||
$route_match = RouteMatch::createFromRequest($request);
|
||||
if ($this->maintenanceMode->applies($route_match)) {
|
||||
// If the site is offline, log out unprivileged users.
|
||||
if ($this->account->isAuthenticated() && !$this->maintenanceMode->exempt($this->account)) {
|
||||
user_logout();
|
||||
// Redirect to homepage.
|
||||
$event->setResponse($this->redirect($this->url('<front>')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
$events[KernelEvents::REQUEST][] = ['onKernelRequestMaintenance', 31];
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\user\EventSubscriber;
|
||||
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Session\AccountInterface;
|
||||
use Drupal\Core\Site\Settings;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
|
||||
use Symfony\Component\HttpKernel\KernelEvents;
|
||||
|
||||
/**
|
||||
* Updates the current user's last access time.
|
||||
*/
|
||||
class UserRequestSubscriber implements EventSubscriberInterface {
|
||||
|
||||
/**
|
||||
* The current account.
|
||||
*
|
||||
* @var \Drupal\Core\Session\AccountInterface
|
||||
*/
|
||||
protected $account;
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* Constructs a new UserRequestSubscriber.
|
||||
*
|
||||
* @param \Drupal\Core\Session\AccountInterface $account
|
||||
* The current user.
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
*/
|
||||
public function __construct(AccountInterface $account, EntityManagerInterface $entity_manager) {
|
||||
$this->account = $account;
|
||||
$this->entityManager = $entity_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the current user's last access time.
|
||||
*
|
||||
* @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
|
||||
* The event to process.
|
||||
*/
|
||||
public function onKernelTerminate(PostResponseEvent $event) {
|
||||
if ($this->account->isAuthenticated() && REQUEST_TIME - $this->account->getLastAccessedTime() > Settings::get('session_write_interval', 180)) {
|
||||
// Do that no more than once per 180 seconds.
|
||||
/** @var \Drupal\user\UserStorageInterface $storage */
|
||||
$storage = $this->entityManager->getStorage('user');
|
||||
$storage->updateLastAccessTimestamp($this->account, REQUEST_TIME);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function getSubscribedEvents() {
|
||||
// Should go before other subscribers start to write their caches. Notably
|
||||
// before \Drupal\Core\EventSubscriber\KernelDestructionSubscriber to
|
||||
// prevent instantiation of destructed services.
|
||||
$events[KernelEvents::TERMINATE][] = ['onKernelTerminate', 300];
|
||||
return $events;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue