-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathRole.php
More file actions
147 lines (110 loc) · 3.27 KB
/
Role.php
File metadata and controls
147 lines (110 loc) · 3.27 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
<?php
namespace Statamic\Auth\File;
use Statamic\Auth\PermissionCache;
use Statamic\Auth\Role as BaseRole;
use Statamic\Events\RoleDeleted;
use Statamic\Events\RoleSaved;
use Statamic\Facades;
use Statamic\Preferences\HasPreferencesInProperty;
use Statamic\Support\Arr;
class Role extends BaseRole
{
use HasPreferencesInProperty;
protected $title;
protected $handle;
protected $originalHandle;
protected $permissions;
public function __construct()
{
$this->permissions = collect();
}
public function title(?string $title = null)
{
if (func_num_args() === 0) {
return $this->title ?? ucfirst($this->handle);
}
$this->title = $title;
return $this;
}
public function id(): string
{
return $this->handle();
}
public function handle(?string $handle = null)
{
if (func_num_args() === 0) {
return $this->handle;
}
if (! $this->originalHandle) {
$this->originalHandle = $this->handle;
}
$this->handle = $handle;
return $this;
}
public function originalHandle()
{
return $this->originalHandle;
}
public function permissions($permissions = null)
{
if (is_null($permissions)) {
return $this->permissions;
}
$this->permissions = collect($permissions);
app(PermissionCache::class)->clear();
return $this;
}
public function addPermission($permission)
{
$this->permissions = $this->permissions
->merge(Arr::wrap($permission))
->unique()
->values();
app(PermissionCache::class)->clear();
return $this;
}
public function removePermission($permission)
{
$this->permissions = $this->permissions
->diff(Arr::wrap($permission))
->values();
app(PermissionCache::class)->clear();
return $this;
}
public function hasPermission(string $permission): bool
{
if ($this->permissions->contains($permission)) {
return true;
}
return $this->permissions->contains(function ($rolePermission) use ($permission) {
return $this->matchesWildcard($rolePermission, $permission);
});
}
public function isSuper(): bool
{
return $this->hasPermission('super');
}
protected function matchesWildcard(string $wildcardPermission, string $requestedPermission): bool
{
if (! str_contains($wildcardPermission, '*')) {
return false;
}
$pattern = preg_quote($wildcardPermission, '/');
$pattern = str_replace('\*', '.*', $pattern);
$pattern = '/^'.$pattern.'$/';
return (bool) preg_match($pattern, $requestedPermission);
}
public function save()
{
// TODO: Move this logic into \Statamic\Auth\Role.php to be consistent with \Statamic\Auth\UserGroup?
Facades\Role::save($this);
RoleSaved::dispatch($this);
return $this;
}
public function delete()
{
// TODO: Move this logic into \Statamic\Auth\Role.php to be consistent with \Statamic\Auth\UserGroup?
Facades\Role::delete($this);
RoleDeleted::dispatch($this);
}
}