Passed
Push — master ( 31adf1...6657ff )
by Anthony
02:17
created

AccessRights   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 148
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 10
c 0
b 0
f 0
wmc 22

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testRight() 0 12 2
A getUserRights() 0 9 2
A in_array_recursive() 0 16 4
A testRouteRight() 0 13 3
A __construct() 0 8 1
C onKernelController() 0 26 7
A getRightsListOfUser() 0 10 3
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
	private $module;
19
	
20
	/**
21
	 * AccessRights constructor.
22
	 * @param ContainerInterface $em
23
	 * @param RouterInterface $router
24
	 * @param Session $session
25
	 * @param RequestStack $request
26
	 * @param Globals $globals
27
	 * @param ModuleService $module
28
	 */
29
	public function __construct(ContainerInterface $em, RouterInterface $router, Session $session, RequestStack $request, Globals $globals, ModuleService $module)
30
	{
31
		$this->em = $em;
32
		$this->router = $router;
33
		$this->session = $session;
34
		$this->request = $request;
35
		$this->globals = $globals;
36
		$this->module = $module;
37
	}
38
	
39
	public function onKernelController()
40
	{
41
		$route = $this->request->getCurrentRequest()->get("_route");
42
		$admin_page = explode("_", $route)[0];
43
		
44
		//to show admin panel
45
		if (in_array($route, ["_profiler", "_profiler_search_bar", "_wdt"])) {
46
			return;
47
		}
48
		
49
		$ribs_admin_rights = json_decode(file_get_contents($this->globals->getBaseBundlePath() . "/Resources/json/ribsadmin_rights.json"));
50
		$modules_rights = $this->module->getModuleRights();
51
		$ribs_admin_rights = (object) array_merge((array) $ribs_admin_rights, (array) $modules_rights);
52
		
53
		if ($admin_page == "ribsadmin" && strpos($route, "login") == false && strpos($route, "register") == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($route, 'register') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($route, 'login') of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
54
			$route_right = $this->in_array_recursive($route, $ribs_admin_rights);
55
			
56
			if ($route_right === false) {
57
				throw new AccessDeniedException("No access");
58
			}
59
			
60
			if ($this->testRouteRight($route_right) === true) {
61
				return;
62
			}
63
			
64
			throw new AccessDeniedException("No access");
65
		}
66
	}
67
	
68
	/**
69
	 * @param string $right
70
	 * @return bool
71
	 * function that allow to test a right directly in the view
72
	 */
73
	public function testRight(string $right): bool
74
	{
75
		$user_rights = $this->getUserRights();
76
		$list_rights = $this->getRightsListOfUser();
77
		
78
		$all_rights = array_merge($user_rights, $list_rights);
79
		
80
		if (in_array($right, $all_rights)) {
81
			return true;
82
		}
83
		
84
		return false;
85
	}
86
	
87
	/**
88
	 * @param array $route_right
89
	 * @return bool
90
	 * test if route_right is found in users rights
91
	 */
92
	private function testRouteRight(array $route_right): bool {
93
		$user_rights = $this->getUserRights();
94
		$list_rights = $this->getRightsListOfUser();
95
		
96
		$all_rights = array_merge($user_rights, $list_rights);
97
		
98
		foreach ($all_rights as $right) {
99
			if (in_array($right, $route_right)) {
100
				return true;
101
			}
102
		}
103
		
104
		return false;
105
	}
106
	
107
	/**
108
	 * @param $needle
109
	 * @param $haystack
110
	 * @return bool|mixed
111
	 * fonction that search if the right contain an url or more
112
	 */
113
	private function in_array_recursive($needle, $haystack)
114
	{
115
		$rights = [];
116
		$it = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($haystack));
117
		
118
		foreach ($it AS $element => $value) {
119
			if ($value == $needle) {
120
				$rights[] = $it->getInnerIterator()["right"];
121
			}
122
		}
123
		
124
		if (count($rights) === 0) {
125
			return false;
126
		}
127
		
128
		return $rights;
129
	}
130
	
131
	
132
	/**
133
	 * @return array function that retun a array that contain all user rights or empty array if no right found
134
	 */
135
	private function getUserRights(): array
136
	{
137
		$user_rights = $this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRights();
138
		
139
		if ($user_rights) {
140
			return explode(",", $user_rights);
141
		}
142
		
143
		return [""];
144
	}
145
	
146
	/**
147
	 * @return array function that retun a array that contain all rights of rattached list right of the current user
148
	 */
149
	private function getRightsListOfUser(): array {
150
		if ($this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRightList()) {
151
			$user_rights = $this->em->get("security.token_storage")->getToken()->getUser()->getUser()->getAccessRightList()->getAccessRights();
152
			
153
			if ($user_rights) {
154
				return explode(",", $user_rights);
155
			}
156
		}
157
		
158
		return [""];
159
	}
160
}