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

Loader::hasActiveBackendUser()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
rs 10
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;
0 ignored issues
show
introduced by
The private property $isFrontendUserInitialized is not used, and could be removed.
Loading history...
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 hasActiveBackendUser()
67
    {
68
        return ($GLOBALS['BE_USER'] ?? null) instanceof BackendUserAuthentication &&
69
            $GLOBALS['BE_USER']->user['uid'] > 0;
70
    }
71
72
    /**
73
     * @return BackendUserAuthentication
74
     * @throws LogicException
75
     */
76
    public function getBackendUser()
77
    {
78
        if ($this->hasActiveBackendUser() === false) {
79
            throw new LogicException('be-user is not initialized - initialize with BE-user with method \'initializeBackendUser\'');
80
        }
81
        return $GLOBALS['BE_USER'];
82
    }
83
84
    /**
85
     * Checks if a frontend user is logged in and the session is active.
86
     *
87
     * @return bool
88
     */
89
    public function hasActiveFrontendUser()
90
    {
91
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
92
            $GLOBALS['TSFE']->fe_user instanceof FrontendUserAuthentication &&
93
            isset($GLOBALS['TSFE']->fe_user->user['uid']);
94
    }
95
96
    /**
97
     * @return FrontendUserAuthentication
98
     * @throws LogicException
99
     */
100
    public function getFrontendUser()
101
    {
102
        if ($this->hasActiveFrontendUser() === false) {
103
            throw new LogicException('fe-user is not initialized');
104
        }
105
        return $GLOBALS['TSFE']->fe_user;
106
    }
107
108
    /**
109
     * enable the frontend-rendering
110
     *
111
     * @param integer $pageId
112
     * @param integer $type
113
     *
114
     * @return void
115
     */
116
    public function initializeFrontendRendering($pageId = 0, $type = 0)
117
    {
118
        if ($this->isFrontendInitialized()) {
119
            // FE is already initialized - this can happen when we use/call internal REST-endpoints inside of a normal TYPO3-page
120
            $this->isFrontendRenderingInitialized = true;
121
        }
122
        if ($this->isFrontendRenderingInitialized) {
123
            return;
124
        }
125
126
        $this->getTypoScriptFrontendController($pageId, $type);
127
128
        $this->isFrontendRenderingInitialized = true;
129
    }
130
131
    /**
132
     * Checks if the frontend is initialized.
133
     *
134
     * @return bool
135
     */
136
    protected function isFrontendInitialized()
137
    {
138
        return ($GLOBALS['TSFE'] ?? null) instanceof TypoScriptFrontendController &&
139
            $GLOBALS['TSFE']->tmpl instanceof TemplateService;
140
    }
141
142
    /**
143
     * @param integer $pageId
144
     * @param integer $type
145
     * @return TypoScriptFrontendController
146
     */
147
    private function getTypoScriptFrontendController($pageId = 0, $type = 0)
148
    {
149
        if ($this->isFrontendInitialized()) {
150
            return $GLOBALS['TSFE'];
151
        }
152
153
        $context = GeneralUtility::makeInstance(Context::class);
154
        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
155
        $site = $siteFinder->getSiteByPageId($pageId);
156
        $pageArguments = new PageArguments($pageId, $type, [], [], []);
157
        $GLOBALS['TSFE'] = GeneralUtility::makeInstance(
158
            TypoScriptFrontendController::class,
159
            $context,
160
            $site,
161
            $site->getDefaultLanguage(),
162
            $pageArguments
163
        );
164
        $GLOBALS['TSFE']->sys_page = GeneralUtility::makeInstance(PageRepository::class, $context);
165
        $GLOBALS['TSFE']->tmpl = GeneralUtility::makeInstance(TemplateService::class);
166
167
        return $GLOBALS['TSFE'];
168
    }
169
}
170