• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-form-consent / 11823110482

13 Nov 2024 06:03PM UTC coverage: 94.261%. Remained the same
11823110482

Pull #329

github

web-flow
[TASK] Update ssch/typo3-rector to v2.11.0

| datasource | package           | from   | to     |
| ---------- | ----------------- | ------ | ------ |
| packagist  | ssch/typo3-rector | 2.10.2 | 2.11.0 |
Pull Request #329: [TASK] Update ssch/typo3-rector to v2.11.0

772 of 819 relevant lines covered (94.26%)

14.47 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.73
/Classes/Controller/ConsentController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "form_consent".
7
 *
8
 * Copyright (C) 2021-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3FormConsent\Controller;
25

26
use EliasHaeussler\Typo3FormConsent\Domain;
27
use EliasHaeussler\Typo3FormConsent\Event;
28
use EliasHaeussler\Typo3FormConsent\Registry;
29
use Psr\Http\Message;
30
use TYPO3\CMS\Core;
31
use TYPO3\CMS\Extbase;
32

33
/**
34
 * ConsentController
35
 *
36
 * @author Elias Häußler <elias@haeussler.dev>
37
 * @license GPL-2.0-or-later
38
 */
39
final class ConsentController extends Extbase\Mvc\Controller\ActionController
40
{
41
    public function __construct(
18✔
42
        private readonly Domain\Repository\ConsentRepository $consentRepository,
43
        private readonly Extbase\Persistence\PersistenceManagerInterface $persistenceManager,
44
    ) {}
18✔
45

46
    public function initializeAction(): void
18✔
47
    {
48
        if ($this->isPreviewRequested()) {
18✔
49
            $this->actionMethodName = 'previewAction';
1✔
50
            $this->request = $this->request->withControllerActionName('preview');
1✔
51
            $this->arguments->removeAll();
1✔
52
        }
53
    }
54

55
    /**
56
     * @throws Extbase\Persistence\Exception\IllegalObjectTypeException
57
     * @throws Core\Http\PropagateResponseException
58
     * @throws Extbase\Persistence\Exception\UnknownObjectException
59
     */
60
    public function approveAction(string $hash, string $email, bool $verify = false): Message\ResponseInterface
12✔
61
    {
62
        $consent = $this->consentRepository->findOneByValidationHash($hash);
12✔
63

64
        // Add template variable
65
        $this->view->assign('consent', $consent);
12✔
66

67
        // Early return if consent could not be found
68
        if ($consent === null) {
12✔
69
            return $this->createErrorResponse('invalidConsent');
1✔
70
        }
71

72
        // Early return if given email does not match registered email
73
        if ($email !== $consent->getEmail()) {
11✔
74
            return $this->createErrorResponse('invalidEmail');
1✔
75
        }
76

77
        // Early return if consent is already approved
78
        if ($consent->isApproved()) {
10✔
79
            return $this->createErrorResponse('alreadyApproved');
1✔
80
        }
81

82
        // Render required user verification button
83
        if ($verify) {
10✔
84
            $this->view->assign('verificationNeeded', true);
1✔
85

86
            return $this->createHtmlResponse();
1✔
87
        }
88

89
        // Register consent state
90
        Registry\ConsentManagerRegistry::registerConsent($consent);
10✔
91

92
        // Approve consent
93
        $consent->setApproved();
10✔
94
        $consent->setValidUntil(null);
10✔
95

96
        // Dispatch approve event
97
        try {
98
            $event = new Event\ApproveConsentEvent($consent);
10✔
99
            $this->eventDispatcher->dispatch($event);
10✔
100
        } catch (\Exception $exception) {
1✔
101
            return $this->createErrorResponse('unexpectedError', $exception);
1✔
102
        }
103

104
        // Update approved consent
105
        $this->consentRepository->update($consent);
9✔
106
        $this->persistenceManager->persistAll();
9✔
107

108
        return $this->createHtmlResponse($event->getResponse());
9✔
109
    }
110

111
    /**
112
     * @throws Extbase\Persistence\Exception\IllegalObjectTypeException
113
     * @throws Core\Http\PropagateResponseException
114
     * @throws Extbase\Persistence\Exception\UnknownObjectException
115
     */
116
    public function dismissAction(string $hash, string $email, bool $verify = false): Message\ResponseInterface
9✔
117
    {
118
        $consent = $this->consentRepository->findOneByValidationHash($hash);
9✔
119

120
        // Add template variable
121
        $this->view->assign('consent', $consent);
9✔
122

123
        // Early return if consent could not be found
124
        if ($consent === null) {
9✔
125
            return $this->createErrorResponse('invalidConsent');
1✔
126
        }
127

128
        // Early return if given email does not match registered email
129
        if ($consent->getEmail() !== $email) {
9✔
130
            return $this->createErrorResponse('invalidEmail');
1✔
131
        }
132

133
        // Render required user verification button
134
        if ($verify) {
8✔
135
            $this->view->assign('verificationNeeded', true);
1✔
136

137
            return $this->createHtmlResponse();
1✔
138
        }
139

140
        // Register consent state
141
        Registry\ConsentManagerRegistry::registerConsent($consent);
8✔
142

143
        // Un-approve consent
144
        $consent->setDismissed();
8✔
145
        $consent->setValidUntil(null);
8✔
146

147
        // Dispatch dismiss event
148
        try {
149
            $event = new Event\DismissConsentEvent($consent);
8✔
150
            $this->eventDispatcher->dispatch($event);
8✔
151
        } catch (\Exception $exception) {
1✔
152
            return $this->createErrorResponse('unexpectedError', $exception);
1✔
153
        }
154

155
        // Obfuscate submitted data
156
        $consent->setData(null);
7✔
157
        $consent->setOriginalRequestParameters(null);
7✔
158

159
        // Remove dismissed consent
160
        $this->consentRepository->update($consent);
7✔
161
        $this->consentRepository->remove($consent);
7✔
162
        $this->persistenceManager->persistAll();
7✔
163

164
        return $this->createHtmlResponse($event->getResponse());
7✔
165
    }
166

167
    /**
168
     * Dummy preview action for use in backend context.
169
     *
170
     * This action is not part of any frontend plugin. It is used as dummy action for
171
     * preview requests during an active backend session.
172
     *
173
     * NOTE: Method must not be private, otherwise action is not callable by ActionController.
174
     *
175
     * @see ConsentController::initializeAction()
176
     * @see ConsentController::isPreviewRequested()
177
     */
178
    protected function previewAction(): Message\ResponseInterface
1✔
179
    {
180
        return $this->htmlResponse();
1✔
181
    }
182

183
    /**
184
     * @throws Core\Http\PropagateResponseException
185
     */
186
    private function createErrorResponse(string $reason, \Throwable $exception = null): Message\ResponseInterface
5✔
187
    {
188
        $this->view->assign('error', true);
5✔
189
        $this->view->assign('reason', $reason);
5✔
190
        $this->view->assign('exception', $exception);
5✔
191

192
        return $this->createHtmlResponse();
5✔
193
    }
194

195
    /**
196
     * @throws Core\Http\PropagateResponseException
197
     */
198
    private function createHtmlResponse(Message\ResponseInterface $previous = null): Message\ResponseInterface
17✔
199
    {
200
        if ($previous === null) {
17✔
201
            return $this->htmlResponse();
10✔
202
        }
203

204
        if ($previous->getStatusCode() >= 300) {
7✔
205
            throw new Core\Http\PropagateResponseException($previous, 1645646663);
3✔
206
        }
207

208
        $content = (string)$previous->getBody();
4✔
209

210
        if (trim($content) !== '') {
4✔
211
            return $this->htmlResponse($content);
2✔
212
        }
213

214
        return $this->htmlResponse();
2✔
215
    }
216

217
    private function isPreviewRequested(): bool
18✔
218
    {
219
        // Early return if no backend session is active
220
        if ($this->getBackendUser() === null) {
18✔
221
            return false;
18✔
222
        }
223

224
        // Early return if at least one argument is given
225
        if ($this->request->getArguments() !== []) {
1✔
226
            return false;
×
227
        }
228

229
        return true;
1✔
230
    }
231

232
    private function getBackendUser(): ?Core\Authentication\BackendUserAuthentication
18✔
233
    {
234
        $backendUser = $GLOBALS['BE_USER'] ?? null;
18✔
235

236
        if ($backendUser instanceof Core\Authentication\BackendUserAuthentication) {
18✔
237
            return $backendUser;
1✔
238
        }
239

240
        return null;
18✔
241
    }
242
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc