|
1
|
|
|
<?php namespace Arcanedev\LaravelAuth\Models\Traits; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Contracts\Auth\Models\Role; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait AuthRoleTrait |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelAuth\Traits |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
* |
|
11
|
|
|
* @property \Illuminate\Database\Eloquent\Collection roles |
|
12
|
|
|
* |
|
13
|
|
|
* @method \Illuminate\Database\Eloquent\Relations\BelongsToMany roles() |
|
14
|
|
|
* @method \Arcanedev\LaravelAuth\Models\Traits\AuthRoleTrait load(mixed $relations) |
|
15
|
|
|
*/ |
|
16
|
|
|
trait AuthRoleTrait |
|
17
|
|
|
{ |
|
18
|
|
|
/* ----------------------------------------------------------------- |
|
19
|
|
|
| Check Methods |
|
20
|
|
|
| ----------------------------------------------------------------- |
|
21
|
|
|
*/ |
|
22
|
|
|
/** |
|
23
|
|
|
* Check if user has the given role (Role Model or Id). |
|
24
|
|
|
* |
|
25
|
|
|
* @param \Arcanesoft\Contracts\Auth\Models\Role|int $id |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool |
|
28
|
|
|
*/ |
|
29
|
45 |
|
public function hasRole($id) |
|
30
|
|
|
{ |
|
31
|
45 |
|
if ($id instanceof Role) $id = $id->getKey(); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
45 |
|
return $this->roles->contains('id', $id); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Check if has all roles. |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Illuminate\Support\Collection|array $roles |
|
40
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
6 |
|
public function isAll($roles, &$failed = null) |
|
45
|
|
|
{ |
|
46
|
6 |
|
$this->isOne($roles, $failed); |
|
47
|
|
|
|
|
48
|
6 |
|
return $failed->isEmpty(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Check if has at least one role. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Illuminate\Support\Collection|array $roles |
|
55
|
|
|
* @param \Illuminate\Support\Collection &$failed |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
12 |
|
public function isOne($roles, &$failed = null) |
|
60
|
|
|
{ |
|
61
|
12 |
|
$roles = is_array($roles) ? collect($roles) : $roles; |
|
62
|
|
|
|
|
63
|
12 |
|
$failed = $roles->reject(function ($role) { |
|
64
|
12 |
|
return $this->hasRoleSlug($role); |
|
65
|
12 |
|
})->values(); |
|
66
|
|
|
|
|
67
|
12 |
|
return $roles->count() !== $failed->count(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Check if has a role by its slug. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $slug |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
18 |
|
public function hasRoleSlug($slug) |
|
78
|
|
|
{ |
|
79
|
18 |
|
return ! $this->roles->filter->hasSlug($slug)->isEmpty(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/* ----------------------------------------------------------------- |
|
83
|
|
|
| Other Methods |
|
84
|
|
|
| ----------------------------------------------------------------- |
|
85
|
|
|
*/ |
|
86
|
|
|
/** |
|
87
|
|
|
* Load all roles. |
|
88
|
|
|
* |
|
89
|
|
|
* @param bool $load |
|
90
|
|
|
* |
|
91
|
|
|
* @return self |
|
92
|
|
|
*/ |
|
93
|
51 |
|
protected function loadRoles($load = true) |
|
94
|
|
|
{ |
|
95
|
51 |
|
return $load ? $this->load('roles') : $this; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.