1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maklad\Permission\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use MongoDB\Laravel\Eloquent\Model; |
7
|
|
|
use Maklad\Permission\Contracts\PermissionInterface; |
8
|
|
|
use Maklad\Permission\Exceptions\PermissionAlreadyExists; |
9
|
|
|
use Maklad\Permission\Exceptions\PermissionDoesNotExist; |
10
|
|
|
use Maklad\Permission\Guard; |
11
|
|
|
use Maklad\Permission\Helpers; |
12
|
|
|
use Maklad\Permission\PermissionRegistrar; |
13
|
|
|
use Maklad\Permission\Traits\HasRoles; |
14
|
|
|
use Maklad\Permission\Traits\RefreshesPermissionCache; |
15
|
|
|
use ReflectionException; |
16
|
|
|
use function app; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class Permission |
20
|
|
|
* @property string $_id |
21
|
|
|
* @package Maklad\Permission\Models |
22
|
|
|
*/ |
23
|
|
|
class Permission extends Model implements PermissionInterface |
24
|
|
|
{ |
25
|
|
|
use HasRoles; |
26
|
|
|
use RefreshesPermissionCache; |
27
|
|
|
|
28
|
|
|
public $guarded = ['id']; |
29
|
|
|
protected Helpers $helpers; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Permission constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $attributes |
35
|
|
|
* |
36
|
123 |
|
* @throws ReflectionException |
37
|
|
|
*/ |
38
|
123 |
|
public function __construct(array $attributes = []) |
39
|
|
|
{ |
40
|
123 |
|
$attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); |
41
|
|
|
|
42
|
123 |
|
parent::__construct($attributes); |
43
|
|
|
|
44
|
123 |
|
$this->helpers = new Helpers(); |
45
|
123 |
|
|
46
|
|
|
$this->setTable(config('permission.collection_names.permissions')); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Create new Permission |
51
|
|
|
* |
52
|
|
|
* @param array $attributes |
53
|
|
|
* |
54
|
|
|
* @return $this|\Illuminate\Database\Eloquent\Model |
55
|
|
|
* @throws PermissionAlreadyExists |
56
|
123 |
|
* @throws ReflectionException |
57
|
|
|
*/ |
58
|
123 |
|
public static function create(array $attributes = []): \Illuminate\Database\Eloquent\Model|static |
|
|
|
|
59
|
123 |
|
{ |
60
|
|
|
$helpers = new Helpers(); |
61
|
123 |
|
$attributes['guard_name'] = $attributes['guard_name'] ?? (new Guard())->getDefaultName(static::class); |
62
|
123 |
|
|
63
|
123 |
|
if (static::getPermissions()->where('name', $attributes['name'])->where( |
64
|
123 |
|
'guard_name', |
65
|
1 |
|
$attributes['guard_name'] |
66
|
1 |
|
)->first()) { |
67
|
1 |
|
$name = (string)$attributes['name']; |
68
|
|
|
$guardName = (string)$attributes['guard_name']; |
69
|
|
|
throw new PermissionAlreadyExists($helpers->getPermissionAlreadyExistsMessage($name, $guardName)); |
70
|
123 |
|
} |
71
|
|
|
|
72
|
|
|
return static::query()->create($attributes); |
73
|
|
|
} |
74
|
123 |
|
|
75
|
|
|
/** |
76
|
|
|
* Find or create permission by its name (and optionally guardName). |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* @param string|null $guardName |
80
|
|
|
* |
81
|
|
|
* @return PermissionInterface |
82
|
|
|
* @throws ReflectionException |
83
|
|
|
*/ |
84
|
|
|
public static function findOrCreate(string $name, string $guardName = null): PermissionInterface |
85
|
|
|
{ |
86
|
|
|
$guardName = $guardName ?? (new Guard())->getDefaultName(static::class); |
87
|
1 |
|
|
88
|
|
|
$permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) { |
89
|
1 |
|
return $permission->name === $name && $permission->guard_name === $guardName; |
90
|
|
|
})->first(); |
91
|
1 |
|
|
92
|
1 |
|
if (!$permission) { |
93
|
1 |
|
$permission = static::create(['name' => $name, 'guard_name' => $guardName]); |
94
|
1 |
|
} |
95
|
|
|
|
96
|
1 |
|
return $permission; |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
1 |
|
* A permission can be applied to roles. |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
|
|
public function rolesQuery(): mixed |
104
|
|
|
{ |
105
|
|
|
$roleClass = $this->getRoleClass(); |
106
|
|
|
return $roleClass->query()->where('permission_ids', 'all', [$this->_id]); |
107
|
123 |
|
} |
108
|
|
|
|
109
|
123 |
|
/** |
110
|
|
|
* A permission can be applied to roles. |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function getRolesAttribute(): mixed |
114
|
|
|
{ |
115
|
|
|
return $this->rolesQuery()->get(); |
116
|
2 |
|
} |
117
|
|
|
|
118
|
2 |
|
/** |
119
|
|
|
* A permission belongs to some users of the model associated with its guard. |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function usersQuery(): mixed |
123
|
|
|
{ |
124
|
|
|
$usersClass = app($this->helpers->getModelForGuard($this->attributes['guard_name'])); |
125
|
|
|
return $usersClass->query()->where('permission_ids', 'all', [$this->_id]); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* A permission belongs to some users of the model associated with its guard. |
130
|
|
|
* @return mixed |
131
|
44 |
|
*/ |
132
|
|
|
public function getUsersAttribute(): mixed |
133
|
44 |
|
{ |
134
|
|
|
return $this->usersQuery()->get(); |
135
|
44 |
|
} |
136
|
|
|
|
137
|
44 |
|
/** |
138
|
7 |
|
* Find a permission by its name (and optionally guardName). |
139
|
7 |
|
* |
140
|
|
|
* @param string $name |
141
|
|
|
* @param string|null $guardName |
142
|
38 |
|
* |
143
|
|
|
* @return PermissionInterface |
144
|
|
|
* @throws ReflectionException |
145
|
|
|
*/ |
146
|
|
|
public static function findByName(string $name, string $guardName = null): PermissionInterface |
147
|
|
|
{ |
148
|
|
|
$guardName = $guardName ?? (new Guard())->getDefaultName(static::class); |
149
|
123 |
|
|
150
|
|
|
$permission = static::getPermissions()->filter(function ($permission) use ($name, $guardName) { |
151
|
123 |
|
return $permission->name === $name && $permission->guard_name === $guardName; |
152
|
|
|
})->first(); |
153
|
|
|
|
154
|
|
|
if (!$permission) { |
155
|
|
|
$helpers = new Helpers(); |
156
|
|
|
throw new PermissionDoesNotExist($helpers->getPermissionDoesNotExistMessage($name, $guardName)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $permission; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Get the current cached permissions. |
164
|
|
|
* @return Collection |
165
|
|
|
*/ |
166
|
|
|
protected static function getPermissions(): Collection |
167
|
|
|
{ |
168
|
|
|
return app(PermissionRegistrar::class)->getPermissions(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|