Passed
Push — main ( 61dc13...0de980 )
by Felix
02:21
created

Loader::initializeBackendUser()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 4
nop 0
dl 0
loc 17
ccs 0
cts 12
cp 0
crap 20
rs 9.9
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Loader::initializeFrontendUser() 0 3 1
1
<?php
2
namespace Aoe\Restler\System\TYPO3;
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 TYPO3\CMS\Core\Context\Context;
29
use TYPO3\CMS\Core\Core\Bootstrap;
30
use TYPO3\CMS\Core\Routing\PageArguments;
31
use TYPO3\CMS\Core\SingletonInterface;
32
use TYPO3\CMS\Core\Site\SiteFinder;
33
use TYPO3\CMS\Core\TimeTracker\TimeTracker;
34
use TYPO3\CMS\Core\TypoScript\TemplateService;
35
use TYPO3\CMS\Core\Utility\GeneralUtility;
36
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
37
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication;
38
use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
39
use TYPO3\CMS\Frontend\Page\PageRepository;
40
use TYPO3\CMS\Frontend\Utility\EidUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Frontend\Utility\EidUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
use LogicException;
42
43
/**
44
 * @package Restler
45
 */
46
class Loader implements SingletonInterface
47
{
48
    /**
49
     * defines, if usage of frontend-user is enabled (this is needed, if the eID-script must determine the frontend-user)
50
     *
51
     * @var boolean
52
     */
53
    private $isFrontendUserInitialized = false;
54
    /**
55
     * defines, if frontend-rendering is enabled (this is needed, if the eID-script must render some content-elements or RTE-fields)
56
     *
57
     * @var boolean
58
     */
59
    private $isFrontendRenderingInitialized = false;
60
61
    /**
62
     * Checks if a backend user is logged in.
63
     *
64
     * @return bool
65
     */
66
    public function hasBackendUser()
67
    {
68
        return ($GLOBALS['BE_USER'] ?? null) instanceof BackendUserAuthentication;
69
    }
70
71
    /**
72
     * @return BackendUserAuthentication
73
     * @throws LogicException
74
     */
75
    public function getBackendUser()
76
    {
77
        if ($this->hasBackendUser() === false) {
78
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendUser\'');
79
        }
80
        return $GLOBALS['BE_USER'];
81
    }
82
83
    /**
84
     * @return FrontendUserAuthentication
85
     * @throws LogicException
86
     */
87
    public function getFrontendUser()
88
    {
89
        if ($this->isFrontendUserInitialized === false) {
90
            throw new LogicException('fe-user is not initialized - initialize with FE-user with method \'initializeFrontendUser\'');
91
        }
92
        return $GLOBALS['TSFE']->fe_user;
93
    }
94
95
    /**
96
     * enable the usage of frontend-user
97
     *
98
     * @param integer $pageId
99
     * @param integer $type
100
     */
101
    public function initializeFrontendUser($pageId = 0, $type = 0)
0 ignored issues
show
Unused Code introduced by
The parameter $pageId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
    public function initializeFrontendUser(/** @scrutinizer ignore-unused */ $pageId = 0, $type = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

101
    public function initializeFrontendUser($pageId = 0, /** @scrutinizer ignore-unused */ $type = 0)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
    {
103
        $this->isFrontendUserInitialized = true;
104
    }
105
106
    /**
107
     * enable the frontend-rendering
108
     *
109
     * @param integer $pageId
110
     * @param integer $type
111
     *
112
     * @return void
113
     */
114
    public function initializeFrontendRendering($pageId = 0, $type = 0)
115
    {
116
        if ($this->isFrontendInitialized()) {
117
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
118
            $this->isFrontendRenderingInitialized = true;
119
        }
120
        if ($this->isFrontendRenderingInitialized === true) {
121
            return;
122
        }
123
124
        $this->getTsfe($pageId, $type);
125
126
        $this->isFrontendRenderingInitialized = true;
127
    }
128
129
    /**
130
     * Checks if a frontend user is logged in and the session is active.
131
     *
132
     * @return bool
133
     */
134
    protected function isFrontendInitialized()
135
    {
136
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
137
            $GLOBALS['TSFE']->tmpl instanceof TemplateService;
138
    }
139
140
    /**
141
     * Checks if a frontend user is logged in and the session is active.
142
     *
143
     * @return bool
144
     */
145
    protected function hasActiveFrontendUserSession()
146
    {
147
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
148
            $GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication &&
149
            isset($GLOBALS['TSFE']->fe_user->user['uid']);
150
    }
151
152
    /**
153
     * @param integer $pageId
154
     * @param integer $type
155
     * @return TypoScriptFrontendController
156
     */
157
    private function getTsfe($pageId = 0, $type = 0)
158
    {
159
        if (array_key_exists('TSFE', $GLOBALS) && $GLOBALS['TSFE'] instanceof TypoScriptFrontendController) {
160
            return $GLOBALS['TSFE'];
161
        }
162
163
        $context = GeneralUtility::makeInstance(Context::class);
164
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
165
        $site = $siteFinder->getSiteByPageId($pageId);
166
        $pageArguments = new PageArguments($pageId, $type, [], [], []);
167
        $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
168
            TypoScriptFrontendController::class,
169
            $context,
170
            $site,
171
            $site->getDefaultLanguage(),
172
            $pageArguments
173
        );
174
        $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class, $context);
175
        $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
176
177
        return $GLOBALS['TSFE'];
178
    }
179
}
180