Passed
Push — devel-3.0 ( af8b21...5a06ca )
by Rubén
03:22
created

AccountForm::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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\Forms;
26
27
use Psr\Container\ContainerInterface;
28
use SP\Core\Acl\ActionsInterface;
29
use SP\Core\Exceptions\ValidationException;
30
use SP\Services\Account\AccountPresetService;
31
use SP\Services\Account\AccountRequest;
32
33
/**
34
 * Class AccountForm
35
 *
36
 * @package SP\Account
37
 */
38
final class AccountForm extends FormBase implements FormInterface
39
{
40
    /**
41
     * @var AccountRequest
42
     */
43
    protected $accountRequest;
44
    /**
45
     * @var AccountPresetService
46
     */
47
    private $accountPresetService;
48
49
    /**
50
     * Validar el formulario
51
     *
52
     * @param $action
53
     *
54
     * @return AccountForm
55
     * @throws ValidationException
56
     * @throws \SP\Core\Exceptions\ConstraintException
57
     * @throws \SP\Core\Exceptions\NoSuchPropertyException
58
     * @throws \SP\Core\Exceptions\QueryException
59
     */
60
    public function validate($action)
61
    {
62
        switch ($action) {
63
            case ActionsInterface::ACCOUNT_EDIT_PASS:
64
                $this->analyzeRequestData();
65
                $this->checkPassword();
66
                $this->accountPresetService->checkPasswordPreset($this->accountRequest);
67
                break;
68
            case ActionsInterface::ACCOUNT_EDIT:
69
                $this->analyzeRequestData();
70
                $this->checkCommon();
71
                break;
72
            case ActionsInterface::ACCOUNT_CREATE:
73
            case ActionsInterface::ACCOUNT_COPY:
74
                $this->analyzeRequestData();
75
                $this->checkCommon();
76
                $this->checkPassword();
77
                $this->accountPresetService->checkPasswordPreset($this->accountRequest);
78
                break;
79
        }
80
81
        return $this;
82
    }
83
84
    /**
85
     * Analizar los datos de la petición HTTP
86
     *
87
     * @return void
88
     */
89
    protected function analyzeRequestData()
90
    {
91
        $this->accountRequest = new AccountRequest();
92
        $this->accountRequest->id = $this->itemId;
93
        $this->accountRequest->name = $this->request->analyzeString('name');
94
        $this->accountRequest->clientId = $this->request->analyzeInt('client_id', 0);
95
        $this->accountRequest->categoryId = $this->request->analyzeInt('category_id', 0);
96
        $this->accountRequest->login = $this->request->analyzeString('login');
97
        $this->accountRequest->url = $this->request->analyzeString('url');
98
        $this->accountRequest->notes = $this->request->analyzeString('notes');
99
        $this->accountRequest->userEditId = $this->context->getUserData()->getId();
100
        $this->accountRequest->otherUserEdit = (int)$this->request->analyzeBool('other_user_edit_enabled', false);
101
        $this->accountRequest->otherUserGroupEdit = (int)$this->request->analyzeBool('other_usergroup_edit_enabled', false);
102
        $this->accountRequest->pass = $this->request->analyzeEncrypted('password');
103
        $this->accountRequest->isPrivate = (int)$this->request->analyzeBool('private_enabled', false);
104
        $this->accountRequest->isPrivateGroup = (int)$this->request->analyzeBool('private_group_enabled', false);
105
        $this->accountRequest->passDateChange = $this->request->analyzeInt('password_date_expire_unix');
106
        $this->accountRequest->parentId = $this->request->analyzeInt('parent_account_id');
107
        $this->accountRequest->userGroupId = $this->request->analyzeInt('main_usergroup_id');
108
109
        // Arrays
110
        $accountOtherGroupsView = $this->request->analyzeArray('other_usergroups_view');
111
        $accountOtherGroupsEdit = $this->request->analyzeArray('other_usergroups_edit');
112
        $accountOtherUsersView = $this->request->analyzeArray('other_users_view');
113
        $accountOtherUsersEdit = $this->request->analyzeArray('other_users_edit');
114
        $accountTags = $this->request->analyzeArray('tags');
115
116
        $this->accountRequest->updateUserGroupPermissions = $this->request->analyzeInt('other_usergroups_view_update') === 1 || $this->request->analyzeInt('other_usergroups_edit_update') === 1;
117
        $this->accountRequest->updateUserPermissions = $this->request->analyzeInt('other_users_view_update') === 1 || $this->request->analyzeInt('other_users_edit_update') === 1;
118
        $this->accountRequest->updateTags = $this->request->analyzeInt('tags_update') === 1;
119
120
        if ($accountOtherUsersView) {
121
            $this->accountRequest->usersView = $accountOtherUsersView;
122
        }
123
        if ($accountOtherUsersEdit) {
124
            $this->accountRequest->usersEdit = $accountOtherUsersEdit;
125
        }
126
127
        if ($accountOtherGroupsView) {
128
            $this->accountRequest->userGroupsView = $accountOtherGroupsView;
129
        }
130
131
        if ($accountOtherGroupsEdit) {
132
            $this->accountRequest->userGroupsEdit = $accountOtherGroupsEdit;
133
        }
134
135
        if ($accountTags) {
136
            $this->accountRequest->tags = $accountTags;
137
        }
138
    }
139
140
    /**
141
     * @throws ValidationException
142
     */
143
    private function checkPassword()
144
    {
145
        if ($this->accountRequest->parentId > 0) {
146
            return;
147
        }
148
149
        if (!$this->accountRequest->pass) {
150
            throw new ValidationException(__u('Es necesaria una clave'));
151
        }
152
153
        if ($this->request->analyzeEncrypted('password_repeat') !== $this->accountRequest->pass) {
154
            throw new ValidationException(__u('Las claves no coinciden'));
155
        }
156
    }
157
158
    /**
159
     * @throws ValidationException
160
     */
161
    private function checkCommon()
162
    {
163
        if (!$this->accountRequest->name) {
164
            throw new ValidationException(__u('Es necesario un nombre de cuenta'));
165
        }
166
167
        if (!$this->accountRequest->clientId) {
168
            throw new ValidationException(__u('Es necesario un nombre de cliente'));
169
        }
170
171
        if (!$this->accountRequest->login) {
172
            throw new ValidationException(__u('Es necesario un usuario'));
173
        }
174
175
        if (!$this->accountRequest->categoryId) {
176
            throw new ValidationException(__u('Es necesario una categoría'));
177
        }
178
    }
179
180
    /**
181
     * @return AccountRequest
182
     */
183
    public function getItemData()
184
    {
185
        return $this->accountRequest;
186
    }
187
188
    /**
189
     * @param ContainerInterface $dic
190
     */
191
    protected function initialize($dic)
192
    {
193
        $this->accountPresetService = $dic->get(AccountPresetService::class);
194
    }
195
}