|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait Roleable |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelAuth\Traits |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
|
12
|
|
|
* @property \Illuminate\Database\Eloquent\Collection active_roles |
|
13
|
|
|
* |
|
14
|
|
|
* @method \Illuminate\Database\Eloquent\Relations\BelongsToMany roles() |
|
15
|
|
|
* @method \Arcanedev\LaravelAuth\Models\Traits\Roleable load(mixed $relations) |
|
16
|
|
|
*/ |
|
17
|
|
|
trait Roleable |
|
18
|
|
|
{ |
|
19
|
|
|
/* ----------------------------------------------------------------- |
|
20
|
|
|
| Getters & Setters |
|
21
|
|
|
| ----------------------------------------------------------------- |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get the active roles collection. |
|
26
|
|
|
* |
|
27
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
28
|
|
|
*/ |
|
29
|
48 |
|
public function getActiveRolesAttribute() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->roles->filter(function (Role $role) { |
|
32
|
48 |
|
return $role->isActive(); |
|
33
|
48 |
|
}); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/* ----------------------------------------------------------------- |
|
37
|
|
|
| Check Methods |
|
38
|
|
|
| ----------------------------------------------------------------- |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check if user has the given role (Role Model or Id). |
|
43
|
|
|
* |
|
44
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $id |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
48 |
|
public function hasRole($id) |
|
49
|
|
|
{ |
|
50
|
48 |
|
if ($id instanceof Role) $id = $id->getKey(); |
|
51
|
|
|
|
|
52
|
48 |
|
return $this->active_roles->contains('id', $id); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Check if has all roles. |
|
57
|
|
|
* |
|
58
|
|
|
* @param \Illuminate\Support\Collection|array $roles |
|
59
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
60
|
|
|
* |
|
61
|
|
|
* @return bool |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function isAll($roles, &$failed = null) |
|
64
|
|
|
{ |
|
65
|
6 |
|
$this->isOne($roles, $failed); |
|
66
|
|
|
|
|
67
|
6 |
|
return $failed->isEmpty(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if has at least one role. |
|
72
|
|
|
* |
|
73
|
|
|
* @param \Illuminate\Support\Collection|array $roles |
|
74
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
26 |
|
public function isOne($roles, &$failed = null) |
|
79
|
|
|
{ |
|
80
|
26 |
|
$roles = is_array($roles) ? collect($roles) : $roles; |
|
81
|
|
|
|
|
82
|
|
|
$failed = $roles->reject(function ($role) { |
|
83
|
15 |
|
return $this->hasRoleSlug($role); |
|
84
|
15 |
|
})->values(); |
|
85
|
|
|
|
|
86
|
15 |
|
return $roles->count() !== $failed->count(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Check if has a role by its slug. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $slug |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function hasRoleSlug($slug) |
|
97
|
|
|
{ |
|
98
|
21 |
|
$roles = $this->active_roles->filter(function (Role $role) use ($slug) { |
|
99
|
21 |
|
return $role->hasSlug($slug); |
|
100
|
21 |
|
}); |
|
101
|
|
|
|
|
102
|
21 |
|
return ! $roles->isEmpty(); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/* ----------------------------------------------------------------- |
|
106
|
|
|
| Other Methods |
|
107
|
|
|
| ----------------------------------------------------------------- |
|
108
|
|
|
*/ |
|
109
|
|
|
/** |
|
110
|
|
|
* Load all roles. |
|
111
|
|
|
* |
|
112
|
|
|
* @param bool $load |
|
113
|
|
|
* |
|
114
|
|
|
* @return self |
|
115
|
|
|
*/ |
|
116
|
54 |
|
protected function loadRoles($load = true) |
|
117
|
|
|
{ |
|
118
|
54 |
|
return $load ? $this->load('roles') : $this; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|