Completed
Push — master ( 215405...4fb0e8 )
by Torben
03:27
created

FrontendUserService::isUserLoggedIn()   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 0
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;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Saltedpasswords\Salt\SaltFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use TYPO3\CMS\Saltedpasswords\Utility\SaltedPasswordsUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Saltedpassword...\SaltedPasswordsUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
16
/**
17
 * Class FrontendUserService
18
 */
19
class FrontendUserService
20
{
21
    /**
22
     * The session key
23
     */
24
    const SESSION_KEY = 'mustChangePasswordReason';
25
26
    /**
27
     * @var SettingsService
28
     */
29
    protected $settingsService = null;
30
31
    /**
32
     * @param SettingsService $settingsService
33
     */
34
    public function injectSettingsService(\Derhansen\FeChangePwd\Service\SettingsService $settingsService)
35
    {
36
        $this->settingsService = $settingsService;
37
    }
38
39
    /**
40
     * Returns if the frontend user must change the password
41
     *
42
     * @param array $feUserRecord
43
     * @return bool
44
     */
45
    public function mustChangePassword($feUserRecord)
46
    {
47
        $reason = '';
48
        $result = false;
49
        $mustChangePassword = $feUserRecord['must_change_password'] ?? 0;
50
        $passwordExpiryTimestamp = $feUserRecord['password_expiry_date'] ?? 0;
51
        if ((bool)$mustChangePassword) {
52
            $reason = 'forcedChange';
53
            $result = true;
54
        } elseif (((int)$passwordExpiryTimestamp > 0 && (int)$passwordExpiryTimestamp < time())) {
55
            $reason = 'passwordExpired';
56
            $result = true;
57
        }
58
59
        if ($result) {
60
            // Store reason for password change in user session
61
            $this->getFrontendUser()->setKey('ses', self::SESSION_KEY, $reason);
62
            $this->getFrontendUser()->storeSessionData();
63
        }
64
        return $result;
65
    }
66
67
    /**
68
     * Returns the reason for the password change stored in the session
69
     *
70
     * @return mixed
71
     */
72
    public function getMustChangePasswordReason()
73
    {
74
        return $this->getFrontendUser()->getKey('ses', self::SESSION_KEY);
75
    }
76
77
    /**
78
     * Updates the password of the current user if a current user session exist
79
     *
80
     * @param string $newPassword
81
     * @return void
82
     */
83
    public function updatePassword($newPassword)
84
    {
85
        if (!$this->isUserLoggedIn()) {
86
            return;
87
        }
88
89
        // Use md5 as fallback
90
        $password = md5($newPassword);
91
92
        // If salted passwords is enabled, salt the new password
93
        if (SaltedPasswordsUtility::isUsageEnabled('FE')) {
94
            $objSalt = SaltFactory::getSaltingInstance(null);
95
            if (is_object($objSalt)) {
96
                $password = $objSalt->getHashedPassword($newPassword);
97
            }
98
        }
99
100
        $userTable = $this->getFrontendUser()->user_table;
101
        $userUid = $this->getFrontendUser()->user['uid'];
102
        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($userTable);
103
        $queryBuilder->getRestrictions()->removeAll();
104
        $queryBuilder->update($userTable)
105
            ->set('password', $password)
106
            ->set('must_change_password', 0)
107
            ->set('password_expiry_date', $this->settingsService->getPasswordExpiryTimestamp())
108
            ->set('tstamp', (int)$GLOBALS['EXEC_TIME'])
109
            ->where(
110
                $queryBuilder->expr()->eq(
111
                    'uid',
112
                    $queryBuilder->createNamedParameter($userUid, \PDO::PARAM_INT)
113
                )
114
            )
115
            ->execute();
116
117
        // Unset reason for password change in user session
118
        $this->getFrontendUser()->setKey('ses', self::SESSION_KEY, null);
119
    }
120
121
    /**
122
     * Returns is there is a current user login
123
     *
124
     * @return bool
125
     */
126
    public function isUserLoggedIn()
127
    {
128
        return  $GLOBALS['TSFE']->loginUser;
129
    }
130
131
    /**
132
     * Returns the frontendUserAuthentication
133
     *
134
     * @return \TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
135
     */
136
    protected function getFrontendUser()
137
    {
138
        return $GLOBALS['TSFE']->fe_user;
139
    }
140
}
141