Passed
Push — devel-3.0 ( 7ceb3a...2fc71e )
by Rubén
03:58
created

ItemPresetController   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 314
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 134
dl 0
loc 314
rs 9.92
c 0
b 0
f 0
wmc 31

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getSearchGrid() 0 9 1
A searchAction() 0 11 2
A initialize() 0 5 1
A editAction() 0 20 3
A deleteAction() 0 33 4
A saveCreateAction() 0 29 4
A saveEditAction() 0 29 4
A setViewData() 0 30 5
A createAction() 0 28 4
A viewAction() 0 19 3
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 SP\Core\Acl\Acl;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\Core\Exceptions\ValidationException;
31
use SP\DataModel\ItemPresetData;
32
use SP\Http\JsonResponse;
33
use SP\Modules\Web\Controllers\Helpers\Grid\ItemPresetGrid;
34
use SP\Modules\Web\Controllers\Helpers\ItemPresetHelper;
35
use SP\Modules\Web\Controllers\Traits\ItemTrait;
36
use SP\Modules\Web\Controllers\Traits\JsonTrait;
37
use SP\Modules\Web\Forms\ItemsPresetForm;
38
use SP\Mvc\Controller\CrudControllerInterface;
39
use SP\Services\ItemPreset\ItemPresetInterface;
40
use SP\Services\ItemPreset\ItemPresetService;
41
use SP\Util\Filter;
42
43
/**
44
 * Class AccountDefaultPermissionController
45
 *
46
 * @package SP\Modules\Web\Controllers
47
 */
48
class ItemPresetController extends ControllerBase implements CrudControllerInterface
49
{
50
    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\ItemPresetController: $data, $key
Loading history...
51
52
    /**
53
     * @var ItemPresetService
54
     */
55
    protected $itemPresetService;
56
57
    /**
58
     * View action
59
     *
60
     * @param $id
61
     *
62
     * @return bool
63
     */
64
    public function viewAction($id)
65
    {
66
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_VIEW)) {
67
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
68
        }
69
70
        $this->view->assign('header', __('Ver Valor'));
71
        $this->view->assign('isView', true);
72
73
        try {
74
            $this->setViewData($id);
75
76
            $this->eventDispatcher->notifyEvent('show.itemPreset', new Event($this));
77
78
            return $this->returnJsonResponseData(['html' => $this->render()]);
79
        } catch (\Exception $e) {
80
            processException($e);
81
82
            return $this->returnJsonResponseException($e);
83
        }
84
    }
85
86
    /**
87
     * Sets view data for displaying permissions' data
88
     *
89
     * @param int    $id
90
     * @param string $type
91
     *
92
     * @throws \SP\Core\Exceptions\ConstraintException
93
     * @throws \SP\Core\Exceptions\NoSuchPropertyException
94
     * @throws \SP\Core\Exceptions\QueryException
95
     * @throws \SP\Repositories\NoSuchItemException
96
     */
97
    protected function setViewData(int $id = null, string $type = null)
98
    {
99
        $this->view->addTemplate('item_preset', 'itemshow');
100
101
        $itemPresetData = $id ? $this->itemPresetService->getById($id) : new ItemPresetData();
102
103
        $itemPresetHelper = $this->dic->get(ItemPresetHelper::class);
104
105
        if ($itemPresetData->getType() === null) {
0 ignored issues
show
introduced by
The condition $itemPresetData->getType() === null is always false.
Loading history...
106
            $itemPresetData->setType($type);
107
        }
108
109
        switch ($itemPresetData->getType()) {
110
            case ItemPresetInterface::ITEM_TYPE_PERMISSION:
111
                $itemPresetHelper->makeAccountPermissionView($itemPresetData);
112
                break;
113
            default:
114
                $itemPresetHelper->makeDefaultPresetView();
115
        }
116
117
        $this->view->assign('preset', $itemPresetData);
118
        $this->view->assign('sk', $this->session->generateSecurityKey());
119
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE));
120
121
        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...
122
            $this->view->assign('disabled', 'disabled');
123
            $this->view->assign('readonly', 'readonly');
124
        } else {
125
            $this->view->assign('disabled');
126
            $this->view->assign('readonly');
127
        }
128
    }
129
130
    /**
131
     * Search action
132
     *
133
     * @return bool
134
     * @throws \SP\Core\Exceptions\ConstraintException
135
     * @throws \SP\Core\Exceptions\QueryException
136
     */
137
    public function searchAction()
138
    {
139
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_SEARCH)) {
140
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
141
        }
142
143
        $this->view->addTemplate('datagrid-table', 'grid');
144
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
145
        $this->view->assign('data', $this->getSearchGrid());
146
147
        return $this->returnJsonResponseData(['html' => $this->render()]);
148
    }
149
150
    /**
151
     * getSearchGrid
152
     *
153
     * @return $this
154
     * @throws \SP\Core\Exceptions\ConstraintException
155
     * @throws \SP\Core\Exceptions\QueryException
156
     */
157
    protected function getSearchGrid()
158
    {
159
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
160
161
        $grid = $this->dic->get(ItemPresetGrid::class);
162
163
        return $grid->updatePager(
164
            $grid->getGrid($this->itemPresetService->search($itemSearchData)),
165
            $itemSearchData
166
        );
167
    }
168
169
    /**
170
     * Create action
171
     */
172
    public function createAction()
173
    {
174
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_CREATE)) {
175
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
176
        }
