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