/
Role.php
124 lines (93 loc) · 2.53 KB
/
Role.php
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
<?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 (is_null($title)) {
return $this->title;
}
$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);
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();
return $this;
}
public function hasPermission(string $permission): bool
{
return $this->permissions->contains($permission);
}
public function isSuper(): bool
{
return $this->hasPermission('super');
}
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);
}
}