|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
trait HasAccess |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var \Illuminate\Support\Collection |
|
9
|
|
|
*/ |
|
10
|
|
|
private static $abilities; |
|
11
|
|
|
|
|
12
|
|
|
protected static $observer = \Sco\Admin\Component\Observer::class; |
|
13
|
|
|
|
|
14
|
|
|
protected $observables = []; |
|
15
|
|
|
|
|
16
|
|
|
public static function bootHasAccess() |
|
17
|
|
|
{ |
|
18
|
|
|
static::$abilities = new Collection(); |
|
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
static::observe(static::$observer); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function isView() |
|
24
|
|
|
{ |
|
25
|
|
|
return method_exists($this, 'callView') && $this->can('view'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function isCreate() |
|
29
|
|
|
{ |
|
30
|
|
|
return method_exists($this, 'callCreate') && $this->can('create'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function isEdit() |
|
34
|
|
|
{ |
|
35
|
|
|
return method_exists($this, 'callEdit') && $this->can('edit'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function isDelete() |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->can('delete'); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function isDestroy() |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public function isRestore() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function isRestorableModel() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->getRepository()->isRestorable(); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function observe($class) |
|
59
|
|
|
{ |
|
60
|
|
|
$instance = new static; |
|
61
|
|
|
|
|
62
|
|
|
$className = is_string($class) ? $class : get_class($class); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($instance->getObservableAbilities() as $ability) { |
|
65
|
|
|
if (method_exists($class, $ability)) { |
|
66
|
|
|
$instance->registerAccess( |
|
67
|
|
|
$ability, |
|
68
|
|
|
[$instance->app->make($className), $ability] |
|
|
|
|
|
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Get the observable event names. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
|
|
public function getObservableAbilities() |
|
80
|
|
|
{ |
|
81
|
|
|
return array_merge( |
|
82
|
|
|
[ |
|
83
|
|
|
'view', 'create', 'edit', 'delete', |
|
84
|
|
|
'destroy', 'restore', |
|
85
|
|
|
], |
|
86
|
|
|
$this->observables |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function registerAccess($ability, $callback) |
|
91
|
|
|
{ |
|
92
|
|
|
static::$abilities->put($ability, $callback); |
|
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param string $ability |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
final public function can($ability) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!static::$abilities->has($ability)) { |
|
|
|
|
|
|
103
|
|
|
return false; |
|
104
|
|
|
} |
|
105
|
|
|
$value = static::$abilities->get($ability); |
|
|
|
|
|
|
106
|
|
|
if (is_callable($value)) { |
|
107
|
|
|
return call_user_func_array( |
|
108
|
|
|
$value, |
|
109
|
|
|
[$this] |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
return $value ? true : false; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: