Passed
Push — master ( a10e59...b3c3af )
by Torben
01:55
created

FrontendUserService::injectSettingsService()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Derhansen\FeChangePwd\Service;
3
4
/*
5
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Database\ConnectionPool;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
use TYPO3\CMS\Saltedpasswords\Salt\SaltFactory;
14
use TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility;
15
16
/**
17
 * Class FrontendUserService
18
 */
19
class FrontendUserService
20
{
21
    /**
22
     * @var SettingsService
23
     */
24
    protected $settingsService = null;
25
26
    /**
27
     * @param SettingsService $settingsService
28
     */
29
    public function injectSettingsService(\Derhansen\FeChangePwd\Service\SettingsService $settingsService)
30
    {
31
        $this->settingsService = $settingsService;
32
    }
33
34
    /**
35
     * Returns if the frontend user must change the password
36
     *
37
     * @param array $feUserRecord
38
     * @return bool
39
     */
40
    public function mustChangePassword($feUserRecord)
41
    {
42
        $result = false;
43
        $mustChangePassword = $feUserRecord['must_change_password'] ?? 0;
44
        $passwordExpiryTimestamp = $feUserRecord['password_expiry_date'] ?? 0;
45
        if ((bool)$mustChangePassword ||
46
            ((int)$passwordExpiryTimestamp > 0 && (int)$passwordExpiryTimestamp < time())
47
        ) {
48
            $result = true;
49
        }
50
        return $result;
51
    }
52
53
    /**
54
     * Updates the password of the current user
55
     *
56
     * @param string $newPassword
57
     * @return void
58
     */
59
    public function updatePassword($newPassword)
60
    {
61
        // First use md5 as fallback
62
        $password = md5($newPassword);
63
64
        // If salted passwords is enabled, salt the new password
65
        if (SaltedPasswordsUtility::isUsageEnabled('FE')) {
66
            $objSalt = SaltFactory::getSaltingInstance(null);
67
            if (is_object($objSalt)) {
68
                $password = $objSalt->getHashedPassword($newPassword);
69
            }
70
        }
71
72
        $userTable = $this->getFrontendUser()->user_table;
73
        $userUid = $this->getFrontendUser()->user['uid'];
74
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($userTable);
75
        $queryBuilder->getRestrictions()->removeAll();
76
        $queryBuilder->update($userTable)
77
            ->set('password', $password)
78
            ->set('must_change_password', 0)
79
            ->set('password_expiry_date', $this->settingsService->getPasswordExpiryTimestamp())
80
            ->set('tstamp', (int)$GLOBALS['EXEC_TIME'])
81
            ->where(
82
                $queryBuilder->expr()->eq(
83
                    'uid',
84
                    $queryBuilder->createNamedParameter($userUid, \PDO::PARAM_INT)
85
                )
86
            )
87
            ->execute();
88
    }
89
90
    /**
91
     * Returns the frontendUserAuthentication
92
     *
93
     * @return \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
94
     */
95
    protected function getFrontendUser()
96
    {
97
        return $GLOBALS['TSFE']->fe_user;
98
    }
99
}
100