Passed
Push — devel-3.0 ( 2fc71e...3e43d6 )
by Rubén
03:39
created

AccountHelper::setViewForBlank()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 41
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 27
nc 1
nop 1
dl 0
loc 41
rs 8.5546
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\Helpers\Account;
26
27
use SP\Core\Acl\AccountPermissionException;
28
use SP\Core\Acl\Acl;
29
use SP\Core\Acl\ActionsInterface;
30
use SP\Core\Acl\UnauthorizedPageException;
31
use SP\Core\Exceptions\SPException;
32
use SP\DataModel\AccountPermission;
33
use SP\DataModel\AccountPrivate;
34
use SP\DataModel\Dto\AccountAclDto;
35
use SP\DataModel\Dto\AccountDetailsResponse;
36
use SP\Http\Uri;
37
use SP\Modules\Web\Controllers\Helpers\HelperBase;
38
use SP\Modules\Web\Controllers\Traits\ItemTrait;
39
use SP\Mvc\View\Components\SelectItemAdapter;
40
use SP\Repositories\NoSuchItemException;
41
use SP\Services\Account\AccountAcl;
42
use SP\Services\Account\AccountAclService;
43
use SP\Services\Account\AccountHistoryService;
44
use SP\Services\Account\AccountService;
45
use SP\Services\Category\CategoryService;
46
use SP\Services\Client\ClientService;
47
use SP\Services\Crypt\MasterPassService;
48
use SP\Services\ItemPreset\ItemPresetInterface;
49
use SP\Services\ItemPreset\ItemPresetService;
50
use SP\Services\PublicLink\PublicLinkService;
51
use SP\Services\Tag\TagService;
52
use SP\Services\User\UpdatedMasterPassException;
53
use SP\Services\User\UserService;
54
use SP\Services\UserGroup\UserGroupService;
55
56
/**
57
 * Class AccountHelper
58
 *
59
 * @package SP\Modules\Web\Controllers\Helpers
60
 */
