1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Terranet\Administrator\Services; |
4
|
|
|
|
5
|
|
|
use Czim\Paperclip\Attachment\Attachment; |
|
|
|
|
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Gate; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use Terranet\Administrator\Actions\RemoveSelected; |
10
|
|
|
use Terranet\Administrator\Actions\SaveOrder; |
11
|
|
|
use Terranet\Administrator\AdminRequest; |
12
|
|
|
use Terranet\Administrator\Contracts\Services\CrudActions as CrudActionsContract; |
13
|
|
|
use Terranet\Administrator\Contracts\Services\Saver; |
|
|
|
|
14
|
|
|
use Terranet\Administrator\Exception; |
15
|
|
|
use Terranet\Administrator\Requests\UpdateRequest; |
16
|
|
|
use Terranet\Administrator\Scaffolding; |
17
|
|
|
use Terranet\Administrator\Traits\ExportsCollection; |
18
|
|
|
use Terranet\Rankable\Rankable; |
|
|
|
|
19
|
|
|
|
20
|
|
|
class CrudActions implements CrudActionsContract |
21
|
|
|
{ |
22
|
|
|
use ExportsCollection; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** @var Scaffolding */ |
25
|
|
|
protected $module; |
26
|
|
|
|
27
|
|
|
/** @var AdminRequest */ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* CrudActions constructor. |
32
|
|
|
* |
33
|
|
|
* @param $module |
34
|
|
|
* @param AdminRequest $request |
35
|
|
|
*/ |
36
|
|
|
public function __construct($module, AdminRequest $request) |
37
|
|
|
{ |
38
|
|
|
$this->module = $module; |
39
|
|
|
$this->request = $request; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Default custom list of actions. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function actions() |
48
|
|
|
{ |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Default list of batch actions. |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function batchActions() |
58
|
|
|
{ |
59
|
|
|
$actions = [RemoveSelected::class]; |
60
|
|
|
|
61
|
|
|
if ($this->module->model() instanceof Rankable) { |
62
|
|
|
array_push($actions, SaveOrder::class); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $actions; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Update item callback. |
70
|
|
|
* |
71
|
|
|
* @param $eloquent |
72
|
|
|
* @param UpdateRequest $request |
73
|
|
|
* |
74
|
|
|
* @throws Exception |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* |
78
|
|
|
* @internal param $repository |
79
|
|
|
*/ |
80
|
|
|
public function save($eloquent, UpdateRequest $request) |
81
|
|
|
{ |
82
|
|
|
$saver = $this->module->saver($eloquent, $request); |
83
|
|
|
|
84
|
|
|
return $saver->sync(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Destroy an attachment. |
89
|
|
|
* |
90
|
|
|
* @param $item |
91
|
|
|
* @param $attachment |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
|
|
public function detachFile(Model $item, $attachment) |
96
|
|
|
{ |
97
|
|
|
try { |
98
|
|
|
$item->$attachment = Attachment::NULL_ATTACHMENT; |
99
|
|
|
$item->save(); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} catch (\Exception $e) { |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Destroy item callback. |
109
|
|
|
* |
110
|
|
|
* @param Model $item |
111
|
|
|
* |
112
|
|
|
* @throws \Exception |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function delete(Model $item) |
117
|
|
|
{ |
118
|
|
|
if (method_exists($item, 'trashed') && $item->trashed()) { |
119
|
|
|
return $item->forceDelete(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $item->delete(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Determine if the user is authorized to make this request. |
127
|
|
|
* |
128
|
|
|
* @param string $method |
129
|
|
|
* @param $model |
130
|
|
|
* @param null $module |
|
|
|
|
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function authorize($method, $model = null, $module = null) |
135
|
|
|
{ |
136
|
|
|
$accessGate = Gate::forUser($user = $this->request->user()); |
137
|
|
|
$module = $module ?: $this->module; |
|
|
|
|
138
|
|
|
$model = $model ?: $module->model(); |
139
|
|
|
$method = Str::camel($method); |
140
|
|
|
|
141
|
|
|
if (($policy = $accessGate->getPolicyFor($model)) && method_exists($policy, $method)) { |
142
|
|
|
return $accessGate->allows($method, $model); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return true; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
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