1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
49 | class Role extends AbstractModel implements RoleContract |
||
50 | { |
||
51 | /* ----------------------------------------------------------------- |
||
52 | | Traits |
||
53 | | ----------------------------------------------------------------- |
||
54 | */ |
||
55 | |||
56 | use Activatable; |
||
57 | |||
58 | /* ----------------------------------------------------------------- |
||
59 | | Properties |
||
60 | | ----------------------------------------------------------------- |
||
61 | */ |
||
62 | |||
63 | /** |
||
64 | * The attributes that are mass assignable. |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $fillable = ['name', 'slug', 'description']; |
||
69 | |||
70 | /** |
||
71 | * The event map for the model. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $dispatchesEvents = [ |
||
76 | 'creating' => CreatingRole::class, |
||
77 | 'created' => CreatedRole::class, |
||
78 | 'updating' => UpdatingRole::class, |
||
79 | 'updated' => UpdatedRole::class, |
||
80 | 'saving' => SavingRole::class, |
||
81 | 'saved' => SavedRole::class, |
||
82 | 'deleting' => DeletingRole::class, |
||
83 | 'deleted' => DeletedRole::class, |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * The attributes that should be casted to native types. |
||
88 | * |
||
89 | * @var array |
||
90 | */ |
||
91 | protected $casts = [ |
||
92 | 'id' => 'integer', |
||
93 | 'is_active' => 'boolean', |
||
94 | 'is_locked' => 'boolean', |
||
95 | ]; |
||
96 | |||
97 | /* ----------------------------------------------------------------- |
||
98 | | Constructor |
||
99 | | ----------------------------------------------------------------- |
||
100 | */ |
||
101 | |||
102 | /** |
||
103 | * Create a new Eloquent model instance. |
||
104 | * |
||
105 | * @param array $attributes |
||
106 | */ |
||
107 | 76 | public function __construct(array $attributes = []) |
|
113 | |||
114 | /* ----------------------------------------------------------------- |
||
115 | | Relationships |
||
116 | | ----------------------------------------------------------------- |
||
117 | */ |
||
118 | |||
119 | /** |
||
120 | * Role belongs to many users. |
||
121 | * |
||
122 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
123 | */ |
||
124 | 12 | public function users() |
|
125 | { |
||
126 | return $this |
||
127 | 12 | ->belongsToMany( |
|
128 | 12 | config('laravel-auth.users.model', User::class), |
|
129 | 12 | $this->getPrefix().config('laravel-auth.role-user.table', 'permission_role') |
|
130 | ) |
||
131 | 12 | ->using(Pivots\RoleUser::class) |
|
132 | 12 | ->withTimestamps(); |
|
133 | } |
||
134 | |||
135 | /** |
||
136 | * Role belongs to many permissions. |
||
137 | * |
||
138 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
139 | */ |
||
140 | 22 | public function permissions() |
|
141 | { |
||
142 | return $this |
||
143 | 22 | ->belongsToMany( |
|
144 | 22 | config('laravel-auth.permissions.model', Permission::class), |
|
145 | 22 | $this->getPrefix().config('laravel-auth.permission-role.table', 'permission_role') |
|
146 | ) |
||
147 | 22 | ->using(Pivots\PermissionRole::class) |
|
148 | 22 | ->withTimestamps(); |
|
149 | } |
||
150 | |||
151 | /* ----------------------------------------------------------------- |
||
152 | | Getters & Setters |
||
153 | | ----------------------------------------------------------------- |
||
154 | */ |
||
155 | |||
156 | /** |
||
157 | * Set the name attribute. |
||
158 | * |
||
159 | * @param string $name |
||
160 | */ |
||
161 | 66 | public function setNameAttribute($name) |
|
166 | |||
167 | /** |
||
168 | * Set the slug attribute. |
||
169 | * |
||
170 | * @param string $slug |
||
171 | */ |
||
172 | 66 | public function setSlugAttribute($slug) |
|
176 | |||
177 | /* ------------------------------------------------------------------------------------------------ |
||
178 | | CRUD Functions |
||
179 | | ------------------------------------------------------------------------------------------------ |
||
180 | */ |
||
181 | |||
182 | /** |
||
183 | * Activate the model. |
||
184 | * |
||
185 | * @param bool $save |
||
186 | * |
||
187 | * @return bool |
||
188 | */ |
||
189 | 2 | public function activate($save = true) |
|
193 | |||
194 | /** |
||
195 | * Deactivate the model. |
||
196 | * |
||
197 | * @param bool $save |
||
198 | * |
||
199 | * @return bool |
||
200 | */ |
||
201 | 4 | public function deactivate($save = true) |
|
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 | 6 | public function attachUser($user, $reload = true) |
|
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 | 2 | public function detachUser($user, $reload = true) |
|
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 | 4 | public function detachAllUsers($reload = true) |
|
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 | 18 | public function attachPermission($permission, $reload = true) |
|
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 | 2 | public function detachPermission($permission, $reload = true) |
|
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 | 4 | public function detachAllPermissions($reload = true) |
|
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 | 6 | public function hasUser($id) |
|
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 | 18 | public function hasPermission($id) |
|
359 | |||
360 | /** |
||
361 | * Check if role is associated with a permission by slug. |
||
362 | * |
||
363 | * @param string $slug |
||
364 | * |
||
365 | * @return bool |
||
366 | */ |
||
367 | 6 | public function can($slug) |
|
376 | |||
377 | /** |
||
378 | * Check if a role is associated with any of given permissions. |
||
379 | * |
||
380 | * @param \Illuminate\Support\Collection|array $permissions |
||
381 | * @param \Illuminate\Support\Collection &$failed |
||
382 | * |
||
383 | * @return bool |
||
384 | */ |
||
385 | 4 | public function canAny($permissions, &$failed = null) |
|
395 | |||
396 | /** |
||
397 | * Check if role is associated with all given permissions. |
||
398 | * |
||
399 | * @param \Illuminate\Support\Collection|array $permissions |
||
400 | * @param \Illuminate\Support\Collection &$failed |
||
401 | * |
||
402 | * @return bool |
||
403 | */ |
||
404 | 2 | public function canAll($permissions, &$failed = null) |
|
410 | |||
411 | /** |
||
412 | * Check if the role is locked. |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | 4 | public function isLocked() |
|
420 | |||
421 | /** |
||
422 | * Check if slug is the same as the given value. |
||
423 | * |
||
424 | * @param string $value |
||
425 | * |
||
426 | * @return bool |
||
427 | */ |
||
428 | 14 | public function hasSlug($value) |
|
432 | |||
433 | /* ----------------------------------------------------------------- |
||
434 | | Other Methods |
||
435 | | ----------------------------------------------------------------- |
||
436 | */ |
||
437 | |||
438 | /** |
||
439 | * Load the users. |
||
440 | * |
||
441 | * @param bool $load |
||
442 | * |
||
443 | * @return self |
||
444 | */ |
||
445 | 8 | protected function loadUsers($load = true) |
|
449 | |||
450 | /** |
||
451 | * Load the permissions. |
||
452 | * |
||
453 | * @param bool $load |
||
454 | * |
||
455 | * @return self |
||
456 | */ |
||
457 | 18 | protected function loadPermissions($load = true) |
|
461 | |||
462 | /** |
||
463 | * Slugify the value. |
||
464 | * |
||
465 | * @param string $value |
||
466 | * |
||
467 | * @return string |
||
468 | */ |
||
469 | 66 | protected function slugify($value) |
|
473 | } |
||
474 |