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

AccountPasswordHelper::getPassword()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 3
nop 3
dl 0
loc 14
rs 9.9666
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\Controllers\Helpers\Account;
26
27
use SP\Core\Acl\Acl;
28
use SP\Core\Acl\ActionsInterface;
29
use SP\Core\Crypt\Crypt;
30
use SP\Core\Crypt\Session as CryptSession;
31
use SP\DataModel\AccountPassData;
32
use SP\Modules\Web\Controllers\Helpers\HelperBase;
33
use SP\Modules\Web\Controllers\Helpers\HelperException;
34
use SP\Services\Crypt\MasterPassService;
35
use SP\Util\ImageUtil;
36
37
/**
38
 * Class AccountPasswordHelper
39
 *
40
 * @package SP\Modules\Web\Controllers\Helpers
41
 */
42
final class AccountPasswordHelper extends HelperBase
43
{
44
    /**
45
     * @var Acl
46
     */
47
    private $acl;
48
49
    /**
50
     * @param AccountPassData $accountData
51
     *
52
     * @param bool            $useImage
53
     *
54
     * @return array
55
     * @throws HelperException
56
     * @throws \Defuse\Crypto\Exception\CryptoException
57
     * @throws \SP\Repositories\NoSuchItemException
58
     * @throws \SP\Services\ServiceException
59
     * @throws \SP\Core\Exceptions\FileNotFoundException
60
     */
61
    public function getPasswordView(AccountPassData $accountData, bool $useImage)
62
    {
63
        $this->checkActionAccess();
64
65
        $this->view->addTemplate('viewpass');
66
67
        $this->view->assign('header', __('Clave de Cuenta'));
68
        $this->view->assign('isImage', (int)$useImage);
69
70
        $pass = $this->getPasswordClear($accountData);
71
72
        if ($useImage) {
73
            $imageUtil = $this->dic->get(ImageUtil::class);
74
75
            $this->view->assign('login', $imageUtil->convertText($accountData->getLogin()));
76
            $this->view->assign('pass', $imageUtil->convertText($pass));
77
        } else {
78
            $this->view->assign('login', $accountData->getLogin());
79
            $this->view->assign('pass', htmlentities($pass));
80
        }
81
82
        $this->view->assign('sk', $this->context->generateSecurityKey());
83
84
        return [
85
            'useimage' => $useImage,
86
            'html' => $this->view->render()
87
        ];
88
    }
89
90
    /**
91
     * @throws HelperException
92
     */
93
    private function checkActionAccess()
94
    {
95
        if (!$this->acl->checkUserAccess(ActionsInterface::ACCOUNT_VIEW_PASS)) {
96
            throw new HelperException(__u('No tiene permisos para acceder a esta cuenta'));
97
        }
98
    }
99
100
    /**
101
     * Returns account's password
102
     *
103
     * @param AccountPassData $accountData
104
     *
105
     * @return string
106
     * @throws HelperException
107
     * @throws \Defuse\Crypto\Exception\CryptoException
108
     * @throws \SP\Repositories\NoSuchItemException
109
     * @throws \SP\Services\ServiceException
110
     */
111
    public function getPasswordClear(AccountPassData $accountData)
112
    {
113
        $this->checkActionAccess();
114
115
        if (!$this->dic->get(MasterPassService::class)->checkUserUpdateMPass($this->context->getUserData()->getLastUpdateMPass())) {
116
            throw new HelperException(__('Clave maestra actualizada') . '<br>' . __('Reinicie la sesión para cambiarla'));
117
        }
118
119
        return trim(Crypt::decrypt($accountData->getPass(), $accountData->getKey(), CryptSession::getSessionKey($this->context)));
120
    }
121
122
    protected function initialize()
123
    {
124
        $this->acl = $this->dic->get(Acl::class);
125
    }
126
}