PasswordController   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 34
c 2
b 0
f 0
dl 0
loc 101
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setFeUserPasswordHashToArguments() 0 10 1
A updateAction() 0 16 3
A getErrorFlashMessage() 0 3 1
A injectFrontendUserService() 0 3 1
A initializeUpdateAction() 0 11 3
A getFrontendUser() 0 3 1
A editAction() 0 10 1
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
    public function injectFrontendUserService(FrontendUserService $frontendUserService): void
32
    {
33
        $this->frontendUserService = $frontendUserService;
34
    }
35
36
    /**
37
     * Edit action
38
     *
39
     * @return ResponseInterface
40
     */
41
    public function editAction(): ResponseInterface
42
    {
43
        $changePassword = new ChangePassword();
44
        $changePassword->setChangeHmac($this->frontendUserService->getChangeHmac());
45
        $this->view->assignMultiple([
46
            'changePasswordReason' => $this->frontendUserService->getMustChangePasswordReason(),
47
            'changePassword' => $changePassword,
48
        ]);
49
50
        return $this->htmlResponse();
51
    }
52
53
    /**
54
     * Ensure a valid changeHmac is provided
55
     *
56
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
57
     * @throws InvalidHashException
58
     */
59
    public function initializeUpdateAction(): void
60
    {
61
        $changePasswordArray = $this->request->getArgument('changePassword');
62
        $changeHmac = $changePasswordArray['changeHmac'] ? (string)$changePasswordArray['changeHmac'] : '';
63
        if (!$this->frontendUserService->validateChangeHmac($changeHmac)) {
64
            throw new InvalidHashException(
65
                'Possible CSRF detected. Ensure a valid "changeHmac" is provided.',
66
                1572672118931
67
            );
68
        }
69
        $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

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