Passed
Push — master ( 842f16...60595a )
by Rubén
12:20 queued 11s
created

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

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
28
use DI\DependencyException;
29
use DI\NotFoundException;
30
use Exception;
31
use SP\Core\Acl\Acl;
32
use SP\Core\Events\Event;
33
use SP\Core\Events\EventMessage;
34
use SP\Core\Exceptions\ConstraintException;
35
use SP\Core\Exceptions\QueryException;
36
use SP\Core\Exceptions\SessionTimeout;
37
use SP\Core\Exceptions\SPException;
38
use SP\Core\Exceptions\ValidationException;
39
use SP\DataModel\ClientData;
40
use SP\Http\JsonResponse;
41
use SP\Modules\Web\Controllers\Helpers\Grid\ClientGrid;
42
use SP\Modules\Web\Controllers\Traits\ItemTrait;
43
use SP\Modules\Web\Controllers\Traits\JsonTrait;
44
use SP\Modules\Web\Forms\ClientForm;
45
use SP\Mvc\Controller\CrudControllerInterface;
46
use SP\Repositories\NoSuchItemException;
47
use SP\Services\Auth\AuthException;
48
use SP\Services\Client\ClientService;
49
use SP\Services\ServiceException;
50
51
/**
52
 * Class ClientController
53
 *
54
 * @package SP\Modules\Web\Controllers
55
 */
56
final class ClientController extends ControllerBase implements CrudControllerInterface
57
{
58
    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\ClientController: $data, $key
Loading history...
59
60
    /**
61
     * @var ClientService
62
     */
63
    protected $clientService;
64
65
    /**
66
     * Search action
67
     *
68
     * @return bool
69
     * @throws DependencyException
70
     * @throws NotFoundException
71
     * @throws ConstraintException
72
     * @throws QueryException
73
     * @throws SPException
74
     */
75
    public function searchAction()
76
    {
77
        $this->checkSecurityToken($this->previousSk, $this->request);
78
79
        if (!$this->acl->checkUserAccess(Acl::CLIENT_SEARCH)) {
80
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
81
        }
82
83
        $this->view->addTemplate('datagrid-table', 'grid');
84
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
85
        $this->view->assign('data', $this->getSearchGrid());
86
87
        return $this->returnJsonResponseData(['html' => $this->render()]);
88
    }
89
90
    /**
91
     * getSearchGrid
92
     *
93
     * @return $this
94
     * @throws DependencyException
95
     * @throws NotFoundException
96
     * @throws ConstraintException
97
     * @throws QueryException
98
     */
99
    protected function getSearchGrid()
100
    {
101
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
102
103
        $clientGrid = $this->dic->get(ClientGrid::class);
104
105
        return $clientGrid->updatePager($clientGrid->getGrid($this->clientService->search($itemSearchData)), $itemSearchData);
106
    }
107
108
    /**
109
     * Create action
110
     */
111
    public function createAction()
112
    {
113
        try {
114
            $this->checkSecurityToken($this->previousSk, $this->request);
115
116
            if (!$this->acl->checkUserAccess(Acl::CLIENT_CREATE)) {
117
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
118
            }
119
120
            $this->view->assign('header', __('New Client'));
121
            $this->view->assign('isView', false);
122
            $this->view->assign('route', 'client/saveCreate');
123
124
            $this->setViewData();
125
126
            $this->eventDispatcher->notifyEvent('show.client.create', new Event($this));
127
128
            return $this->returnJsonResponseData(['html' => $this->render()]);
129
        } catch (Exception $e) {
130
            processException($e);
131
132
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
133
134
            return $this->returnJsonResponseException($e);
135
        }
136
    }
137
138
    /**
139
     * Sets view data for displaying client's data
140
     *
141
     * @param $clientId
142
     *
143
     * @throws ConstraintException
144
     * @throws QueryException
145
     * @throws ServiceException
146
     * @throws NoSuchItemException
147
     */
148
    protected function setViewData($clientId = null)
149
    {
150
        $this->view->addTemplate('client', 'itemshow');
151
152
        $client = $clientId ? $this->clientService->getById($clientId) : new ClientData();
153
154
        $this->view->assign('client', $client);
155
156
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ITEMS_MANAGE));
157
158
        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...
The condition $this->view->isView === true is always false.
Loading history...
159
            $this->view->assign('disabled', 'disabled');
160
            $this->view->assign('readonly', 'readonly');
161
        } else {
162
            $this->view->assign('disabled', false);
163
            $this->view->assign('readonly', false);
164
        }
165
166
        $this->view->assign('showViewCustomPass', $this->acl->checkUserAccess(Acl::CUSTOMFIELD_VIEW_PASS));
167
        $this->view->assign('customFields', $this->getCustomFieldsForItem(Acl::CLIENT, $clientId));
168
    }
169
170
    /**
171
     * Edit action
172
     *
173
     * @param $id
174
     *
175
     * @return bool
176
     */
177
    public function editAction($id)
