Passed
Push — devel-3.0 ( 543c48...5b8639 )
by Rubén
03:28
created

AccountForm::validate()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 25
nc 6
nop 1
dl 0
loc 29
rs 8.8977
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->analyzeItems();
71
                $this->checkCommon();
72
                break;
73
            case ActionsInterface::ACCOUNT_CREATE:
74
            case ActionsInterface::ACCOUNT_COPY:
75
                $this->analyzeRequestData();
76
                $this->analyzeItems();
77
                $this->checkCommon();
78
                $this->checkPassword();
79
                $this->accountPresetService->checkPasswordPreset($this->accountRequest);
80
                break;
81
            case ActionsInterface::ACCOUNTMGR_BULK_EDIT:
82
                $this->analyzeRequestData();
83
                $this->analyzeItems();
84
                $this->analyzeBulkEdit();
85
                break;
86
        }
87
88
        return $this;
89
    }
90
91
    /**
92
     * Analizar los datos de la petición HTTP
93
     *
94
     * @return void
95
     */
96
    protected function analyzeRequestData()
97
    {
98
        $this->accountRequest->id = $this->itemId;
99
        $this->accountRequest->name = $this->request->analyzeString('name');
100
        $this->accountRequest->clientId = $this->request->analyzeInt('client_id');
101
        $this->accountRequest->categoryId = $this->request->analyzeInt('category_id');
102
        $this->accountRequest->login = $this->request->analyzeString('login');
103
        $this->accountRequest->url = $this->request->analyzeString('url');
104
        $this->accountRequest->notes = $this->request->analyzeString('notes');
105
        $this->accountRequest->userEditId = $this->context->getUserData()->getId();
106
        $this->accountRequest->otherUserEdit = (int)$this->request->analyzeBool('other_user_edit_enabled', false);
107
        $this->accountRequest->otherUserGroupEdit = (int)$this->request->analyzeBool('other_usergroup_edit_enabled', false);
108
        $this->accountRequest->pass = $this->request->analyzeEncrypted('password');
109
        $this->accountRequest->isPrivate = (int)$this->request->analyzeBool('private_enabled', false);
110
        $this->accountRequest->isPrivateGroup = (int)$this->request->analyzeBool('private_group_enabled', false);
111
112
        if ($this->request->analyzeInt('password_date_expire')) {
113
            $this->accountRequest->passDateChange = $this->request->analyzeInt('password_date_expire_unix');
114
        }
115
116
        $this->accountRequest->parentId = $this->request->analyzeInt('parent_account_id');
117
        $this->accountRequest->userId = $this->request->analyzeInt('owner_id');
118
        $this->accountRequest->userGroupId = $this->request->analyzeInt('main_usergroup_id');
119
    }
120
121
    /**
122
     * @throws ValidationException
123
     */
124
    private function checkPassword()
125
    {
126
        if ($this->accountRequest->parentId > 0) {
127
            return;
128
        }
129
130
        if (!$this->accountRequest->pass) {
131
            throw new ValidationException(__u('Es necesaria una clave'));
132
        }
133
134
        if ($this->request->analyzeEncrypted('password_repeat') !== $this->accountRequest->pass) {
135
            throw new ValidationException(__u('Las claves no coinciden'));
136
        }
137
    }
138
139
    /**
140
     * analyzeItems
141
     */
142
    private function analyzeItems()
143
    {
144
        if ($this->request->analyzeInt('other_users_view_update') === 1) {
145
            $this->accountRequest->usersView = $this->request->analyzeArray('other_users_view', null, []);
146
        }
147
148
        if ($this->request->analyzeInt('other_users_edit_update') === 1) {
149
            $this->accountRequest->usersEdit = $this->request->analyzeArray('other_users_edit', null, []);
150
        }
151
152
        if ($this->request->analyzeInt('other_usergroups_view_update') === 1) {
153
            $this->accountRequest->userGroupsView = $this->request->analyzeArray('other_usergroups_view', null, []);
154
        }
155
156
        if ($this->request->analyzeInt('other_usergroups_edit_update') === 1) {
157
            $this->accountRequest->userGroupsEdit = $this->request->analyzeArray('other_usergroups_edit', null, []);
158
        }
159
160
        if ($this->request->analyzeInt('tags_update') === 1) {
161
            $this->accountRequest->tags = $this->request->analyzeArray('tags', null, []);
162
        }
163
    }
164
165
    /**
166
     * @throws ValidationException
167
     */
168
    private function checkCommon()
169
    {
170
        if (!$this->accountRequest->name) {
171
            throw new ValidationException(__u('Es necesario un nombre de cuenta'));
172
        }
173
174
        if (!$this->accountRequest->clientId) {
175
            throw new ValidationException(__u('Es necesario un nombre de cliente'));
176
        }
177
178
        if (!$this->accountRequest->categoryId) {
179
            throw new ValidationException(__u('Es necesario una categoría'));
180
        }
181
    }
182
183
    /**
184
     * analyzeBulkEdit
185
     */
186
    private function analyzeBulkEdit()
187
    {
188
        if ($this->request->analyzeBool('clear_permission_users_view', false)) {
189
            $this->accountRequest->usersView = [];
190
        }
191
192
        if ($this->request->analyzeBool('clear_permission_users_edit', false)) {
193
            $this->accountRequest->usersEdit = [];
194
        }
195
196
        if ($this->request->analyzeBool('clear_permission_usergroups_view', false)) {
197
            $this->accountRequest->userGroupsView = [];
198
        }
199
200
        if ($this->request->analyzeBool('clear_permission_usergroups_edit', false)) {
201
            $this->accountRequest->userGroupsEdit = [];
202
        }
203
    }
204
205
    /**
206
     * @return AccountRequest
207
     */
208
    public function getItemData()
209
    {
210
        return $this->accountRequest;
211
    }
212
213
    /**
214
     * @param ContainerInterface $dic
215
     */
216
    protected function initialize($dic)
217
    {
218
        $this->accountPresetService = $dic->get(AccountPresetService::class);
219
        $this->accountRequest = new AccountRequest();
220
    }
221
}