This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Yajra\Acl\Traits; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Builder; |
||
6 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
||
7 | use Yajra\Acl\Models\Role; |
||
8 | |||
9 | /** |
||
10 | * @property \Illuminate\Database\Eloquent\Collection|Role[] roles |
||
11 | * @method static Builder havingRoles($roleIds) |
||
12 | * @method static Builder havingRolesBySlugs($slugs) |
||
13 | */ |
||
14 | trait InteractsWithRole |
||
15 | { |
||
16 | private $roleClass; |
||
17 | |||
18 | /** |
||
19 | * Check if user has the given role. |
||
20 | * |
||
21 | * @param string|array $role |
||
22 | * @return bool |
||
23 | */ |
||
24 | public function hasRole($role): bool |
||
25 | { |
||
26 | if (is_array($role)) { |
||
27 | $roles = $this->getRoleSlugs(); |
||
28 | |||
29 | $intersection = array_intersect($roles, (array) $role); |
||
30 | $intersectionCount = count($intersection); |
||
31 | |||
32 | return $intersectionCount > 0; |
||
33 | } |
||
34 | |||
35 | return $this->roles->contains('slug', $role); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get all user roles. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | public function getRoleSlugs(): array |
||
44 | { |
||
45 | return $this->roles->pluck('slug')->toArray(); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Attach a role to user using slug. |
||
50 | * |
||
51 | * @param string $slug |
||
52 | */ |
||
53 | public function attachRoleBySlug(string $slug) |
||
54 | { |
||
55 | $this->attachRole($this->findRoleBySlug($slug)); |
||
56 | |||
57 | $this->load('roles'); |
||
0 ignored issues
–
show
|
|||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Attach a role to user. |
||
62 | * |
||
63 | * @param mixed $role |
||
64 | * @param array $attributes |
||
65 | * @param bool $touch |
||
66 | */ |
||
67 | public function attachRole($role, array $attributes = [], $touch = true) |
||
68 | { |
||
69 | $this->roles()->attach($role, $attributes, $touch); |
||
70 | |||
71 | $this->load('roles'); |
||
0 ignored issues
–
show
It seems like
load() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Model can have many roles. |
||
76 | * |
||
77 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
78 | */ |
||
79 | public function roles(): BelongsToMany |
||
80 | { |
||
81 | return $this->belongsToMany(config('acl.role', Role::class))->withTimestamps(); |
||
0 ignored issues
–
show
It seems like
belongsToMany() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Find a role by slug. |
||
86 | * |
||
87 | * @param string $slug |
||
88 | * @return \Illuminate\Database\Eloquent\Model|static |
||
89 | * @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
||
90 | */ |
||
91 | protected function findRoleBySlug(string $slug): Role |
||
92 | { |
||
93 | return $this->getRoleClass()->newQuery()->where('slug', $slug)->firstOrFail(); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get Role class. |
||
98 | * |
||
99 | * @return Role |
||
100 | */ |
||
101 | public function getRoleClass(): Role |
||
102 | { |
||
103 | if (!isset($this->roleClass)) { |
||
104 | $this->roleClass = resolve(config('acl.role')); |
||
105 | } |
||
106 | |||
107 | return $this->roleClass; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Query scope for user having the given roles. |
||
112 | * |
||
113 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
114 | * @param mixed $roles |
||
115 | * @return \Illuminate\Database\Eloquent\Builder |
||
116 | */ |
||
117 | public function scopeHavingRoles(Builder $query, $roles): Builder |
||
118 | { |
||
119 | return $query->whereExists(function ($query) use ($roles) { |
||
120 | $query->selectRaw('1') |
||
121 | ->from('role_user') |
||
122 | ->whereRaw('role_user.user_id = users.id') |
||
123 | ->whereIn('role_id', $roles); |
||
124 | }); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Query scope for user having the given roles by slugs. |
||
129 | * |
||
130 | * @param \Illuminate\Database\Eloquent\Builder $query |
||
131 | * @param mixed $slugs |
||
132 | * @return \Illuminate\Database\Eloquent\Builder |
||
133 | */ |
||
134 | public function scopeHavingRolesBySlugs(Builder $query, $slugs): Builder |
||
135 | { |
||
136 | return $query->whereHas('roles', function ($query) use ($slugs) { |
||
137 | $query->whereIn('roles.slug', $slugs); |
||
138 | }); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Revokes the given role from the user using slug. |
||
143 | * |
||
144 | * @param string|array $slug |
||
145 | * @param bool $touch |
||
146 | * @return int |
||
147 | */ |
||
148 | public function revokeRoleBySlug($slug, $touch = true): int |
||
149 | { |
||
150 | $roles = $this->getRoleClass() |
||
151 | ->newQuery() |
||
152 | ->whereIn('slug', (array) $slug) |
||
153 | ->get(); |
||
154 | |||
155 | $detached = $this->roles()->detach($roles, $touch); |
||
156 | |||
157 | $this->load('roles'); |
||
0 ignored issues
–
show
It seems like
load() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
158 | |||
159 | return $detached; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Revokes the given role from the user. |
||
164 | * |
||
165 | * @param mixed $role |
||
166 | * @param bool $touch |
||
167 | * @return int |
||
168 | */ |
||
169 | public function revokeRole($role, $touch = true): int |
||
170 | { |
||
171 | $detached = $this->roles()->detach($role, $touch); |
||
172 | |||
173 | $this->load('roles'); |
||
0 ignored issues
–
show
It seems like
load() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
174 | |||
175 | return $detached; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Syncs the given role(s) with the user. |
||
180 | * |
||
181 | * @param \Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $roles |
||
182 | * @param bool $detaching |
||
183 | * @return array |
||
184 | */ |
||
185 | public function syncRoles($roles, $detaching = true): array |
||
186 | { |
||
187 | $synced = $this->roles()->sync($roles, $detaching); |
||
188 | |||
189 | $this->load('roles'); |
||
0 ignored issues
–
show
It seems like
load() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
190 | |||
191 | return $synced; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Revokes all roles from the user. |
||
196 | * |
||
197 | * @return int |
||
198 | */ |
||
199 | public function revokeAllRoles(): int |
||
200 | { |
||
201 | $detached = $this->roles()->detach(); |
||
202 | |||
203 | $this->load('roles'); |
||
0 ignored issues
–
show
It seems like
load() must be provided by classes using this trait. How about adding it as abstract method to this trait?
This check looks for methods that are used by a trait but not required by it. To illustrate, let’s look at the following code example trait Idable {
public function equalIds(Idable $other) {
return $this->getId() === $other->getId();
}
}
The trait Adding the ![]() |
|||
204 | |||
205 | return $detached; |
||
206 | } |
||
207 | } |
||
208 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.