1 | <?php namespace Arcanedev\LaravelAuth\Models; |
||
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 | * @var array |
||
83 | */ |
||
84 | protected $dispatchesEvents = [ |
||
85 | 'creating' => CreatingRole::class, |
||
86 | 'created' => CreatedRole::class, |
||
87 | 'updating' => UpdatingRole::class, |
||
88 | 'updated' => UpdatedRole::class, |
||
89 | 'saving' => SavingRole::class, |
||
90 | 'saved' => SavedRole::class, |
||
91 | 'deleting' => DeletingRole::class, |
||
92 | 'deleted' => DeletedRole::class, |
||
93 | ]; |
||
94 | |||
95 | /* ----------------------------------------------------------------- |
||
96 | | Constructor |
||
97 | | ----------------------------------------------------------------- |
||
98 | */ |
||
99 | |||
100 | /** |
||
101 | * Create a new Eloquent model instance. |
||
102 | * |
||
103 | * @param array $attributes |
||
104 | */ |
||
105 | 76 | public function __construct(array $attributes = []) |
|
111 | |||
112 | /* ----------------------------------------------------------------- |
||
113 | | Relationships |
||
114 | | ----------------------------------------------------------------- |
||
115 | */ |
||
116 | |||
117 | /** |
||
118 | * Role belongs to many users. |
||
119 | * |
||
120 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
121 | */ |
||
122 | 12 | public function users() |
|
132 | |||
133 | /** |
||
134 | * Role belongs to many permissions. |
||
135 | * |
||
136 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
137 | */ |
||
138 | 22 | public function permissions() |
|
148 | |||
149 | /* ----------------------------------------------------------------- |
||
150 | | Getters & Setters |
||
151 | | ----------------------------------------------------------------- |
||
152 | */ |
||
153 | |||
154 | /** |
||
155 | * Set the name attribute. |
||
156 | * |
||
157 | * @param string $name |
||
158 | */ |
||
159 | 66 | public function setNameAttribute($name) |
|
164 | |||
165 | /** |
||
166 | * Set the slug attribute. |
||
167 | * |
||
168 | * @param string $slug |
||
169 | */ |
||
170 | 66 | public function setSlugAttribute($slug) |
|
174 | |||
175 | /* ------------------------------------------------------------------------------------------------ |
||
176 | | CRUD Functions |
||
177 | | ------------------------------------------------------------------------------------------------ |
||
178 | */ |
||
179 | |||
180 | /** |
||
181 | * Activate the model. |
||
182 | * |
||
183 | * @param bool $save |
||
184 | * |
||
185 | * @return bool |
||
186 | */ |
||
187 | 2 | public function activate($save = true) |
|
191 | |||
192 | /** |
||
193 | * Deactivate the model. |
||
194 | * |
||
195 | * @param bool $save |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 4 | public function deactivate($save = true) |
|
203 | |||
204 | /** |
||
205 | * Attach a permission to a role. |
||
206 | * |
||
207 | * @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
||
208 | * @param bool $reload |
||
209 | */ |
||
210 | 6 | public function attachUser($user, $reload = true) |
|
220 | |||
221 | // TODO: Adding attach multiple users to a role ? |
||
222 | |||
223 | /** |
||
224 | * Detach a user from a role. |
||
225 | * |
||
226 | * @param \Arcanesoft\Contracts\Auth\Models\User|int $user |
||
227 | * @param bool $reload |
||
228 | * |
||
229 | * @return int |
||
230 | */ |
||
231 | 2 | public function detachUser($user, $reload = true) |
|
241 | |||
242 | // TODO: Adding detach multiple users to a role ? |
||
243 | |||
244 | /** |
||
245 | * Detach all users from a role. |
||
246 | * |
||
247 | * @param bool $reload |
||
248 | * |
||
249 | * @return int |
||
250 | */ |
||
251 | 4 | public function detachAllUsers($reload = true) |
|
261 | |||
262 | /** |
||
263 | * Attach a permission to a role. |
||
264 | * |
||
265 | * @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
||
266 | * @param bool $reload |
||
267 | */ |
||
268 | 18 | public function attachPermission($permission, $reload = true) |
|
278 | |||
279 | // TODO: Adding attach multiple permissions to a role ? |
||
280 | |||
281 | /** |
||
282 | * Detach a permission from a role. |
||
283 | * |
||
284 | * @param \Arcanesoft\Contracts\Auth\Models\Permission|int $permission |
||
285 | * @param bool $reload |
||
286 | * |
||
287 | * @return int |
||
288 | */ |
||
289 | 2 | public function detachPermission($permission, $reload = true) |
|
301 | |||
302 | // TODO: Adding detach multiple permissions to a role ? |
||
303 | |||
304 | /** |
||
305 | * Detach all permissions from a role. |
||
306 | * |
||
307 | * @param bool $reload |
||
308 | * |
||
309 | * @return int |
||
310 | */ |
||
311 | 4 | public function detachAllPermissions($reload = true) |
|
323 | |||
324 | /* ----------------------------------------------------------------- |
||
325 | | Check Methods |
||
326 | | ----------------------------------------------------------------- |
||
327 | */ |
||
328 | |||
329 | /** |
||
330 | * Check if role has the given user (User Model or Id). |
||
331 | * |
||
332 | * @param \Arcanesoft\Contracts\Auth\Models\User|int $id |
||
333 | * |
||
334 | * @return bool |
||
335 | */ |
||
336 | 6 | public function hasUser($id) |
|
342 | |||
343 | /** |
||
344 | * Check if role has the given permission (Permission Model or Id). |
||
345 | * |
||
346 | * @param \Arcanesoft\Contracts\Auth\Models\Permission|int $id |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | 18 | public function hasPermission($id) |
|
356 | |||
357 | /** |
||
358 | * Check if role is associated with a permission by slug. |
||
359 | * |
||
360 | * @param string $slug |
||
361 | * |
||
362 | * @return bool |
||
363 | */ |
||
364 | 6 | public function can($slug) |
|
370 | |||
371 | /** |
||
372 | * Check if a role is associated with any of given permissions. |
||
373 | * |
||
374 | * @param \Illuminate\Support\Collection|array $permissions |
||
375 | * @param \Illuminate\Support\Collection &$failed |
||
376 | * |
||
377 | * @return bool |
||
378 | */ |
||
379 | 4 | public function canAny($permissions, &$failed = null) |
|
389 | |||
390 | /** |
||
391 | * Check if role is associated with all given permissions. |
||
392 | * |
||
393 | * @param \Illuminate\Support\Collection|array $permissions |
||
394 | * @param \Illuminate\Support\Collection &$failed |
||
395 | * |
||
396 | * @return bool |
||
397 | */ |
||
398 | 2 | public function canAll($permissions, &$failed = null) |
|
404 | |||
405 | /** |
||
406 | * Check if the role is locked. |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | 4 | public function isLocked() |
|
414 | |||
415 | /** |
||
416 | * Check if slug is the same as the given value. |
||
417 | * |
||
418 | * @param string $value |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | 14 | public function hasSlug($value) |
|
426 | |||
427 | /* ----------------------------------------------------------------- |
||
428 | | Other Methods |
||
429 | | ----------------------------------------------------------------- |
||
430 | */ |
||
431 | |||
432 | /** |
||
433 | * Load the users. |
||
434 | * |
||
435 | * @param bool $load |
||
436 | * |
||
437 | * @return self |
||
438 | */ |
||
439 | 8 | protected function loadUsers($load = true) |
|
443 | |||
444 | /** |
||
445 | * Load the permissions. |
||
446 | * |
||
447 | * @param bool $load |
||
448 | * |
||
449 | * @return self |
||
450 | */ |
||
451 | 18 | protected function loadPermissions($load = true) |
|
455 | |||
456 | /** |
||
457 | * Slugify the value. |
||
458 | * |
||
459 | * @param string $value |
||
460 | * |
||
461 | * @return string |
||
462 | */ |
||
463 | 66 | protected function slugify($value) |
|
467 | } |
||
468 |