1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sco\Admin\Component; |
4
|
|
|
|
5
|
|
|
use Sco\Admin\Contracts\AccessInterface; |
6
|
|
|
use Sco\Admin\Contracts\ComponentInterface; |
7
|
|
|
|
8
|
|
|
abstract class Access implements AccessInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Sco\Admin\Contracts\ComponentInterface |
12
|
|
|
*/ |
13
|
|
|
protected $component; |
14
|
|
|
|
15
|
|
|
private $abilities = [ |
16
|
|
|
'view' => true, |
17
|
|
|
'create' => true, |
18
|
|
|
'edit' => true, |
19
|
|
|
'delete' => true, |
20
|
|
|
'destroy' => true, |
21
|
|
|
'restore' => true, |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
public function __construct(ComponentInterface $component) |
25
|
|
|
{ |
26
|
|
|
$this->component = $component; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function isView() |
30
|
|
|
{ |
31
|
|
|
return method_exists($this->getComponent(), 'callView') |
32
|
|
|
&& $this->can('view'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function isCreate() |
36
|
|
|
{ |
37
|
|
|
return method_exists($this->getComponent(), 'callCreate') |
38
|
|
|
&& $this->can('create'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function isEdit() |
42
|
|
|
{ |
43
|
|
|
return method_exists($this->getComponent(), 'callEdit') |
44
|
|
|
&& $this->can('edit'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function isDelete() |
48
|
|
|
{ |
49
|
|
|
return $this->can('delete'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isDestroy() |
53
|
|
|
{ |
54
|
|
|
return $this->isRestorableModel() && $this->can('destroy'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isRestore() |
58
|
|
|
{ |
59
|
|
|
return $this->isRestorableModel() && $this->can('restore'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function isRestorableModel() |
63
|
|
|
{ |
64
|
|
|
return $this->getComponent()->getRepository()->isRestorable(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $ability |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
final public function can($ability) |
73
|
|
|
{ |
74
|
|
|
if (!isset($this->abilities[$ability])) { |
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (is_callable($this->abilities[$ability])) { |
79
|
|
|
return call_user_func_array( |
80
|
|
|
$this->abilities[$ability], |
81
|
|
|
[$this->getComponent()] |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
return $this->abilities[$ability] ? true : false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getComponent() |
88
|
|
|
{ |
89
|
|
|
return $this->component; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|