class AccessDeniedSubscriber in SAML Authentication 8.2
Same name and namespace in other branches
- 8.3 src/EventSubscriber/AccessDeniedSubscriber.php \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber
- 4.x src/EventSubscriber/AccessDeniedSubscriber.php \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber
Redirects logged-in users when access is denied to /saml/login.
Hierarchy
- class \Drupal\samlauth\EventSubscriber\AccessDeniedSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AccessDeniedSubscriber
1 string reference to 'AccessDeniedSubscriber'
1 service uses AccessDeniedSubscriber
File
- src/
EventSubscriber/ AccessDeniedSubscriber.php, line 17
Namespace
Drupal\samlauth\EventSubscriberView source
class AccessDeniedSubscriber implements EventSubscriberInterface {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* Constructs a new redirect subscriber.
*
* @param \Drupal\Core\Session\AccountInterface $account
* The current user.
*/
public function __construct(AccountInterface $account) {
$this->account = $account;
}
/**
* 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 && $this->account
->isAuthenticated()) {
$route_name = RouteMatch::createFromRequest($event
->getRequest())
->getRouteName();
switch ($route_name) {
case 'samlauth.saml_controller_login':
case 'samlauth.saml_controller_acs':
// Redirect an authenticated user to the profile page.
$url = Url::fromRoute('entity.user.canonical', [
'user' => $this->account
->id(),
])
->toString();
$event
->setResponse(new LocalRedirectResponse($url));
}
}
}
/**
* {@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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AccessDeniedSubscriber:: |
protected | property | The current user. | |
AccessDeniedSubscriber:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
AccessDeniedSubscriber:: |
public | function | Redirects users when access is denied. | |
AccessDeniedSubscriber:: |
public | function | Constructs a new redirect subscriber. |