|
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
|
|
|
* @var \Illuminate\Support\Collection |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $abilities; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Access observer class |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $observer = \Sco\Admin\Component\Observer::class; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* User exposed observable abilities. |
|
24
|
|
|
* |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $observables = []; |
|
28
|
|
|
|
|
29
|
|
|
public function bootHasAccess() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->abilities = new Collection(); |
|
32
|
|
|
|
|
33
|
|
|
$this->observe($this->observer); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function isView() |
|
37
|
|
|
{ |
|
38
|
|
|
return method_exists($this, 'callView') && $this->can('view'); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function isCreate() |
|
42
|
|
|
{ |
|
43
|
|
|
return method_exists($this, 'callCreate') && $this->can('create'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function isEdit() |
|
47
|
|
|
{ |
|
48
|
|
|
return method_exists($this, 'callEdit') && $this->can('edit'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isDelete() |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->can('delete'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function isDestroy() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function isRestore() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function isRestorableModel() |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->getRepository()->isRestorable(); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Register an observer with the Component. |
|
73
|
|
|
* |
|
74
|
|
|
* @param $class |
|
75
|
|
|
*/ |
|
76
|
|
|
public function observe($class) |
|
77
|
|
|
{ |
|
78
|
|
|
$className = is_string($class) ? $class : get_class($class); |
|
79
|
|
|
|
|
80
|
|
|
foreach ($this->getObservableAbilities() as $ability) { |
|
81
|
|
|
if (method_exists($class, $ability)) { |
|
82
|
|
|
$this->registerAbility( |
|
83
|
|
|
$ability, |
|
84
|
|
|
$className . '@' . $ability |
|
85
|
|
|
); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Get the observable ability names. |
|
92
|
|
|
* |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getObservableAbilities() |
|
96
|
|
|
{ |
|
97
|
|
|
return array_merge( |
|
98
|
|
|
[ |
|
99
|
|
|
'view', 'create', 'edit', 'delete', |
|
100
|
|
|
'destroy', 'restore', |
|
101
|
|
|
], |
|
102
|
|
|
$this->observables |
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function registerAbility($ability, $callback) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->abilities->put($ability, $this->makeAbilityCallback($callback)); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
protected function makeAbilityCallback($callback) |
|
112
|
|
|
{ |
|
113
|
|
|
return function ($component) use ($callback) { |
|
114
|
|
|
if (is_callable($callback)) { |
|
115
|
|
|
return $callback($component); |
|
116
|
|
|
} |
|
117
|
|
|
if (is_string($callback)) { |
|
118
|
|
|
list($class, $method) = Str::parseCallback($callback); |
|
119
|
|
|
return call_user_func([$this->app->make($class), $method], $component); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
}; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $ability |
|
126
|
|
|
* |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
final public function can($ability) |
|
130
|
|
|
{ |
|
131
|
|
|
if (!$this->abilities->has($ability)) { |
|
132
|
|
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
$value = $this->abilities->get($ability); |
|
135
|
|
|
|
|
136
|
|
|
return $value($this) ? true : false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getAccesses() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->abilities->mapWithKeys(function ($item, $key) { |
|
142
|
|
|
return [$key => $this->can($key)]; |
|
143
|
|
|
}); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.