1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller; |
13
|
|
|
|
14
|
|
|
use Phalcon\Events\Manager; |
15
|
|
|
use Phalcon\Mvc\Dispatcher; |
16
|
|
|
use Zemit\Di\Injectable; |
17
|
|
|
use Zemit\Mvc\Controller\AbstractTrait\AbstractInjectable; |
18
|
|
|
|
19
|
|
|
trait Behavior |
20
|
|
|
{ |
21
|
|
|
use AbstractInjectable; |
22
|
|
|
|
23
|
|
|
public function beforeExecuteRoute(Dispatcher $dispatcher): void |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
// @todo use eventsManager from service provider instead |
26
|
|
|
$this->eventsManager->enablePriorities(true); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// @todo see if we can implement receiving an array of responses globally: V2 |
29
|
|
|
// $this->eventsManager->collectResponses(true); |
30
|
|
|
|
31
|
|
|
// retrieve events based on the config roles and features |
32
|
|
|
$permissions = $this->config->get('permissions')->toArray() ?? []; |
33
|
|
|
$featureList = $permissions['features'] ?? []; |
34
|
|
|
$roleList = $permissions['roles'] ?? []; |
35
|
|
|
|
36
|
|
|
foreach ($roleList as $role => $rolePermission) { |
37
|
|
|
// do not attach other roles behaviors |
38
|
|
|
if (!$this->identity->hasRole([$role])) { |
|
|
|
|
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (isset($rolePermission['features'])) { |
43
|
|
|
foreach ($rolePermission['features'] as $feature) { |
44
|
|
|
$rolePermission = array_merge_recursive($rolePermission, $featureList[$feature] ?? []); |
45
|
|
|
// @todo remove duplicates |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$behaviorsContext = $rolePermission['behaviors'] ?? []; |
50
|
|
|
foreach ($behaviorsContext as $className => $behaviors) { |
51
|
|
|
if (is_int($className) || get_class($this) === $className) { |
52
|
|
|
$this->attachBehaviors($behaviors, 'rest'); |
53
|
|
|
} |
54
|
|
|
if ($this->getModelClassName() === $className) { |
|
|
|
|
55
|
|
|
$this->attachBehaviors($behaviors, 'model'); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Attach a behavior to the events' manager. |
63
|
|
|
* |
64
|
|
|
* @param string $behavior The name of the behavior to attach. |
65
|
|
|
* @param string $eventType Optional. The event type to attach the behavior to. Default is 'rest'. |
66
|
|
|
* |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
|
|
public function attachBehavior(string $behavior, string $eventType = 'rest'): void |
70
|
|
|
{ |
71
|
|
|
$event = new $behavior(); |
72
|
|
|
|
73
|
|
|
// inject DI |
74
|
|
|
if ($event instanceof Injectable || method_exists($event, 'setDI')) { |
75
|
|
|
$event->setDI($this->getDI()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// attach behavior |
79
|
|
|
$this->eventsManager->attach($event->eventType ?? $eventType, $event, $event->priority ?? Manager::DEFAULT_PRIORITY); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Attach behaviors to the current object. |
84
|
|
|
* |
85
|
|
|
* @param array $behaviors The array of behaviors to attach. |
86
|
|
|
* @param string $eventType The event type to attach the behaviors to. Default is 'rest'. |
87
|
|
|
* |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
public function attachBehaviors(array $behaviors = [], string $eventType = 'rest'): void |
91
|
|
|
{ |
92
|
|
|
foreach ($behaviors as $behavior) { |
93
|
|
|
$this->attachBehavior($behavior, $eventType); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.