1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Sco\Admin\Component\Concerns; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
trait HasPermission |
8
|
|
|
{ |
9
|
|
|
protected $permObserve; |
10
|
|
|
|
11
|
|
|
protected $permissions; |
12
|
|
|
|
13
|
|
|
protected $permMethods = [ |
14
|
|
|
'view', 'create', 'edit', |
15
|
|
|
'delete', 'destroy', 'restore', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
public function isView() |
19
|
|
|
{ |
20
|
|
|
return method_exists($this, 'callView') && $this->can('view'); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function isCreate() |
24
|
|
|
{ |
25
|
|
|
return method_exists($this, 'callCreate') && $this->can('create'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function isEdit() |
29
|
|
|
{ |
30
|
|
|
return method_exists($this, 'callEdit') && $this->can('edit'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function isDelete() |
34
|
|
|
{ |
35
|
|
|
return $this->can('delete'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function isDestroy() |
39
|
|
|
{ |
40
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function isRestore() |
44
|
|
|
{ |
45
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isRestorableModel() |
49
|
|
|
{ |
50
|
|
|
$this->getRepository()->isRestorable(); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public static function bootHasPermission() |
54
|
|
|
{ |
55
|
|
|
$instance = new static; |
56
|
|
|
|
57
|
|
|
$instance->initPermissions(); |
58
|
|
|
if ($instance->permObserve) { |
59
|
|
|
$instance->registerObserver($instance->permObserve); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function initPermissions() |
64
|
|
|
{ |
65
|
|
|
foreach ($this->permMethods as $method) { |
66
|
|
|
$this->registerPermission($method, true); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function registerObserver($class = null) |
71
|
|
|
{ |
72
|
|
|
if (!$class) { |
73
|
|
|
return; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$className = is_string($class) ? $class : get_class($class); |
77
|
|
|
|
78
|
|
|
foreach ($this->permMethods as $method) { |
79
|
|
|
if (method_exists($class, $method)) { |
80
|
|
|
$this->registerPermission($method, [$className, $method]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function registerPermission($permission, $callback) |
86
|
|
|
{ |
87
|
|
|
$this->permissions[$permission] = $callback; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function can($permission) |
91
|
|
|
{ |
92
|
|
|
if (!isset($this->permissions[$permission])) { |
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
if (is_callable($this->permissions[$permission])) { |
96
|
|
|
return call_user_func_array($this->permissions[$permission], $this); |
97
|
|
|
} else { |
98
|
|
|
return $this->permissions[$permission] ? true : false; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getPermissions() |
103
|
|
|
{ |
104
|
|
|
$data = collect(); |
105
|
|
|
foreach ($this->permMethods as $perm) { |
106
|
|
|
$method = 'is' . ucfirst($perm); |
107
|
|
|
if (!method_exists($this, $method)) { |
108
|
|
|
$method = 'can'; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
$data->put($perm, $this->{$method}($perm)); |
112
|
|
|
} |
113
|
|
|
return $data; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
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.