|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexander Torosh <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Web; |
|
7
|
|
|
|
|
8
|
|
|
use Phalcon\Acl\Adapter\Memory; |
|
9
|
|
|
use Phalcon\Annotations\Adapter\Memory as AnnotationsMemory; |
|
10
|
|
|
use Phalcon\Di\AbstractInjectionAware; |
|
11
|
|
|
use Phalcon\Di\DiInterface; |
|
12
|
|
|
use Phalcon\Events\Event; |
|
13
|
|
|
use Phalcon\Events\Manager; |
|
14
|
|
|
use Phalcon\Mvc\Dispatcher; |
|
15
|
|
|
use Web\Exceptions\AccessDeniedException; |
|
16
|
|
|
|
|
17
|
|
|
class EventsManager extends AbstractInjectionAware |
|
18
|
|
|
{ |
|
19
|
|
|
// @var $eventsManager Manager |
|
20
|
|
|
private $eventsManager; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(DiInterface $container) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->setDI($container); |
|
25
|
|
|
$this->init(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getEventsManager(): Manager |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->eventsManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
private function init() |
|
34
|
|
|
{ |
|
35
|
|
|
$eventsManager = new Manager(); |
|
36
|
|
|
|
|
37
|
|
|
$eventsManager->attach('dispatch:beforeExecuteRoute', function (Event $event, Dispatcher $dispatcher) { |
|
38
|
|
|
return $this->dispatchBeforeExecuteRoute($dispatcher); |
|
39
|
|
|
}); |
|
40
|
|
|
|
|
41
|
|
|
$this->eventsManager = $eventsManager; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @throws AccessDeniedException |
|
46
|
|
|
*/ |
|
47
|
|
|
private function dispatchBeforeExecuteRoute(Dispatcher $dispatcher) |
|
48
|
|
|
{ |
|
49
|
|
|
$container = $this->getDI(); |
|
50
|
|
|
|
|
51
|
|
|
// Acl annotations |
|
52
|
|
|
$this->handleControllerAccessAnnotations($container, $dispatcher); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @throws AccessDeniedException |
|
57
|
|
|
*/ |
|
58
|
|
|
private function handleControllerAccessAnnotations(DiInterface $container, Dispatcher $dispatcher) |
|
59
|
|
|
{ |
|
60
|
|
|
// @var $acl Memory |
|
61
|
|
|
$acl = $container->get('acl'); |
|
62
|
|
|
|
|
63
|
|
|
// @TODO Replace with real value |
|
64
|
|
|
$sessionRole = 'editor'; |
|
65
|
|
|
|
|
66
|
|
|
// @var $annotations AnnotationsMemory |
|
67
|
|
|
$annotations = $container->get('annotations'); |
|
68
|
|
|
|
|
69
|
|
|
$controllerName = $dispatcher->getControllerClass(); |
|
70
|
|
|
$actionName = $dispatcher->getActionName() |
|
71
|
|
|
.'Action'; |
|
72
|
|
|
|
|
73
|
|
|
$acl->addComponent($controllerName, $actionName); |
|
74
|
|
|
|
|
75
|
|
|
$data = $annotations->getMethod($controllerName, $actionName); |
|
76
|
|
|
|
|
77
|
|
|
if ($data->has('Access')) { |
|
78
|
|
|
$annotationAccess = $data->get('Access'); |
|
79
|
|
|
$accessRoles = $annotationAccess->getArguments(); |
|
80
|
|
|
|
|
81
|
|
|
if (!empty($accessRoles)) { |
|
82
|
|
|
foreach ($accessRoles as $role) { |
|
83
|
|
|
$acl->allow($role, $controllerName, $actionName); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (!$acl->isAllowed($sessionRole, $controllerName, $actionName)) { |
|
89
|
|
|
throw new AccessDeniedException("Sorry, but you don't have an access to this page."); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|