02d39081828f2a8c1442aeabfb967a44ba5d3289
[TYPO3CMS/Extensions/sr_feuser_register.git] / Classes / Hooks / UsergroupHooks.php
1 <?php
2 namespace SJBR\SrFeuserRegister\Hooks;
3
4 /*
5 * Copyright notice
6 *
7 * (c) 2008-2011 Franz Holzinger <franz@ttproducts.de>
8 * (c) 2012-2017 Stanislas Rolland <typo3(arobas)sjbr.ca>
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 2 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 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 */
25
26 use TYPO3\CMS\Core\Utility\GeneralUtility;
27 use TYPO3\CMS\Core\Utility\MathUtility;
28
29 /**
30 * Hooks for the usergroup field
31 */
32 class UsergroupHooks
33 {
34 /**
35 * Modify the form fields configuration depending on the $cmdKey
36 *
37 * @param array $conf: the configuration array
38 * @param string $cmdKey: the command key
39 * @return void
40 */
41 public function modifyConf(array &$conf, $cmdKey)
42 {
43 // Add usergroup to the list of fields if the user is allowed to select user groups
44 // Except when only updating password
45 if ($cmdKey !== 'password') {
46 if ($conf[$cmdKey . '.']['allowUserGroupSelection']) {
47 $conf[$cmdKey . '.']['fields'] = implode(',', array_unique(GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['fields'] . ',usergroup', true)));
48 } else {
49 // Remove usergroup from the list of fields and required fields if the user is not allowed to select user groups
50 $conf[$cmdKey . '.']['fields'] = implode(',', array_diff(GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['fields'], true), array('usergroup')));
51 $conf[$cmdKey . '.']['required'] = implode(',', array_diff(GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['required'], true), array('usergroup')));
52 }
53 }
54 }
55
56 /**
57 * Get the array of user groups reserved for control of the registration process
58 *
59 * @param array $conf: the plugin configuration
60 * @return array the reserved user groups
61 */
62 public function getReservedValues(array $conf)
63 {
64 $reservedValues = array_merge(
65 GeneralUtility::trimExplode(',', $conf['create.']['overrideValues.']['usergroup'], true),
66 GeneralUtility::trimExplode(',', $conf['invite.']['overrideValues.']['usergroup'], true),
67 GeneralUtility::trimExplode(',', $conf['setfixed.']['APPROVE.']['usergroup'], true),
68 GeneralUtility::trimExplode(',', $conf['setfixed.']['ACCEPT.']['usergroup'], true)
69 );
70 return array_unique($reservedValues);
71 }
72
73 /**
74 * Remove reserved user groups from the usergroup field of an array
75 *
76 * @param array $row: array
77 * @return void
78 */
79 public function removeReservedValues(array &$row, array $conf)
80 {
81 if (isset($row['usergroup'])) {
82 $reservedValues = $this->getReservedValues($conf);
83 if (is_array($row['usergroup'])) {
84 $userGroupArray = $row['usergroup'];
85 $bUseArray = true;
86 } else {
87 $userGroupArray = explode(',', $row['usergroup']);
88 $bUseArray = false;
89 }
90 $userGroupArray = array_diff($userGroupArray, $reservedValues);
91 if ($bUseArray) {
92 $row['usergroup'] = $userGroupArray;
93 } else {
94 $row['usergroup'] = implode(',', $userGroupArray);
95 }
96 }
97 }
98
99 public function removeInvalidValues(array $conf, $cmdKey, array &$row)
100 {
101 if (isset($row['usergroup']) && $conf[$cmdKey . '.']['allowUserGroupSelection']) {
102 // Todo
103 } else {
104 // The setting of the usergroups is allowed
105 $row['usergroup'] = '';
106 }
107 }
108
109 /**
110 * Processes data before entering the database
111 *
112 * @return void
113 */
114 public function parseOutgoingData($theTable, $fieldname, $foreignTable, $cmdKey, $pid, array $conf, array $dataArray, array $origArray, array &$parsedArray)
115 {
116 $valuesArray = array();
117 if (isset($origArray[$fieldname]) && is_array($origArray[$fieldname])) {
118 $valuesArray = $origArray[$fieldname];
119 if ($conf[$cmdKey . '.']['keepUnselectableUserGroups']) {
120 // Get usergroups that are not selectable
121 if (class_exists('TYPO3\\CMS\\Core\\Database\\ConnectionPool')) {
122 $queryBuilder = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\ConnectionPool::class)
123 ->getQueryBuilderForTable($foreignTable)
124 ->select('uid')
125 ->from($foreignTable);
126 $this->getAllowedWhereClause($foreignTable, $pid, $conf, $cmdKey, false, $queryBuilder);
127 $query = $queryBuilder
128 ->execute();
129 $rowArray = [];
130 while ($row = $query->fetch()) {
131 $rowArray['uid'] = $row;
132 }
133 } else {
134 // TYPO3 CMS 7 LTS
135 $whereClause = $this->getAllowedWhereClause($foreignTable, $pid, $conf, $cmdKey, false);
136 $rowArray = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('uid', $foreignTable, $whereClause, '', '', '', 'uid');
137 }
138 if (!empty($rowArray) && is_array($rowArray)) {
139 $keepValues = array_keys($rowArray);
140 }
141 } else {
142 $keepValues = $this->getReservedValues($conf);
143 }
144 $valuesArray = array_intersect($valuesArray, $keepValues);
145 }
146 if (isset($dataArray[$fieldname]) && is_array($dataArray[$fieldname])) {
147 $dataArray[$fieldname] = array_unique(array_merge($dataArray[$fieldname], $valuesArray));
148 $parsedArray[$fieldname] = $dataArray[$fieldname];
149 }
150 }
151
152 /**
153 * Contruct a where clause to select the user groups that are allowed to be kept
154 *
155 * @return string the where clause
156 */
157 public function getAllowedWhereClause($theTable, $pid, array $conf, $cmdKey, $bAllow = true, $queryBuilder = null)
158 {
159 if ($queryBuilder === null) {
160 // TYPO3 CMS 7 LTS
161 return $this->getCompatibleAllowedWhereClause($theTable, $pid, $conf, $cmdKey, $bAllow);
162 }
163
164 $pidArray = GeneralUtility::trimExplode(',', $conf['userGroupsPidList'], true);
165 if (empty($pidArray)) {
166 $pidArray = GeneralUtility::trimExplode(',', $pid, true);
167 }
168 if (!empty($pidArray)) {
169 $pidArray = array_map('intval', $pidArray);
170 if (empty($queryBuilder->getQueryPart('where'))) {
171 $queryBuilder->where($queryBuilder->expr()->in('pid', $pidArray));
172 } else {
173 $queryBuilder->andWhere($queryBuilder->expr()->in('pid', $pidArray));
174 }
175 }
176
177 $allowedUserGroupArray = array();
178 $allowedSubgroupArray = array();
179 $deniedUserGroupArray = array();
180 $this->getAllowedValues($conf, $cmdKey, $allowedUserGroupArray, $allowedSubgroupArray, $deniedUserGroupArray);
181 if ($allowedUserGroupArray['0'] !== 'ALL') {
182 if ($bAllow) {
183 $allowedUserGroupExpression = $queryBuilder->expr()->in('uid', $allowedUserGroupArray);
184 } else {
185 $allowedUserGroupExpression = $queryBuilder->expr()->notIn('uid', $allowedUserGroupArray);
186 }
187 }
188 if (count($allowedSubgroupArray)) {
189 if ($bAllow) {
190 $allowedSubgroupExpression = $queryBuilder->expr()->in('subgroup', $allowedSubgroupArray);
191 } else {
192 $allowedSubgroupExpression = $queryBuilder->expr()->notIn('subgroup', $allowedSubgroupArray);
193 }
194 }
195 if ($allowedUserGroupExpression && $allowedSubgroupExpression) {
196 if ($bAllow) {
197 $allowedExpression = $queryBuilder->expr()->orX($allowedUserGroupExpression, $allowedSubgroupExpression);
198 } else {
199 $allowedExpression = $queryBuilder->expr()->andX($allowedUserGroupExpression, $allowedSubgroupExpression);
200 }
201 } else {
202 if ($allowedUserGroupExpression) {
203 $allowedExpression = $allowedUserGroupExpression;
204 }
205 if ($allowedSubgroupExpression) {
206 $allowedExpression = $allowedSubgroupExpression;
207 }
208 }
209 $deniedUserGroupArray = array_merge($deniedUserGroupArray, $this->getReservedValues($conf));
210 if (count($deniedUserGroupArray)) {
211 if ($bAllow) {
212 $deniedExpression = $queryBuilder->expr()->notIn('uid', $deniedUserGroupArray);
213 } else {
214 $deniedExpression = $queryBuilder->expr()->in('uid', $deniedUserGroupArray);
215 }
216 }
217 if ($allowedExpression && $deniedExpression) {
218 if ($bAllow) {
219 $expression = $queryBuilder->expr()->andX($allowedExpression, $deniedExpression);
220 } else {
221 $expression = $queryBuilder->expr()->orX($allowedExpression, $deniedExpression);
222 }
223 } else {
224 if ($allowedExpression) {
225 $expression = $allowedExpression;
226 }
227 if ($deniedExpression) {
228 $expression = $deniedExpression;
229 }
230 }
231 if ($expression) {
232 if (empty($queryBuilder->getQueryPart('where'))) {
233 $queryBuilder->where($expression);
234 } else {
235 $queryBuilder->andWhere($expression);
236 }
237 }
238 return '';
239 }
240
241 /**
242 * TYPO3 CMS 7 LTS
243 *
244 * Contruct a where clause to select the user groups that are allowed to be kept
245 *
246 * @return string the where clause
247 */
248 protected function getCompatibleAllowedWhereClause($theTable, $pid, array $conf, $cmdKey, $bAllow = true)
249 {
250 $whereClause = '';
251 $subgroupWhereClauseArray = array();
252 $pidArray = array();
253 $tmpArray = GeneralUtility::trimExplode(',', $conf['userGroupsPidList'], true);
254 if (count($tmpArray)) {
255 foreach ($tmpArray as $value) {
256 $valueIsInt = MathUtility::canBeInterpretedAsInteger($value);
257 if ($valueIsInt) {
258 $pidArray[] = (int) $value;
259 }
260 }
261 }
262 if (count($pidArray) > 0) {
263 $whereClause = ' pid IN (\'' . implode('\',\'', $pidArray) . '\') ';
264 } else {
265 $whereClause = ' pid IN (\'' . implode('\',\'', GeneralUtility::trimExplode(',', $pid, true)) . '\') ';
266 }
267
268 $whereClausePart2 = '';
269 $whereClausePart2Array = array();
270
271 $allowedUserGroupArray = array();
272 $allowedSubgroupArray = array();
273 $deniedUserGroupArray = array();
274 $this->getAllowedValues($conf, $cmdKey, $allowedUserGroupArray, $allowedSubgroupArray, $deniedUserGroupArray);
275 if ($allowedUserGroupArray['0'] !== 'ALL') {
276 $uidArray = $GLOBALS['TYPO3_DB']->fullQuoteArray($allowedUserGroupArray, $theTable);
277 $subgroupWhereClauseArray[] = 'uid ' . ($bAllow ? 'IN' : 'NOT IN') . ' (' . implode(',', $uidArray) . ')';
278 }
279 if (count($allowedSubgroupArray)) {
280 $subgroupArray = $GLOBALS['TYPO3_DB']->fullQuoteArray($allowedSubgroupArray, $theTable);
281 $subgroupWhereClauseArray[] = 'subgroup ' . ($bAllow ? 'IN' : 'NOT IN') . ' (' . implode(',', $subgroupArray) . ')';
282 }
283 if (count($subgroupWhereClauseArray)) {
284 $subgroupWhereClause .= implode(' ' . ($bAllow ? 'OR' : 'AND') . ' ', $subgroupWhereClauseArray);
285 $whereClausePart2Array[] = '( ' . $subgroupWhereClause . ' )';
286 }
287 $deniedUserGroupArray = array_merge($deniedUserGroupArray, $this->getReservedValues($conf));
288 if (count($deniedUserGroupArray)) {
289 $uidArray = $GLOBALS['TYPO3_DB']->fullQuoteArray($deniedUserGroupArray, $theTable);
290 $whereClausePart2Array[] = 'uid ' . ($bAllow ? 'NOT IN' : 'IN') . ' (' . implode(',', $uidArray) . ')';
291 }
292 if (count($whereClausePart2Array)) {
293 $whereClausePart2 = implode(' ' . ($bAllow ? 'AND' : 'OR') . ' ', $whereClausePart2Array);
294 $whereClause .= ' AND (' . $whereClausePart2 . ')';
295 }
296 return $whereClause;
297 }
298
299 /**
300 * Get the allowed values for user groups
301 *
302 * @param array $conf: the configuration array
303 * @param string $cmdKey: the command key
304 * @return void
305 */
306 public function getAllowedValues(array $conf, $cmdKey, array &$allowedUserGroupArray, array &$allowedSubgroupArray, array &$deniedUserGroupArray)
307 {
308 $allowedUserGroupArray = GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['allowedUserGroups'], true);
309 if ($allowedUserGroupArray['0'] !== 'ALL') {
310 $allowedUserGroupArray = array_map('intval', $allowedUserGroupArray);
311 }
312 $allowedSubgroupArray = array_map('intval', GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['allowedSubgroups'], true));
313 $deniedUserGroupArray = array_map('intval', GeneralUtility::trimExplode(',', $conf[$cmdKey . '.']['deniedUserGroups'], true));
314 }
315
316 /**
317 * Restrict the input values array to allowed values
318 *
319 * @param array $values: input values array
320 * @param array $conf: the configuration array
321 * @param string $cmdKey: the command key
322 * @return void
323 */
324 public function restrictToSelectableValues(array $values, array $conf, $cmdKey)
325 {
326 $restrictedValues = $values;
327 $reservedValues = $this->getReservedValues($conf);
328 $allowedUserGroupArray = array();
329 $allowedSubgroupArray = array();
330 $deniedUserGroupArray = array();
331 $this->getAllowedValues($conf, $cmdKey, $allowedUserGroupArray, $allowedSubgroupArray, $deniedUserGroupArray);
332 if (!empty($allowedUserGroupArray) && $allowedUserGroupArray['0'] !== 'ALL') {
333 $restrictedValues = array_intersect($restrictedValues, $allowedUserGroupArray);
334 }
335 if (!empty($allowedSubgroupArray)) {
336 $restrictedValues = array_intersect($restrictedValues, $allowedSubgroupArray);
337 }
338 $restrictedValues = array_diff($restrictedValues, $deniedUserGroupArray);
339 $restrictedValues = array_diff($restrictedValues, $reservedValues);
340 return $restrictedValues;
341 }
342 }