1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component\Concerns; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
trait HasAccess |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* The abilities of access. |
12
|
|
|
* |
13
|
|
|
* @var \Illuminate\Support\Collection |
14
|
|
|
*/ |
15
|
|
|
protected $abilities; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Access observer class. |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $observer; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* User exposed observable abilities. |
26
|
|
|
* |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $observables = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Initialize access. |
33
|
|
|
*/ |
34
|
|
|
public function bootHasAccess() |
35
|
|
|
{ |
36
|
|
|
$this->abilities = new Collection(); |
37
|
|
|
|
38
|
|
|
$this->observe($this->observer); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Determine if the entity have access to display. |
43
|
|
|
* |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function isDisplay() |
47
|
|
|
{ |
48
|
|
|
return method_exists($this, 'callDisplay') && $this->can('display'); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check if the entity have access to create. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isCreate() |
57
|
|
|
{ |
58
|
|
|
return method_exists($this, 'callCreate') && $this->can('create'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check if the entity have access to edit. |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isEdit() |
67
|
|
|
{ |
68
|
|
|
return method_exists($this, 'callEdit') && $this->can('edit'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Check if the entity have access to delete. |
73
|
|
|
* |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function isDelete() |
77
|
|
|
{ |
78
|
|
|
return $this->can('delete'); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if the entity have access to destroy. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isDestroy() |
87
|
|
|
{ |
88
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Check if the entity have access to restore. |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
*/ |
96
|
|
|
public function isRestore() |
97
|
|
|
{ |
98
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Whether the model can be restored |
103
|
|
|
* |
104
|
|
|
* @return mixed |
105
|
|
|
*/ |
106
|
|
|
protected function isRestorableModel() |
107
|
|
|
{ |
108
|
|
|
return $this->getRepository()->isRestorable(); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Register an observer with the Component. |
113
|
|
|
* |
114
|
|
|
* @param $class |
115
|
|
|
*/ |
116
|
|
|
public function observe($class) |
117
|
|
|
{ |
118
|
|
|
$className = is_string($class) ? $class : get_class($class); |
119
|
|
|
|
120
|
|
|
if (!class_exists($className)) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($this->getObservableAbilities() as $ability) { |
125
|
|
|
if (method_exists($class, $ability)) { |
126
|
|
|
$this->registerAbility($ability, $className . '@' . $ability); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the observable ability names. |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getObservableAbilities() |
137
|
|
|
{ |
138
|
|
|
return array_merge([ |
139
|
|
|
'display', |
140
|
|
|
'create', |
141
|
|
|
'edit', |
142
|
|
|
'delete', |
143
|
|
|
'destroy', |
144
|
|
|
'restore', |
145
|
|
|
], $this->observables); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* register ability to access. |
150
|
|
|
* |
151
|
|
|
* @param string $ability |
152
|
|
|
* @param string|\Closure $callback |
153
|
|
|
*/ |
154
|
|
|
public function registerAbility($ability, $callback) |
155
|
|
|
{ |
156
|
|
|
$this->abilities->put($ability, $this->makeAbilityCallback($callback)); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param string|\Closure $callback |
161
|
|
|
* |
162
|
|
|
* @return \Closure |
163
|
|
|
*/ |
164
|
|
|
protected function makeAbilityCallback($callback) |
165
|
|
|
{ |
166
|
|
|
return function ($component) use ($callback) { |
167
|
|
|
if (is_callable($callback)) { |
168
|
|
|
return $callback($component); |
169
|
|
|
} |
170
|
|
|
if (is_string($callback)) { |
171
|
|
|
list($class, $method) = Str::parseCallback($callback); |
172
|
|
|
|
173
|
|
|
return call_user_func([$this->app->make($class), $method], $component); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
}; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Determine if the entity has a given ability. |
180
|
|
|
* |
181
|
|
|
* @param string $ability |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
final public function can($ability) |
186
|
|
|
{ |
187
|
|
|
if (! $this->abilities->has($ability)) { |
188
|
|
|
return false; |
189
|
|
|
} |
190
|
|
|
$value = $this->abilities->get($ability); |
191
|
|
|
|
192
|
|
|
return $value($this) ? true : false; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Get all ability. |
197
|
|
|
* |
198
|
|
|
* @return Collection |
199
|
|
|
*/ |
200
|
|
|
public function getAccesses() |
201
|
|
|
{ |
202
|
|
|
return $this->abilities->mapWithKeys(function ($item, $key) { |
203
|
|
|
return [$key => $this->can($key)]; |
204
|
|
|
}); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
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.