1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ThallesDella\GateKeeper; |
4
|
|
|
|
5
|
|
|
use ThallesDella\GateKeeper\Modules\CSRF; |
6
|
|
|
use ThallesDella\GateKeeper\Modules\Request; |
7
|
|
|
use ThallesDella\GateKeeper\Roles\Roles; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Gate Keeper | Class GateKeeper [ GATE KEEPER ] |
11
|
|
|
* |
12
|
|
|
* @category GateKeeper |
13
|
|
|
* @package ThallesDella\GateKeeper |
14
|
|
|
* @author Thalles D. koester <[email protected]> |
15
|
|
|
* @license https://choosealicense.com/licenses/mit/ MIT |
16
|
|
|
* @link https://github.com/thallesdella/gate-keeper |
17
|
|
|
*/ |
18
|
|
|
class GateKeeper |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* UserInfo instance |
22
|
|
|
* |
23
|
|
|
* @var UserInfo |
24
|
|
|
*/ |
25
|
|
|
private $user_info; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Roles instance |
29
|
|
|
* |
30
|
|
|
* @var Roles |
31
|
|
|
*/ |
32
|
|
|
private $roles; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* GateKeeper constructor. |
36
|
|
|
* |
37
|
|
|
* @param string|null $userRole Role of the login user or null for guest |
38
|
|
|
*/ |
39
|
|
|
public function __construct(?string $userRole) |
40
|
|
|
{ |
41
|
|
|
$this->roles = new Roles(); // Should always be on top |
42
|
|
|
$this->generateUserInfo($userRole); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get CSRF |
47
|
|
|
* |
48
|
|
|
* @return CSRF Csrf property |
49
|
|
|
*/ |
50
|
|
|
public static function csrf(): CSRF |
51
|
|
|
{ |
52
|
|
|
return new CSRF(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get Requests |
57
|
|
|
* |
58
|
|
|
* @return Request Requests property |
59
|
|
|
*/ |
60
|
|
|
public static function requests(): Request |
61
|
|
|
{ |
62
|
|
|
return new Request(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get Roles |
67
|
|
|
* |
68
|
|
|
* @return Roles Roles property |
69
|
|
|
*/ |
70
|
|
|
public function roles(): Roles |
71
|
|
|
{ |
72
|
|
|
return $this->roles; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get User Info |
77
|
|
|
* |
78
|
|
|
* @return UserInfo UserInfo property |
79
|
|
|
*/ |
80
|
|
|
public function userInfo(): UserInfo |
81
|
|
|
{ |
82
|
|
|
return $this->user_info; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Verify if visitor is in group |
87
|
|
|
* |
88
|
|
|
* @param string $groupName Group to be verified |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function visitorInGroup(string $groupName = 'guest'): bool |
93
|
|
|
{ |
94
|
|
|
if ($groupName == 'guest') { |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ($this->verifyGroup($groupName)) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Check is user have permission |
106
|
|
|
* |
107
|
|
|
* @param string $permission Permission to be checked |
108
|
|
|
* |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
public function checkPermission(string $permission): bool |
112
|
|
|
{ |
113
|
|
|
return in_array($permission, $this->user_info->permissions); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Get registered roles |
118
|
|
|
* |
119
|
|
|
* @return array Array of the registered roles |
120
|
|
|
*/ |
121
|
|
|
public function getRoles(): array |
122
|
|
|
{ |
123
|
|
|
return $this->roles->getRoles()->getArrayCopy(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Generate instance of the class userInfo |
128
|
|
|
* |
129
|
|
|
* @param string|null $userRole Role of the login user or null for guest |
130
|
|
|
* |
131
|
|
|
* @return void |
132
|
|
|
*/ |
133
|
|
|
private function generateUserInfo(?string $userRole = null): void |
134
|
|
|
{ |
135
|
|
|
if (isset($this->user_info->role) && empty($userRole)) { |
136
|
|
|
$this->user_info = new UserInfo($this->user_info->role, $this->roles); |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
$this->user_info = new UserInfo($userRole, $this->roles); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Verify if user is in group |
144
|
|
|
* |
145
|
|
|
* @param string $groupName Name of the group to restrict access |
146
|
|
|
* |
147
|
|
|
* @return bool |
148
|
|
|
*/ |
149
|
|
|
private function verifyGroup(string $groupName): bool |
150
|
|
|
{ |
151
|
|
|
$groupUser = $this->roles->getGroup($this->user_info->role); |
152
|
|
|
$groupAccess = $this->roles->getGroup($groupName); |
153
|
|
|
|
154
|
|
|
if ($groupUser->id >= $groupAccess->id) { |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
} |