-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
179 lines (151 loc) · 5.35 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* @link https://github.com/monarc-project for the canonical source repository
* @copyright Copyright (c) 2016-2024 Luxembourg House of Cybersecurity LHC.lu - Licensed under GNU Affero GPL v3
* @license MONARC is licensed under GNU Affero General Public License version 3
*/
namespace Monarc\BackOffice;
use Laminas\Stdlib\ResponseInterface;
use Monarc\Core\Service\ConnectedUserService;
use Laminas\Console\Request;
use Laminas\Mvc\ModuleRouteListener;
use Laminas\Mvc\MvcEvent;
use Laminas\Permissions\Rbac\Rbac;
use Laminas\Permissions\Rbac\Role;
use Laminas\View\Model\JsonModel;
class Module
{
public function onBootstrap(MvcEvent $e)
{
if (!$e->getRequest() instanceof Request) {
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
$this->initRbac($e);
$eventManager->attach(MvcEvent::EVENT_ROUTE, [$this, 'checkRbac'], 0);
$eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onDispatchError'], 0);
$eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, [$this, 'onRenderError'], 0);
}
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function onDispatchError($e)
{
return $this->getJsonModelError($e);
}
public function onRenderError($e)
{
return $this->getJsonModelError($e);
}
public function getJsonModelError($e)
{
$error = $e->getError();
if (!$error) {
return;
}
$exception = $e->getParam('exception');
$exceptionJson = [];
if ($exception !== null) {
$exceptionJson = [
'class' => get_class($exception),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'stacktrace' => $exception->getTraceAsString(),
];
if ($exception->getCode() >= 400 && $exception->getCode() < 600) {
$e->getResponse()->setStatusCode($exception->getCode());
}
}
$errorJson = [
'message' => $exception ? $exception->getMessage(
) : 'An error occurred during execution; please try again later.',
'error' => $error,
'exception' => $exceptionJson,
];
if ($error === 'error-router-no-match') {
$errorJson['message'] = 'Resource not found.';
}
if ($exception !== null && $exception->getCode() === 400) {
$model = new JsonModel([
'errors' => [json_decode($exception->getMessage(), true, 512, JSON_THROW_ON_ERROR)],
]);
} else {
$model = new JsonModel(['errors' => [$errorJson]]);
}
$e->setResult($model);
return $model;
}
/**
* init Rbac
*
* @param MvcEvent $e
*/
public function initRbac(MvcEvent $e)
{
$sm = $e->getApplication()->getServiceManager();
$config = $sm->get('Config');
$globalPermissions = isset($config['permissions']) ? $config['permissions'] : [];
$rolesPermissions = isset($config['roles']) ? $config['roles'] : [];
$rbac = new Rbac();
foreach ($rolesPermissions as $role => $permissions) {
$role = new Role($role);
//global permissions
foreach ($globalPermissions as $globalPermission) {
if (!$role->hasPermission($globalPermission)) {
$role->addPermission($globalPermission);
}
}
//role permissions
foreach ($permissions as $permission) {
if (!$role->hasPermission($permission)) {
$role->addPermission($permission);
}
}
$rbac->addRole($role);
}
//add role for guest (user not logged)
$role = new Role('guest');
foreach ($globalPermissions as $globalPermission) {
if (!$role->hasPermission($globalPermission)) {
$role->addPermission($globalPermission);
}
}
$rbac->addRole($role);
//setting to view
$e->getViewModel()->rbac = $rbac;
}
/**
* Check Rbac
*
* @param MvcEvent $e
*
* @return ResponseInterface|void
*/
public function checkRbac(MvcEvent $e)
{
$route = $e->getRouteMatch()->getMatchedRouteName();
$sm = $e->getApplication()->getServiceManager();
/** @var ConnectedUserService $connectedUserService */
$connectedUserService = $sm->get(ConnectedUserService::class);
$connectedUser = $connectedUserService->getConnectedUser();
$roles[] = 'guest';
if ($connectedUser !== null) {
$roles = $connectedUser->getRolesArray();
}
$isGranted = false;
foreach ($roles as $role) {
if ($e->getViewModel()->rbac->isGranted($role, $route)) {
$isGranted = true;
break;
}
}
if (!$isGranted) {
$response = $e->getResponse();
$response->setStatusCode($connectedUser === null ? 401 : 403);
return $response;
}
}
}