178
    {
179
        try {
180
            $this->checkSecurityToken($this->previousSk, $this->request);
181
182
            if (!$this->acl->checkUserAccess(Acl::CLIENT_EDIT)) {
183
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
184
            }
185
186
            $this->view->assign('header', __('Edit Client'));
187
            $this->view->assign('isView', false);
188
            $this->view->assign('route', 'client/saveEdit/' . $id);
189
190
            $this->setViewData($id);
191
192
            $this->eventDispatcher->notifyEvent('show.client.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::CLIENT_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->clientService->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\Client\Clien...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

221
                $this->clientService->deleteByIdBatch(/** @scrutinizer ignore-type */ $this->getItemsIdFromRequest($this->request));
Loading history...
222
223
                $this->deleteCustomFieldsForItem(Acl::CLIENT, $id);
224
225
                $this->eventDispatcher->notifyEvent('delete.client.selection',
226
                    new Event($this, EventMessage::factory()
227
                        ->addDescription(__u('Clients deleted')))
228
                );
229
230
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Clients deleted'));
231
            }
232
            $this->clientService->delete($id);
233
234
            $this->deleteCustomFieldsForItem(Acl::CLIENT, $id);
235
236
            $this->eventDispatcher->notifyEvent('delete.client',
237
                new Event($this, EventMessage::factory()
238
                    ->addDescription(__u('Client deleted'))
239
                    ->addDetail(__u('Client'), $id))
240
            );
241
242
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Client deleted'));
243
        } catch (Exception $e) {
244
            processException($e);
245
246
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
247
248
            return $this->returnJsonResponseException($e);
249
        }
250
    }
251
252
    /**
253
     * Saves create action
254
     */
255
    public function saveCreateAction()
256
    {
257
        try {
258
            $this->checkSecurityToken($this->previousSk, $this->request);
259
260
            if (!$this->acl->checkUserAccess(Acl::CLIENT_CREATE)) {
261
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
262
            }
263
264
            $form = new ClientForm($this->dic);
265
            $form->validate(Acl::CLIENT_CREATE);
266
267
            $itemData = $form->getItemData();
268
269
            $id = $this->clientService->create($itemData);
270
271
            $this->eventDispatcher->notifyEvent('create.client',
272
                new Event($this,
273
                    EventMessage::factory()
274
                        ->addDescription(__u('Client added'))
275
                        ->addDetail(__u('Client'), $itemData->getName()))
276
            );
277
278
            $this->addCustomFieldsForItem(Acl::CLIENT, $id, $this->request);
279
280
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Client added'));
281
        } catch (ValidationException $e) {
282
            return $this->returnJsonResponseException($e);
283
        } catch (Exception $e) {
284
            processException($e);
285
286
            $this->eventDispatcher->notifyEvent('exception', new Event($e));
287
288
            return $this->returnJsonResponseException($e);
289
        }
290
    }
291
292
    /**
293
     * Saves edit action
294
     *
295
     * @param $id
296
     *
297
     * @return bool
298
     */
299
    public function saveEditAction($id)
300
    {
301
        try {
302
            $this->checkSecurityToken($this->previousSk, $this->request);
303
304
            if (!$this->acl->checkUserAccess(Acl::CLIENT_EDIT)) {
305
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
306
            }
307
308
            $form = new ClientForm($this->dic, $id);
309
            $form->validate(Acl::CLIENT_EDIT);
310
311
            $this->clientService->update($form->getItemData());
312
313
            $this->eventDispatcher->notifyEvent('edit.client',
314
                new Event($this,
315
                    EventMessage::factory()
316
                        ->addDescription(__u('Client updated'))
317
                        ->addDetail(__u('Client'), $id))
318
            );
319
320
            $this->updateCustomFieldsForItem(Acl::CLIENT, $id, $this->request);
321
322
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Client updated'));
323
        } catch (ValidationException $e) {
324
            return $this->returnJsonResponseException($e);
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
     * View action
336
     *
337
     * @param $id
338
     *
339
     * @return bool
340
     */
341
    public function viewAction($id)
342
    {
343
        try {
344
            $this->checkSecurityToken($this->previousSk, $this->request);
345
346
            if (!$this->acl->checkUserAccess(Acl::CLIENT_VIEW)) {
347
                return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('You don\'t have permission to do this operation'));
348
            }
349
350
            $this->view->assign('header', __('View Client'));
351
            $this->view->assign('isView', true);
352
353
            $this->setViewData($id);
354
355
            $this->eventDispatcher->notifyEvent('show.client', new Event($this));
356
357
            return $this->returnJsonResponseData(['html' => $this->render()]);
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
     * Initialize class
369
     *
370
     * @throws AuthException
371
     * @throws DependencyException
372
     * @throws NotFoundException
373
     * @throws SessionTimeout
374
     */
375
    protected function initialize()
376
    {
377
        $this->checkLoggedIn();
378
379
        $this->clientService = $this->dic->get(ClientService::class);
380
    }
381
}