Passed
Push — master ( 5f60a5...391298 )
by Timo
24:49
created

Rootline::push()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 6
cts 12
cp 0.5
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 11
nc 4
nop 1
crap 6
1
<?php
2
namespace ApacheSolrForTypo3\Solr\Access;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2015 Ingo Renner <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use TYPO3\CMS\Core\Utility\GeneralUtility;
28
use TYPO3\CMS\Frontend\Page\PageRepository;
29
30
/**
31
 * "Access Rootline", represents all pages and specifically those setting
32
 * frontend user group access restrictions in a page's rootline.
33
 *
34
 * The access rootline only contains pages which set frontend user access
35
 * restrictions and extend them to sub-pages. The format is as follows:
36
 *
37
 * pageId1:group1,group2/pageId2:group3/c:group1,group4,groupN
38
 *
39
 * The single elements of the access rootline are separated by a slash
40
 * character. All but the last elements represent pages, the last element
41
 * defines the access restrictions applied to the page's content elements
42
 * and records shown on the page.
43
 * Each page element is composed by the page ID of the page setting frontend
44
 * user access restrictions, a colon, and a comma separated list of frontend
45
 * user group IDs restricting access to the page.
46
 * The content access element does not have a page ID, instead it replaces
47
 * the ID by a lower case C.
48
 *
49
 * The groups for page elements are compared using OR, so the user needs to be
50
 * a member of only one of the groups listed for a page. The elements are
51
 * checked combined using AND, so the user must be member of at least one
52
 * group in each page element. However, the groups in the content access
53
 * element are checked using AND. So the user must be member of all the groups
54
 * listed in the content access element to see the document.
55
 *
56
 * An access rootline for a generic record could instead be short like this:
57
 *
58
 * r:group1,group2,groupN
59
 *
60
 * In this case the lower case R tells us that we're dealing with a record
61
 * like tt_news or the like. For records the groups are checked using OR
62
 * instead of using AND as it would be the case with content elements.
63
 *
64
 * @author Ingo Renner <[email protected]>
65
 */
66
class Rootline
67
{
68
69
    /**
70
     * Delimiter for page and content access right elements in the rootline.
71
     *
72
     * @var string
73
     */
74
    const ELEMENT_DELIMITER = '/';
75
76
    /**
77
     * Storage for access rootline elements
78
     *
79
     * @var array
80
     */
81
    protected $rootlineElements = [];
82
83
    /**
84
     * Constructor, turns a string representation of an access rootline into an
85
     * object representation.
86
     *
87
     * @param string $accessRootline Access Rootline String representation.
88
     */
89 70
    public function __construct($accessRootline = null)
90
    {
91 70
        if (!is_null($accessRootline)) {
92 69
            $rawRootlineElements = explode(self::ELEMENT_DELIMITER,
93 69
                $accessRootline);
94 69
            foreach ($rawRootlineElements as $rawRootlineElement) {
95
                try {
96 69
                    $this->push(GeneralUtility::makeInstance(RootlineElement::class, $rawRootlineElement));
97 69
                } catch (RootlineElementFormatException $e) {
98
                    // just ignore the faulty element for now, might log this later
99
                }
100
            }
101
        }
102 70
    }
103
104
    /**
105
     * Adds an Access Rootline Element to the end of the rootline.
106
     *
107
     * @param RootlineElement $rootlineElement Element to add.
108
     */
109 70
    public function push(RootlineElement $rootlineElement)
110
    {
111 70
        $lastElementIndex = max(0, (count($this->rootlineElements) - 1));
112
113 70
        if (!empty($this->rootlineElements[$lastElementIndex])) {
114 1
            if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_CONTENT) {
115
                throw new RootlineElementFormatException(
116
                    'Can not add an element to an Access Rootline whose\' last element is a content type element.',
117
                    1294422132
118
                );
119
            }
120
121 1
            if ($this->rootlineElements[$lastElementIndex]->getType() == RootlineElement::ELEMENT_TYPE_RECORD) {
122
                throw new RootlineElementFormatException(
123
                    'Can not add an element to an Access Rootline whose\' last element is a record type element.',
124
                    1308343423
125
                );
126
            }
127
        }
128
129 70
        $this->rootlineElements[] = $rootlineElement;
130 70
    }
