1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
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 = []) |
|
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() |
|
119 | |||
120 | /** |
||
121 | * Role belongs to many permissions. |
||
122 | * |
||
123 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
124 | */ |
||
125 | 33 | public function permissions() |
|
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) |
|
151 | |||
152 | /** |
||
153 | * Set the slug attribute. |
||
154 | * |
||
155 | * @param string $slug |
||
156 | */ |
||
157 | 99 | public function setSlugAttribute($slug) |
|
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) |
|
178 | |||
179 | /** |
||
180 | * Deactivate the model. |
||
181 | * |
||
182 | * @param bool $save |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 6 | public function deactivate($save = true) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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) |
|
395 | |||
396 | /** |
||
397 | * Check if the role is locked. |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | 6 | public function isLocked() |
|
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) |
|
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) |
|
434 | |||
435 | /** |
||
436 | * Load the permissions. |
||
437 | * |
||
438 | * @param bool $load |
||
439 | * |
||
440 | * @return self |
||
441 | */ |
||
442 | 27 | protected function loadPermissions($load = true) |
|
446 | |||
447 | /** |
||
448 | * Slugify the value. |
||
449 | * |
||
450 | * @param string $value |
||
451 | * |
||
452 | * @return string |
||
453 | */ |
||
454 | 99 | protected function slugify($value) |
|
458 | } |
||
459 |