Passed
Push — master ( 26261c...609574 )
by Rubén
03:30
created

app/modules/web/Controllers/CategoryController.php (4 issues)

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\CategoryData;
32
use SP\Http\JsonResponse;
33
use SP\Modules\Web\Controllers\Helpers\Grid\CategoryGrid;
34
use SP\Modules\Web\Controllers\Traits\ItemTrait;
35
use SP\Modules\Web\Controllers\Traits\JsonTrait;
36
use SP\Modules\Web\Forms\CategoryForm;
37
use SP\Mvc\Controller\CrudControllerInterface;
38
use SP\Services\Category\CategoryService;
39
40
/**
41
 * Class CategoryController
42
 *
43
 * @package SP\Modules\Web\Controllers
44
 */
45
final class CategoryController extends ControllerBase implements CrudControllerInterface
46
{
47
    use JsonTrait, ItemTrait;
0 ignored issues
show
The trait SP\Modules\Web\Controllers\Traits\ItemTrait requires some properties which are not provided by SP\Modules\Web\Controllers\CategoryController: $data, $key
Loading history...
48
49
    /**
50
     * @var CategoryService
51
     */
52
    protected $categoryService;
53
54
    /**
55
     * Search action
56
     *
57
     * @return bool
58
     * @throws \DI\DependencyException
59
     * @throws \DI\NotFoundException
60
     * @throws \SP\Core\Exceptions\ConstraintException
61
     * @throws \SP\Core\Exceptions\QueryException
62
     * @throws \SP\Core\Exceptions\SPException
63
     */
64
    public function searchAction()
65
    {
66
        $this->checkSecurityToken($this->previousSk, $this->request);
67
68
        if (!$this->acl->checkUserAccess(Acl::CATEGORY_SEARCH)) {
69
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
70
        }
71
72
        $this->view->addTemplate('datagrid-table', 'grid');
73
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
74
        $this->view->assign('data', $this->getSearchGrid());
75
76
        return $this->returnJsonResponseData(['html' => $this->render()]);
77
    }
78
79
    /**
80
     * getSearchGrid
81
     *
82
     * @return $this
83
     * @throws \DI\DependencyException
84
     * @throws \DI\NotFoundException
85
     * @throws \SP\Core\Exceptions\ConstraintException
86
     * @throws \SP\Core\Exceptions\QueryException
87
     */
88
    protected function getSearchGrid()
89
    {
90
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
91
92
        $itemsGridHelper = $this->dic->get(CategoryGrid::class);
93
94
        return $itemsGridHelper->updatePager($itemsGridHelper->getGrid($this->categoryService->search($itemSearchData)), $itemSearchData);
95
    }
96
97
    /**
98
     * Create action
99
     */
100
    public function createAction()
101
    {
102
        try {
103
            $this->checkSecurityToken($this->previousSk, $this->request);
104
105
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_CREATE)) {
106
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
107
            }
108
109
            $this->view->assign('header', __('New Category'));
110
            $this->view->assign('isView', false);
111
            $this->view->assign('route', 'category/saveCreate');
112
113
            $this->setViewData();
114
115
            $this->eventDispatcher->notifyEvent('show.category.create', new Event($this));
116
117
            return $this->returnJsonResponseData(['html' => $this->render()]);
118
        } catch (\Exception $e) {
119
            processException($e);
120
121
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
122
123
            return $this->returnJsonResponseException($e);
124
        }
125
    }
126
127
    /**
128
     * Sets view data for displaying category's data
129
     *
130
     * @param $categoryId
131
     *
132
     * @throws \SP\Core\Exceptions\ConstraintException
133
     * @throws \SP\Core\Exceptions\QueryException
134
     * @throws \SP\Services\ServiceException
135
     * @throws \SP\Repositories\NoSuchItemException
136
     */
137
    protected function setViewData($categoryId = null)
138
    {
139
        $this->view->addTemplate('category', 'itemshow');
140
141
        $category = $categoryId ? $this->categoryService->getById($categoryId) : new CategoryData();
142
143
        $this->view->assign('category', $category);
144
145
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ITEMS_MANAGE));
146
147
        if ($this->view->isView === true) {
0 ignored issues
show
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...
148
            $this->view->assign('disabled', 'disabled');
149
            $this->view->assign('readonly', 'readonly');
150
        } else {
151
            $this->view->assign('disabled', false);
152
            $this->view->assign('readonly', false);
153
        }
154
155
        $this->view->assign('showViewCustomPass', $this->acl->checkUserAccess(Acl::CUSTOMFIELD_VIEW_PASS));
156
        $this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::CATEGORY, $categoryId));
157
    }
158
159
    /**
160
     * Edit action
161
     *
162
     * @param $id
163
     *
164
     * @return bool
165
     */
166
    public function editAction($id)
167
    {
168
        try {
169
            $this->checkSecurityToken($this->previousSk, $this->request);
170
171
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_EDIT)) {
172
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
173
            }
174
175
            $this->view->assign('header', __('Edit Category'));
176
            $this->view->assign('isView', false);
177
            $this->view->assign('route', 'category/saveEdit/' . $id);
178
179
180
            $this->setViewData($id);
181
182
            $this->eventDispatcher->notifyEvent('show.category.edit', new Event($this));
183
184
            return $this->returnJsonResponseData(['html' => $this->render()]);
