Passed
Push — devel-3.0 ( 330e85...5f7f30 )
by Rubén
03:30
created

AuthTokenForm::checkCommon()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 0
dl 0
loc 14
rs 9.2222
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\AuthTokenData;
30
use SP\Services\AuthToken\AuthTokenService;
31
32
/**
33
 * Class ApiTokenForm
34
 *
35
 * @package SP\Modules\Web\Forms
36
 */
37
final class AuthTokenForm extends FormBase implements FormInterface
38
{
39
    /**
40
     * @var AuthTokenData
41
     */
42
    protected $authTokenData;
43
    /**
44
     * @var bool
45
     */
46
    protected $refresh = false;
47
48
    /**
49
     * Validar el formulario
50
     *
51
     * @param $action
52
     *
53
     * @return AuthTokenForm
54
     * @throws \SP\Core\Exceptions\ValidationException
55
     */
56
    public function validate($action)
57
    {
58
        switch ($action) {
59
            case ActionsInterface::AUTHTOKEN_CREATE:
60
            case ActionsInterface::AUTHTOKEN_EDIT:
61
                $this->analyzeRequestData();
62
                $this->checkCommon();
63
                break;
64
        }
65
66
        return $this;
67
    }
68
69
    /**
70
     * Analizar los datos de la petición HTTP
71
     *
72
     * @return void
73
     */
74
    protected function analyzeRequestData()
75
    {
76
        $this->refresh = $this->request->analyzeBool('refreshtoken', false);
77
78
        $this->authTokenData = new AuthTokenData();
79
        $this->authTokenData->setId($this->itemId);
80
        $this->authTokenData->setUserId($this->request->analyzeInt('users'));
81
        $this->authTokenData->setActionId($this->request->analyzeInt('actions'));
82
        $this->authTokenData->setHash($this->request->analyzeEncrypted('pass'));
83
    }
84
85
    /**
86
     * @throws ValidationException
87
     */
88
    protected function checkCommon()
89
    {
90
        if ($this->authTokenData->getUserId() === 0) {
91
            throw new ValidationException(__u('Usuario no indicado'));
92
        }
93
94
        if ($this->authTokenData->getActionId() === 0) {
95
            throw new ValidationException(__u('Acción no indicada'));
96
        }
97
98
        if ((AuthTokenService::isSecuredAction($this->authTokenData->getActionId()) || $this->isRefresh())
99
            && $this->authTokenData->getHash() === ''
100
        ) {
101
            throw new ValidationException(__u('La clave no puede estar en blanco'));
102
        }
103
    }
104
105
    /**
106
     * @return bool
107
     */
108
    public function isRefresh()
109
    {
110
        return $this->refresh;
111
    }
112
113
    /**
114
     * @return AuthTokenData
115
     */
116
    public function getItemData()
117
    {
118
        return $this->authTokenData;
119
    }
120
}