__getWWWAuthenticateString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Aoe\Restler\Controller;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2024 AOE GmbH <[email protected]>
9
 *
10
 *  All rights reserved
11
 *
12
 *  This script is part of the TYPO3 project. The TYPO3 project is
13
 *  free software; you can redistribute it and/or modify
14
 *  it under the terms of the GNU General Public License as published by
15
 *  the Free Software Foundation; either version 3 of the License, or
16
 *  (at your option) any later version.
17
 *
18
 *  The GNU General Public License can be found at
19
 *  http://www.gnu.org/copyleft/gpl.html.
20
 *
21
 *  This script is distributed in the hope that it will be useful,
22
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 *  GNU General Public License for more details.
25
 *
26
 *  This copyright notice MUST APPEAR in all copies of the script!
27
 ***************************************************************/
28
29
use Aoe\Restler\System\TYPO3\Loader;
30
use Luracast\Restler\iAuthenticate;
31
use Luracast\Restler\Restler;
32
use Luracast\Restler\Scope;
33
use TYPO3\CMS\Core\SingletonInterface;
34
35
/**
36
 * This class checks, if client is allowed to access the requested and protected API-class
37
 * This class checks, if FE-user is logged in
38
 */
39
class FeUserAuthenticationController implements iAuthenticate, SingletonInterface
40
{
41
    /**
42
     * This property defines (when it's set), the argument-name, which contains the pageId,
43
     * which should be used to initialize TYPO3
44
     * This property will be automatically set by restler, when in
45
     * the API-class/controller this is configured (in PHPdoc/annotations)
46
     *
47
     * Where do we set this property?
48
     * When the property should be used, than it must be set inside the PHPdoc-comment of
49
     * the API-class-method, which handle the API-request
50
     *
51
     * Syntax:
52
     * The PHPdoc-comment must look like this:
53
     * @class [className] {@[propertyName] [propertyValue]}
54
     *
55
     * Example:
56
     * When this controller should use a specific pageId while initializing TYPO3 (this is needed, when we want to
57
     * render TYPO3-contentElements, after the user is authenticated), than the PHPdoc-comment must look like this:
58
     * @class Aoe\Restler\Controller\FeUserAuthenticationController {@argumentNameOfPageId pageId}
59
     *
60
     * @see \Aoe\RestlerExamples\Controller\ContentController::getContentElementByUidForLoggedInFeUser
61
     */
62
    public string $argumentNameOfPageId = '';
63
64
    /**
65
     * This property defines (when it's set), that this controller should check authentication
66
     * This property will be automatically set by restler, when in the API-class/controller this
67
     * is configured (in PHPdoc/annotations)
68
     *
69
     * Where do we set this property?
70
     * When the property should be used, than it must be set inside the PHPdoc-comment of the API-class-method,
71
     * which handle the API-request
72
     *
73
     * Syntax:
74
     * The PHPdoc-comment must look like this:
75
     * @class [className] {@[propertyName] [propertyValue]}
76
     *
77
     * Example:
78
     * When this controller should be used for authentication-checks, than the PHPdoc-comment must look like this:
79
     * @class Aoe\Restler\Controller\FeUserAuthenticationController {@checkAuthentication true}
80
     *
81
     * @see \Aoe\RestlerExamples\Controller\FeUserController::getDataOfLoggedInFeUser
82
     * @see \Aoe\RestlerExamples\Controller\ContentController::getContentElementByUidForLoggedInFeUser
83
     */
84
    public bool $checkAuthentication = false;
85
86
    /**
87
     * Instance of Restler class injected at runtime.
88
     *
89
     * @var Restler
90
     */
91
    public $restler;
92
93 6
    public function __construct(
94
        private readonly Loader $typo3Loader
95 6
    ) {
96 6
        $this->restler = Scope::get('Restler');
97 6
    }
98
99
    /**
100
     * This method checks, if client is allowed to access the requested API-class
101
     */
102
    // phpcs:ignore
103 3
    public function __isAllowed(): bool
104
    {
105 3
        if (!$this->checkAuthentication) {
106
            // this controller is not responsible for the authentication
107 1
            return false;
108
        }
109
110 2
        $this->typo3Loader->initializeFrontendUser($this->determinePageId());
111
112 2
        return $this->typo3Loader->hasActiveFrontendUser();
113
    }
114
115
    /**
116
     * return dummy string, because we DON'T need that for our case (we use NO base-authentification via REST-API)
117
     *
118
     * @see \Luracast\Restler\iAuthenticate
119
     */
120
    // phpcs:ignore
121 1
    public function __getWWWAuthenticateString(): string
122
    {
123 1
        return '';
124
    }
125
126
    /**
127
     * List of page IDs (comma separated) or page ID where to look for frontend user records
128
     */
129 2
    private function determinePageId(): string
130
    {
131 2
        if ($this->argumentNameOfPageId !== '' && $this->argumentNameOfPageId !== '0') {
132 2
            return $this->argumentNameOfPageId;
133
        }
134
135
        return $this->determinePageIdFromArguments();
136
    }
137
138
    /**
139
     * determine pageId from arguments, which restler has detected
140
     * We need the pageId, when we want to render TYPO3-contentElements, after the user is authenticated
141
     */
142 2
    private function determinePageIdFromArguments(): string
143
    {
144 2
        if (!array_key_exists($this->argumentNameOfPageId, $this->restler->apiMethodInfo->arguments)) {
145 1
            return '0';
146
        }
147
148 1
        $index = $this->restler->apiMethodInfo->arguments[$this->argumentNameOfPageId];
149 1
        return $this->restler->apiMethodInfo->parameters[$index];
150
    }
151
}
152