Passed
Push — main ( 537ac2...5e2501 )
by Felix
02:27
created

FeUserAuthenticationController::determinePageId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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