Passed
Push — devel-3.0 ( 7ceb3a...2fc71e )
by Rubén
03:58
created

ItemsPresetForm::analyzeRequestData()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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