Passed
Pull Request — release-11.2.x (#3528)
by Rafael
09:36
created

Rootline::getGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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\Core\Utility\RootlineUtility;
29
use TYPO3\CMS\Frontend\Page\PageRepository;
30
31
/**
32
 * "Access Rootline", represents all pages and specifically those setting
33
 * frontend user group access restrictions in a page's rootline.
34
 *
35
 * The access rootline only contains pages which set frontend user access
36
 * restrictions and extend them to sub-pages. The format is as follows:
37
 *
38
 * pageId1:group1,group2/pageId2:group3/c:group1,group4,groupN
39
 *
40
 * The single elements of the access rootline are separated by a slash
41
 * character. All but the last elements represent pages, the last element
42
 * defines the access restrictions applied to the page's content elements
43
 * and records shown on the page.
44
 * Each page element is composed by the page ID of the page setting frontend
45
 * user access restrictions, a colon, and a comma separated list of frontend
46
 * user group IDs restricting access to the page.
47
 * The content access element does not have a page ID, instead it replaces
48
 * the ID by a lower case C.
49
 *
50
 * The groups for page elements are compared using OR, so the user needs to be
51
 * a member of only one of the groups listed for a page. The elements are
52
 * checked combined using AND, so the user must be member of at least one
53
 * group in each page element. However, the groups in the content access
54
 * element are checked using AND. So the user must be member of all the groups
55
 * listed in the content access element to see the document.
56
 *
57
 * An access rootline for a generic record could instead be short like this:
58
 *
59
 * r:group1,group2,groupN
60
 *
61
 * In this case the lower case R tells us that we're dealing with a record
62
 * like tt_news or the like. For records the groups are checked using OR
63
 * instead of using AND as it would be the case with content elements.
64
 *
65
 * @author Ingo Renner <[email protected]>
66
 */
67
class Rootline
68
{
69
70
    /**
71
     * Delimiter for page and content access right elements in the rootline.
72
     *
73
     * @var string
74
     */
75
    const ELEMENT_DELIMITER = '/';
76
77
    /**
78
     * Storage for access rootline elements
79
     *
80
     * @var array
81
     */
82
    protected $rootlineElements = [];
83
84
    /**
85
     * Constructor, turns a string representation of an access rootline into an
86
     * object representation.
87
     *
88
     * @param string $accessRootline Access Rootline String representation.
89
     */
90 41
    public function __construct($accessRootline = null)
91
    {
92 41
        if (!is_null($accessRootline)) {
93 40
            $rawRootlineElements = explode(self::ELEMENT_DELIMITER, $accessRootline);
94 40
            foreach ($rawRootlineElements as $rawRootlineElement) {
95
                try {
96 40
                    $this->push(GeneralUtility::makeInstance(RootlineElement::class, /** @scrutinizer ignore-type */ $rawRootlineElement));
97
                } catch (RootlineElementFormatException $e) {
98
                    // just ignore the faulty element for now, might log this later
99
                }
100
            }
101
        }
102 41
    }
103
104
    /**
105
     * Adds an Access Rootline Element to the end of the rootline.
106
     *
107
     * @param RootlineElement $rootlineElement Element to add.
108
     */
109 41
    public function push(RootlineElement $rootlineElement)
110
    {
111 41
        $lastElementIndex = max(0, (count($this->rootlineElements) - 1));
112
113 41
        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 41
        $this->rootlineElements[] = $rootlineElement;
130 41
    }
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 21
    public static function getAccessRootlineByPageId(
140
        $pageId,
141
        $mountPointParameter = ''
142
    ) {
143
        /* @var Rootline $accessRootline */
144 21
        $accessRootline = GeneralUtility::makeInstance(Rootline::class);
145 21
        $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPointParameter);
146
        try {
147 21
            $rootline = $rootlineUtility->get();
148
        } catch (\RuntimeException $e) {
149
            $rootline = [];
150
        }
151 21
        $rootline = array_reverse($rootline);
152
        // parent pages
153 21
        foreach ($rootline as $pageRecord) {
154 21
            if ($pageRecord['fe_group']
155 21
                && $pageRecord['extendToSubpages']
156 21
                && $pageRecord['uid'] != $pageId
157
            ) {
158
                $accessRootline->push(GeneralUtility::makeInstance(
159
                    RootlineElement::class,
160
                    /** @scrutinizer ignore-type */ $pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group']
161
                ));
162
            }
163
        }
164
165
            /** @var  $pageSelector PageRepository */
166 21
        $pageSelector = GeneralUtility::makeInstance(PageRepository::class);
167
168
        // current page
169 21
        $currentPageRecord = $pageSelector->getPage($pageId, true);
170 21
        if ($currentPageRecord['fe_group']) {
171 3
            $accessRootline->push(GeneralUtility::makeInstance(
172
                RootlineElement::class,
173 3
                /** @scrutinizer ignore-type */ $currentPageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $currentPageRecord['fe_group']
174
            ));
175
        }
176
177 21
        return $accessRootline;
178
    }
179
180
    /**
181
     * Returns the string representation of the access rootline.
182
     *
183
     * @return string String representation of the access rootline.
184
     */
185 38
    public function __toString()
186
    {
187 38
        $stringElements = [];
188
189 38
        foreach ($this->rootlineElements as $rootlineElement) {
190 20
            $stringElements[] = (string)$rootlineElement;
191
        }
192
193 38
        return implode(self::ELEMENT_DELIMITER, $stringElements);
194
    }
195
196
    /**
197
     * Gets a the groups in the Access Rootline.
198
     *
199
     * @return array An array of sorted, unique user group IDs required to access a page.
200
     */
201 40
    public function getGroups()
202
    {
203 40
        $groups = [];
204
205 40
        foreach ($this->rootlineElements as $rootlineElement) {
206 22
            $rootlineElementGroups = $rootlineElement->getGroups();
207 22
            $groups = array_merge($groups, $rootlineElementGroups);
208
        }
209
210 40
        $groups = $this->cleanGroupArray($groups);
211
212 40
        return $groups;
213
    }
214
215
    /**
216
     * Cleans an array of frontend user group IDs. Removes duplicates and sorts
217
     * the array.
218
     *
219
     * @param array $groups An array of frontend user group IDs
220
     * @return array An array of cleaned frontend user group IDs, unique, sorted.
221
     */
222 44
    public static function cleanGroupArray(array $groups)
223
    {
224 44
        $groups = array_unique($groups); // removes duplicates
225 44
        sort($groups, SORT_NUMERIC); // sort
226
227 44
        return $groups;
228
    }
229
}
230