1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Traits; |
2
|
|
|
|
3
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
4
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Trait AuthRoleTrait |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\LaravelAuth\Traits |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
* |
12
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
13
|
|
|
* |
14
|
|
|
* @method \Illuminate\Database\Eloquent\Relations\BelongsToMany roles() |
15
|
|
|
* @method \Arcanedev\LaravelAuth\Traits\AuthRoleTrait load(mixed $relations) |
16
|
|
|
*/ |
17
|
|
|
trait AuthRoleTrait |
18
|
|
|
{ |
19
|
|
|
/* ------------------------------------------------------------------------------------------------ |
20
|
|
|
| Role CRUD Functions |
21
|
|
|
| ------------------------------------------------------------------------------------------------ |
22
|
|
|
*/ |
23
|
|
|
/** |
24
|
|
|
* Attach a role to a user. |
25
|
|
|
* |
26
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
27
|
|
|
* @param bool $reload |
28
|
|
|
*/ |
29
|
120 |
|
public function attachRole($role, $reload = true) |
30
|
|
|
{ |
31
|
120 |
|
if ( ! $this->hasRole($role)) { |
32
|
120 |
|
$this->roles()->attach($role); |
33
|
120 |
|
$this->loadRoles($reload); |
34
|
90 |
|
} |
35
|
120 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Sync the roles by its slugs. |
39
|
|
|
* |
40
|
|
|
* @param array $slugs |
41
|
|
|
* @param bool $reload |
42
|
|
|
* |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
16 |
|
public function syncRoles(array $slugs, $reload = true) |
46
|
|
|
{ |
47
|
16 |
|
/** @var \Illuminate\Database\Eloquent\Collection $roles */ |
48
|
16 |
|
$roles = app(RoleContract::class)->whereIn('slug', $slugs)->get(); |
49
|
12 |
|
$synced = $this->roles()->sync( |
50
|
|
|
$roles->pluck('id')->toArray() |
51
|
16 |
|
); |
52
|
16 |
|
|
53
|
|
|
$this->loadRoles($reload); |
54
|
16 |
|
|
55
|
|
|
return $synced; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Detach a role from a user. |
60
|
|
|
* |
61
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
62
|
|
|
* @param bool $reload |
63
|
|
|
* |
64
|
16 |
|
* @return int |
65
|
|
|
*/ |
66
|
16 |
|
public function detachRole($role, $reload = true) |
67
|
16 |
|
{ |
68
|
|
|
if ($role instanceof Eloquent) { |
|
|
|
|
69
|
16 |
|
$role = (array) $role->getKey(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$results = $this->roles()->detach($role); |
73
|
|
|
$this->loadRoles($reload); |
74
|
|
|
|
75
|
|
|
return $results; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Detach all roles from a user. |
80
|
|
|
* |
81
|
|
|
* @param bool $reload |
82
|
|
|
* |
83
|
120 |
|
* @return int |
84
|
|
|
*/ |
85
|
120 |
|
public function detachAllRoles($reload = true) |
86
|
120 |
|
{ |
87
|
90 |
|
$results = $this->roles()->detach(); |
88
|
|
|
$this->loadRoles($reload); |
89
|
120 |
|
|
90
|
|
|
return $results; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/* ------------------------------------------------------------------------------------------------ |
94
|
|
|
| Check Functions |
95
|
|
|
| ------------------------------------------------------------------------------------------------ |
96
|
|
|
*/ |
97
|
|
|
/** |
98
|
|
|
* Check if user has the given role (Role Model or Id). |
99
|
|
|
* |
100
|
16 |
|
* @param mixed $id |
101
|
|
|
* |
102
|
16 |
|
* @return bool |
103
|
|
|
*/ |
104
|
16 |
|
public function hasRole($id) |
105
|
|
|
{ |
106
|
|
|
if ($id instanceof Eloquent) |
|
|
|
|
107
|
|
|
$id = $id->getKey(); |
108
|
|
|
|
109
|
|
|
return $this->roles->contains($id); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check if has all roles. |
114
|
|
|
* |
115
|
32 |
|
* @param array $roles |
116
|
|
|
* @param array &$failedRoles |
117
|
32 |
|
* |
118
|
32 |
|
* @return bool |
119
|
32 |
|
*/ |
120
|
24 |
|
public function isAll(array $roles, array &$failedRoles = []) |
121
|
24 |
|
{ |
122
|
|
|
$this->isOne($roles, $failedRoles); |
123
|
32 |
|
|
124
|
|
|
return count($failedRoles) === 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Check if has at least one role. |
129
|
|
|
* |
130
|
|
|
* @param array $roles |
131
|
|
|
* @param array &$failedRoles |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
|
|
*/ |
135
|
48 |
|
public function isOne(array $roles, array &$failedRoles = []) |
136
|
48 |
|
{ |
137
|
48 |
|
foreach ($roles as $role) { |
138
|
|
|
if ( ! $this->hasRoleSlug($role)) |
139
|
48 |
|
$failedRoles[] = $role; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return count($roles) !== count($failedRoles); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Check if has a role by its slug. |
147
|
|
|
* |
148
|
|
|
* @param string $slug |
149
|
|
|
* |
150
|
|
|
* @return bool |
151
|
|
|
*/ |
152
|
|
|
public function hasRoleSlug($slug) |
153
|
120 |
|
{ |
154
|
|
|
$roles = $this->roles->filter(function(RoleContract $role) use ($slug) { |
155
|
120 |
|
return $role->checkSlug($slug); |
156
|
|
|
}); |
157
|
|
|
|
158
|
|
|
return ! $roles->isEmpty(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/* ------------------------------------------------------------------------------------------------ |
162
|
|
|
| Other Functions |
163
|
|
|
| ------------------------------------------------------------------------------------------------ |
164
|
|
|
*/ |
165
|
|
|
/** |
166
|
|
|
* Load all roles. |
167
|
|
|
* |
168
|
|
|
* @param bool $load |
169
|
|
|
* |
170
|
|
|
* @return self |
171
|
|
|
*/ |
172
|
|
|
protected function loadRoles($load = true) |
173
|
|
|
{ |
174
|
|
|
return $load ? $this->load('roles') : $this; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.