-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRole.php
More file actions
executable file
·167 lines (150 loc) · 4.07 KB
/
Role.php
File metadata and controls
executable file
·167 lines (150 loc) · 4.07 KB
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
<?php
declare(strict_types=1);
namespace MaplePHP\Roles;
use MaplePHP\Roles\Exceptions\RolesException;
use MaplePHP\Roles\Interfaces\RoleInterface;
class Role implements RoleInterface
{
protected const ROLES = [
1 => "admin",
2 => "editor",
3 => "author",
4 => "member"
];
private $userRole;
private $data;
/**
* Set the current user role This will usally come from the database
* @param int|null $userRole
*/
public function __construct(?int $userRole = null)
{
if (!is_null($userRole)) {
$this->setUserRole($userRole);
}
}
/**
* Set the current user role This will usally come from the database
* @param int $userRole
* @return void
*/
public function setUserRole(int $userRole): void
{
$this->userRole = $userRole;
}
/**
* Set propagate a role with permissinons. Default will role permission be in a denied state.
* @param array|int $roleKey e.g r=1&i=1&u=1&d=1 Or [self::AUTHOR => "r=1&i=1&u=1&d=0"]
* @param string|null $queryStr r=1&i=1&u=1&d=1
*/
public function set(array|int $roleKey, ?string $queryStr = null): void
{
if (is_array($roleKey)) {
foreach ($roleKey as $role => $perm) {
$this->data[$role] = new Permission($perm);
}
} else {
$this->data[$roleKey] = new Permission($queryStr);
}
}
/**
* Get role int number from role key
* @param string $roleTitle
* @return int
*/
public static function getRole(string $roleTitle): int
{
$roleTitle = strtolower($roleTitle);
$role = array_search($roleTitle, static::ROLES);
if ($role === false) {
throw new RolesException("The user role ({$roleTitle}) does not exist.", 1);
}
return $role;
}
/**
* Check if role exists
* @param int $role
* @return bool
*/
public function hasRole(int $role): bool
{
return isset(static::ROLES[$role]);
}
/**
* Get role
* @param int $roleKey
* @return object
*/
public function getPermission(int $roleKey): object
{
if (is_null($this->userRole)) {
throw new RolesException("You need to specify the current users role with @setUserRole or the @constructer.", 1);
}
$this->validateRole($roleKey);
return $this->data[$roleKey];
}
/**
* List all currently supported roles
* @return array
*/
public static function getSupportedRoles(): array
{
return static::ROLES;
}
/**
* Check if the current user has the "right" permission
* @return bool
*/
public function hasRead(): bool
{
return ($this->getPermission($this->userRole)->getRead() > 0);
}
/**
* Check if the current user has the "insert" permission
* @return bool
*/
public function hasInsert(): bool
{
return ($this->getPermission($this->userRole)->getInsert() > 0);
}
/**
* Check if the current user has the "update" permission
* @return bool
*/
public function hasUpdate(): bool
{
return ($this->getPermission($this->userRole)->getUpdate() > 0);
}
/**
* Check if the current user has the "delete" permission
* @return bool
*/
public function hasDelete(): bool
{
return ($this->getPermission($this->userRole)->getDelete() > 0);
}
/**
* Validate roles
* @param int $role
* @return bool
*/
final protected function validateRole(int $role): bool
{
if (!$this->hasRole($role)) {
throw new RolesException("The user role ({$role}) does not exist.", 1);
}
if (!isset($this->data[$role])) {
$this->placeholder($role);
}
return true;
}
/**
* Placeholder
* @param int $role
* @return void
*/
final protected function placeholder(int $role): void
{
$this->data[$role] = new Permission(null);
}
}