1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Event; |
4
|
|
|
|
5
|
|
|
use App\Controller\AdminAuthenticatedInterface; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
8
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
10
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
11
|
|
|
use Symfony\Component\HttpKernel\HttpKernel; |
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
13
|
|
|
|
14
|
|
|
class AdminSubscriber implements EventSubscriberInterface |
15
|
|
|
{ |
16
|
|
|
private $administrators; |
17
|
|
|
|
18
|
|
|
public function __construct(ContainerInterface $container) |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$this->administrators = explode(',', env('ADMINISTRATORS')); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function onKernelController(FilterControllerEvent $event) |
24
|
|
|
{ |
25
|
|
|
$controller = $event->getController(); |
26
|
|
|
|
27
|
|
|
/* |
28
|
|
|
* $controller passed can be either a class or a Closure. |
29
|
|
|
* This is not usual in Symfony but it may happen. |
30
|
|
|
* If it is a class, it comes in array format |
31
|
|
|
*/ |
32
|
|
|
if (!is_array($controller)) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($controller[0] instanceof AdminAuthenticatedInterface) { |
37
|
|
|
$isAdmin = false; |
38
|
|
|
if (isset($_SESSION['phpCAS']['user'])) { |
39
|
|
|
if (in_array($_SESSION['phpCAS']['user'], $this->administrators)) |
40
|
|
|
$isAdmin = true; |
41
|
|
|
} |
42
|
|
|
if (!$isAdmin) throw new AccessDeniedHttpException('Access forbidden.'); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
47
|
|
|
{ |
48
|
|
|
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) |
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
|
53
|
|
|
public static function getSubscribedEvents() |
54
|
|
|
{ |
55
|
|
|
return [ KernelEvents::CONTROLLER => 'onKernelController' ]; |
56
|
|
|
} |
57
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.