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

ItemsPresetForm::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
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\ValidationException;
29
use SP\DataModel\AccountPermission;
30
use SP\DataModel\AccountPrivate;
31
use SP\DataModel\ItemPresetData;
32
use SP\Services\ItemPreset\ItemPresetInterface;
33
use SP\Services\ItemPreset\ItemPresetRequest;
34
35
/**
36
 * Class ItemsPresetForm
37
 *
38
 * @package SP\Modules\Web\Forms
39
 */
40
final class ItemsPresetForm extends FormBase implements FormInterface
41
{
42
    /**
43
     * @var ItemPresetRequest
44
     */
45
    protected $itemPresetRequest;
46
47
    /**
48
     * Validar el formulario
49
     *
50
     * @param $action
51
     *
52
     * @return ItemsPresetForm
53
     * @throws ValidationException
54
     */
55
    public function validate($action)
56
    {
57
        switch ($action) {
58
            case ActionsInterface::ITEMPRESET_CREATE:
59
            case ActionsInterface::ITEMPRESET_EDIT:
60
                $this->analyzeRequestData();
61
                $this->checkCommon();
62
                break;
63
        }
64
65
        return $this;
66
    }
67
68
    /**
69
     * Analizar los datos de la petición HTTP
70
     *
71
     * @return void
72
     * @throws ValidationException
73
     */
74
    protected function analyzeRequestData()
75
    {
76
        $itemPresetData = new ItemPresetData();
77
78
        if ($this->itemId > 0) {
79
            $itemPresetData->setId($this->itemId);
80
        }
81
82
        if ($userId = $this->request->analyzeInt('user_id')) {
83
            $itemPresetData->setUserId($userId);
84
        }
85
86
        if ($userGroupId = $this->request->analyzeInt('user_group_id')) {
87
            $itemPresetData->setUserGroupId($userGroupId);
88
        }
89
90
        if ($userProfileId = $this->request->analyzeInt('user_profile_id')) {
91
            $itemPresetData->setUserProfileId($userProfileId);
92
        }
93
94
        $itemPresetData->setFixed((int)$this->request->analyzeBool('fixed_enabled', false));
95
        $itemPresetData->setPriority($this->request->analyzeInt('priority'));
96
        $itemPresetData->setType($this->request->analyzeString('type'));
97
98
        switch ($itemPresetData->getType()) {
99
            case ItemPresetInterface::ITEM_TYPE_ACCOUNT_PERMISSION:
100
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makePermissionPreset());
101
                break;
102
            case ItemPresetInterface::ITEM_TYPE_ACCOUNT_PRIVATE:
103
                $this->itemPresetRequest = new ItemPresetRequest($itemPresetData, $this->makePrivatePreset());
104
                break;
105
            default:
106
                throw new ValidationException(__u('Tipo de valor no definido o incorrecto'));
107
        }
108
    }
109
110
    /**
111
     * @return AccountPermission
112
     * @throws ValidationException
113
     */
114
    private function makePermissionPreset()
115
    {
116
        $accountPermission = new AccountPermission();
117
        $accountPermission->setUsersView($this->request->analyzeArray('users_view', null, []));
118
        $accountPermission->setUsersEdit($this->request->analyzeArray('users_edit', null, []));
119
        $accountPermission->setUserGroupsView($this->request->analyzeArray('user_groups_view', null, []));
120
        $accountPermission->setUserGroupsEdit($this->request->analyzeArray('user_groups_edit', null, []));
121
122
        if (!$accountPermission->hasItems()) {
123
            throw new ValidationException(__u('No hay permisos definidos'));
124
        }
125
126
        return $accountPermission;
127
    }
128
129
    /**
130
     * @return AccountPrivate
131
     */
132
    private function makePrivatePreset()
133
    {
134
        $accountPrivate = new AccountPrivate();
135
        $accountPrivate->setPrivateUser($this->request->analyzeBool('private_user_enabled', false));
136
        $accountPrivate->setPrivateGroup($this->request->analyzeBool('private_group_enabled', false));
137
138
        return $accountPrivate;
139
    }
140
141
    /**
142
     * @throws ValidationException
143
     */
144
    protected function checkCommon()
145
    {
146
        $itemPresetData = $this->itemPresetRequest->getItemPresetData();
147
148
        if (!$itemPresetData->getUserId()
149
            && !$itemPresetData->getUserGroupId()
150
            && !$itemPresetData->getUserProfileId()
151
        ) {
152
            throw new ValidationException(__u('Es necesario asignar un elemento del tipo usuario, grupo o perfil'));
153
        }
154
    }
155
156
    /**
157
     * @return ItemPresetRequest
158
     */
159
    public function getItemData()
160
    {
161
        return $this->itemPresetRequest;
162
    }
163
}