Passed
Push — master ( 8c95d4...e694c7 )
by Torben
03:09 queued 11s
created

PasswordController::getErrorFlashMessage()   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
declare(strict_types=1);
3
namespace Derhansen\FeChangePwd\Controller;
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
use Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword;
13
use Derhansen\FeChangePwd\Service\FrontendUserService;
14
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
15
16
/**
17
 * Class PasswordController
18
 */
19
class PasswordController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
20
{
21
    /**
22
     * @var FrontendUserService
23
     */
24
    protected $frontendUserService = null;
25
26
    /**
27
     * @param FrontendUserService $frontendUserService
28
     */
29
    public function injectFrontendUserService(
30
        \Derhansen\FeChangePwd\Service\FrontendUserService $frontendUserService
31
    ) {
32
        $this->frontendUserService = $frontendUserService;
33
    }
34
35
    /**
36
     * Edit action
37
     *
38
     * @return void
39
     */
40
    public function editAction()
41
    {
42
        $changePassword = $this->objectManager->get(ChangePassword::class);
43
        $changePassword->setChangeHmac($this->frontendUserService->getChangeHmac());
44
        $this->view->assignMultiple([
45
            'changePasswordReason' => $this->frontendUserService->getMustChangePasswordReason(),
46
            'changePassword' => $changePassword
47
        ]);
48
    }
49
50
    /**
51
     * Ensure a valid changeHmac is provided
52
     *
53
     * @throws \TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException
54
     * @throws \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException
55
     */
56
    public function initializeUpdateAction()
57
    {
58
        $changePasswordArray = $this->request->getArgument('changePassword');
59
        $changeHmac = $changePasswordArray['changeHmac'] ? (string)$changePasswordArray['changeHmac'] : '';
60
        if (!$this->frontendUserService->validateChangeHmac($changeHmac)) {
61
            throw new \TYPO3\CMS\Extbase\Security\Exception\InvalidHashException(
62
                'Possible CSRF detected. Ensure a valid "changeHmac" is provided.',
63
                1572672118931
64
            );
65
        }
66
    }
67
68
    /**
69
     * Update action
70
     *
71
     * @param \Derhansen\FeChangePwd\Domain\Model\Dto\ChangePassword $changePassword
72
     * @validate $changePassword \Derhansen\FeChangePwd\Validation\Validator\ChangePasswordValidator
73
     *
74
     * @return void
75
     */
76
    public function updateAction(ChangePassword $changePassword)
77
    {
78
        $this->frontendUserService->updatePassword($changePassword->getPassword1());
79
        if (isset($this->settings['afterPasswordChangeAction']) &&
80
            $this->settings['afterPasswordChangeAction'] === 'redirect') {
81
            $this->addFlashMessage(
82
                LocalizationUtility::translate('passwordUpdated', 'fe_change_pwd'),
83
                LocalizationUtility::translate('passwordUpdated.title', 'fe_change_pwd')
84
            );
85
            $this->redirect('edit');
86
        }
87
    }
88
89
    /**
90
     * Suppress default flash messages
91
     *
92
     * @return bool
93
     */
94
    protected function getErrorFlashMessage()
95
    {
96
        return false;
97
    }
98
}
99