61
final class AccountHelper extends HelperBase
62
{
63
    use 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...s\Account\AccountHelper: $data, $key
Loading history...
64
65
    /**
66
     * @var  Acl
67
     */
68
    private $acl;
69
    /**
70
     * @var AccountService
71
     */
72
    private $accountService;
73
    /**
74
     * @var AccountHistoryService
75
     */
76
    private $accountHistoryService;
77
    /**
78
     * @var PublicLinkService
79
     */
80
    private $publicLinkService;
81
    /**
82
     * @var ItemPresetService
83
     */
84
    private $itemPresetService;
85
    /**
86
     * @var string
87
     */
88
    private $actionId;
89
    /**
90
     * @var AccountAcl
91
     */
92
    private $accountAcl;
93
    /**
94
     * @var int con el Id de la cuenta
95
     */
96
    private $accountId;
97
    /**
98
     * @var bool
99
     */
100
    private $isView = false;
101
102
    /**
103
     * Sets account's view variables
104
     *
105
     * @param AccountDetailsResponse $accountDetailsResponse
106
     * @param int                    $actionId
107
     *
108
     * @throws AccountPermissionException
109
     * @throws SPException
110
     * @throws UnauthorizedPageException
111
     * @throws UpdatedMasterPassException
112
     * @throws \Psr\Container\ContainerExceptionInterface
113
     * @throws \Psr\Container\NotFoundExceptionInterface
114
     */
115
    public function setViewForAccount(AccountDetailsResponse $accountDetailsResponse, $actionId)
116
    {
117
        $this->accountId = $accountDetailsResponse->getAccountVData()->getId();
118
        $this->actionId = $actionId;
119
120
        $this->checkActionAccess();
121
        $this->accountAcl = $this->checkAccess($accountDetailsResponse);
122
123
        $accountData = $accountDetailsResponse->getAccountVData();
124
125
        $accountActionsDto = new AccountActionsDto($this->accountId, null, $accountData->getParentId());
126
127
        $selectUsers = SelectItemAdapter::factory(UserService::getItemsBasic());
128
        $selectUserGroups = SelectItemAdapter::factory(UserGroupService::getItemsBasic());
129
        $selectTags = SelectItemAdapter::factory(TagService::getItemsBasic());
130
131
        $usersView = SelectItemAdapter::getIdFromArrayOfObjects(
132
            array_filter($accountDetailsResponse->getUsers(), function ($value) {
133
                return (int)$value->isEdit === 0;
134
            }));
135
136
        $usersEdit = SelectItemAdapter::getIdFromArrayOfObjects(
137
            array_filter($accountDetailsResponse->getUsers(), function ($value) {
138
                return (int)$value->isEdit === 1;
139
            }));
140
141
        $userGroupsView = SelectItemAdapter::getIdFromArrayOfObjects(
142
            array_filter($accountDetailsResponse->getUserGroups(), function ($value) {
143
                return (int)$value->isEdit === 0;
144
            }));
145
146
        $userGroupsEdit = SelectItemAdapter::getIdFromArrayOfObjects(
147
            array_filter($accountDetailsResponse->getUserGroups(), function ($value) {
148
                return (int)$value->isEdit === 1;
149
            }));
150
151
        $this->view->assign('otherUsersView', $selectUsers->getItemsFromModelSelected($usersView, $accountData->getUserId()));
152
        $this->view->assign('otherUsersEdit', $selectUsers->getItemsFromModelSelected($usersEdit, $accountData->getUserId()));
153
        $this->view->assign('otherUserGroupsView', $selectUserGroups->getItemsFromModelSelected($userGroupsView, $accountData->getUserGroupId()));
154
        $this->view->assign('otherUserGroupsEdit', $selectUserGroups->getItemsFromModelSelected($userGroupsEdit, $accountData->getUserGroupId()));
155
156
        $this->view->assign('users', $selectUsers->getItemsFromModelSelected([$accountData->getUserId()]));
157
        $this->view->assign('userGroups', $selectUserGroups->getItemsFromModelSelected([$accountData->getUserGroupId()]));
158
159
        $this->view->assign('tags', $selectTags->getItemsFromModelSelected(SelectItemAdapter::getIdFromArrayOfObjects($accountDetailsResponse->getTags())));
160
161
        $this->view->assign('historyData', $this->accountHistoryService->getHistoryForAccount($this->accountId));
162
163
        $this->view->assign('isModified', strtotime($accountData->getDateEdit()) !== false);
164
        $this->view->assign('maxFileSize', round($this->configData->getFilesAllowedSize() / 1024, 1));
165
        $this->view->assign('filesAllowedExts', implode(',', $this->configData->getFilesAllowedExts()));
166
167
        if ($this->configData->isPublinksEnabled() && $this->accountAcl->isShowLink()) {
168
            try {
169
                $publicLinkData = $this->publicLinkService->getHashForItem($this->accountId);
170
                $accountActionsDto->setPublicLinkId($publicLinkData->getId());
0 ignored issues
show
Bug introduced by
$publicLinkData->getId() of type integer is incompatible with the type boolean expected by parameter $publicLinkId of SP\Modules\Web\Controlle...sDto::setPublicLinkId(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

170
                $accountActionsDto->setPublicLinkId(/** @scrutinizer ignore-type */ $publicLinkData->getId());
Loading history...
171
172
                $this->view->assign('publicLinkUrl', PublicLinkService::getLinkForHash($publicLinkData->getHash()));
173
                $this->view->assign('publicLinkId', $publicLinkData->getId());
174
            } catch (NoSuchItemException $e) {
175
                $this->view->assign('publicLinkId', 0);
176
                $this->view->assign('publicLinkUrl', null);
177
            }
178
179
            $this->view->assign('publicLinkShow', true);
180
        } else {
181
            $this->view->assign('publicLinkShow', false);
182
        }
183
184
        $userData = $this->context->getUserData();
185
        $userProfileData = $this->context->getUserProfile();
186
187
        $this->view->assign('allowPrivate', ($userProfileData->isAccPrivate() && $accountData->getUserId() === $userData->getId()) || $userData->getIsAdminApp());
188
        $this->view->assign('allowPrivateGroup', ($userProfileData->isAccPrivateGroup() && $accountData->getUserGroupId() === $userData->getUserGroupId()) || $userData->getIsAdminApp());
189
190
        $this->view->assign('accountPassDate', date('Y-m-d H:i:s', $accountData->getPassDate()));
191
        $this->view->assign('accountPassDateChange', $accountData->getPassDateChange() > 0 && date('Y-m-d', $accountData->getPassDateChange() ?: 0));
192
        $this->view->assign('linkedAccounts', $this->accountService->getLinked($this->accountId));
193
194
        $this->view->assign('accountId', $accountData->getId());
195
        $this->view->assign('accountData', $accountData);
196
        $this->view->assign('gotData', true);
197
198
        $accountActionsHelper = $this->dic->get(AccountActionsHelper::class);
199
200
        $this->view->assign('accountActions', $accountActionsHelper->getActionsForAccount($this->accountAcl, $accountActionsDto));
201
        $this->view->assign('accountActionsMenu', $accountActionsHelper->getActionsGrouppedForAccount($this->accountAcl, $accountActionsDto));
202
203
        $this->setViewCommon();
204
    }
205
206
    /**
207
     * @throws NoSuchItemException
208
     * @throws UnauthorizedPageException
209
     * @throws UpdatedMasterPassException
210
     * @throws \SP\Services\ServiceException
211
     */
212
    public function checkActionAccess()
213
    {
214
        if (!$this->acl->checkUserAccess($this->actionId)) {
0 ignored issues
show
Bug introduced by
$this->actionId of type string is incompatible with the type integer expected by parameter $action of SP\Core\Acl\Acl::checkUserAccess(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
        if (!$this->acl->checkUserAccess(/** @scrutinizer ignore-type */ $this->actionId)) {
Loading history...
215
            throw new UnauthorizedPageException(UnauthorizedPageException::INFO);
216
        }
217
218
        if (!$this->dic->get(MasterPassService::class)
219
            ->checkUserUpdateMPass($this->context->getUserData()->getLastUpdateMPass())) {
220
            throw new UpdatedMasterPassException(UpdatedMasterPassException::INFO);
221
        }
222
    }
223
224
    /**
225
     * Comprobar si el usuario dispone de acceso al módulo
226
     *
227
     * @param AccountDetailsResponse $accountDetailsResponse
228
     *
229
     * @return AccountAcl
230
     * @throws AccountPermissionException
231
     * @throws \SP\Core\Exceptions\ConstraintException
232
     * @throws \SP\Core\Exceptions\QueryException
233
     */
234
    protected function checkAccess(AccountDetailsResponse $accountDetailsResponse)
235
    {
236
        $accountAcl = $this->dic->get(AccountAclService::class)->getAcl($this->actionId, AccountAclDto::makeFromAccount($accountDetailsResponse));
237
238
        if ($accountAcl === null || $accountAcl->checkAccountAccess($this->actionId) === false) {
239
            throw new AccountPermissionException(AccountPermissionException::INFO);
240
        }
241
242
        return $accountAcl;
243
    }
244
245
    /**
246
     * Sets account's view common data
247
     *
248
     * @throws \SP\Core\Exceptions\ConstraintException
249
     * @throws \SP\Core\Exceptions\QueryException
250
     * @throws \SP\Services\ServiceException
251
     */
252
    protected function setViewCommon()
253
    {
254
        $this->view->assign('actionId', $this->actionId);
255
        $this->view->assign('isView', $this->isView);
256
257
        $this->view->assign('accountIsHistory', false);
258
259
        $this->view->assign('customFields', $this->getCustomFieldsForItem(ActionsInterface::ACCOUNT, $this->accountId));
260
        $this->view->assign('categories', SelectItemAdapter::factory($this->dic->get(CategoryService::class)->getAllBasic())->getItemsFromModel());
261
        $this->view->assign('clients', SelectItemAdapter::factory($this->dic->get(ClientService::class)->getAllForUser())->getItemsFromModel());
262
263
        $this->view->assign('mailRequestEnabled', $this->configData->isMailRequestsEnabled());
264
        $this->view->assign('passToImageEnabled', $this->configData->isAccountPassToImage());
265
266
        $this->view->assign('otherAccounts', $this->accountService->getForUser($this->accountId));
267
268
        $this->view->assign('addClientEnabled', !$this->isView && $this->acl->checkUserAccess(ActionsInterface::CLIENT));
269
        $this->view->assign('addClientRoute', Acl::getActionRoute(ActionsInterface::CLIENT_CREATE));
270
271
        $this->view->assign('addCategoryEnabled', !$this->isView && $this->acl->checkUserAccess(ActionsInterface::CATEGORY));
272
        $this->view->assign('addCategoryRoute', Acl::getActionRoute(ActionsInterface::CATEGORY_CREATE));
273
274
        $this->view->assign('fileListRoute', Acl::getActionRoute(ActionsInterface::ACCOUNT_FILE_LIST));
275
        $this->view->assign('fileUploadRoute', Acl::getActionRoute(ActionsInterface::ACCOUNT_FILE_UPLOAD));
276
277
        $this->view->assign('disabled', $this->isView ? 'disabled' : '');
278
        $this->view->assign('readonly', $this->isView ? 'readonly' : '');
279
280
        $this->view->assign('showViewCustomPass', $this->accountAcl->isShowViewPass());
281
        $this->view->assign('accountAcl', $this->accountAcl);
282
283
        $this->view->assign('deepLink', $this->getDeepLink());
284
    }
285
286
    /**
287
     * @return string
288
     */
289
    private function getDeepLink()
290
    {
291
        $route = Acl::getActionRoute($this->actionId) . ($this->accountId ? '/' . $this->accountId : '');
292
293
        $uri = new Uri('index.php');
294
        $uri->addParam('r', $route);
295
296
        return $uri->getUriSigned($this->configData->getPasswordSalt());
297
    }
298
299
    /**
300
     * Sets account's view for a blank form
301
     *
302
     * @param $actionId
303
     *
304
     * @return void
305
     * @throws NoSuchItemException
306
     * @throws UnauthorizedPageException
307
     * @throws UpdatedMasterPassException
308
     * @throws \SP\Core\Exceptions\ConstraintException
309
     * @throws \SP\Core\Exceptions\QueryException
310
     * @throws \SP\Services\ServiceException
311
     * @throws \SP\Core\Exceptions\NoSuchPropertyException
312
     */
313
    public function setViewForBlank($actionId)
314
    {
315
        $this->actionId = $actionId;
316
        $this->accountAcl = new AccountAcl($actionId);
317
318
        $this->checkActionAccess();
319
320
        $userProfileData = $this->context->getUserProfile();
321
        $userData = $this->context->getUserData();
322
323
        $this->accountAcl->setShowPermission($userData->getIsAdminApp() || $userData->getIsAdminAcc() || $userProfileData->isAccPermission());
324
325
        $accountPrivate = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE)
326
            ->hydrate(AccountPrivate::class) ?: new AccountPrivate();
327
328
        $accountPermission = $this->itemPresetService->getForCurrentUser(ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION)
329
            ->hydrate(AccountPermission::class) ?: new AccountPermission();
330
331
        $selectUsers = SelectItemAdapter::factory(UserService::getItemsBasic());
332
        $selectUserGroups = SelectItemAdapter::factory(UserGroupService::getItemsBasic());
333
        $selectTags = SelectItemAdapter::factory(TagService::getItemsBasic());
334
335
        $this->view->assign('accountPassDateChange', date('Y-m-d', time() + 7776000));
336
        $this->view->assign('otherUsersView', $selectUsers->getItemsFromModelSelected($accountPermission->getUsersView()));
337
        $this->view->assign('otherUsersEdit', $selectUsers->getItemsFromModelSelected($accountPermission->getUsersEdit()));
338
        $this->view->assign('otherUserGroupsView', $selectUserGroups->getItemsFromModelSelected($accountPermission->getUserGroupsView()));
339
        $this->view->assign('otherUserGroupsEdit', $selectUserGroups->getItemsFromModelSelected($accountPermission->getUserGroupsEdit()));
340
341
        $this->view->assign('userGroups', $selectUserGroups->getItemsFromModel());
342
        $this->view->assign('tags', $selectTags->getItemsFromModel());
343
344
        $this->view->assign('allowPrivate', $userProfileData->isAccPrivate() || $userData->getIsAdminApp());
345
        $this->view->assign('allowPrivateGroup', $userProfileData->isAccPrivateGroup() || $userData->getIsAdminApp());
346
        $this->view->assign('accountPrivate', $accountPrivate);
347
348
        $this->view->assign('accountId', 0);
349
        $this->view->assign('gotData', false);
350
351
        $this->view->assign('accountActions', $this->dic->get(AccountActionsHelper::class)->getActionsForAccount($this->accountAcl, new AccountActionsDto($this->accountId)));
352
353
        $this->setViewCommon();
354
    }
355
356
    /**
357
     * Sets account's view variables
358
     *
359
     * @param AccountDetailsResponse $accountDetailsResponse
360
     * @param int                    $actionId
361
     *
362
     * @return bool
363
     * @throws NoSuchItemException
364
     * @throws UnauthorizedPageException
365
     * @throws UpdatedMasterPassException
366
     * @throws \SP\Services\ServiceException
367
     */
368
    public function setViewForRequest(AccountDetailsResponse $accountDetailsResponse, $actionId)
369
    {
370
        $this->accountId = $accountDetailsResponse->getAccountVData()->getId();
371
        $this->actionId = $actionId;
372
        $this->accountAcl = new AccountAcl($actionId);
373
374
        $this->checkActionAccess();
375
376
        $accountData = $accountDetailsResponse->getAccountVData();
377
378
        $this->view->assign('accountId', $accountData->getId());
379
        $this->view->assign('accountData', $accountDetailsResponse->getAccountVData());
380
381
        $this->view->assign('accountActions', $this->dic->get(AccountActionsHelper::class)->getActionsForAccount($this->accountAcl, new AccountActionsDto($this->accountId, null, $accountData->getParentId())));
382
383
        return true;
384
    }
385
386
    /**
387
     * @param bool $isView
388
     */
389
    public function setIsView($isView)
390
    {
391
        $this->isView = (bool)$isView;
392
    }
393
394
    /**
395
     * @throws \Psr\Container\ContainerExceptionInterface
396
     * @throws \Psr\Container\NotFoundExceptionInterface
397
     */
398
    protected function initialize()
399
    {
400
        $this->acl = $this->dic->get(Acl::class);
401
        $this->accountService = $this->dic->get(AccountService::class);
402
        $this->accountHistoryService = $this->dic->get(AccountHistoryService::class);
403
        $this->publicLinkService = $this->dic->get(PublicLinkService::class);
404
        $this->itemPresetService = $this->dic->get(ItemPresetService::class);
405
406
        $this->view->assign('changesHash');
407
        $this->view->assign('chkUserEdit');
408
        $this->view->assign('chkGroupEdit');
409
        $this->view->assign('sk', $this->context->generateSecurityKey());
410
    }
411
}