131
132
    /**
133
     * Gets the Access Rootline for a specific page Id.
134
     *
135
     * @param int $pageId The page Id to generate the Access Rootline for.
136
     * @param string $mountPointParameter The mount point parameter for generating the rootline.
137
     * @return \ApacheSolrForTypo3\Solr\Access\Rootline Access Rootline for the given page Id.
138
     */
139 14
    public static function getAccessRootlineByPageId(
140
        $pageId,
141
        $mountPointParameter = ''
142
    ) {
143 14
        $accessRootline = GeneralUtility::makeInstance(Rootline::class);
144
145 14
        $pageSelector = GeneralUtility::makeInstance(PageRepository::class);
146 14
        $pageSelector->init(false);
147 14
        $rootline = $pageSelector->getRootLine($pageId, $mountPointParameter);
148 14
        $rootline = array_reverse($rootline);
149
        // parent pages
150 14
        foreach ($rootline as $pageRecord) {
151 14
            if ($pageRecord['fe_group']
152 14
                && $pageRecord['extendToSubpages']
153 14
                && $pageRecord['uid'] != $pageId
154
            ) {
155
                $accessRootline->push(GeneralUtility::makeInstance(
156
                    RootlineElement::class,
157 14
                    $pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group']
0 ignored issues
show
Bug introduced by
$pageRecord['uid'] . Apa...$pageRecord['fe_group'] of type string is incompatible with the type array<integer,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance(). ( Ignorable by Annotation )

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

157
                    /** @scrutinizer ignore-type */ $pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group']
Loading history...
158
                ));
159
            }
160
        }
161
162
        // current page
163 14
        $currentPageRecord = $pageSelector->getPage($pageId);
164 14
        if ($currentPageRecord['fe_group']) {
165 2
            $accessRootline->push(GeneralUtility::makeInstance(
166 2
                RootlineElement::class,
167 2
                $currentPageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $currentPageRecord['fe_group']
168
            ));
169
        }
170
171 14
        return $accessRootline;
172
    }
173
174
    /**
175
     * Returns the string representation of the access rootline.
176
     *
177
     * @return string String representation of the access rootline.
178
     */
179 67
    public function __toString()
180
    {
181 67
        $stringElements = [];
182
183 67
        foreach ($this->rootlineElements as $rootlineElement) {
184 56
            $stringElements[] = (string)$rootlineElement;
185
        }
186
187 67
        return implode(self::ELEMENT_DELIMITER, $stringElements);
188
    }
189
190
    /**
191
     * Gets a the groups in the Access Rootline.
192
     *
193
     * @return array An array of sorted, unique user group IDs required to access a page.
194
     */
195 69
    public function getGroups()
196
    {
197 69
        $groups = [];
198
199 69
        foreach ($this->rootlineElements as $rootlineElement) {
200 58
            $rootlineElementGroups = $rootlineElement->getGroups();
201 58
            $groups = array_merge($groups, $rootlineElementGroups);
202
        }
203
204 69
        $groups = $this->cleanGroupArray($groups);
205
206 69
        return $groups;
207
    }
208
209
    /**
210
     * Cleans an array of frontend user group IDs. Removes duplicates and sorts
211
     * the array.
212
     *
213
     * @param array $groups An array of frontend user group IDs
214
     * @return array An array of cleaned frontend user group IDs, unique, sorted.
215
     */
216 73
    public static function cleanGroupArray(array $groups)
217
    {
218 73
        $groups = array_unique($groups); // removes duplicates
219 73
        sort($groups, SORT_NUMERIC); // sort
220
221 73
        return $groups;
222
    }
223
}
224