1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Service; |
4
|
|
|
|
5
|
|
|
use PiouPiou\RibsAdminBundle\Entity\Module; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
8
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
9
|
|
|
use Symfony\Component\Routing\RouterInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
11
|
|
|
|
12
|
|
|
class AccessRights |
13
|
|
|
{ |
14
|
|
|
private $em; |
15
|
|
|
private $router; |
16
|
|
|
private $session; |
17
|
|
|
private $request; |
18
|
|
|
private $globals; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* AccessRights constructor. |
22
|
|
|
* @param ContainerInterface $em |
23
|
|
|
* @param RouterInterface $router |
24
|
|
|
* @param Session $session |
25
|
|
|
* @param RequestStack $request |
26
|
|
|
*/ |
27
|
|
|
public function __construct(ContainerInterface $em, RouterInterface $router, Session $session, RequestStack $request, Globals $globals) |
28
|
|
|
{ |
29
|
|
|
$this->em = $em; |
30
|
|
|
$this->router = $router; |
31
|
|
|
$this->session = $session; |
32
|
|
|
$this->request = $request; |
33
|
|
|
$this->globals = $globals; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function onKernelController() |
37
|
|
|
{ |
38
|
|
|
$route = $this->request->getCurrentRequest()->get("_route"); |
39
|
|
|
$admin_page = explode("_", $route)[0]; |
40
|
|
|
|
41
|
|
|
//to show admin panel |
42
|
|
|
if (in_array($route, ["_profiler", "_profiler_search_bar", "_wdt"])) { |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$ribs_admin_rights = json_decode(file_get_contents($this->globals->getBaseBundlePath() . "/Resources/json/ribsadmin_rights.json")); |
47
|
|
|
$modules_rights = $this->getModuleRights(); |
48
|
|
|
$ribs_admin_rights = (object) array_merge((array) $ribs_admin_rights, (array) $modules_rights); |
49
|
|
|
|
50
|
|
|
if ($admin_page == "ribsadmin" && strpos($route, "login") == false && strpos($route, "register") == false) { |
|
|
|
|
51
|
|
|
$route_right = $this->in_array_recursive($route, $ribs_admin_rights); |
52
|
|
|
|
53
|
|
|
if ($route_right === false) { |
54
|
|
|
throw new AccessDeniedException("No access"); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if ($this->testRouteRight($route_right) === true) { |
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw new AccessDeniedException("No access"); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return object |
67
|
|
|
* function that return all modules rights |
68
|
|
|
*/ |
69
|
|
|
private function getModuleRights() |
70
|
|
|
{ |
71
|
|
|
$modules = $this->em->get("doctrine")->getRepository(Module::class)->findBy([ |
72
|
|
|
"active" => true, |
73
|
|
|
"displayed" => true |
74
|
|
|
]); |
75
|
|
|
$rights = []; |
76
|
|
|
|
77
|
|
|
foreach ($modules as $module) { |
78
|
|
|
$rights[] = json_decode(file_get_contents($this->globals->getBaseBundlePath($module->getPackageName()) . "/Resources/json/ribsadmin_rights.json")); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return (object)$rights; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $right |
86
|
|
|
* @return bool |
87
|
|
|
* function that allow to test a right directly in the view |
88
|
|
|
*/ |
89
|
|
|
public function testRight(string $right): bool |
90
|
|
|
{ |
91
|
|
|
$user_rights = $this->getUserRights(); |
92
|
|
|
$list_rights = $this->getRightsListOfUser(); |
93
|
|
|
|
94
|
|
|
$all_rights = array_merge($user_rights, $list_rights); |
95
|
|
|
|
96
|
|
|
if (in_array($right, $all_rights)) { |
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param array $route_right |
105
|
|
|
* @return bool |
106
|
|
|
* test if route_right is found in users rights |
107
|
|
|
*/ |
108
|
|
|
private function testRouteRight(array $route_right): bool { |
109
|
|
|
$user_rights = $this->getUserRights(); |
110
|
|
|
$list_rights = $this->getRightsListOfUser(); |
111
|
|
|
|
112
|
|
|
$all_rights = array_merge($user_rights, $list_rights); |
113
|
|
|
|
114
|
|
|
foreach ($all_rights as $right) { |
115
|
|
|
if (in_array($right, $route_right)) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param $needle |
125
|
|
|
* @param $haystack |
126
|
|
|
* @return bool|mixed |
127
|
|
|
* fonction that search if the right contain an url or more |
128
|
|
|
*/ |
129
|
|
|
private function in_array_recursive($needle, $haystack) |
130
|
|
|
{ |
131
|
|
|
$rights = []; |
132
|
|
|
$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($haystack)); |
133
|
|
|
|
134
|
|
|
foreach ($it AS $element => $value) { |
135
|
|
|
if ($value == $needle) { |
136
|
|
|
$rights[] = $it->getInnerIterator()["right"]; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if (count($rights) === 0) { |
141
|
|
|
return false; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $rights; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return array function that retun a array that contain all user rights or empty array if no right found |
150
|
|
|
*/ |
151
|
|
|
private function getUserRights(): array |
152
|
|
|
{ |
153
|
|
|
$user_rights = $this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRights(); |
154
|
|
|
|
155
|
|
|
if ($user_rights) { |
156
|
|
|
return explode(",", $user_rights); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return [""]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @return array function that retun a array that contain all rights of rattached list right of the current user |
164
|
|
|
*/ |
165
|
|
|
private function getRightsListOfUser(): array { |
166
|
|
|
if ($this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRightList()) { |
167
|
|
|
$user_rights = $this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRightList()->getAccessRights(); |
168
|
|
|
|
169
|
|
|
if ($user_rights) { |
170
|
|
|
return explode(",", $user_rights); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
return [""]; |
175
|
|
|
} |
176
|
|
|
} |