Passed
Push — 3.0 ( b611c9...572608 )
by Rubén
03:38
created

NotificationController::deleteAction()   A

Complexity

Conditions 5
Paths 19

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 19
nop 1
dl 0
loc 41
rs 9.1928
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 SP\Core\Acl\Acl;
28
use SP\Core\Events\Event;
29
use SP\Core\Events\EventMessage;
30
use SP\DataModel\NotificationData;
31
use SP\Http\JsonResponse;
32
use SP\Modules\Web\Controllers\Helpers\Grid\NotificationGrid;
33
use SP\Modules\Web\Controllers\Traits\ItemTrait;
34
use SP\Modules\Web\Controllers\Traits\JsonTrait;
35
use SP\Modules\Web\Forms\NotificationForm;
36
use SP\Mvc\Controller\CrudControllerInterface;
37
use SP\Mvc\View\Components\SelectItemAdapter;
38
use SP\Services\Notification\NotificationService;
39
use SP\Services\User\UserService;
40
41
/**
42
 * Class NotificationController
43
 *
44
 * @package SP\Modules\Web\Controllers
45
 */
46
final class NotificationController extends ControllerBase implements CrudControllerInterface
47
{
48
    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\NotificationController: $data, $key
Loading history...
49
50
    /**
51
     * @var NotificationService
52
     */
53
    protected $notificationService;
54
55
    /**
56
     * indexAction
57
     *
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 indexAction()
65
    {
66
        $this->checkSecurityToken($this->previousSk, $this->request);
67
68
        if (!$this->acl->checkUserAccess(Acl::NOTIFICATION)) {
69
            return;
70
        }
71
72
        $this->view->addTemplate('index');
73
74
        $this->view->assign('data', $this->getSearchGrid());
75
76
        $this->view();
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
        $notificationGrid = $this->dic->get(NotificationGrid::class);
93
94
        return $notificationGrid->updatePager($notificationGrid->getGrid($this->notificationService->search($itemSearchData)), $itemSearchData);
95
    }
96
97
    /**
98
     * View action
99
     *
100
     * @param $id
101
     *
102
     * @return bool
103
     */
104
    public function viewAction($id)
105
    {
106
        try {
107
            $this->checkSecurityToken($this->previousSk, $this->request);
108
109
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_VIEW)) {
110
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
111
            }
112
113
            $this->view->assign('header', __('View Notification'));
114
            $this->view->assign('isView', true);
115
116
            $this->setViewData($id);
117
118
            $this->eventDispatcher->notifyEvent('show.notification', new Event($this));
119
120
            return $this->returnJsonResponseData(['html' => $this->render()]);
121
        } catch (\Exception $e) {
122
            processException($e);
123
124
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
125
126
            return $this->returnJsonResponseException($e);
127
        }
128
    }
129
130
    /**
131
     * Sets view data for displaying notification's data
132
     *
133
     * @param $notificationId
134
     *
135
     * @throws \SP\Core\Exceptions\ConstraintException
136
     * @throws \SP\Core\Exceptions\QueryException
137
     * @throws \SP\Repositories\NoSuchItemException
138
     */
139
    protected function setViewData($notificationId = null)
140
    {
141
        $this->view->addTemplate('notification');
142
143
        $notification = $notificationId ? $this->notificationService->getById($notificationId) : new NotificationData();
144
145
        $this->view->assign('notification', $notification);
146
147
        if ($this->userData->getIsAdminApp()) {
148
            $this->view->assign('users', SelectItemAdapter::factory(UserService::getItemsBasic())->getItemsFromModelSelected([$notification->userId]));
149
        }
150
151
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::NOTIFICATION));
152
153
        if ($this->view->isView === true) {
0 ignored issues
show
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...
introduced by
The condition $this->view->isView === true is always false.
Loading history...
154
            $this->view->assign('disabled', 'disabled');
155
            $this->view->assign('readonly', 'readonly');
156
        } else {
157
            $this->view->assign('disabled', false);
158
            $this->view->assign('readonly', false);
159
        }
160
    }
161
162
    /**
163
     * @return bool
164
     * @throws \DI\DependencyException
165
     * @throws \DI\NotFoundException
166
     * @throws \SP\Core\Exceptions\ConstraintException
167
     * @throws \SP\Core\Exceptions\QueryException
168
     * @throws \SP\Core\Exceptions\SPException
169
     */
170
    public function searchAction()
171
    {
172
        $this->checkSecurityToken($this->previousSk, $this->request);
173
174
        if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_SEARCH)) {
175
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
176
        }
177
178
        $this->view->addTemplate('datagrid-table', 'grid');
179
        $this->view->assign('data', $this->getSearchGrid());
180
181
        return $this->returnJsonResponseData(['html' => $this->render()]);
182
    }
183
184
    /**
185
     * Create action
186
     */
187
    public function createAction()
188
    {
189
        try {
190
            $this->checkSecurityToken($this->previousSk, $this->request);
191
192
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_CREATE)) {
193
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
194
            }
195
196
            $this->view->assign('header', __('New Notification'));
197
            $this->view->assign('isView', false);
198
            $this->view->assign('route', 'notification/saveCreate');
199
200
            $this->setViewData();
201
202
            $this->eventDispatcher->notifyEvent('show.notification.create', new Event($this));
203
204
            return $this->returnJsonResponseData(['html' => $this->render()]);
205
        } catch (\Exception $e) {
206
            processException($e);
207
208
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
209
210
            return $this->returnJsonResponseException($e);
211
        }
212
    }
