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