Passed
Push — main ( 63b0eb...e16704 )
by Felix
03:05
created

FeUserAuthenticationController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 126
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 126
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 as TYPO3Loader;
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
     * @var string
62
     */
63
    public $argumentNameOfPageId = '';
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
     * @var boolean
85
     */
86
    public $checkAuthentication = false;
87
88
    /**
89
     * Instance of Restler class injected at runtime.
90
     *
91
     * @var Restler
92
     */
93
    public $restler;
94
    /**
95
     * @var TYPO3Loader
96
     */
97
    private $typo3Loader;
98
99
    /**
100
     * @param TYPO3Loader $typo3Loader
101
     */
102 6
    public function __construct(TYPO3Loader $typo3Loader)
103
    {
104 6
        $this->typo3Loader = $typo3Loader;
105 6
        $this->restler = Scope::get('Restler');
106 6
    }
107
108
    /**
109
     * This method checks, if client is allowed to access the requested API-class
110
     *
111
     * @return boolean
112
     */
113 3
    public function __isAllowed()
114
    {
115 3
        if ($this->checkAuthentication !== true) {
116
            // this controller is not responsible for the authentication
117 1
            return false;
118
        }
119
120 2
        $this->typo3Loader->initializeFrontendUser($this->determinePageId());
121
122 2
        return $this->typo3Loader->hasActiveFrontendUser();
123
    }
124
125
    /**
126
     * return dummy string, because we DON'T need that for our case (we use NO base-authentification via REST-API)
127
     *
128
     * @return string
129
     * @see \Luracast\Restler\iAuthenticate
130
     */
131 1
    public function __getWWWAuthenticateString()
132
    {
133 1
        return '';
134
    }
135
136
    /**
137
     * List of page IDs (comma separated) or page ID where to look for frontend user records
138
     *
139
     * @return string
140
     */
141 2
    private function determinePageId()
142
    {
143 2
        if (empty($this->argumentNameOfPageId) === false) {
144 2
            return $this->argumentNameOfPageId;
145
        }
146
147
        return $this->determinePageIdFromArguments();
148
    }
149
150
    /**
151
     * determine pageId from arguments, which restler has detected
152
     * We need the pageId, when we want to render TYPO3-contentElements, after the user is authenticated
153
     *
154
     * @return string
155
     */
156 2
    private function determinePageIdFromArguments()
157
    {
158 2
        if (array_key_exists($this->argumentNameOfPageId, $this->restler->apiMethodInfo->arguments) === false) {
159 1
            return '0';
160
        }
161
162 1
        $index = $this->restler->apiMethodInfo->arguments[$this->argumentNameOfPageId];
163 1
        return $this->restler->apiMethodInfo->parameters[$index];
164
    }
165
}
166