Passed
Push — main ( 04a742...5b603f )
by
unknown
04:37 queued 12s
created

FeUserAuthenticationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 112
ccs 19
cts 20
cp 0.95
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __isAllowed() 0 10 2
A determinePageIdFromArguments() 0 8 2
A __getWWWAuthenticateString() 0 3 1
A determinePageId() 0 7 2
1
<?php
2
3
namespace Aoe\Restler\Controller;
4
5
/***************************************************************
6
 *  Copyright notice
7
 *
8
 *  (c) 2021 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
34
/**
35
 * This class checks, if client is allowed to access the requested and protected API-class
36
 * This class checks, if FE-user is logged in
37
 */
38
class FeUserAuthenticationController implements iAuthenticate
39
{
40
    /**
41
     * This property defines (when it's set), the argument-name, which contains the pageId,
42
     * which should be used to initialize TYPO3
43
     * This property will be automatically set by restler, when in
44
     * the API-class/controller this is configured (in PHPdoc/annotations)
45
     *
46
     * Where do we set this property?
47
     * When the property should be used, than it must be set inside the PHPdoc-comment of
48
     * the API-class-method, which handle the API-request
49
     *
50
     * Syntax:
51
     * The PHPdoc-comment must look like this:
52
     * @class [className] {@[propertyName] [propertyValue]}
53
     *
54
     * Example:
55
     * When this controller should use a specific pageId while initializing TYPO3 (this is needed, when we want to
56
     * render TYPO3-contentElements, after the user is authenticated), than the PHPdoc-comment must look like this:
57
     * @class Aoe\Restler\Controller\FeUserAuthenticationController {@argumentNameOfPageId pageId}
58
     *
59
     * @see \Aoe\RestlerExamples\Controller\ContentController::getContentElementByUidForLoggedInFeUser
60
     */
61
    public string $argumentNameOfPageId = '';
62
    /**
63
     * This property defines (when it's set), that this controller should check authentication
64
     * This property will be automatically set by restler, when in the API-class/controller this
65
     * is configured (in PHPdoc/annotations)
66
     *
67
     * Where do we set this property?
68
     * When the property should be used, than it must be set inside the PHPdoc-comment of the API-class-method,
69
     * which handle the API-request
70
     *
71
     * Syntax:
72
     * The PHPdoc-comment must look like this:
73
     * @class [className] {@[propertyName] [propertyValue]}
74
     *
75
     * Example:
76
     * When this controller should be used for authentication-checks, than the PHPdoc-comment must look like this:
77
     * @class Aoe\Restler\Controller\FeUserAuthenticationController {@checkAuthentication true}
78
     *
79
     * @see \Aoe\RestlerExamples\Controller\FeUserController::getDataOfLoggedInFeUser
80
     * @see \Aoe\RestlerExamples\Controller\ContentController::getContentElementByUidForLoggedInFeUser
81
     */
82
    public bool $checkAuthentication = false;
83
84
    /**
85
     * Instance of Restler class injected at runtime.
86
     *
87
     * @var Restler
88
     */
89
    public $restler;
90
91
    private Loader $typo3Loader;
92
93 6
    public function __construct(Loader $typo3Loader)
94
    {
95 6
        $this->typo3Loader = $typo3Loader;
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 (!empty($this->argumentNameOfPageId)) {
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