|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Model as Eloquent; |
|
4
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role as RoleContract; |
|
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
|
|
|
* Detach a role from a user. |
|
39
|
|
|
* |
|
40
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $role |
|
41
|
|
|
* @param bool $reload |
|
42
|
|
|
* |
|
43
|
|
|
* @return int |
|
44
|
|
|
*/ |
|
45
|
16 |
|
public function detachRole($role, $reload = true) |
|
46
|
|
|
{ |
|
47
|
16 |
|
if ($role instanceof Eloquent) { |
|
|
|
|
|
|
48
|
16 |
|
$role = (array) $role->getKey(); |
|
49
|
12 |
|
} |
|
50
|
|
|
|
|
51
|
16 |
|
$results = $this->roles()->detach($role); |
|
52
|
16 |
|
$this->loadRoles($reload); |
|
53
|
|
|
|
|
54
|
16 |
|
return $results; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Detach all roles from a user. |
|
59
|
|
|
* |
|
60
|
|
|
* @param bool $reload |
|
61
|
|
|
* |
|
62
|
|
|
* @return int |
|
63
|
|
|
*/ |
|
64
|
16 |
|
public function detachAllRoles($reload = true) |
|
65
|
|
|
{ |
|
66
|
16 |
|
$results = $this->roles()->detach(); |
|
67
|
16 |
|
$this->loadRoles($reload); |
|
68
|
|
|
|
|
69
|
16 |
|
return $results; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
73
|
|
|
| Check Functions |
|
74
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
75
|
|
|
*/ |
|
76
|
|
|
/** |
|
77
|
|
|
* Check if user has the given role (Role Model or Id). |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $id |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
120 |
|
public function hasRole($id) |
|
84
|
|
|
{ |
|
85
|
120 |
|
if ($id instanceof Eloquent) { |
|
|
|
|
|
|
86
|
120 |
|
$id = $id->getKey(); |
|
87
|
90 |
|
} |
|
88
|
|
|
|
|
89
|
120 |
|
return $this->roles->contains($id); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check if has all roles. |
|
94
|
|
|
* |
|
95
|
|
|
* @param array $roles |
|
96
|
|
|
* @param array &$failedRoles |
|
97
|
|
|
* |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
16 |
|
public function isAll(array $roles, array &$failedRoles = []) |
|
101
|
|
|
{ |
|
102
|
16 |
|
$this->isOne($roles, $failedRoles); |
|
103
|
|
|
|
|
104
|
16 |
|
return count($failedRoles) === 0; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Check if has at least one role. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $roles |
|
111
|
|
|
* @param array &$failedRoles |
|
112
|
|
|
* |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
32 |
|
public function isOne(array $roles, array &$failedRoles = []) |
|
116
|
|
|
{ |
|
117
|
32 |
|
foreach ($roles as $role) { |
|
118
|
32 |
|
if ( ! $this->is($role)) { |
|
119
|
32 |
|
$failedRoles[] = $role; |
|
120
|
24 |
|
} |
|
121
|
24 |
|
} |
|
122
|
|
|
|
|
123
|
32 |
|
return count($roles) !== count($failedRoles); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Check if has a role by its slug. |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $slug |
|
130
|
|
|
* |
|
131
|
|
|
* @return bool |
|
132
|
|
|
*/ |
|
133
|
|
|
public function is($slug) |
|
134
|
|
|
{ |
|
135
|
48 |
|
$roles = $this->roles->filter(function(RoleContract $role) use ($slug) { |
|
136
|
48 |
|
return $role->checkSlug($slug); |
|
137
|
48 |
|
}); |
|
138
|
|
|
|
|
139
|
48 |
|
return $roles->count() === 1; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
143
|
|
|
| Other Functions |
|
144
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
145
|
|
|
*/ |
|
146
|
|
|
/** |
|
147
|
|
|
* Load all roles. |
|
148
|
|
|
* |
|
149
|
|
|
* @param bool $load |
|
150
|
|
|
* |
|
151
|
|
|
* @return self |
|
152
|
|
|
*/ |
|
153
|
120 |
|
protected function loadRoles($load = true) |
|
154
|
|
|
{ |
|
155
|
120 |
|
return $load ? $this->load('roles') : $this; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto 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
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.