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