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 Mpociot\Teamwork\Traits; |
||
4 | |||
5 | use Illuminate\Database\Eloquent\Model; |
||
6 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||
7 | use Illuminate\Support\Facades\Config; |
||
8 | use Mpociot\Teamwork\Events\UserJoinedTeam; |
||
9 | use Mpociot\Teamwork\Events\UserLeftTeam; |
||
10 | use Mpociot\Teamwork\Exceptions\UserNotInTeamException; |
||
11 | |||
12 | trait UserHasTeams |
||
13 | { |
||
14 | /** |
||
15 | * Many-to-Many relations with the user model. |
||
16 | * |
||
17 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
18 | */ |
||
19 | public function teams() |
||
20 | { |
||
21 | return $this->belongsToMany(Config::get('teamwork.team_model'), Config::get('teamwork.team_user_table'), 'user_id', 'team_id')->withTimestamps(); |
||
0 ignored issues
–
show
|
|||
22 | } |
||
23 | |||
24 | /** |
||
25 | * has-one relation with the current selected team model. |
||
26 | * |
||
27 | * @return \Illuminate\Database\Eloquent\Relations\HasOne |
||
28 | */ |
||
29 | public function currentTeam() |
||
30 | { |
||
31 | return $this->hasOne(Config::get('teamwork.team_model'), 'id', 'current_team_id'); |
||
0 ignored issues
–
show
It seems like
hasOne() 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 ![]() |
|||
32 | } |
||
33 | |||
34 | /** |
||
35 | * @return mixed |
||
36 | */ |
||
37 | public function ownedTeams() |
||
38 | { |
||
39 | return $this->teams()->where('owner_id', '=', $this->getKey()); |
||
0 ignored issues
–
show
It seems like
getKey() 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 ![]() |
|||
40 | } |
||
41 | |||
42 | /** |
||
43 | * One-to-Many relation with the invite model. |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function invites() |
||
47 | { |
||
48 | return $this->hasMany(Config::get('teamwork.invite_model'), 'email', 'email'); |
||
0 ignored issues
–
show
It seems like
hasMany() 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 ![]() |
|||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Boot the user model |
||
53 | * Attach event listener to remove the many-to-many records when trying to delete |
||
54 | * Will NOT delete any records if the user model uses soft deletes. |
||
55 | * |
||
56 | * @return void|bool |
||
57 | */ |
||
58 | public static function bootUserHasTeams() |
||
59 | { |
||
60 | static::deleting(function (Model $user) { |
||
61 | if (! method_exists(Config::get('teamwork.user_model'), 'bootSoftDeletes')) { |
||
62 | $user->teams()->sync([]); |
||
63 | } |
||
64 | |||
65 | return true; |
||
66 | }); |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Returns if the user owns a team. |
||
71 | * |
||
72 | * @return bool |
||
73 | */ |
||
74 | public function isOwner() |
||
75 | { |
||
76 | return ($this->teams()->where('owner_id', '=', $this->getKey())->first()) ? true : false; |
||
0 ignored issues
–
show
It seems like
getKey() 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 ![]() |
|||
77 | } |
||
78 | |||
79 | /** |
||
80 | * Wrapper method for "isOwner". |
||
81 | * |
||
82 | * @return bool |
||
83 | */ |
||
84 | public function isTeamOwner() |
||
85 | { |
||
86 | return $this->isOwner(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param $team |
||
91 | * @return mixed |
||
92 | */ |
||
93 | protected function retrieveTeamId($team) |
||
94 | { |
||
95 | if (is_object($team)) { |
||
96 | $team = $team->getKey(); |
||
97 | } |
||
98 | if (is_array($team) && isset($team['id'])) { |
||
99 | $team = $team['id']; |
||
100 | } |
||
101 | |||
102 | return $team; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Returns if the user owns the given team. |
||
107 | * |
||
108 | * @param mixed $team |
||
109 | * @return bool |
||
110 | */ |
||
111 | public function isOwnerOfTeam($team) |
||
112 | { |
||
113 | $team_id = $this->retrieveTeamId($team); |
||
114 | |||
115 | return ($this->teams() |
||
116 | ->where('owner_id', $this->getKey()) |
||
0 ignored issues
–
show
It seems like
getKey() 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 ![]() |
|||
117 | ->where('team_id', $team_id)->first() |
||
118 | ) ? true : false; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Alias to eloquent many-to-many relation's attach() method. |
||
123 | * |
||
124 | * @param mixed $team |
||
125 | * @param array $pivotData |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function attachTeam($team, $pivotData = []) |
||
129 | { |
||
130 | $team = $this->retrieveTeamId($team); |
||
131 | /* |
||
132 | * If the user has no current team, |
||
133 | * use the attached one |
||
134 | */ |
||
135 | if (is_null($this->current_team_id)) { |
||
136 | $this->current_team_id = $team; |
||
0 ignored issues
–
show
The property
current_team_id does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
137 | $this->save(); |
||
0 ignored issues
–
show
It seems like
save() 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 ![]() |
|||
138 | |||
139 | if ($this->relationLoaded('currentTeam')) { |
||
0 ignored issues
–
show
It seems like
relationLoaded() 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 ![]() |
|||
140 | $this->load('currentTeam'); |
||
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 ![]() |
|||
141 | } |
||
142 | } |
||
143 | |||
144 | // Reload relation |
||
145 | $this->load('teams'); |
||
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 ![]() |
|||
146 | |||
147 | if (! $this->teams->contains($team)) { |
||
0 ignored issues
–
show
The property
teams does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
![]() |
|||
148 | $this->teams()->attach($team, $pivotData); |
||
149 | |||
150 | event(new UserJoinedTeam($this, $team)); |
||
151 | |||
152 | if ($this->relationLoaded('teams')) { |
||
0 ignored issues
–
show
It seems like
relationLoaded() 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 ![]() |
|||
153 | $this->load('teams'); |
||
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 ![]() |
|||
154 | } |
||
155 | } |
||
156 | |||
157 | return $this; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * Alias to eloquent many-to-many relation's detach() method. |
||
162 | * |
||
163 | * @param mixed $team |
||
164 | * @return $this |
||
165 | */ |
||
166 | public function detachTeam($team) |
||
167 | { |
||
168 | $team = $this->retrieveTeamId($team); |
||
169 | $this->teams()->detach($team); |
||
170 | |||
171 | event(new UserLeftTeam($this, $team)); |
||
172 | |||
173 | if ($this->relationLoaded('teams')) { |
||
0 ignored issues
–
show
It seems like
relationLoaded() 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 | $this->load('teams'); |
||
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 ![]() |
|||
175 | } |
||
176 | |||
177 | /* |
||
178 | * If the user has no more teams, |
||
179 | * unset the current_team_id |
||
180 | */ |
||
181 | if ($this->teams()->count() === 0 || $this->current_team_id === $team) { |
||
182 | $this->current_team_id = null; |
||
183 | $this->save(); |
||
0 ignored issues
–
show
It seems like
save() 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 ![]() |
|||
184 | |||
185 | if ($this->relationLoaded('currentTeam')) { |
||
0 ignored issues
–
show
It seems like
relationLoaded() 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 ![]() |
|||
186 | $this->load('currentTeam'); |
||
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 ![]() |
|||
187 | } |
||
188 | } |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Attach multiple teams to a user. |
||
195 | * |
||
196 | * @param mixed $teams |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function attachTeams($teams) |
||
200 | { |
||
201 | foreach ($teams as $team) { |
||
202 | $this->attachTeam($team); |
||
203 | } |
||
204 | |||
205 | return $this; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Detach multiple teams from a user. |
||
210 | * |
||
211 | * @param mixed $teams |
||
212 | * @return $this |
||
213 | */ |
||
214 | public function detachTeams($teams) |
||
215 | { |
||
216 | foreach ($teams as $team) { |
||
217 | $this->detachTeam($team); |
||
218 | } |
||
219 | |||
220 | return $this; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Switch the current team of the user. |
||
225 | * |
||
226 | * @param object|array|int $team |
||
227 | * @return $this |
||
228 | * @throws ModelNotFoundException |
||
229 | * @throws UserNotInTeamException |
||
230 | */ |
||
231 | public function switchTeam($team) |
||
232 | { |
||
233 | if ($team !== 0 && $team !== null) { |
||
234 | $team = $this->retrieveTeamId($team); |
||
235 | $teamModel = Config::get('teamwork.team_model'); |
||
236 | $teamObject = ( new $teamModel() )->find($team); |
||
237 | if (! $teamObject) { |
||
238 | $exception = new ModelNotFoundException(); |
||
239 | $exception->setModel($teamModel); |
||
240 | throw $exception; |
||
241 | } |
||
242 | if (! $teamObject->users->contains($this->getKey())) { |
||
0 ignored issues
–
show
It seems like
getKey() 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 ![]() |
|||
243 | $exception = new UserNotInTeamException(); |
||
244 | $exception->setTeam($teamObject->name); |
||
245 | throw $exception; |
||
246 | } |
||
247 | } |
||
248 | $this->current_team_id = $team; |
||
249 | $this->save(); |
||
0 ignored issues
–
show
It seems like
save() 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 ![]() |
|||
250 | |||
251 | if ($this->relationLoaded('currentTeam')) { |
||
0 ignored issues
–
show
It seems like
relationLoaded() 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 ![]() |
|||
252 | $this->load('currentTeam'); |
||
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 ![]() |
|||
253 | } |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | } |
||
258 |
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.