1 | <?php |
||
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 | 49 | public function __construct($accessRootline = null) |
|
90 | { |
||
91 | 49 | if (!is_null($accessRootline)) { |
|
92 | 48 | $rawRootlineElements = explode(self::ELEMENT_DELIMITER, |
|
93 | 48 | $accessRootline); |
|
94 | 48 | foreach ($rawRootlineElements as $rawRootlineElement) { |
|
95 | try { |
||
96 | 48 | $this->push(GeneralUtility::makeInstance(RootlineElement::class, $rawRootlineElement)); |
|
97 | } catch (RootlineElementFormatException $e) { |
||
98 | // just ignore the faulty element for now, might log this later |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | 49 | } |
|
103 | |||
104 | /** |
||
105 | * Adds an Access Rootline Element to the end of the rootline. |
||
106 | * |
||
107 | * @param RootlineElement $rootlineElement Element to add. |
||
108 | */ |
||
109 | 49 | public function push(RootlineElement $rootlineElement) |
|
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 | 39 | public static function getAccessRootlineByPageId( |
|
140 | $pageId, |
||
141 | $mountPointParameter = '' |
||
142 | ) { |
||
143 | 39 | $accessRootline = GeneralUtility::makeInstance(Rootline::class); |
|
144 | |||
145 | 39 | $pageSelector = GeneralUtility::makeInstance(PageRepository::class); |
|
146 | 39 | $pageSelector->init(false); |
|
147 | 39 | $rootline = $pageSelector->getRootLine($pageId, $mountPointParameter); |
|
148 | 39 | $rootline = array_reverse($rootline); |
|
149 | // parent pages |
||
150 | 39 | foreach ($rootline as $pageRecord) { |
|
151 | 39 | if ($pageRecord['fe_group'] |
|
152 | 2 | && $pageRecord['extendToSubpages'] |
|
153 | && $pageRecord['uid'] != $pageId |
||
154 | ) { |
||
155 | $accessRootline->push(GeneralUtility::makeInstance( |
||
156 | RootlineElement::class, |
||
157 | $pageRecord['uid'] . RootlineElement::PAGE_ID_GROUP_DELIMITER . $pageRecord['fe_group'] |
||
158 | )); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | // current page |
||
163 | 39 | $currentPageRecord = $pageSelector->getPage($pageId); |
|
164 | 39 | 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 | 39 | 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 | 46 | public function __toString() |
|
180 | { |
||
181 | 46 | $stringElements = []; |
|
182 | |||
183 | 46 | foreach ($this->rootlineElements as $rootlineElement) { |
|
184 | 10 | $stringElements[] = (string)$rootlineElement; |
|
185 | } |
||
186 | |||
187 | 46 | 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 | 48 | public function getGroups() |
|
196 | { |
||
197 | 48 | $groups = []; |
|
198 | |||
199 | 48 | foreach ($this->rootlineElements as $rootlineElement) { |
|
200 | 12 | $rootlineElementGroups = $rootlineElement->getGroups(); |
|
201 | 12 | $groups = array_merge($groups, $rootlineElementGroups); |
|
202 | } |
||
203 | |||
204 | 48 | $groups = $this->cleanGroupArray($groups); |
|
205 | |||
206 | 48 | 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 | 52 | public static function cleanGroupArray(array $groups) |
|
223 | } |
||
224 |