-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermission.php
More file actions
executable file
·121 lines (109 loc) · 2.77 KB
/
Permission.php
File metadata and controls
executable file
·121 lines (109 loc) · 2.77 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
<?php
declare(strict_types=1);
namespace MaplePHP\Roles;
use MaplePHP\Roles\Exceptions\RolesException;
use MaplePHP\Roles\Interfaces\PermissionInterface;
class Permission implements PermissionInterface
{
protected const SYMLINK = [
"r" => "read",
"i" => "insert",
"u" => "update",
"d" => "delete"
];
protected int $read = 0;
protected int $insert = 0;
protected int $update = 0;
protected int $delete = 0;
/**
* propagate th permissions
* @param string|null $propagate
*/
public function __construct(?string $propagate = null)
{
$this->propagate($propagate);
}
/**
* Roles class will use call to read the proteted permissions objects
* @param string $name
* @param array $args
* @return int
*/
public function __call(string $name, array $args): int
{
$name = substr(strtolower($name), 3);
if (in_array($name, static::SYMLINK)) {
return $this->{$name};
}
throw new RolesException("The role {$name} does not exists in Permission class!", 1);
}
/**
* Add read permission
* @param int $role
* @return self
*/
public function read(int $role): self
{
$this->read = $role;
return $this;
}
/**
* Add insert permission
* @param int $role
* @return self
*/
public function insert(int $role): self
{
$this->insert = $role;
return $this;
}
/**
* Add update permission
* @param int $role
* @return self
*/
public function update(int $role): self
{
$this->update = $role;
return $this;
}
/**
* Add delete permission
* @param int $role
* @return self
*/
public function delete(int $role): self
{
$this->delete = $role;
return $this;
}
/**
* propagate the permission
* @param string|null $propagate
* @return void
*/
final protected function propagate(?string $propagate = null): void
{
if (!is_null($propagate)) {
parse_str($propagate, $arr);
foreach ($arr as $role => $permInt) {
$roleMethod = $this->getSymlink($role);
if (is_null($roleMethod)) {
$roleMethod = $role;
}
if (method_exists($this, $roleMethod)) {
$this->{$roleMethod}((int)$permInt);
}
}
}
}
/**
* Get get the permission name from shortcut
* @param string $roleMethod (r, i, u, d)
* @return string|null
*/
final protected function getSymlink(string $roleMethod): ?string
{
return (static::SYMLINK[$roleMethod] ?? null);
}
}