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

ItemsPresetForm::makeSessionTimeoutreset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 9
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 SP\Core\Acl\ActionsInterface;
28
use SP\Core\Exceptions\InvalidArgumentException;
29
use SP\Core\Exceptions\ValidationException;
30
use SP\DataModel\ItemPreset\AccountPermission;
31
use SP\DataModel\ItemPreset\AccountPrivate;
32
use SP\DataModel\ItemPreset\Password;
33
use SP\DataModel\ItemPreset\SessionTimeout;
34
use SP\DataModel\ItemPresetData;
35
use SP\Mvc\Controller\Validators\Validator;
36
use SP\Services\ItemPreset\ItemPresetInterface;
37
use SP\Services\ItemPreset\ItemPresetRequest;
38
39
/**
40
 * Class ItemsPresetForm
41
 *
42
 * @package SP\Modules\Web\Forms
43
 */
44
final class ItemsPresetForm extends FormBase implements FormInterface
45
{
46
    /**
47
     * @var ItemPresetRequest
48
     */
49
    protected $itemPresetRequest;
50
51
    /**
52
     * Validar el formulario
53
     *
54
     * @param $action
55
     *
56
     * @return ItemsPresetForm
57
     * @throws ValidationException
58
     */
59
    public function validate($action)
60
    {
61
        switch ($action) {
62
            case ActionsInterface::ITEMPRESET_CREATE:
63
            case ActionsInterface::ITEMPRESET_EDIT:
64
                $this->analyzeRequestData();
65
                $this->checkCommon();
66
                break;
67
        }
68
69
        return $this;
70
    }
71
72
    /**
73
     * Analizar los datos de la petición HTTP
74
     *
75
     * @return void
76
     * @throws ValidationException
77
     */
78
    protected function analyzeRequestData()
79
    {
80
        $itemPresetData = new ItemPresetData();
81
82
        if ($this->itemId > 0) {
83
            $itemPresetData->setId($this->itemId);
84
        }
85
86
        if ($userId = $this->request->analyzeInt('user_id')) {
87
            $itemPresetData->setUserId($userId);
88
        }
89
90
        if ($userGroupId = $this->request->analyzeInt('user_group_id')) {
91
            $itemPresetData->setUserGroupId($userGroupId);
92
        }
93
94
        if ($userProfileId = $this->request->analyzeInt('user_profile_id')) {
95
            $itemPresetData->setUserProfileId($userProfileId);
96
        }
97
98
        $itemPresetData->setFixed((int)$this->request->analyzeBool('fixed_enabled', false));
99
        $itemPresetData->setPriority($this->request->analyzeInt('priority'));
100
        $itemPresetData->setType($this->request->analyzeString('type'));
101
102
        switch ($itemPresetData->getType()) {
103
            case ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION:
104
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makePermissionPreset());
105
                break;
106
            case ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE:
107
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makePrivatePreset());
108
                break;
109
            case ItemPresetInterface::ITEM_TYPE_SESSION_TIMEOUT:
110
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makeSessionTimeoutPreset());
111
                break;
112
            case ItemPresetInterface::ITEM_TYPE_ACCOUNT_PASSWORD:
113
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makePasswordPreset());
114
                break;
115
            default:
116
                throw new ValidationException(__u('Tipo de valor no definido o incorrecto'));
117
        }
118
    }
119
120
    /**
121
     * @return AccountPermission
122
     * @throws ValidationException
123
     */
124
    private function makePermissionPreset()
125
    {
126
        $accountPermission = new AccountPermission();
127
        $accountPermission->setUsersView($this->request->analyzeArray('users_view', null, []));
128
        $accountPermission->setUsersEdit($this->request->analyzeArray('users_edit', null, []));
129
        $accountPermission->setUserGroupsView($this->request->analyzeArray('user_groups_view', null, []));
130
        $accountPermission->setUserGroupsEdit($this->request->analyzeArray('user_groups_edit', null, []));
131
132
        if (!$accountPermission->hasItems()) {
133
            throw new ValidationException(__u('No hay permisos definidos'));
134
        }
135
136
        return $accountPermission;
137
    }
138
139
    /**
140
     * @return AccountPrivate
141
     */
142
    private function makePrivatePreset()
143
    {
144
        $accountPrivate = new AccountPrivate();
145
        $accountPrivate->setPrivateUser($this->request->analyzeBool('private_user_enabled', false));
146
        $accountPrivate->setPrivateGroup($this->request->analyzeBool('private_group_enabled', false));
147
148
        return $accountPrivate;
149
    }
150
151
    /**
152
     * @return SessionTimeout
153
     * @throws ValidationException
154
     */
155
    private function makeSessionTimeoutPreset()
156
    {
157
        try {
158
            return new SessionTimeout(
159
                $this->request->analyzeString('ip_address'),
160
                $this->request->analyzeInt('timeout')
161
            );
162
        } catch (InvalidArgumentException $e) {
163
            throw new ValidationException($e->getMessage());
164
        }
165
    }
166
167
    /**
168
     * @return Password
169
     * @throws ValidationException
170
     */
171
    private function makePasswordPreset()
172
    {
173
        $password = new Password();
174
        $password->setLength($this->request->analyzeInt('length', 1));
175
        $password->setExpireTime($this->request->analyzeInt('expire_time', 0));
176
        $password->setScore($this->request->analyzeInt('score', 0));
177
178
        $regex = $this->request->analyzeUnsafeString('regex');
179
180
        if (!empty($regex)) {
181
            if (Validator::isRegex($regex) === false) {
182
                throw new ValidationException(__u('Expresión regular inválida'));
183
            }
184
        }
185
186
        $password->setRegex($regex);
187
        $password->setUseNumbers($this->request->analyzeBool('use_numbers_enabled', false));
188
        $password->setUseLetters($this->request->analyzeBool('use_letters_enabled', false));
189
        $password->setUseSymbols($this->request->analyzeBool('use_symbols_enabled', false));
190
        $password->setUseLower($this->request->analyzeBool('use_lower_enabled', false));
191
        $password->setUseUpper($this->request->analyzeBool('use_upper_enabled', false));
192
        $password->setUseImage($this->request->analyzeBool('use_image_enabled', false));
193
194
        return $password;
195
    }
196
197
    /**
198
     * @throws ValidationException
199
     */
200
    protected function checkCommon()
201
    {
202
        $itemPresetData = $this->itemPresetRequest->getItemPresetData();
203
204
        if (!$itemPresetData->getUserId()
205
            && !$itemPresetData->getUserGroupId()
206
            && !$itemPresetData->getUserProfileId()
207
        ) {
208
            throw new ValidationException(__u('Es necesario asignar un elemento del tipo usuario, grupo o perfil'));
209
        }
210
    }
211
212
    /**
213
     * @return ItemPresetRequest
214
     */
215
    public function getItemData()
216
    {
217
        return $this->itemPresetRequest;
218
    }
219
}