1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Explicit Architecture POC, |
7
|
|
|
* which is created on top of the Symfony Demo application. |
8
|
|
|
* |
9
|
|
|
* (c) Herberto Graça <[email protected]> |
10
|
|
|
* |
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
12
|
|
|
* file that was distributed with this source code. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Acme\App\Infrastructure\Auth\Authorization; |
16
|
|
|
|
17
|
|
|
use Acme\App\Core\Port\Auth\Authorization\AccessDeniedException; |
18
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
19
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
20
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
21
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException as SymfonyAccessDeniedException; |
22
|
|
|
|
23
|
|
|
final class AccessDeniedExceptionSubscriber implements EventSubscriberInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Return the subscribed events, their methods and possibly their priorities |
27
|
|
|
* (the higher the priority the earlier the method is called). |
28
|
|
|
* |
29
|
|
|
* @see http://symfony.com/doc/current/event_dispatcher.html#creating-an-event-subscriber |
30
|
|
|
*/ |
31
|
|
|
public static function getSubscribedEvents(): array |
32
|
|
|
{ |
33
|
|
|
return [ |
34
|
|
|
KernelEvents::EXCEPTION => ['handleException', 256], |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function handleException(GetResponseForExceptionEvent $event): void |
39
|
|
|
{ |
40
|
|
|
$exception = $event->getException(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
switch (true) { |
43
|
|
|
case $exception instanceof AccessDeniedException: |
44
|
|
|
/* |
45
|
|
|
* We translate our AccessDeniedException into Symfony AccessDeniedException, |
46
|
|
|
* so that the system continues to respond in the same way, by redirecting to the login screen |
47
|
|
|
*/ |
48
|
|
|
$newException = new SymfonyAccessDeniedException($exception->getMessage(), $exception); |
49
|
|
|
$newException->setAttributes(array_merge($exception->getRoleList(), [$exception->getAction()])); |
50
|
|
|
$newException->setSubject($exception->getSubject()); |
51
|
|
|
|
52
|
|
|
$event->setException($newException); |
|
|
|
|
53
|
|
|
|
54
|
|
|
break; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.