Passed
Push — devel-3.0 ( 5f7f30...cd1038 )
by Rubén
03:44
created

AccountDefaultPermissionController::setViewData()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 33
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 22
nc 4
nop 1
dl 0
loc 33
rs 9.568
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\Core\Exceptions\ValidationException;
31
use SP\DataModel\AccountDefaultPermissionData;
32
use SP\DataModel\AccountPermission;
33
use SP\Http\JsonResponse;
34
use SP\Modules\Web\Controllers\Helpers\Grid\AccountDefaultPermissionGrid;
35
use SP\Modules\Web\Controllers\Traits\ItemTrait;
36
use SP\Modules\Web\Controllers\Traits\JsonTrait;
37
use SP\Modules\Web\Forms\AccountDefaultPermissionForm;
38
use SP\Mvc\Controller\CrudControllerInterface;
39
use SP\Mvc\View\Components\SelectItemAdapter;
40
use SP\Services\Account\AccountDefaultPermissionService;
41
use SP\Services\User\UserService;
42
use SP\Services\UserGroup\UserGroupService;
43
44
/**
45
 * Class AccountDefaultPermissionController
46
 *
47
 * @package SP\Modules\Web\Controllers
48
 */
49
class AccountDefaultPermissionController extends ControllerBase implements CrudControllerInterface
50
{
51
    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\Controlle...ultPermissionController: $data, $key
Loading history...
52
53
    /**
54
     * @var AccountDefaultPermissionService
55
     */
56
    protected $accountDefaultPermissionService;
57
58
    /**
59
     * View action
60
     *
61
     * @param $id
62
     *
63
     * @return bool
64
     */
65
    public function viewAction($id)
66
    {
67
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_VIEW)) {
68
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
69
        }
70
71
        $this->view->assign('header', __('Ver Permiso'));
72
        $this->view->assign('isView', true);
73
74
        try {
75
            $this->setViewData($id);
76
77
            $this->eventDispatcher->notifyEvent('show.accountDefaultPermission', new Event($this));
78
79
            return $this->returnJsonResponseData(['html' => $this->render()]);
80
        } catch (\Exception $e) {
81
            processException($e);
82
83
            return $this->returnJsonResponseException($e);
84
        }
85
    }
86
87
    /**
88
     * Sets view data for displaying permissions' data
89
     *
90
     * @param $permissionId
91
     *
92
     * @throws \SP\Core\Exceptions\ConstraintException
93
     * @throws \SP\Core\Exceptions\QueryException
94
     * @throws \SP\Repositories\NoSuchItemException
95
     */
96
    protected function setViewData($permissionId = null)
97
    {
98
        $this->view->addTemplate('account_default_permission', 'itemshow');
99
100
        $accountDefaultPermissionData = $permissionId ? $this->accountDefaultPermissionService->getById($permissionId) : new AccountDefaultPermissionData();
101
        $accountPermission = $accountDefaultPermissionData->getAccountPermission() ?: new AccountPermission();
102
103
        $this->view->assign('permission', $accountDefaultPermissionData);
104
105
        $users = SelectItemAdapter::factory(UserService::getItemsBasic());
106
107
        $this->view->assign('users', $users->getItemsFromModelSelected([$accountDefaultPermissionData->getUserId()]));
108
        $this->view->assign('usersView', $users->getItemsFromModelSelected($accountPermission->getUsersView()));
109
        $this->view->assign('usersEdit', $users->getItemsFromModelSelected($accountPermission->getUsersEdit()));
110
111
        $userGroups = SelectItemAdapter::factory(UserGroupService::getItemsBasic());
112
113
        $this->view->assign('userGroups', $userGroups->getItemsFromModelSelected([$accountDefaultPermissionData->getUserGroupId()]));
114
        $this->view->assign('userGroupsView', $userGroups->getItemsFromModelSelected($accountPermission->getUserGroupsView()));
115
        $this->view->assign('userGroupsEdit', $userGroups->getItemsFromModelSelected($accountPermission->getUserGroupsEdit()));
116
117
        $this->view->assign('userProfiles', SelectItemAdapter::factory(UserGroupService::getItemsBasic())
118
            ->getItemsFromModelSelected([$accountDefaultPermissionData->getUserProfileId()]));
119
120
        $this->view->assign('sk', $this->session->generateSecurityKey());
121
        $this->view->assign('nextAction', Acl::getActionRoute(Acl::ACCESS_MANAGE));
122
123
        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...
124
            $this->view->assign('disabled', 'disabled');
125
            $this->view->assign('readonly', 'readonly');
126
        } else {
127
            $this->view->assign('disabled');
128
            $this->view->assign('readonly');
129
        }
