Passed
Push — devel-3.0 ( 67d9a6...e0263f )
by Rubén
04:37
created

PluginController::deleteAction()   A

Complexity

Conditions 4
Paths 12

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 12
nop 1
dl 0
loc 26
rs 9.7666
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Modules\Web\Controllers;
26
27
use Psr\Container\ContainerExceptionInterface;
28
use Psr\Container\NotFoundExceptionInterface;
29
use SP\Core\Acl\Acl;
30
use SP\Core\Events\Event;
31
use SP\Core\Events\EventMessage;
32
use SP\DataModel\PluginData;
33
use SP\Http\JsonResponse;
34
use SP\Modules\Web\Controllers\Helpers\Grid\PluginGrid;
35
use SP\Modules\Web\Controllers\Traits\ItemTrait;
36
use SP\Modules\Web\Controllers\Traits\JsonTrait;
37
use SP\Plugin\PluginManager;
38
use SP\Services\Plugin\PluginService;
39
40
/**
41
 * Class PluginController
42
 *
43
 * @package web\Controllers
44
 */
45
final class PluginController extends ControllerBase
46
{
47
    use JsonTrait, ItemTrait;
0 ignored issues
show
introduced by
The trait SP\Modules\Web\Controllers\Traits\ItemTrait requires some properties which are not provided by SP\Modules\Web\Controllers\PluginController: $data, $key
Loading history...
48
49
    /**
50
     * @var PluginService
51
     */
52
    protected $pluginService;
53
54
    /**
55
     * indexAction
56
     *
57
     * @throws \DI\DependencyException
58
     * @throws \DI\NotFoundException
59
     * @throws \SP\Core\Exceptions\ConstraintException
60
     * @throws \SP\Core\Exceptions\QueryException
61
     * @throws \SP\Core\Exceptions\SPException
62
     */
63
    public function indexAction()
64
    {
65
        $this->checkSecurityToken($this->previousSk, $this->request);
66
67
        if (!$this->acl->checkUserAccess(Acl::PLUGIN)) {
68
            return;
69
        }
70
71
        $this->view->addTemplate('index');
72
73
        $this->view->assign('data', $this->getSearchGrid());
74
75
        $this->view();
76
    }
77
78
    /**
79
     * getSearchGrid
80
     *
81
     * @return $this
82
     * @throws \DI\DependencyException
83
     * @throws \DI\NotFoundException
84
     * @throws \SP\Core\Exceptions\ConstraintException
85
     * @throws \SP\Core\Exceptions\QueryException
86
     */
87
    protected function getSearchGrid()
88
    {
89
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
90
91
        $pluginGrid = $this->dic->get(PluginGrid::class);
92
93
        return $pluginGrid->updatePager($pluginGrid->getGrid($this->pluginService->search($itemSearchData)), $itemSearchData);
94
    }
95
96
    /**
97
     * Search action
98
     *
99
     * @return bool
100
     * @throws \DI\DependencyException
101
     * @throws \DI\NotFoundException
102
     * @throws \SP\Core\Exceptions\ConstraintException
103
     * @throws \SP\Core\Exceptions\QueryException
104
     * @throws \SP\Core\Exceptions\SPException
105
     */
106
    public function searchAction()
107
    {
108
        $this->checkSecurityToken($this->previousSk, $this->request);
109
110
        if (!$this->acl->checkUserAccess(Acl::PLUGIN_SEARCH)) {
111
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
112
        }
113
114
        $this->view->addTemplate('datagrid-table', 'grid');
115
        $this->view->assign('data', $this->getSearchGrid());
116
117
        return $this->returnJsonResponseData(['html' => $this->render()]);
118
    }
119
120
    /**
121
     * View action
122
     *
123
     * @param $id
124
     *
125
     * @return bool
126
     */
127
    public function viewAction($id)
128
    {
129
        try {
130
            $this->checkSecurityToken($this->previousSk, $this->request);
131
132
            if (!$this->acl->checkUserAccess(Acl::PLUGIN_VIEW)) {
133
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
134
            }
135
136
            $this->view->assign('header', __('Ver Plugin'));
137
            $this->view->assign('isView', true);
138
139
            $this->setViewData($id);
140
141
            $this->eventDispatcher->notifyEvent('show.plugin', new Event($this));
142
143
            return $this->returnJsonResponseData(['html' => $this->render()]);
144
        } catch (\Exception $e) {
145
            processException($e);
146
147
            return $this->returnJsonResponseException($e);
148
        }
149
    }
150
151
    /**
152
     * Sets view data for displaying items's data
153
     *
154
     * @param $pluginId
155
     *
156
     * @throws \DI\DependencyException
157
     * @throws \DI\NotFoundException
158
     * @throws \SP\Core\Exceptions\ConstraintException
159
     * @throws \SP\Core\Exceptions\QueryException
160
     * @throws \SP\Repositories\NoSuchItemException
161
     */
162
    protected function setViewData($pluginId = null)
163
    {
164
        $this->view->addTemplate('plugin');
165
166
        $pluginData = $pluginId ? $this->pluginService->getById($pluginId) : new PluginData();
167
        $pluginInfo = $this->dic->get(PluginManager::class)->getPluginInfo($pluginData->name);
168
169
        $this->view->assign('plugin', $pluginData);
170
        $this->view->assign('pluginInfo', $pluginInfo);
171
172
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ITEMS_MANAGE));
173
174
        if ($this->view->isView === true) {
0 ignored issues
show
introduced by
The condition $this->view->isView === true is always false.
Loading history...
Bug Best Practice introduced by
The property isView does not exist on SP\Mvc\View\Template. Since you implemented __get, consider adding a @property annotation.
Loading history...
175
            $this->view->assign('disabled', 'disabled');
176
            $this->view->assign('readonly', 'readonly');
177
        } else {
178
            $this->view->assign('disabled', false);
179
            $this->view->assign('readonly', false);
180
        }
