|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Terranet\Administrator; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
|
|
6
|
|
|
use Illuminate\Support\Facades\Cache; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Terranet\Administrator\Actions\Collection; |
|
9
|
|
|
use Terranet\Administrator\Contracts\ActionsManager as ActionsManagerContract; |
|
10
|
|
|
use Terranet\Administrator\Contracts\Module; |
|
11
|
|
|
use Terranet\Administrator\Contracts\Services\CrudActions; |
|
12
|
|
|
|
|
13
|
|
|
class ActionsManager implements ActionsManagerContract |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var CrudActions |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $service; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var Module |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $module; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* List of item-related actions. |
|
27
|
|
|
* |
|
28
|
|
|
* @var array |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $actions; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* List of global actions. |
|
34
|
|
|
* |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $globalActions; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if resource is readonly - has no actions. |
|
41
|
|
|
* |
|
42
|
|
|
* @var null|bool |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $readonly; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* ActionsManager constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param CrudActions $service |
|
50
|
|
|
* @param Module $module |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(CrudActions $service, Module $module) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->service = $service; |
|
55
|
|
|
|
|
56
|
|
|
$this->module = $module; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Fetch module's single (per item) actions. |
|
61
|
|
|
* |
|
62
|
|
|
* @return Collection |
|
63
|
|
|
*/ |
|
64
|
|
|
public function actions() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->scaffoldActions(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Fetch module's batch actions. |
|
71
|
|
|
* |
|
72
|
|
|
* @return Collection |
|
73
|
|
|
*/ |
|
74
|
|
|
public function batch() |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->scaffoldBatch(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Determine if the user is authorized to make this request. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $ability |
|
83
|
|
|
* @param $model |
|
84
|
|
|
* |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
|
|
public function authorize($ability, ?Model $model = null) |
|
88
|
|
|
{ |
|
89
|
|
|
// for most cases it is enough to set |
|
90
|
|
|
// permissions in Resource object. |
|
91
|
|
|
if (method_exists($this->module, $abilityMethod = 'can'.Str::title(Str::camel($ability)))) { |
|
92
|
|
|
$user = auth('admin')->user(); |
|
|
|
|
|
|
93
|
|
|
$cacheID = $user->getAuthIdentifier().':'.$ability.($model ? '_'.$model->getKey() : ''); |
|
94
|
|
|
|
|
95
|
|
|
return Cache::remember($cacheID, 1, function () use ($user, $ability, $model) { |
|
96
|
|
|
$abilityMethod = 'can'.Str::title(Str::camel($ability)); |
|
97
|
|
|
|
|
98
|
|
|
return $this->module->$abilityMethod($user, $ability, $model); |
|
99
|
|
|
}); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// Ask Actions Service for action permissions. |
|
103
|
|
|
return $this->service->authorize($ability, $model, $this->module); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Checks if resource has no Actions at all / Readonly mode. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function readonly() |
|
110
|
|
|
{ |
|
111
|
|
|
if (null === $this->readonly) { |
|
112
|
|
|
$this->readonly = false; |
|
113
|
|
|
|
|
114
|
|
|
// check for <Module>::readonly() method. |
|
115
|
|
|
if (method_exists($this->module, 'readonly')) { |
|
116
|
|
|
$this->readonly = $this->module->readonly(); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->readonly; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Call handler method. |
|
125
|
|
|
* |
|
126
|
|
|
* @param string $method |
|
127
|
|
|
* @param array $arguments |
|
128
|
|
|
* |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function exec(string $method, array $arguments = []) |
|
132
|
|
|
{ |
|
133
|
|
|
// execute custom action |
|
134
|
|
|
if (Str::startsWith($method, 'action::')) { |
|
135
|
|
|
$handler = $this->scaffoldActions()->find( |
|
136
|
|
|
str_replace('action::', '', $method) |
|
137
|
|
|
); |
|
138
|
|
|
|
|
139
|
|
|
return \call_user_func_array([$handler, 'handle'], $arguments); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
// Execute batch action |
|
143
|
|
|
if (Str::startsWith($method, 'batch::')) { |
|
144
|
|
|
$handler = $this->scaffoldBatch()->find( |
|
145
|
|
|
str_replace('batch::', '', $method) |
|
146
|
|
|
); |
|
147
|
|
|
|
|
148
|
|
|
return \call_user_func_array([$handler, 'handle'], $arguments); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
// Execute CRUD action |
|
152
|
|
|
return \call_user_func_array([$this->service, $method], (array) $arguments); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Parse handler class for per-item and global actions. |
|
157
|
|
|
* |
|
158
|
|
|
* @return Collection |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function scaffoldActions() |
|
161
|
|
|
{ |
|
162
|
|
|
return new Collection($this->service->actions()); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Parse handler class for per-item and global actions. |
|
167
|
|
|
* |
|
168
|
|
|
* @return Collection |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function scaffoldBatch() |
|
171
|
|
|
{ |
|
172
|
|
|
return new Collection($this->service->batchActions()); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths