1 | <?php |
||||
2 | /** |
||||
3 | * sysPass |
||||
4 | * |
||||
5 | * @author nuxsmin |
||||
6 | * @link https://syspass.org |
||||
7 | * @copyright 2012-2019, 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 DI\DependencyException; |
||||
28 | use DI\NotFoundException; |
||||
29 | use Exception; |
||||
30 | use SP\Core\Acl\Acl; |
||||
31 | use SP\Core\Events\Event; |
||||
32 | use SP\Core\Events\EventMessage; |
||||
33 | use SP\Core\Exceptions\ConstraintException; |
||||
34 | use SP\Core\Exceptions\QueryException; |
||||
35 | use SP\Core\Exceptions\SessionTimeout; |
||||
36 | use SP\Core\Exceptions\SPException; |
||||
37 | use SP\Core\Exceptions\ValidationException; |
||||
38 | use SP\DataModel\CategoryData; |
||||
39 | use SP\Http\JsonResponse; |
||||
40 | use SP\Modules\Web\Controllers\Helpers\Grid\CategoryGrid; |
||||
41 | use SP\Modules\Web\Controllers\Traits\ItemTrait; |
||||
42 | use SP\Modules\Web\Controllers\Traits\JsonTrait; |
||||
43 | use SP\Modules\Web\Forms\CategoryForm; |
||||
44 | use SP\Mvc\Controller\CrudControllerInterface; |
||||
45 | use SP\Repositories\NoSuchItemException; |
||||
46 | use SP\Services\Auth\AuthException; |
||||
47 | use SP\Services\Category\CategoryService; |
||||
48 | use SP\Services\ServiceException; |
||||
49 | |||||
50 | /** |
||||
51 | * Class CategoryController |
||||
52 | * |
||||
53 | * @package SP\Modules\Web\Controllers |
||||
54 | */ |
||||
55 | final class CategoryController extends ControllerBase implements CrudControllerInterface |
||||
56 | { |
||||
57 | use JsonTrait, ItemTrait; |
||||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||||
58 | |||||
59 | /** |
||||
60 | * @var CategoryService |
||||
61 | */ |
||||
62 | protected $categoryService; |
||||
63 | |||||
64 | /** |
||||
65 | * Search action |
||||
66 | * |
||||
67 | * @return bool |
||||
68 | * @throws DependencyException |
||||
69 | * @throws NotFoundException |
||||
70 | * @throws ConstraintException |
||||
71 | * @throws QueryException |
||||
72 | * @throws SPException |
||||
73 | */ |
||||
74 | public function searchAction() |
||||
75 | { |
||||
76 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
77 | |||||
78 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_SEARCH)) { |
||||
79 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
80 | } |
||||
81 | |||||
82 | $this->view->addTemplate('datagrid-table', 'grid'); |
||||
83 | $this->view->assign('index', $this->request->analyzeInt('activetab', 0)); |
||||
84 | $this->view->assign('data', $this->getSearchGrid()); |
||||
85 | |||||
86 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||||
87 | } |
||||
88 | |||||
89 | /** |
||||
90 | * getSearchGrid |
||||
91 | * |
||||
92 | * @return $this |
||||
93 | * @throws DependencyException |
||||
94 | * @throws NotFoundException |
||||
95 | * @throws ConstraintException |
||||
96 | * @throws QueryException |
||||
97 | */ |
||||
98 | protected function getSearchGrid() |
||||
99 | { |
||||
100 | $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request); |
||||
101 | |||||
102 | $itemsGridHelper = $this->dic->get(CategoryGrid::class); |
||||
103 | |||||
104 | return $itemsGridHelper->updatePager($itemsGridHelper->getGrid($this->categoryService->search($itemSearchData)), $itemSearchData); |
||||
105 | } |
||||
106 | |||||
107 | /** |
||||
108 | * Create action |
||||
109 | */ |
||||
110 | public function createAction() |
||||
111 | { |
||||
112 | try { |
||||
113 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
114 | |||||
115 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_CREATE)) { |
||||
116 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
117 | } |
||||
118 | |||||
119 | $this->view->assign('header', __('New Category')); |
||||
120 | $this->view->assign('isView', false); |
||||
121 | $this->view->assign('route', 'category/saveCreate'); |
||||
122 | |||||
123 | $this->setViewData(); |
||||
124 | |||||
125 | $this->eventDispatcher->notifyEvent('show.category.create', new Event($this)); |
||||
126 | |||||
127 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||||
128 | } catch (Exception $e) { |
||||
129 | processException($e); |
||||
130 | |||||
131 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
132 | |||||
133 | return $this->returnJsonResponseException($e); |
||||
134 | } |
||||
135 | } |
||||
136 | |||||
137 | /** |
||||
138 | * Sets view data for displaying category's data |
||||
139 | * |
||||
140 | * @param $categoryId |
||||
141 | * |
||||
142 | * @throws ConstraintException |
||||
143 | * @throws QueryException |
||||
144 | * @throws ServiceException |
||||
145 | * @throws NoSuchItemException |
||||
146 | */ |
||||
147 | protected function setViewData($categoryId = null) |
||||
148 | { |
||||
149 | $this->view->addTemplate('category', 'itemshow'); |
||||
150 | |||||
151 | $category = $categoryId ? $this->categoryService->getById($categoryId) : new CategoryData(); |
||||
152 | |||||
153 | $this->view->assign('category', $category); |
||||
154 | |||||
155 | $this->view->assign('nextAction', Acl::getActionRoute(Acl::ITEMS_MANAGE)); |
||||
156 | |||||
157 | if ($this->view->isView === true) { |
||||
0 ignored issues
–
show
The property
isView does not exist on SP\Mvc\View\Template . Since you implemented __get , consider adding a @property annotation.
Loading history...
|
|||||
158 | $this->view->assign('disabled', 'disabled'); |
||||
159 | $this->view->assign('readonly', 'readonly'); |
||||
160 | } else { |
||||
161 | $this->view->assign('disabled', false); |
||||
162 | $this->view->assign('readonly', false); |
||||
163 | } |
||||
164 | |||||
165 | $this->view->assign('showViewCustomPass', $this->acl->checkUserAccess(Acl::CUSTOMFIELD_VIEW_PASS)); |
||||
166 | $this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::CATEGORY, $categoryId)); |
||||
167 | } |
||||
168 | |||||
169 | /** |
||||
170 | * Edit action |
||||
171 | * |
||||
172 | * @param $id |
||||
173 | * |
||||
174 | * @return bool |
||||
175 | */ |
||||
176 | public function editAction($id) |
||||
177 | { |
||||
178 | try { |
||||
179 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
180 | |||||
181 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_EDIT)) { |
||||
182 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
183 | } |
||||
184 | |||||
185 | $this->view->assign('header', __('Edit Category')); |
||||
186 | $this->view->assign('isView', false); |
||||
187 | $this->view->assign('route', 'category/saveEdit/' . $id); |
||||
188 | |||||
189 | |||||
190 | $this->setViewData($id); |
||||
191 | |||||
192 | $this->eventDispatcher->notifyEvent('show.category.edit', new Event($this)); |
||||
193 | |||||
194 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||||
195 | } catch (Exception $e) { |
||||
196 | processException($e); |
||||
197 | |||||
198 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
199 | |||||
200 | return $this->returnJsonResponseException($e); |
||||
201 | } |
||||
202 | } |
||||
203 | |||||
204 | /** |
||||
205 | * Delete action |
||||
206 | * |
||||
207 | * @param $id |
||||
208 | * |
||||
209 | * @return bool |
||||
210 | */ |
||||
211 | public function deleteAction($id = null) |
||||
212 | { |
||||
213 | try { |
||||
214 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
215 | |||||
216 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_DELETE)) { |
||||
217 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
218 | } |
||||
219 | |||||
220 | if ($id === null) { |
||||
221 | $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
Loading history...
|
|||||
222 | |||||
223 | $this->deleteCustomFieldsForItem(Acl::CATEGORY, $id); |
||||
224 | |||||
225 | $this->eventDispatcher->notifyEvent('delete.category', |
||||
226 | new Event($this, |
||||
227 | EventMessage::factory() |
||||
228 | ->addDescription(__u('Categories deleted'))) |
||||
229 | ); |
||||
230 | |||||
231 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Categories deleted')); |
||||
232 | } |
||||
233 | |||||
234 | $this->categoryService->delete($id); |
||||
235 | |||||
236 | $this->deleteCustomFieldsForItem(Acl::CATEGORY, $id); |
||||
237 | |||||
238 | $this->eventDispatcher->notifyEvent('delete.category', |
||||
239 | new Event($this, |
||||
240 | EventMessage::factory() |
||||
241 | ->addDescription(__u('Category deleted')) |
||||
242 | ->addDetail(__u('Category'), $id)) |
||||
243 | ); |
||||
244 | |||||
245 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category deleted')); |
||||
246 | } catch (Exception $e) { |
||||
247 | processException($e); |
||||
248 | |||||
249 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
250 | |||||
251 | return $this->returnJsonResponseException($e); |
||||
252 | } |
||||
253 | } |
||||
254 | |||||
255 | /** |
||||
256 | * Saves create action |
||||
257 | */ |
||||
258 | public function saveCreateAction() |
||||
259 | { |
||||
260 | try { |
||||
261 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
262 | |||||
263 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_CREATE)) { |
||||
264 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
265 | } |
||||
266 | |||||
267 | $form = new CategoryForm($this->dic); |
||||
268 | $form->validate(Acl::CATEGORY_CREATE); |
||||
269 | |||||
270 | $itemData = $form->getItemData(); |
||||
271 | |||||
272 | $id = $this->categoryService->create($itemData); |
||||
273 | |||||
274 | $this->eventDispatcher->notifyEvent('create.category', |
||||
275 | new Event($this, |
||||
276 | EventMessage::factory() |
||||
277 | ->addDescription(__u('Category added')) |
||||
278 | ->addDetail(__u('Category'), $itemData->getName())) |
||||
279 | ); |
||||
280 | |||||
281 | $this->addCustomFieldsForItem(Acl::CATEGORY, $id, $this->request); |
||||
282 | |||||
283 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category added')); |
||||
284 | } catch (ValidationException $e) { |
||||
285 | return $this->returnJsonResponseException($e); |
||||
286 | } catch (Exception $e) { |
||||
287 | processException($e); |
||||
288 | |||||
289 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
290 | |||||
291 | return $this->returnJsonResponseException($e); |
||||
292 | } |
||||
293 | } |
||||
294 | |||||
295 | /** |
||||
296 | * Saves edit action |
||||
297 | * |
||||
298 | * @param $id |
||||
299 | * |
||||
300 | * @return bool |
||||
301 | */ |
||||
302 | public function saveEditAction($id) |
||||
303 | { |
||||
304 | try { |
||||
305 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
306 | |||||
307 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_EDIT)) { |
||||
308 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
309 | } |
||||
310 | |||||
311 | $form = new CategoryForm($this->dic, $id); |
||||
312 | $form->validate(Acl::CATEGORY_EDIT); |
||||
313 | |||||
314 | $itemData = $form->getItemData(); |
||||
315 | |||||
316 | $this->categoryService->update($itemData); |
||||
317 | |||||
318 | $this->eventDispatcher->notifyEvent('edit.category', |
||||
319 | new Event($this, |
||||
320 | EventMessage::factory() |
||||
321 | ->addDescription(__u('Category updated')) |
||||
322 | ->addDetail(__u('Category'), $itemData->getName())) |
||||
323 | ); |
||||
324 | |||||
325 | $this->updateCustomFieldsForItem(Acl::CATEGORY, $id, $this->request); |
||||
326 | |||||
327 | return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Category updated')); |
||||
328 | } catch (ValidationException $e) { |
||||
329 | return $this->returnJsonResponseException($e); |
||||
330 | } catch (Exception $e) { |
||||
331 | processException($e); |
||||
332 | |||||
333 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
334 | |||||
335 | return $this->returnJsonResponseException($e); |
||||
336 | } |
||||
337 | } |
||||
338 | |||||
339 | /** |
||||
340 | * View action |
||||
341 | * |
||||
342 | * @param $id |
||||
343 | * |
||||
344 | * @return bool |
||||
345 | */ |
||||
346 | public function viewAction($id) |
||||
347 | { |
||||
348 | try { |
||||
349 | $this->checkSecurityToken($this->previousSk, $this->request); |
||||
350 | |||||
351 | if (!$this->acl->checkUserAccess(Acl::CATEGORY_VIEW)) { |
||||
352 | return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation')); |
||||
353 | } |
||||
354 | |||||
355 | $this->view->assign('header', __('View Category')); |
||||
356 | $this->view->assign('isView', true); |
||||
357 | |||||
358 | $this->setViewData($id); |
||||
359 | |||||
360 | $this->eventDispatcher->notifyEvent('show.category', new Event($this)); |
||||
361 | |||||
362 | return $this->returnJsonResponseData(['html' => $this->render()]); |
||||
363 | } catch (Exception $e) { |
||||
364 | processException($e); |
||||
365 | |||||
366 | $this->eventDispatcher->notifyEvent('exception', new Event($e)); |
||||
367 | |||||
368 | return $this->returnJsonResponseException($e); |
||||
369 | } |
||||
370 | } |
||||
371 | |||||
372 | /** |
||||
373 | * Initialize class |
||||
374 | * |
||||
375 | * @throws AuthException |
||||
376 | * @throws DependencyException |
||||
377 | * @throws NotFoundException |
||||
378 | * @throws SessionTimeout |
||||
379 | */ |
||||
380 | protected function initialize() |
||||
381 | { |
||||
382 | $this->checkLoggedIn(); |
||||
383 | |||||
384 | $this->categoryService = $this->dic->get(CategoryService::class); |
||||
385 | } |
||||
386 | |||||
387 | } |