Passed
Push — master ( d8df74...725a8f )
by Torben
08:07
created

PasswordController::getFrontendUser()   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
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace Derhansen\FeChangePwd\Controller;
13
14
use Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword;
15
use Derhansen\FeChangePwd\Event\AfterPasswordUpdatedEvent;
16
use Derhansen\FeChangePwd\Service\FrontendUserService;
17
use Psr\Http\Message\ResponseInterface;
18
use TYPO3\CMS\Extbase\Annotation as Extbase;
19
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
20
use TYPO3\CMS\Extbase\Security\Exception\InvalidHashException;
21
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
22
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
23
24
/**
25
 * Class PasswordController
26
 */
27
class PasswordController extends ActionController
28
{
29
    protected FrontendUserService $frontendUserService;
30
31
    /**
32
     * @param FrontendUserService $frontendUserService
33
     */
34
    public function injectFrontendUserService(
35
        FrontendUserService $frontendUserService
36
    ) {
37
        $this->frontendUserService = $frontendUserService;
38
    }
39
40
    /**
41
     * Edit action
42
     *
43
     * @return ResponseInterface
44
     */
45
    public function editAction(): ResponseInterface
46
    {
47
        $changePassword = new ChangePassword();
48
        $changePassword->setChangeHmac($this->frontendUserService->getChangeHmac());
49
        $this->view->assignMultiple([
50
            'changePasswordReason' => $this->frontendUserService->getMustChangePasswordReason(),
51
            'changePassword' => $changePassword
52
        ]);
53
54
        return $this->htmlResponse();
55
    }
56
57
    /**
58
     * Ensure a valid changeHmac is provided
59
     *
60
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
61
     * @throws InvalidHashException
62
     */
63
    public function initializeUpdateAction()
64
    {
65
        $changePasswordArray = $this->request->getArgument('changePassword');
66
        $changeHmac = $changePasswordArray['changeHmac'] ? (string)$changePasswordArray['changeHmac'] : '';
67
        if (!$this->frontendUserService->validateChangeHmac($changeHmac)) {
68
            throw new InvalidHashException(
69
                'Possible CSRF detected. Ensure a valid "changeHmac" is provided.',
70
                1572672118931
71
            );
72
        }
73
        $this->setFeUserPasswordHashToArguments($changePasswordArray);
0 ignored issues
show
Bug introduced by
It seems like $changePasswordArray can also be of type string; however, parameter $changePasswordArray of Derhansen\FeChangePwd\Co...sswordHashToArguments() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
        $this->setFeUserPasswordHashToArguments(/** @scrutinizer ignore-type */ $changePasswordArray);
Loading history...
74
    }
75
76
    /**
77
     * Update action
78
     *
79
     * @param \Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword $changePassword
80
     * @return ResponseInterface
81
     * @Extbase\Validate(param="changePassword", validator="Derhansen\FeChangePwd\Validation\Validator\ChangePasswordValidator")
82
     */
83
    public function updateAction(ChangePassword $changePassword): ResponseInterface
84
    {
85
        $this->frontendUserService->updatePassword($changePassword->getPassword1());
86
87
        $this->eventDispatcher->dispatch(new AfterPasswordUpdatedEvent($changePassword, $this));
88
89
        if (isset($this->settings['afterPasswordChangeAction']) &&
90
            $this->settings['afterPasswordChangeAction'] === 'redirect') {
91
            $this->addFlashMessage(
92
                LocalizationUtility::translate('passwordUpdated', 'FeChangePwd'),
93
                LocalizationUtility::translate('passwordUpdated.title', 'FeChangePwd')
94
            );
95
            $this->redirect('edit');
96
        }
97
98
        return $this->htmlResponse();
99
    }
100
101
    /**
102
     * Sets the current fe_user password (hashed) to request argument "changePassword"
103
     *
104
     * @param array $changePasswordArray
105
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
106
     */
107
    protected function setFeUserPasswordHashToArguments(array $changePasswordArray): void
108
    {
109
        $changePasswordArgument = $this->arguments->getArgument('changePassword');
110
        $propertyMapping = $changePasswordArgument->getPropertyMappingConfiguration();
111
        $propertyMapping->allowProperties('feUserPasswordHash');
112
113
        $changePasswordArray['feUserPasswordHash'] = $this->getFrontendUser()->user['password'];
114
        $arguments = $this->request->getArguments();
115
        $arguments['changePassword'] = $changePasswordArray;
116
        $this->request->setArguments($arguments);
117
    }
118
119
    /**
120
     * Suppress default flash messages
121
     *
122
     * @return bool
123
     */
124
    protected function getErrorFlashMessage(): bool
125
    {
126
        return false;
127
    }
128
129
    protected function getFrontendUser(): FrontendUserAuthentication
130
    {
131
        return $GLOBALS['TSFE']->fe_user;
132
    }
133
}
134