FrontendUser.php 1.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php

declare(strict_types = 1);

namespace T3o\TerRest\Authentication\Provider;

/*
 * This file is part of TYPO3 CMS-extension "ter_rest", created by Oliver Bartsch.
 *
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
 */

use Psr\Http\Message\ServerRequestInterface;
use T3o\TerRest\Authentication\AuthenticationHandlerInterface;
use T3o\TerRest\Authentication\User\ApiUser;
use T3o\TerRest\Authentication\User\ApiUserInterface;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;

/**
 * Authentication provider for login as frontend user
 */
final class FrontendUser implements AuthenticationProviderInterface
{
    protected Context $context;

    public function __construct(Context $context)
    {
        $this->context = $context;
    }

    public function authenticate(
        ServerRequestInterface $request,
        AuthenticationHandlerInterface $handler
    ): ApiUserInterface {
        if ((bool)$this->context->getPropertyFromAspect('frontend.user', 'id')) {
            $frontendUserAuthentication = $request->getAttribute('frontend.user');
            if ($frontendUserAuthentication instanceof FrontendUserAuthentication) {
                $frontendUserAuthentication->fetchGroupData();
            }
        }

        if ($this->context->getPropertyFromAspect('frontend.user', 'isLoggedIn')) {
            return new ApiUser('FeUser', 3);
        }

        return $handler->handle($request);
    }
}