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