1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Bases\Model; |
4
|
|
|
use Arcanedev\LaravelAuth\Models\Relationships\RoleRelationships; |
5
|
|
|
use Arcanedev\LaravelAuth\Traits\Activatable; |
6
|
|
|
use Arcanesoft\Contracts\Auth\Models\Permission as PermissionContract; |
7
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
8
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Role |
13
|
|
|
* |
14
|
|
|
* @package Arcanedev\LaravelAuth\Models |
15
|
|
|
* @author ARCANEDEV <[email protected]> |
16
|
|
|
* |
17
|
|
|
* @property int id |
18
|
|
|
* @property string name |
19
|
|
|
* @property string slug |
20
|
|
|
* @property string description |
21
|
|
|
* @property bool is_active |
22
|
|
|
* @property bool is_locked |
23
|
|
|
* @property \Carbon\Carbon created_at |
24
|
|
|
* @property \Carbon\Carbon updated_at |
25
|
|
|
* @property \Illuminate\Database\Eloquent\Collection users |
26
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
27
|
|
|
*/ |
28
|
|
|
class Role extends Model implements RoleContract |
29
|
|
|
{ |
30
|
|
|
/* ------------------------------------------------------------------------------------------------ |
31
|
|
|
| Traits |
32
|
|
|
| ------------------------------------------------------------------------------------------------ |
33
|
|
|
*/ |
34
|
|
|
use RoleRelationships, Activatable; |
35
|
|
|
|
36
|
|
|
/* ------------------------------------------------------------------------------------------------ |
37
|
|
|
| Properties |
38
|
|
|
| ------------------------------------------------------------------------------------------------ |
39
|
|
|
*/ |
40
|
|
|
/** |
41
|
|
|
* The attributes that are mass assignable. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $fillable = ['name', 'slug', 'description']; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* The attributes that should be casted to native types. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $casts = [ |
53
|
|
|
'is_active' => 'boolean', |
54
|
|
|
'is_locked' => 'boolean', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/* ------------------------------------------------------------------------------------------------ |
58
|
|
|
| Constructor |
59
|
|
|
| ------------------------------------------------------------------------------------------------ |
60
|
|
|
*/ |
61
|
|
|
/** |
62
|
|
|
* Create a new Eloquent model instance. |
63
|
|
|
* |
64
|
|
|
* @param array $attributes |
65
|
|
|
*/ |
66
|
528 |
|
public function __construct(array $attributes = []) |
67
|
|
|
{ |
68
|
528 |
|
$this->setTable(config('laravel-auth.roles.table', 'roles')); |
69
|
|
|
|
70
|
528 |
|
parent::__construct($attributes); |
71
|
528 |
|
} |
72
|
|
|
|
73
|
|
|
/* ------------------------------------------------------------------------------------------------ |
74
|
|
|
| Getters & Setters |
75
|
|
|
| ------------------------------------------------------------------------------------------------ |
76
|
|
|
*/ |
77
|
|
|
/** |
78
|
|
|
* Set the name attribute. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
*/ |
82
|
224 |
|
public function setNameAttribute($name) |
83
|
|
|
{ |
84
|
224 |
|
$this->attributes['name'] = $name; |
85
|
224 |
|
$this->setSlugAttribute($name); |
86
|
224 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Set the slug attribute. |
90
|
|
|
* |
91
|
|
|
* @param string $slug |
92
|
|
|
*/ |
93
|
224 |
|
public function setSlugAttribute($slug) |
94
|
|
|
{ |
95
|
224 |
|
$this->attributes['slug'] = $this->slugify($slug); |
96
|
224 |
|
} |
97
|
|
|
|
98
|
|
|
/* ------------------------------------------------------------------------------------------------ |
99
|
|
|
| CRUD Functions |
100
|
|
|
| ------------------------------------------------------------------------------------------------ |
101
|
|
|
*/ |
102
|
|
|
/** |
103
|
|
|
* Attach a permission to a role. |
104
|
|
|
* |
105
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
106
|
|
|
* @param bool $reload |
107
|
|
|
*/ |
108
|
24 |
|
public function attachUser($user, $reload = true) |
109
|
|
|
{ |
110
|
24 |
|
if ( ! $this->hasUser($user)) { |
111
|
24 |
|
$this->users()->attach($user); |
112
|
24 |
|
$this->loadUsers($reload); |
113
|
18 |
|
} |
114
|
24 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Detach a user from a role. |
118
|
|
|
* |
119
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
120
|
|
|
* @param bool $reload |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
*/ |
124
|
8 |
|
public function detachUser($user, $reload = true) |
125
|
|
|
{ |
126
|
8 |
|
if ($user instanceof Eloquent) |
|
|
|
|
127
|
8 |
|
$user = (array) $user->getKey(); |
128
|
6 |
|
|
129
|
|
|
$result = $this->users()->detach($user); |
130
|
8 |
|
$this->loadUsers($reload); |
131
|
8 |
|
|
132
|
|
|
return $result; |
133
|
8 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Detach all users from a role. |
137
|
|
|
* |
138
|
|
|
* @param bool $reload |
139
|
|
|
* |
140
|
|
|
* @return int |
141
|
|
|
*/ |
142
|
|
|
public function detachAllUsers($reload = true) |
143
|
8 |
|
{ |
144
|
|
|
$result = $this->users()->detach(); |
145
|
8 |
|
$this->loadUsers($reload); |
146
|
8 |
|
|
147
|
|
|
return $result; |
148
|
8 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Check if role has the given user (User Model or Id). |
152
|
|
|
* |
153
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $id |
154
|
|
|
* |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function hasUser($id) |
158
|
24 |
|
{ |
159
|
|
|
if ($id instanceof Eloquent) |
|
|
|
|
160
|
24 |
|
$id = $id->getKey(); |
161
|
24 |
|
|
162
|
18 |
|
return $this->users->contains($id); |
163
|
|
|
} |
164
|
24 |
|
|
165
|
|
|
/** |
166
|
|
|
* Attach a permission to a role. |
167
|
|
|
* |
168
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
169
|
|
|
* @param bool $reload |
170
|
|
|
*/ |
171
|
|
|
public function attachPermission($permission, $reload = true) |
172
|
|
|
{ |
173
|
72 |
|
if ( ! $this->hasPermission($permission)) { |
174
|
|
|
$this->permissions()->attach($permission); |
175
|
72 |
|
$this->loadPermissions($reload); |
176
|
72 |
|
} |
177
|
72 |
|
} |
178
|
54 |
|
|
179
|
72 |
|
/** |
180
|
|
|
* Detach a permission from a role. |
181
|
|
|
* |
182
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
183
|
|
|
* @param bool $reload |
184
|
|
|
* |
185
|
|
|
* @return int |
186
|
|
|
*/ |
187
|
|
|
public function detachPermission($permission, $reload = true) |
188
|
|
|
{ |
189
|
8 |
|
if ($permission instanceof Eloquent) |
|
|
|
|
190
|
|
|
$permission = (array) $permission->getKey(); |
191
|
8 |
|
|
192
|
8 |
|
$result = $this->permissions()->detach($permission); |
193
|
6 |
|
$this->loadPermissions($reload); |
194
|
|
|
|
195
|
8 |
|
return $result; |
196
|
8 |
|
} |
197
|
|
|
|
198
|
8 |
|
/** |
199
|
|
|
* Detach all permissions from a role. |
200
|
|
|
* |
201
|
|
|
* @param bool $reload |
202
|
|
|
* |
203
|
|
|
* @return int |
204
|
|
|
*/ |
205
|
|
|
public function detachAllPermissions($reload = true) |
206
|
|
|
{ |
207
|
|
|
$result = $this->permissions()->detach(); |
208
|
8 |
|
$this->loadPermissions($reload); |
209
|
|
|
|
210
|
8 |
|
return $result; |
211
|
8 |
|
} |
212
|
|
|
|
213
|
8 |
|
/** |
214
|
|
|
* Check if role has the given permission (Permission Model or Id). |
215
|
|
|
* |
216
|
|
|
* @param mixed $id |
217
|
|
|
* |
218
|
|
|
* @return bool |
219
|
|
|
*/ |
220
|
|
|
public function hasPermission($id) |
221
|
|
|
{ |
222
|
|
|
if ($id instanceof Eloquent) |
|
|
|
|
223
|
72 |
|
$id = $id->getKey(); |
224
|
|
|
|
225
|
72 |
|
return $this->permissions->contains($id); |
226
|
72 |
|
} |
227
|
54 |
|
|
228
|
|
|
/* ------------------------------------------------------------------------------------------------ |
229
|
72 |
|
| Check Functions |
230
|
|
|
| ------------------------------------------------------------------------------------------------ |
231
|
|
|
*/ |
232
|
|
|
/** |
233
|
|
|
* Check if role is associated with a permission by slug. |
234
|
|
|
* |
235
|
|
|
* @param string $slug |
236
|
|
|
* |
237
|
|
|
* @return bool |
238
|
|
|
*/ |
239
|
|
|
public function can($slug) |
240
|
|
|
{ |
241
|
|
|
$permissions = $this->permissions->filter(function(PermissionContract $permission) use ($slug) { |
242
|
|
|
return $permission->checkSlug($slug); |
243
|
|
|
}); |
244
|
|
|
|
245
|
24 |
|
return $permissions->count() === 1; |
246
|
24 |
|
} |
247
|
24 |
|
|
248
|
|
|
/** |
249
|
24 |
|
* Check if a role is associated with any of given permissions. |
250
|
|
|
* |
251
|
|
|
* @param array $permissions |
252
|
|
|
* @param array &$failedPermissions |
253
|
|
|
* |
254
|
|
|
* @return bool |
255
|
|
|
*/ |
256
|
|
|
public function canAny(array $permissions, array &$failedPermissions = []) |
257
|
|
|
{ |
258
|
|
|
foreach ($permissions as $permission) { |
259
|
|
|
if ( ! $this->can($permission)) |
260
|
16 |
|
$failedPermissions[] = $permission; |
261
|
|
|
} |
262
|
16 |
|
|
263
|
16 |
|
return count($permissions) !== count($failedPermissions); |
264
|
16 |
|
} |
265
|
12 |
|
|
266
|
12 |
|
/** |
267
|
|
|
* Check if role is associated with all given permissions. |
268
|
16 |
|
* |
269
|
|
|
* @param array $permissions |
270
|
|
|
* @param array &$failedPermissions |
271
|
|
|
* |
272
|
|
|
* @return bool |
273
|
|
|
*/ |
274
|
|
|
public function canAll(array $permissions, array &$failedPermissions = []) |
275
|
|
|
{ |
276
|
|
|
$this->canAny($permissions, $failedPermissions); |
277
|
|
|
|
278
|
|
|
return count($failedPermissions) === 0; |
279
|
8 |
|
} |
280
|
|
|
|
281
|
8 |
|
/** |
282
|
|
|
* Check if the role is locked. |
283
|
8 |
|
* |
284
|
|
|
* @return bool |
285
|
|
|
*/ |
286
|
|
|
public function isLocked() |
287
|
|
|
{ |
288
|
|
|
return $this->is_locked; |
289
|
|
|
} |
290
|
|
|
|
291
|
16 |
|
/** |
292
|
|
|
* Check if slug is the same as the given value. |
293
|
16 |
|
* |
294
|
|
|
* @param string $value |
295
|
|
|
* |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function checkSlug($value) |
299
|
|
|
{ |
300
|
|
|
return $this->slug === $this->slugify($value); |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/* ------------------------------------------------------------------------------------------------ |
304
|
|
|
| Other Functions |
305
|
|
|
| ------------------------------------------------------------------------------------------------ |
306
|
|
|
*/ |
307
|
24 |
|
/** |
308
|
|
|
* Load the users. |
309
|
24 |
|
* |
310
|
|
|
* @param bool $load |
311
|
|
|
* |
312
|
|
|
* @return self |
313
|
|
|
*/ |
314
|
|
|
protected function loadUsers($load = true) |
315
|
|
|
{ |
316
|
|
|
return $load ? $this->load('users') : $this; |
317
|
|
|
} |
318
|
|
|
|
319
|
72 |
|
/** |
320
|
|
|
* Load the permissions. |
321
|
72 |
|
* |
322
|
|
|
* @param bool $load |
323
|
|
|
* |
324
|
|
|
* @return self |
325
|
|
|
*/ |
326
|
|
|
protected function loadPermissions($load = true) |
327
|
|
|
{ |
328
|
|
|
return $load ? $this->load('permissions') : $this; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* Slugify the value. |
333
|
|
|
* |
334
|
|
|
* @param string $value |
335
|
|
|
* |
336
|
|
|
* @return string |
337
|
|
|
*/ |
338
|
|
|
protected function slugify($value) |
339
|
|
|
{ |
340
|
|
|
return Str::slug($value, config('laravel-auth.roles.slug-separator', '-')); |
341
|
|
|
} |
342
|
|
|
} |
343
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.