213
214
    /**
215
     * Edit action
216
     *
217
     * @param $id
218
     *
219
     * @return bool
220
     */
221
    public function editAction($id)
222
    {
223
        try {
224
            $this->checkSecurityToken($this->previousSk, $this->request);
225
226
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_EDIT)) {
227
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
228
            }
229
230
            $this->view->assign('header', __('Edit Notification'));
231
            $this->view->assign('isView', false);
232
            $this->view->assign('route', 'notification/saveEdit/' . $id);
233
234
            $this->setViewData($id);
235
236
            $this->eventDispatcher->notifyEvent('show.notification.edit', new Event($this));
237
238
            return $this->returnJsonResponseData(['html' => $this->render()]);
239
        } catch (\Exception $e) {
240
            processException($e);
241
242
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
243
244
            return $this->returnJsonResponseException($e);
245
        }
246
    }
247
248
    /**
249
     * Delete action
250
     *
251
     * @param $id
252
     *
253
     * @return bool
254
     */
255
    public function deleteAction($id = null)
256
    {
257
        try {
258
            $this->checkSecurityToken($this->previousSk, $this->request);
259
260
            if ($id === null) {
261
                if ($this->userData->getIsAdminApp()) {
262
                    $this->notificationService->deleteAdminBatch($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\Notification...ice::deleteAdminBatch() 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

262
                    $this->notificationService->deleteAdminBatch(/** @scrutinizer ignore-type */ $this->getItemsIdFromRequest($this->request));
Loading history...
263
                } else {
264
                    $this->notificationService->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\Notification...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

264
                    $this->notificationService->deleteByIdBatch(/** @scrutinizer ignore-type */ $this->getItemsIdFromRequest($this->request));
Loading history...
265
                }
266
267
                $this->eventDispatcher->notifyEvent('delete.notification.selection',
268
                    new Event($this,
269
                        EventMessage::factory()
270
                            ->addDescription(__u('Notifications deleted')))
271
                );
272
273
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Notifications deleted'));
274
            }
275
276
            if ($this->userData->getIsAdminApp()) {
277
                $this->notificationService->deleteAdmin($id);
278
            } else {
279
                $this->notificationService->delete($id);
280
            }
281
282
            $this->eventDispatcher->notifyEvent('delete.notification',
283
                new Event($this,
284
                    EventMessage::factory()
285
                        ->addDescription(__u('Notification deleted'))
286
                        ->addDetail(__u('Notification'), $id))
287
            );
288
289
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Notification deleted'));
290
        } catch (\Exception $e) {
291
            processException($e);
292
293
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
294
295
            return $this->returnJsonResponseException($e);
296
        }
297
    }
298
299
    /**
300
     * Check action
301
     *
302
     * @param $id
303
     *
304
     * @return bool
305
     */
306
    public function checkAction($id)
307
    {
308
        try {
309
            $this->checkSecurityToken($this->previousSk, $this->request);
310
311
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_CHECK)) {
312
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
313
            }
314
315
            $this->notificationService->setCheckedById($id);
316
317
            $this->eventDispatcher->notifyEvent('check.notification',
318
                new Event($this,
319
                    EventMessage::factory()
320
                        ->addDescription(__u('Notification read'))
321
                        ->addDetail(__u('Notification'), $id))
322
            );
323
324
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Notification read'));
325
        } catch (\Exception $e) {
326
            processException($e);
327
328
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
329
330
            return $this->returnJsonResponseException($e);
331
        }
332
    }
333
334
    /**
335
     * Saves create action
336
     */
337
    public function saveCreateAction()
338
    {
339
        try {
340
            $this->checkSecurityToken($this->previousSk, $this->request);
341
342
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_CREATE)) {
343
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
344
            }
345
346
            $form = new NotificationForm($this->dic);
347
            $form->validate(Acl::NOTIFICATION_CREATE);
348
349
            $this->notificationService->create($form->getItemData());
350
351
            $this->eventDispatcher->notifyEvent('create.notification',
352
                new Event($this,
353
                    EventMessage::factory()
354
                        ->addDescription(__u('Notification created')))
355
            );
356
357
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Notification created'));
358
        } catch (\Exception $e) {
359
            processException($e);
360
361
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
362
363
            return $this->returnJsonResponseException($e);
364
        }
365
    }
366
367
    /**
368
     * Saves edit action
369
     *
370
     * @param $id
371
     *
372
     * @return bool
373
     */
374
    public function saveEditAction($id)
375
    {
376
        try {
377
            $this->checkSecurityToken($this->previousSk, $this->request);
378
379
            if (!$this->acl->checkUserAccess(Acl::NOTIFICATION_EDIT)) {
380
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
381
            }
382
383
            $form = new NotificationForm($this->dic, $id);
384
            $form->validate(Acl::NOTIFICATION_EDIT);
385
386
            $this->notificationService->update($form->getItemData());
387
388
            $this->eventDispatcher->notifyEvent('edit.notification',
389
                new Event($this,
390
                    EventMessage::factory()
391
                        ->addDescription(__u('Notification updated')))
392
            );
393
394
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Notification updated'));
395
        } catch (\Exception $e) {
396
            processException($e);
397
398
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
399
400
            return $this->returnJsonResponseException($e);
401
        }
402
    }
403
404
    /**
405
     * @throws \DI\DependencyException
406
     * @throws \DI\NotFoundException
407
     * @throws \SP\Services\Auth\AuthException
408
     */
409
    protected function initialize()
410
    {
411
        $this->checkLoggedIn();
412
413
        $this->notificationService = $this->dic->get(NotificationService::class);
414
    }
415
}