181
    }
182
183
    /**
184
     * enableAction
185
     *
186
     * @param $id
187
     *
188
     * @return bool
189
     */
190
    public function enableAction($id)
191
    {
192
        try {
193
            $this->checkSecurityToken($this->previousSk, $this->request);
194
195
            $this->pluginService->toggleEnabled($id, 1);
196
197
            $this->eventDispatcher->notifyEvent('edit.plugin.enable',
198
                new Event($this,
199
                    EventMessage::factory()->addDescription(__u('Plugin habilitado')))
200
            );
201
202
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Plugin habilitado'));
203
        } catch (\Exception $e) {
204
            processException($e);
205
206
            return $this->returnJsonResponseException($e);
207
        }
208
    }
209
210
    /**
211
     * disableAction
212
     *
213
     * @param $id
214
     *
215
     * @return bool
216
     */
217
    public function disableAction($id)
218
    {
219
        try {
220
            $this->checkSecurityToken($this->previousSk, $this->request);
221
222
            $this->pluginService->toggleEnabled($id, 0);
223
224
            $this->eventDispatcher->notifyEvent('edit.plugin.disable',
225
                new Event($this,
226
                    EventMessage::factory()->addDescription(__u('Plugin deshabilitado')))
227
            );
228
229
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Plugin deshabilitado'));
230
        } catch (\Exception $e) {
231
            processException($e);
232
233
            return $this->returnJsonResponseException($e);
234
        }
235
    }
236
237
    /**
238
     * resetAction
239
     *
240
     * @param $id
241
     *
242
     * @return bool
243
     */
244
    public function resetAction($id)
245
    {
246
        try {
247
            $this->checkSecurityToken($this->previousSk, $this->request);
248
249
            $this->pluginService->resetById($id);
250
251
            $this->eventDispatcher->notifyEvent('edit.plugin.reset',
252
                new Event($this,
253
                    EventMessage::factory()->addDescription(__u('Plugin restablecido')))
254
            );
255
256
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Plugin restablecido'));
257
        } catch (\Exception $e) {
258
            processException($e);
259
260
            return $this->returnJsonResponseException($e);
261
        }
262
    }
263
264
    /**
265
     * resetAction
266
     *
267
     * @param $id
268
     *
269
     * @return bool
270
     */
271
    public function deleteAction($id)
272
    {
273
        try {
274
            $this->checkSecurityToken($this->previousSk, $this->request);
275
276
            if (!$this->acl->checkUserAccess(Acl::PLUGIN_DELETE)) {
277
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
278
            }
279
280
            if ($id === null) {
281
                $this->pluginService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
0 ignored issues
show
Bug introduced by
It seems like $this->getItemsIdFromRequest($this->request) can also be of type null; however, parameter $ids of SP\Services\Plugin\Plugi...vice::deleteByIdBatch() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

281
                $this->pluginService->deleteByIdBatch(/** @scrutinizer ignore-type */ $this->getItemsIdFromRequest($this->request));
Loading history...
282
283
                $this->eventDispatcher->notifyEvent('delete.plugin.selection', new Event($this));
284
285
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Plugins eliminados'));
286
            } else {
287
                $this->pluginService->delete($id);
288
289
                $this->eventDispatcher->notifyEvent('delete.plugin', new Event($this));
290
291
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Plugin eliminado'));
292
            }
293
        } catch (\Exception $e) {
294
            processException($e);
295
296
            return $this->returnJsonResponseException($e);
297
        }
298
    }
299
300
    /**
301
     * @throws ContainerExceptionInterface
302
     * @throws NotFoundExceptionInterface
303
     * @throws \SP\Services\Auth\AuthException
304
     */
305
    protected function initialize()
306
    {
307
        $this->checkLoggedIn();
308
309
        $this->pluginService = $this->dic->get(PluginService::class);
310
    }
311
}