Passed
Push — master ( 696b0c...adcfee )
by Torben
02:15
created

PageAccessService::injectQueryGenerator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace Derhansen\FeChangePwd\Service;
3
4
/*
5
 * This file is part of the Extension "fe_change_pwd" for TYPO3 CMS.
6
 *
7
 * For the full copyright and license information, please read the
8
 * LICENSE.txt file that was distributed with this source code.
9
 */
10
11
use TYPO3\CMS\Core\Database\QueryGenerator;
12
use TYPO3\CMS\Core\Utility\GeneralUtility;
13
14
/**
15
 * Class PageAccessService
16
 */
17
class PageAccessService
18
{
19
    /**
20
     * @var SettingsService
21
     */
22
    protected $settingsService = null;
23
24
    /**
25
     * @var QueryGenerator
26
     */
27
    protected $queryGenerator = null;
28
29
    /**
30
     * @param SettingsService $settingsService
31
     */
32
    public function injectSettingsService(\Derhansen\FeChangePwd\Service\SettingsService $settingsService)
33
    {
34
        $this->settingsService = $settingsService;
35
    }
36
37
    /**
38
     * @param QueryGenerator $queryGenerator
39
     */
40
    public function injectQueryGenerator(QueryGenerator $queryGenerator)
41
    {
42
        $this->queryGenerator = $queryGenerator;
43
    }
44
45
    /**
46
     * Returns the redirect mode
47
     *
48
     * @return string
49
     */
50
    public function getRedirectMode()
51
    {
52
        $settings = $this->settingsService->getSettings();
53
        if ((bool)$settings['redirect']['allAccessProtectedPages']) {
54
            $redirectMode = 'allAccessProtectedPages';
55
        } elseif (isset($settings['redirect']['includePageUids']) && $settings['redirect']['includePageUids'] !== '') {
56
            $redirectMode = 'includePageUids';
57
        } else {
58
            $redirectMode = '';
59
        }
60
        return $redirectMode;
61
    }
62
63
    /**
64
     * Returns the configured redirect PID
65
     *
66
     * @return mixed
67
     */
68
    public function getRedirectPid()
69
    {
70
        $settings = $this->settingsService->getSettings();
71
        if (!isset($settings['changePasswordPid']) || (int)$settings['changePasswordPid'] === 0) {
72
            // @todo throw exception
73
        }
74
        return (int)$settings['changePasswordPid'];
75
    }
76
77
    /**
78
     * Returns, if the given page uid is configured as included for redirects
79
     *
80
     * @param int $pageUid
81
     * @return bool
82
     */
83
    public function isIncludePage($pageUid)
84
    {
85
        $settings = $this->settingsService->getSettings();
86
        if (isset($settings['redirect']['includePageUids']) && $settings['redirect']['includePageUids'] !== '') {
87
            $includePids = $this->extendPidListByChildren(
88
                $settings['redirect']['includePageUids'],
89
                $settings['redirect']['includePageUidsRecursionLevel']
90
            );
91
            $includePids = GeneralUtility::intExplode(',', $includePids, true);
92
        } else {
93
            $includePids = [];
94
        }
95
        return in_array($pageUid, $includePids, true);
96
    }
97
98
    /**
99
     * Returns, if the given page uid is configured as excluded from redirects
100
     *
101
     * @param int $pageUid
102
     * @return bool
103
     */
104
    public function isExcludePage($pageUid)
105
    {
106
        $settings = $this->settingsService->getSettings();
107
        if (isset($settings['redirect']['excludePageUids']) && $settings['redirect']['excludePageUids'] !== '') {
108
            $excludePids = $this->extendPidListByChildren(
109
                $settings['redirect']['excludePageUids'],
110
                $settings['redirect']['excludePageUidsRecursionLevel']
111
            );
112
            $excludePids = GeneralUtility::intExplode(',', $excludePids, true);
113
        } else {
114
            $excludePids = [];
115
        }
116
        // Always add the changePasswordPid as exclude PID
117
        $excludePids[] = (int)$settings['changePasswordPid'];
118
        return in_array($pageUid, $excludePids, true);
119
    }
120
121
    /**
122
     * Returns, if the there is an access protected page in the rootline respecting 'extendToSubpages' setting
123
     *
124
     * @param array $rootline
125
     * @return bool
126
     */
127
    public function isAccessProtectedPageInRootline($rootline)
128
    {
129
        $isAccessProtected = false;
130
        $loop = 0;
131
        foreach ($rootline as $rootlinePage) {
132
            $isPublic = ($rootlinePage['fe_group'] === '' || $rootlinePage['fe_group'] === '0');
133
            $extendToSubpages = (bool)$rootlinePage['extendToSubpages'];
134
            if (!$isPublic || (!$isPublic && $extendToSubpages && $loop >= 1)) {
135
                $isAccessProtected = true;
136
                break;
137
            }
138
            $loop++;
139
        }
140
        return $isAccessProtected;
141
    }
142
143
    /**
144
     * Find all ids from given ids and level
145
     *
146
     * @param string $pidList comma separated list of ids
147
     * @param int $recursive recursive levels
148
     * @return string comma separated list of ids
149
     */
150
    protected function extendPidListByChildren($pidList = '', $recursive = 0)
151
    {
152
        $recursive = (int)$recursive;
153
        if ($recursive <= 0) {
154
            return $pidList;
155
        }
156
157
        $recursiveStoragePids = $pidList;
158
        $storagePids = GeneralUtility::intExplode(',', $pidList);
159
        foreach ($storagePids as $startPid) {
160
            if ($startPid >= 0) {
161
                $pids = $this->queryGenerator->getTreeList($startPid, $recursive, 0, 1);
0 ignored issues
show
Bug introduced by
$startPid of type string is incompatible with the type integer expected by parameter $id of TYPO3\CMS\Core\Database\...enerator::getTreeList(). ( Ignorable by Annotation )

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

161
                $pids = $this->queryGenerator->getTreeList(/** @scrutinizer ignore-type */ $startPid, $recursive, 0, 1);
Loading history...
162
                if (strlen($pids) > 0) {
163
                    $recursiveStoragePids .= ',' . $pids;
164
                }
165
            }
166
        }
167
        return GeneralUtility::uniqueList($recursiveStoragePids);
168
    }
169
}
170