|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\AttachedPermissionToRole; |
|
4
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\AttachedUserToRole; |
|
5
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\AttachingPermissionToRole; |
|
6
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\AttachingUserToRole; |
|
7
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\CreatedRole; |
|
8
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\CreatingRole; |
|
9
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DeletedRole; |
|
10
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DeletingRole; |
|
11
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachedAllPermissionsFromRole; |
|
12
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachedAllUsersFromRole; |
|
13
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachedPermissionFromRole; |
|
14
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachedUserFromRole; |
|
15
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachingAllPermissionsFromRole; |
|
16
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachingAllUsersFromRole; |
|
17
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachingPermissionFromRole; |
|
18
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\DetachingUserFromRole; |
|
19
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\SavedRole; |
|
20
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\SavingRole; |
|
21
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\UpdatedRole; |
|
22
|
|
|
use Arcanedev\LaravelAuth\Events\Roles\UpdatingRole; |
|
23
|
|
|
use Arcanedev\LaravelAuth\Models\Traits\Activatable; |
|
24
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
|
25
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
|
26
|
|
|
use Illuminate\Support\Str; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Class Role |
|
30
|
|
|
* |
|
31
|
|
|
* @package Arcanedev\LaravelAuth\Models |
|
32
|
|
|
* @author ARCANEDEV <[email protected]> |
|
33
|
|
|
* |
|
34
|
|
|
* @property int id |
|
35
|
|
|
* @property string name |
|
36
|
|
|
* @property string slug |
|
37
|
|
|
* @property string description |
|
38
|
|
|
* @property bool is_active |
|
39
|
|
|
* @property bool is_locked |
|
40
|
|
|
* @property \Carbon\Carbon created_at |
|
41
|
|
|
* @property \Carbon\Carbon updated_at |
|
42
|
|
|
* |
|
43
|
|
|
* @property \Illuminate\Database\Eloquent\Collection users |
|
44
|
|
|
* @property \Illuminate\Database\Eloquent\Collection permissions |
|
45
|
|
|
* |
|
46
|
|
|
* @property \Arcanedev\LaravelAuth\Models\Pivots\RoleUser|\Arcanedev\LaravelAuth\Models\Pivots\PermissionRole pivot |
|
47
|
|
|
*/ |
|
48
|
|
|
class Role extends AbstractModel implements RoleContract |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
/* ----------------------------------------------------------------- |
|
51
|
|
|
| Traits |
|
52
|
|
|
| ----------------------------------------------------------------- |
|
53
|
|
|
*/ |
|
54
|
|
|
|
|
55
|
|
|
use Activatable; |
|
56
|
|
|
|
|
57
|
|
|
/* ----------------------------------------------------------------- |
|
58
|
|
|
| Properties |
|
59
|
|
|
| ----------------------------------------------------------------- |
|
60
|
|
|
*/ |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The attributes that are mass assignable. |
|
64
|
|
|
* |
|
65
|
|
|
* @var array |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $fillable = ['name', 'slug', 'description']; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The attributes that should be casted to native types. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $casts = [ |
|
75
|
|
|
'is_active' => 'boolean', |
|
76
|
|
|
'is_locked' => 'boolean', |
|
77
|
|
|
]; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* The event map for the model. |
|
81
|
|
|
* |
|
82
|
|
|
* Allows for object-based events for native Eloquent events. |
|
83
|
|
|
* |
|
84
|
|
|
* @var array |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $events = [ |
|
87
|
|
|
'creating' => CreatingRole::class, |
|
88
|
|
|
'created' => CreatedRole::class, |
|
89
|
|
|
'updating' => UpdatingRole::class, |
|
90
|
|
|
'updated' => UpdatedRole::class, |
|
91
|
|
|
'saving' => SavingRole::class, |
|
92
|
|
|
'saved' => SavedRole::class, |
|
93
|
|
|
'deleting' => DeletingRole::class, |
|
94
|
|
|
'deleted' => DeletedRole::class, |
|
95
|
|
|
]; |
|
96
|
|
|
|
|
97
|
|
|
/* ----------------------------------------------------------------- |
|
98
|
|
|
| Constructor |
|
99
|
|
|
| ----------------------------------------------------------------- |
|
100
|
|
|
*/ |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Create a new Eloquent model instance. |
|
104
|
|
|
* |
|
105
|
|
|
* @param array $attributes |
|
106
|
|
|
*/ |
|
107
|
114 |
|
public function __construct(array $attributes = []) |
|
108
|
|
|
{ |
|
109
|
114 |
|
parent::__construct($attributes); |
|
110
|
|
|
|
|
111
|
114 |
|
$this->setTable(config('laravel-auth.roles.table', 'roles')); |
|
112
|
114 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/* ----------------------------------------------------------------- |
|
115
|
|
|
| Relationships |
|
116
|
|
|
| ----------------------------------------------------------------- |
|
117
|
|
|
*/ |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Role belongs to many users. |
|
121
|
|
|
* |
|
122
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
123
|
|
|
*/ |
|
124
|
18 |
|
public function users() |
|
125
|
|
|
{ |
|
126
|
6 |
|
return $this |
|
127
|
18 |
|
->belongsToMany( |
|
128
|
18 |
|
config('laravel-auth.users.model', User::class), |
|
129
|
18 |
|
$this->getPrefix().config('laravel-auth.role-user.table', 'permission_role') |
|
130
|
6 |
|
) |
|
131
|
18 |
|
->using(Pivots\RoleUser::class) |
|
132
|
18 |
|
->withTimestamps(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Role belongs to many permissions. |
|
137
|
|
|
* |
|
138
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
139
|
|
|
*/ |
|
140
|
33 |
|
public function permissions() |
|
141
|
|
|
{ |
|
142
|
11 |
|
return $this |
|
143
|
33 |
|
->belongsToMany( |
|
144
|
33 |
|
config('laravel-auth.permissions.model', Permission::class), |
|
145
|
33 |
|
$this->getPrefix().config('laravel-auth.permission-role.table', 'permission_role') |
|
146
|
11 |
|
) |
|
147
|
33 |
|
->using(Pivots\PermissionRole::class) |
|
148
|
33 |
|
->withTimestamps(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/* ----------------------------------------------------------------- |
|
152
|
|
|
| Getters & Setters |
|
153
|
|
|
| ----------------------------------------------------------------- |
|
154
|
|
|
*/ |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Set the name attribute. |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $name |
|
160
|
|
|
*/ |
|
161
|
99 |
|
public function setNameAttribute($name) |
|
162
|
|
|
{ |
|
163
|
99 |
|
$this->attributes['name'] = $name; |
|
164
|
99 |
|
$this->setSlugAttribute($name); |
|
165
|
99 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Set the slug attribute. |
|
169
|
|
|
* |
|
170
|
|
|
* @param string $slug |
|
171
|
|
|
*/ |
|
172
|
99 |
|
public function setSlugAttribute($slug) |
|
173
|
|
|
{ |
|
174
|
99 |
|
$this->attributes['slug'] = $this->slugify($slug); |
|
175
|
99 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
178
|
|
|
| CRUD Functions |
|
179
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
180
|
|
|
*/ |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Activate the model. |
|
184
|
|
|
* |
|
185
|
|
|
* @param bool $save |
|
186
|
|
|
* |
|
187
|
|
|
* @return bool |
|
188
|
|
|
*/ |
|
189
|
3 |
|
public function activate($save = true) |
|
190
|
|
|
{ |
|
191
|
3 |
|
return $this->switchActive(true, $save); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Deactivate the model. |
|
196
|
|
|
* |
|
197
|
|
|
* @param bool $save |
|
198
|
|
|
* |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
6 |
|
public function deactivate($save = true) |
|
202
|
|
|
{ |
|
203
|
6 |
|
return $this->switchActive(false, $save); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Attach a permission to a role. |
|
208
|
|
|
* |
|
209
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
|
210
|
|
|
* @param bool $reload |
|
211
|
|
|
*/ |
|
212
|
9 |
|
public function attachUser($user, $reload = true) |
|
213
|
|
|
{ |
|
214
|
9 |
|
if ($this->hasUser($user)) return; |
|
215
|
|
|
|
|
216
|
9 |
|
event(new AttachingUserToRole($this, $user)); |
|
217
|
9 |
|
$this->users()->attach($user); |
|
218
|
9 |
|
event(new AttachedUserToRole($this, $user)); |
|
219
|
|
|
|
|
220
|
9 |
|
$this->loadUsers($reload); |
|
221
|
9 |
|
} |
|
222
|
|
|
|
|
223
|
|
|
// TODO: Adding attach multiple users to a role ? |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Detach a user from a role. |
|
227
|
|
|
* |
|
228
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
|
229
|
|
|
* @param bool $reload |
|
230
|
|
|
* |
|
231
|
|
|
* @return int |
|
232
|
|
|
*/ |
|
233
|
3 |
|
public function detachUser($user, $reload = true) |
|
234
|
|
|
{ |
|
235
|
3 |
|
event(new DetachingUserFromRole($this, $user)); |
|
236
|
3 |
|
$results = $this->users()->detach($user); |
|
237
|
3 |
|
event(new DetachedUserFromRole($this, $user, $results)); |
|
238
|
|
|
|
|
239
|
3 |
|
$this->loadUsers($reload); |
|
240
|
|
|
|
|
241
|
3 |
|
return $results; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
// TODO: Adding detach multiple users to a role ? |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Detach all users from a role. |
|
248
|
|
|
* |
|
249
|
|
|
* @param bool $reload |
|
250
|
|
|
* |
|
251
|
|
|
* @return int |
|
252
|
|
|
*/ |
|
253
|
6 |
|
public function detachAllUsers($reload = true) |
|
254
|
|
|
{ |
|
255
|
6 |
|
event(new DetachingAllUsersFromRole($this)); |
|
256
|
6 |
|
$results = $this->users()->detach(); |
|
257
|
6 |
|
event(new DetachedAllUsersFromRole($this, $results)); |
|
258
|
|
|
|
|
259
|
6 |
|
$this->loadUsers($reload); |
|
260
|
|
|
|
|
261
|
6 |
|
return $results; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
/** |
|
265
|
|
|
* Attach a permission to a role. |
|
266
|
|
|
* |
|
267
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
|
268
|
|
|
* @param bool $reload |
|
269
|
|
|
*/ |
|
270
|
27 |
|
public function attachPermission($permission, $reload = true) |
|
271
|
|
|
{ |
|
272
|
27 |
|
if ($this->hasPermission($permission)) return; |
|
273
|
|
|
|
|
274
|
27 |
|
event(new AttachingPermissionToRole($this, $permission)); |
|
275
|
27 |
|
$this->permissions()->attach($permission); |
|
276
|
27 |
|
event(new AttachedPermissionToRole($this, $permission)); |
|
277
|
|
|
|
|
278
|
27 |
|
$this->loadPermissions($reload); |
|
279
|
27 |
|
} |
|
280
|
|
|
|
|
281
|
|
|
// TODO: Adding attach multiple permissions to a role ? |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Detach a permission from a role. |
|
285
|
|
|
* |
|
286
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
|
287
|
|
|
* @param bool $reload |
|
288
|
|
|
* |
|
289
|
|
|
* @return int |
|
290
|
|
|
*/ |
|
291
|
3 |
|
public function detachPermission($permission, $reload = true) |
|
292
|
|
|
{ |
|
293
|
3 |
|
if ( ! $this->hasPermission($permission)) return 0; |
|
294
|
|
|
|
|
295
|
3 |
|
event(new DetachingPermissionFromRole($this, $permission)); |
|
296
|
3 |
|
$results = $this->permissions()->detach($permission); |
|
297
|
3 |
|
event(new DetachedPermissionFromRole($this, $permission, $results)); |
|
298
|
|
|
|
|
299
|
3 |
|
$this->loadPermissions($reload); |
|
300
|
|
|
|
|
301
|
3 |
|
return $results; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
// TODO: Adding detach multiple permissions to a role ? |
|
305
|
|
|
|
|
306
|
|
|
/** |
|
307
|
|
|
* Detach all permissions from a role. |
|
308
|
|
|
* |
|
309
|
|
|
* @param bool $reload |
|
310
|
|
|
* |
|
311
|
|
|
* @return int |
|
312
|
|
|
*/ |
|
313
|
6 |
|
public function detachAllPermissions($reload = true) |
|
314
|
|
|
{ |
|
315
|
6 |
|
if ($this->permissions->isEmpty()) return 0; |
|
316
|
|
|
|
|
317
|
3 |
|
event(new DetachingAllPermissionsFromRole($this)); |
|
318
|
3 |
|
$results = $this->permissions()->detach(); |
|
319
|
3 |
|
event(new DetachedAllPermissionsFromRole($this, $results)); |
|
320
|
|
|
|
|
321
|
3 |
|
$this->loadPermissions($reload); |
|
322
|
|
|
|
|
323
|
3 |
|
return $results; |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
/* ----------------------------------------------------------------- |
|
327
|
|
|
| Check Methods |
|
328
|
|
|
| ----------------------------------------------------------------- |
|
329
|
|
|
*/ |
|
330
|
|
|
|
|
331
|
|
|
/** |
|
332
|
|
|
* Check if role has the given user (User Model or Id). |
|
333
|
|
|
* |
|
334
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\User|int $id |
|
335
|
|
|
* |
|
336
|
|
|
* @return bool |
|
337
|
|
|
*/ |
|
338
|
9 |
|
public function hasUser($id) |
|
339
|
|
|
{ |
|
340
|
9 |
|
if ($id instanceof Eloquent) $id = $id->getKey(); |
|
|
|
|
|
|
341
|
|
|
|
|
342
|
9 |
|
return $this->users->contains('id', $id); |
|
343
|
|
|
} |
|
344
|
|
|
|
|
345
|
|
|
/** |
|
346
|
|
|
* Check if role has the given permission (Permission Model or Id). |
|
347
|
|
|
* |
|
348
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Permission|int $id |
|
349
|
|
|
* |
|
350
|
|
|
* @return bool |
|
351
|
|
|
*/ |
|
352
|
27 |
|
public function hasPermission($id) |
|
353
|
|
|
{ |
|
354
|
27 |
|
if ($id instanceof Eloquent) $id = $id->getKey(); |
|
|
|
|
|
|
355
|
|
|
|
|
356
|
27 |
|
return $this->permissions->contains('id', $id); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
/** |
|
360
|
|
|
* Check if role is associated with a permission by slug. |
|
361
|
|
|
* |
|
362
|
|
|
* @param string $slug |
|
363
|
|
|
* |
|
364
|
|
|
* @return bool |
|
365
|
|
|
*/ |
|
366
|
9 |
|
public function can($slug) |
|
367
|
|
|
{ |
|
368
|
9 |
|
if ( ! $this->isActive()) return false; |
|
369
|
|
|
|
|
370
|
9 |
|
return $this->permissions->filter->hasSlug($slug)->first() !== null; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Check if a role is associated with any of given permissions. |
|
375
|
|
|
* |
|
376
|
|
|
* @param \Illuminate\Support\Collection|array $permissions |
|
377
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
378
|
|
|
* |
|
379
|
|
|
* @return bool |
|
380
|
|
|
*/ |
|
381
|
6 |
|
public function canAny($permissions, &$failed = null) |
|
382
|
|
|
{ |
|
383
|
6 |
|
$permissions = is_array($permissions) ? collect($permissions) : $permissions; |
|
384
|
|
|
|
|
385
|
6 |
|
$failed = $permissions->reject(function ($permission) { |
|
386
|
6 |
|
return $this->can($permission); |
|
387
|
6 |
|
})->values(); |
|
388
|
|
|
|
|
389
|
6 |
|
return $permissions->count() !== $failed->count(); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* Check if role is associated with all given permissions. |
|
394
|
|
|
* |
|
395
|
|
|
* @param \Illuminate\Support\Collection|array $permissions |
|
396
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
397
|
|
|
* |
|
398
|
|
|
* @return bool |
|
399
|
|
|
*/ |
|
400
|
3 |
|
public function canAll($permissions, &$failed = null) |
|
401
|
|
|
{ |
|
402
|
3 |
|
$this->canAny($permissions, $failed); |
|
403
|
|
|
|
|
404
|
3 |
|
return $failed->isEmpty(); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Check if the role is locked. |
|
409
|
|
|
* |
|
410
|
|
|
* @return bool |
|
411
|
|
|
*/ |
|
412
|
6 |
|
public function isLocked() |
|
413
|
|
|
{ |
|
414
|
6 |
|
return $this->is_locked; |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
/** |
|
418
|
|
|
* Check if slug is the same as the given value. |
|
419
|
|
|
* |
|
420
|
|
|
* @param string $value |
|
421
|
|
|
* |
|
422
|
|
|
* @return bool |
|
423
|
|
|
*/ |
|
424
|
21 |
|
public function hasSlug($value) |
|
425
|
|
|
{ |
|
426
|
21 |
|
return $this->slug === $this->slugify($value); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/* ----------------------------------------------------------------- |
|
430
|
|
|
| Other Methods |
|
431
|
|
|
| ----------------------------------------------------------------- |
|
432
|
|
|
*/ |
|
433
|
|
|
|
|
434
|
|
|
/** |
|
435
|
|
|
* Load the users. |
|
436
|
|
|
* |
|
437
|
|
|
* @param bool $load |
|
438
|
|
|
* |
|
439
|
|
|
* @return self |
|
440
|
|
|
*/ |
|
441
|
12 |
|
protected function loadUsers($load = true) |
|
442
|
|
|
{ |
|
443
|
12 |
|
return $load ? $this->load('users') : $this; |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
/** |
|
447
|
|
|
* Load the permissions. |
|
448
|
|
|
* |
|
449
|
|
|
* @param bool $load |
|
450
|
|
|
* |
|
451
|
|
|
* @return self |
|
452
|
|
|
*/ |
|
453
|
27 |
|
protected function loadPermissions($load = true) |
|
454
|
|
|
{ |
|
455
|
27 |
|
return $load ? $this->load('permissions') : $this; |
|
456
|
|
|
} |
|
457
|
|
|
|
|
458
|
|
|
/** |
|
459
|
|
|
* Slugify the value. |
|
460
|
|
|
* |
|
461
|
|
|
* @param string $value |
|
462
|
|
|
* |
|
463
|
|
|
* @return string |
|
464
|
|
|
*/ |
|
465
|
99 |
|
protected function slugify($value) |
|
466
|
|
|
{ |
|
467
|
99 |
|
return Str::slug($value, config('laravel-auth.roles.slug-separator', '-')); |
|
468
|
|
|
} |
|
469
|
|
|
} |
|
470
|
|
|
|