|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* NOTICE OF LICENSE |
|
5
|
|
|
* |
|
6
|
|
|
* Part of the Rinvex Fort Package. |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to The MIT License (MIT) |
|
9
|
|
|
* that is bundled with this package in the LICENSE file. |
|
10
|
|
|
* |
|
11
|
|
|
* Package: Rinvex Fort Package |
|
12
|
|
|
* License: The MIT License (MIT) |
|
13
|
|
|
* Link: https://rinvex.com |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Rinvex\Fort\Traits; |
|
17
|
|
|
|
|
18
|
|
|
use Rinvex\Fort\Models\Ability; |
|
19
|
|
|
use Illuminate\Support\Collection; |
|
20
|
|
|
|
|
21
|
|
|
trait HasAbilities |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Give the given ability to a role/user. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $ability |
|
27
|
|
|
* |
|
28
|
|
|
* @return $this |
|
29
|
|
|
*/ |
|
30
|
|
|
public function giveAbilityTo($ability) |
|
31
|
|
|
{ |
|
32
|
|
|
$origAbility = $ability; |
|
33
|
|
|
|
|
34
|
|
|
// Fire the ability giving event |
|
35
|
|
|
event('rinvex.fort.ability.giving', [$origAbility, $this]); |
|
36
|
|
|
|
|
37
|
|
|
// Single ability slug |
|
38
|
|
|
if (is_string($ability)) { |
|
39
|
|
|
$ability = app('rinvex.fort.ability')->whereSlug($ability)->first(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Single ability model |
|
43
|
|
|
if ($ability instanceof Ability) { |
|
44
|
|
|
if ($this->hasAbilityTo($ability)) { |
|
45
|
|
|
return $this; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
$this->abilities()->attach($ability); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
// Array of ability slugs |
|
52
|
|
|
if (is_array($ability)) { |
|
53
|
|
|
$ability = app('rinvex.fort.ability')->findWhereIn(['slug', $ability]); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Collection of ability models |
|
57
|
|
|
if ($ability instanceof Collection) { |
|
58
|
|
|
$ability = $ability->map(function ($ability) { |
|
59
|
|
|
return $ability instanceof Ability ? $ability->id : $ability; |
|
|
|
|
|
|
60
|
|
|
})->toArray(); |
|
61
|
|
|
|
|
62
|
|
|
$this->abilities()->syncWithoutDetaching($ability); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
// Fire the ability given event |
|
66
|
|
|
event('rinvex.fort.ability.given', [$origAbility, $this]); |
|
67
|
|
|
|
|
68
|
|
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Revoke the given ability from a role/user. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $ability |
|
75
|
|
|
* |
|
76
|
|
|
* @return $this |
|
77
|
|
|
*/ |
|
78
|
|
|
public function revokeAbilityTo($ability) |
|
79
|
|
|
{ |
|
80
|
|
|
$origAbility = $ability; |
|
81
|
|
|
|
|
82
|
|
|
// Fire the ability revoking event |
|
83
|
|
|
event('rinvex.fort.ability.revoking', [$origAbility, $this]); |
|
84
|
|
|
|
|
85
|
|
|
// Single ability slug |
|
86
|
|
|
if (is_string($ability)) { |
|
87
|
|
|
$ability = $this->abilities()->whereSlug($ability)->first(); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Single ability model |
|
91
|
|
|
if ($ability instanceof Ability) { |
|
92
|
|
|
if (! $this->hasAbilityTo($ability)) { |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$this->abilities()->detach($ability); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Array of ability slugs |
|
100
|
|
|
if (is_array($ability)) { |
|
101
|
|
|
$ability = app('rinvex.fort.ability')->findWhereIn(['slug', $ability]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Collection of ability models |
|
105
|
|
|
if ($ability instanceof Collection) { |
|
106
|
|
|
$remove = $ability->map(function ($ability) { |
|
107
|
|
|
return $ability instanceof Ability ? $ability->id : $ability; |
|
|
|
|
|
|
108
|
|
|
})->toArray(); |
|
109
|
|
|
|
|
110
|
|
|
$this->abilities()->sync(array_diff($this->abilities()->getRelatedIds()->toArray(), $remove)); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Fire the ability revoked event |
|
114
|
|
|
event('rinvex.fort.ability.revoked', [$origAbility, $this]); |
|
115
|
|
|
|
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Determine if the user may perform the given ability. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $role |
|
|
|
|
|
|
123
|
|
|
* |
|
124
|
|
|
* @return bool |
|
125
|
|
|
*/ |
|
126
|
|
|
public function hasAbilityTo($ability) |
|
127
|
|
|
{ |
|
128
|
|
|
return $this->hasDirectAbility($ability); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Determine if the user has the given ability. |
|
133
|
|
|
* |
|
134
|
|
|
* @param string|array|\Rinvex\Fort\Models\Ability|\Illuminate\Support\Collection $role |
|
|
|
|
|
|
135
|
|
|
* |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function hasDirectAbility($ability) |
|
139
|
|
|
{ |
|
140
|
|
|
// Single ability slug |
|
141
|
|
|
if (is_string($ability)) { |
|
142
|
|
|
return $this->abilities()->whereSlug($ability)->first() ? true : false; |
|
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// Single ability model |
|
146
|
|
|
if ($ability instanceof Ability) { |
|
147
|
|
|
return $this->abilities->contains('slug', $ability->slug); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
// Array of ability slugs |
|
151
|
|
|
if (is_array($ability)) { |
|
152
|
|
|
return $this->abilities->pluck('slug')->intersect($ability)->isEmpty() ? false : true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// Collection of role models |
|
156
|
|
|
if ($ability instanceof Collection) { |
|
157
|
|
|
return $this->abilities->pluck('slug')->intersect($ability->pluck('slug')->toArray())->isEmpty() ? false : true; |
|
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
return false; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.