177
178
        $args = func_get_args();
179
        $type = null;
180
181
        if (count($args) > 0) {
182
            $type = Filter::getString($args[0]);
183
        }
184
185
        $this->view->assign(__FUNCTION__, 1);
186
        $this->view->assign('header', __('Nuevo Valor'));
187
        $this->view->assign('isView', false);
188
        $this->view->assign('route', 'itemPreset/saveCreate');
189
190
        try {
191
            $this->setViewData(null, $type);
192
193
            $this->eventDispatcher->notifyEvent('show.itemPreset.create', new Event($this));
194
195
            return $this->returnJsonResponseData(['html' => $this->render()]);
196
        } catch (\Exception $e) {
197
            processException($e);
198
199
            return $this->returnJsonResponseException($e);
200
        }
201
    }
202
203
    /**
204
     * Edit action
205
     *
206
     * @param $id
207
     *
208
     * @return bool
209
     */
210
    public function editAction($id)
211
    {
212
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_EDIT)) {
213
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
214
        }
215
216
        $this->view->assign('header', __('Editar Valor'));
217
        $this->view->assign('isView', false);
218
        $this->view->assign('route', 'itemPreset/saveEdit/' . $id);
219
220
        try {
221
            $this->setViewData($id);
222
223
            $this->eventDispatcher->notifyEvent('show.itemPreset.edit', new Event($this));
224
225
            return $this->returnJsonResponseData(['html' => $this->render()]);
226
        } catch (\Exception $e) {
227
            processException($e);
228
229
            return $this->returnJsonResponseException($e);
230
        }
231
    }
232
233
    /**
234
     * Delete action
235
     *
236
     * @param $id
237
     *
238
     * @return bool
239
     */
240
    public function deleteAction($id = null)
241
    {
242
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_DELETE)) {
243
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
244
        }
245
246
        try {
247
            if ($id === null) {
248
                $this->itemPresetService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
249
250
                $this->eventDispatcher->notifyEvent('delete.itemPreset',
251
                    new Event($this,
252
                        EventMessage::factory()
253
                            ->addDescription(__u('Valores eliminados')))
254
                );
255
256
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Valores eliminados'));
257
            }
258
259
            $this->itemPresetService->delete($id);
260
261
            $this->eventDispatcher->notifyEvent('delete.itemPreset',
262
                new Event($this,
263
                    EventMessage::factory()
264
                        ->addDescription(__u('Valor eliminado'))
265
                        ->addDetail(__u('ID'), $id))
266
            );
267
268
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Valor eliminado'));
269
        } catch (\Exception $e) {
270
            processException($e);
271
272
            return $this->returnJsonResponseException($e);
273
        }
274
    }
275
276
    /**
277
     * Saves create action
278
     */
279
    public function saveCreateAction()
280
    {
281
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_CREATE)) {
282
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
283
        }
284
285
        try {
286
            $form = new ItemsPresetForm($this->dic);
287
            $form->validate(Acl::ITEMPRESET_CREATE);
288
289
            $itemData = $form->getItemData();
290
291
            $id = $this->itemPresetService->create($itemData);
292
293
            $this->eventDispatcher->notifyEvent('create.itemPreset',
294
                new Event($this,
295
                    EventMessage::factory()
296
                        ->addDescription(__u('Valor creado'))
297
                        ->addDetail(__u('Tipo'), $itemData->getItemPresetData()->getType())
298
                        ->addDetail(__u('ID'), $id))
299
            );
300
301
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Valor creado'));
302
        } catch (ValidationException $e) {
303
            return $this->returnJsonResponseException($e);
304
        } catch (\Exception $e) {
305
            processException($e);
306
307
            return $this->returnJsonResponseException($e);
308
        }
309
    }
310
311
    /**
312
     * Saves edit action
313
     *
314
     * @param $id
315
     *
316
     * @return bool
317
     */
318
    public function saveEditAction($id)
319
    {
320
        if (!$this->acl->checkUserAccess(Acl::ITEMPRESET_EDIT)) {
321
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
322
        }
323
324
        try {
325
            $form = new ItemsPresetForm($this->dic, $id);
326
            $form->validate(Acl::ITEMPRESET_EDIT);
327
328
            $itemData = $form->getItemData();
329
330
            $this->itemPresetService->update($itemData);
331
332
            $this->eventDispatcher->notifyEvent('edit.itemPreset',
333
                new Event($this,
334
                    EventMessage::factory()
335
                        ->addDescription(__u('Valor actualizado'))
336
                        ->addDetail(__u('Tipo'), $itemData->getItemPresetData()->getType())
337
                        ->addDetail(__u('ID'), $id))
338
            );
339
340
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Valor actualizado'));
341
        } catch (ValidationException $e) {
342
            return $this->returnJsonResponseException($e);
343
        } catch (\Exception $e) {
344
            processException($e);
345
346
            return $this->returnJsonResponseException($e);
347
        }
348
    }
349
350
    /**
351
     * Initialize class
352
     *
353
     * @throws \Psr\Container\ContainerExceptionInterface
354
     * @throws \Psr\Container\NotFoundExceptionInterface
355
     * @throws \SP\Services\Auth\AuthException
356
     */
357
    protected function initialize()
358
    {
359
        $this->checkLoggedIn();
360
361
        $this->itemPresetService = $this->dic->get(ItemPresetService::class);
362
    }
363
}