185
        } catch (\Exception $e) {
186
            processException($e);
187
188
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
189
190
            return $this->returnJsonResponseException($e);
191
        }
192
    }
193
194
    /**
195
     * Delete action
196
     *
197
     * @param $id
198
     *
199
     * @return bool
200
     */
201
    public function deleteAction($id = null)
202
    {
203
        try {
204
            $this->checkSecurityToken($this->previousSk, $this->request);
205
206
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_DELETE)) {
207
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
208
            }
209
210
            if ($id === null) {
211
                $this->categoryService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
0 ignored issues
show
It seems like $this->getItemsIdFromRequest($this->request) can also be of type null; however, parameter $ids of SP\Services\Category\Cat...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

211
                $this->categoryService->deleteByIdBatch(/** @scrutinizer ignore-type */ $this->getItemsIdFromRequest($this->request));
Loading history...
212
213
                $this->deleteCustomFieldsForItem(Acl::CATEGORY, $id);
214
215
                $this->eventDispatcher->notifyEvent('delete.category',
216
                    new Event($this,
217
                        EventMessage::factory()
218
                            ->addDescription(__u('Categories deleted')))
219
                );
220
221
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Categories deleted'));
222
            }
223
224
            $this->categoryService->delete($id);
225
226
            $this->deleteCustomFieldsForItem(Acl::CATEGORY, $id);
227
228
            $this->eventDispatcher->notifyEvent('delete.category',
229
                new Event($this,
230
                    EventMessage::factory()
231
                        ->addDescription(__u('Category deleted'))
232
                        ->addDetail(__u('Category'), $id))
233
            );
234
235
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category deleted'));
236
        } catch (\Exception $e) {
237
            processException($e);
238
239
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
240
241
            return $this->returnJsonResponseException($e);
242
        }
243
    }
244
245
    /**
246
     * Saves create action
247
     */
248
    public function saveCreateAction()
249
    {
250
        try {
251
            $this->checkSecurityToken($this->previousSk, $this->request);
252
253
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_CREATE)) {
254
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
255
            }
256
257
            $form = new CategoryForm($this->dic);
258
            $form->validate(Acl::CATEGORY_CREATE);
259
260
            $itemData = $form->getItemData();
261
262
            $id = $this->categoryService->create($itemData);
263
264
            $this->addCustomFieldsForItem(Acl::CATEGORY, $id, $this->request);
265
266
            $this->eventDispatcher->notifyEvent('create.category',
267
                new Event($this,
268
                    EventMessage::factory()
269
                        ->addDescription(__u('Category added'))
270
                        ->addDetail(__u('Category'), $itemData->getName()))
271
            );
272
273
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category added'));
274
        } catch (ValidationException $e) {
275
            return $this->returnJsonResponseException($e);
276
        } catch (\Exception $e) {
277
            processException($e);
278
279
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
280
281
            return $this->returnJsonResponseException($e);
282
        }
283
    }
284
285
    /**
286
     * Saves edit action
287
     *
288
     * @param $id
289
     *
290
     * @return bool
291
     */
292
    public function saveEditAction($id)
293
    {
294
        try {
295
            $this->checkSecurityToken($this->previousSk, $this->request);
296
297
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_EDIT)) {
298
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
299
            }
300
301
            $form = new CategoryForm($this->dic, $id);
302
            $form->validate(Acl::CATEGORY_EDIT);
303
304
            $itemData = $form->getItemData();
305
306
            $this->categoryService->update($itemData);
307
308
            $this->updateCustomFieldsForItem(Acl::CATEGORY, $id, $this->request);
309
310
            $this->eventDispatcher->notifyEvent('edit.category',
311
                new Event($this,
312
                    EventMessage::factory()
313
                        ->addDescription(__u('Category updated'))
314
                        ->addDetail(__u('Category'), $itemData->getName()))
315
            );
316
317
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category updated'));
318
        } catch (ValidationException $e) {
319
            return $this->returnJsonResponseException($e);
320
        } catch (\Exception $e) {
321
            processException($e);
322
323
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
324
325
            return $this->returnJsonResponseException($e);
326
        }
327
    }
328
329
    /**
330
     * View action
331
     *
332
     * @param $id
333
     *
334
     * @return bool
335
     */
336
    public function viewAction($id)
337
    {
338
        try {
339
            $this->checkSecurityToken($this->previousSk, $this->request);
340
341
            if (!$this->acl->checkUserAccess(Acl::CATEGORY_VIEW)) {
342
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
343
            }
344
345
            $this->view->assign('header', __('View Category'));
346
            $this->view->assign('isView', true);
347
348
            $this->setViewData($id);
349
350
            $this->eventDispatcher->notifyEvent('show.category', new Event($this));
351
352
            return $this->returnJsonResponseData(['html' => $this->render()]);
353
        } catch (\Exception $e) {
354
            processException($e);
355
356
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
357
358
            return $this->returnJsonResponseException($e);
359
        }
360
    }
361
362
    /**
363
     * Initialize class
364
     *
365
     * @throws \Psr\Container\ContainerExceptionInterface
366
     * @throws \Psr\Container\NotFoundExceptionInterface
367
     * @throws \SP\Services\Auth\AuthException
368
     */
369
    protected function initialize()
370
    {
371
        $this->checkLoggedIn();
372
373
        $this->categoryService = $this->dic->get(CategoryService::class);
374
    }
375
376
}