Passed
Push — devel-3.0 ( 5f7f30...cd1038 )
by Rubén
03:44
created

AccountDefaultPermissionForm   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 88
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A analyzeRequestData() 0 30 5
A getItemData() 0 3 1
A checkCommon() 0 11 5
A validate() 0 11 3
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\AccountDefaultPermissionData;
30
use SP\DataModel\AccountPermission;
31
32
/**
33
 * Class AccountDefaultPermissionForm
34
 *
35
 * @package SP\Modules\Web\Forms
36
 */
37
final class AccountDefaultPermissionForm extends FormBase implements FormInterface
38
{
39
    /**
40
     * @var AccountDefaultPermissionData
41
     */
42
    protected $accountDefaultPermissionData;
43
44
    /**
45
     * Validar el formulario
46
     *
47
     * @param $action
48
     *
49
     * @return AccountDefaultPermissionForm
50
     * @throws ValidationException
51
     */
52
    public function validate($action)
53
    {
54
        switch ($action) {
55
            case ActionsInterface::ACCOUNT_DEFAULT_PERMISSION_CREATE:
56
            case ActionsInterface::ACCOUNT_DEFAULT_PERMISSION_EDIT:
57
                $this->analyzeRequestData();
58
                $this->checkCommon();
59
                break;
60
        }
61
62
        return $this;
63
    }
64
65
    /**
66
     * Analizar los datos de la petición HTTP
67
     *
68
     * @return void
69
     */
70
    protected function analyzeRequestData()
71
    {
72
        $this->accountDefaultPermissionData = new AccountDefaultPermissionData();
73
74
        if ($this->itemId > 0) {
75
            $this->accountDefaultPermissionData->setId($this->itemId);
76
        }
77
78
        if ($userId = $this->request->analyzeInt('user_id')) {
79
            $this->accountDefaultPermissionData->setUserId($userId);
80
        }
81
82
        if ($userGroupId = $this->request->analyzeInt('user_group_id')) {
83
            $this->accountDefaultPermissionData->setUserGroupId($userGroupId);
84
        }
85
86
        if ($userProfileId = $this->request->analyzeInt('user_profile_id')) {
87
            $this->accountDefaultPermissionData->setUserProfileId($userProfileId);
88
        }
89
        
90
        $this->accountDefaultPermissionData->setFixed((int)$this->request->analyzeBool('fixed_enabled', false));
91
        $this->accountDefaultPermissionData->setPriority($this->request->analyzeInt('priority'));
92
93
        $accountPermission = new AccountPermission();
94
        $accountPermission->setUsersView($this->request->analyzeArray('users_view', null, []));
95
        $accountPermission->setUsersEdit($this->request->analyzeArray('users_edit', null, []));
96
        $accountPermission->setUserGroupsView($this->request->analyzeArray('user_groups_view', null, []));
97
        $accountPermission->setUserGroupsEdit($this->request->analyzeArray('user_groups_edit', null, []));
98
99
        $this->accountDefaultPermissionData->setAccountPermission($accountPermission);
100
    }
101
102
    /**
103
     * @throws ValidationException
104
     */
105
    protected function checkCommon()
106
    {
107
        if (!$this->accountDefaultPermissionData->getUserId()
108
            && !$this->accountDefaultPermissionData->getUserGroupId()
109
            && !$this->accountDefaultPermissionData->getUserProfileId()
110
        ) {
111
            throw new ValidationException(__u('Es necesario asignar un elemento del tipo usuario, grupo o perfil'));
112
        }
113
114
        if (!$this->accountDefaultPermissionData->getAccountPermission()->hasItems()) {
115
            throw new ValidationException(__u('No hay permisos definidos'));
116
        }
117
    }
118
119
    /**
120
     * @return AccountDefaultPermissionData
121
     */
122
    public function getItemData()
123
    {
124
        return $this->accountDefaultPermissionData;
125
    }
126
}