130
    }
131
132
    /**
133
     * Search action
134
     *
135
     * @return bool
136
     * @throws \SP\Core\Exceptions\ConstraintException
137
     * @throws \SP\Core\Exceptions\QueryException
138
     */
139
    public function searchAction()
140
    {
141
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_SEARCH)) {
142
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
143
        }
144
145
        $this->view->addTemplate('datagrid-table', 'grid');
146
        $this->view->assign('index', $this->request->analyzeInt('activetab', 0));
147
        $this->view->assign('data', $this->getSearchGrid());
148
149
        return $this->returnJsonResponseData(['html' => $this->render()]);
150
    }
151
152
    /**
153
     * getSearchGrid
154
     *
155
     * @return $this
156
     * @throws \SP\Core\Exceptions\ConstraintException
157
     * @throws \SP\Core\Exceptions\QueryException
158
     */
159
    protected function getSearchGrid()
160
    {
161
        $itemSearchData = $this->getSearchData($this->configData->getAccountCount(), $this->request);
162
163
        $grid = $this->dic->get(AccountDefaultPermissionGrid::class);
164
165
        return $grid->updatePager(
166
            $grid->getGrid($this->accountDefaultPermissionService->search($itemSearchData)),
167
            $itemSearchData
168
        );
169
    }
170
171
    /**
172
     * Create action
173
     */
174
    public function createAction()
175
    {
176
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_CREATE)) {
177
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
178
        }
179
180
        $this->view->assign(__FUNCTION__, 1);
181
        $this->view->assign('header', __('Nuevo Permiso'));
182
        $this->view->assign('isView', false);
183
        $this->view->assign('route', 'accountDefaultPermission/saveCreate');
184
185
        try {
186
            $this->setViewData();
187
188
            $this->eventDispatcher->notifyEvent('show.accountDefaultPermission.create', new Event($this));
189
190
            return $this->returnJsonResponseData(['html' => $this->render()]);
191
        } catch (\Exception $e) {
192
            processException($e);
193
194
            return $this->returnJsonResponseException($e);
195
        }
196
    }
197
198
    /**
199
     * Edit action
200
     *
201
     * @param $id
202
     *
203
     * @return bool
204
     */
205
    public function editAction($id)
206
    {
207
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_EDIT)) {
208
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
209
        }
210
211
        $this->view->assign('header', __('Editar Permiso'));
212
        $this->view->assign('isView', false);
213
        $this->view->assign('route', 'accountDefaultPermission/saveEdit/' . $id);
214
215
        try {
216
            $this->setViewData($id);
217
218
            $this->eventDispatcher->notifyEvent('show.accountDefaultPermission.edit', new Event($this));
219
220
            return $this->returnJsonResponseData(['html' => $this->render()]);
221
        } catch (\Exception $e) {
222
            processException($e);
223
224
            return $this->returnJsonResponseException($e);
225
        }
226
    }
227
228
    /**
229
     * Delete action
230
     *
231
     * @param $id
232
     *
233
     * @return bool
234
     */
235
    public function deleteAction($id = null)
236
    {
237
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_DELETE)) {
238
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
239
        }
240
241
        try {
242
            if ($id === null) {
243
                $this->accountDefaultPermissionService->deleteByIdBatch($this->getItemsIdFromRequest($this->request));
244
245
                $this->eventDispatcher->notifyEvent('delete.accountDefaultPermission',
246
                    new Event($this,
247
                        EventMessage::factory()
248
                            ->addDescription(__u('Permisos eliminados')))
249
                );
250
251
                return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Permisos eliminados'));
252
            }
253
254
            $this->accountDefaultPermissionService->delete($id);
255
256
            $this->eventDispatcher->notifyEvent('delete.accountDefaultPermission',
257
                new Event($this,
258
                    EventMessage::factory()
259
                        ->addDescription(__u('Permiso eliminado'))
260
                        ->addDetail(__u('ID'), $id))
261
            );
262
263
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Permiso eliminado'));
264
        } catch (\Exception $e) {
265
            processException($e);
266
267
            return $this->returnJsonResponseException($e);
268
        }
269
    }
270
271
    /**
272
     * Saves create action
273
     */
274
    public function saveCreateAction()
275
    {
276
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_CREATE)) {
277
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
278
        }
279
280
        try {
281
            $form = new AccountDefaultPermissionForm($this->dic);
282
            $form->validate(Acl::ACCOUNT_DEFAULT_PERMISSION_CREATE);
283
284
            $id = $this->accountDefaultPermissionService->create($form->getItemData());
285
286
            $this->eventDispatcher->notifyEvent('create.accountDefaultPermission',
287
                new Event($this,
288
                    EventMessage::factory()
289
                        ->addDescription(__u('Permiso creado'))
290
                        ->addDetail(__u('ID'), $id))
291
            );
292
293
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Permiso creado'));
294
        } catch (ValidationException $e) {
295
            return $this->returnJsonResponseException($e);
296
        } catch (\Exception $e) {
297
            processException($e);
298
299
            return $this->returnJsonResponseException($e);
300
        }
301
    }
302
303
    /**
304
     * Saves edit action
305
     *
306
     * @param $id
307
     *
308
     * @return bool
309
     */
310
    public function saveEditAction($id)
311
    {
312
        if (!$this->acl->checkUserAccess(Acl::ACCOUNT_DEFAULT_PERMISSION_EDIT)) {
313
            return $this->returnJsonResponse(JsonResponse::JSON_ERROR, __u('No tiene permisos para realizar esta operación'));
314
        }
315
316
        try {
317
            $form = new AccountDefaultPermissionForm($this->dic, $id);
318
            $form->validate(Acl::ACCOUNT_DEFAULT_PERMISSION_EDIT);
319
320
            $this->accountDefaultPermissionService->update($form->getItemData());
321
322
            $this->eventDispatcher->notifyEvent('edit.accountDefaultPermission',
323
                new Event($this,
324
                    EventMessage::factory()
325
                        ->addDescription(__u('Permiso actualizado'))
326
                        ->addDetail(__u('ID'), $id))
327
            );
328
329
            return $this->returnJsonResponse(JsonResponse::JSON_SUCCESS, __u('Permiso actualizado'));
330
        } catch (ValidationException $e) {
331
            return $this->returnJsonResponseException($e);
332
        } catch (\Exception $e) {
333
            processException($e);
334
335
            return $this->returnJsonResponseException($e);
336
        }
337
    }
338
339
    /**
340
     * Initialize class
341
     *
342
     * @throws \Psr\Container\ContainerExceptionInterface
343
     * @throws \Psr\Container\NotFoundExceptionInterface
344
     * @throws \SP\Services\Auth\AuthException
345
     */
346
    protected function initialize()
347
    {
348
        $this->checkLoggedIn();
349
350
        $this->accountDefaultPermissionService = $this->dic->get(AccountDefaultPermissionService::class);
351
    }
352
}