PluginProbe
User Access Manager / 2.3.17
User Access Manager v2.3.17
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
← All changes | src/UserGroup/AbstractUserGroup.php +101 -79 trunk2.3.17 View file →
@@ -7,9 +7,11 @@
7 7 use Exception;
8 8 use UserAccessManager\Config\MainConfig;
9 9 use UserAccessManager\Database\Database;
10 10 use UserAccessManager\Object\ObjectHandler;
11 -use UserAccessManager\ObjectMembership\Exception\MissingObjectMembershipHandlerException;
11 +use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
12 +use UserAccessManager\Util\Util;
13 +use UserAccessManager\Wrapper\Php;
12 14 use UserAccessManager\Wrapper\Wordpress;
13 15
14 16 abstract class AbstractUserGroup
15 17 {
@@ -29,11 +31,13 @@
29 31 /**
30 32 * @throws UserGroupTypeException
31 33 */
32 34 public function __construct(
35 + protected Php $php,
33 36 protected Wordpress $wordpress,
34 37 protected Database $database,
35 38 protected MainConfig $config,
39 + protected Util $util,
36 40 protected ObjectHandler $objectHandler,
37 41 protected AssignedObjectsLoader $assignedObjectsLoader,
38 42 protected int|string|null $id = null
39 43 ) {
@@ -119,24 +123,15 @@
119 123 $this->resetObjects();
120 124 }
121 125
122 126 /**
123 - * Returns the general object type of an assignable object type, null if the object type can't be assigned.
124 - */
125 - private function getAssignableGeneralObjectType(string $objectType): ?string
126 - {
127 - $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
128 -
129 - return ($generalObjectType !== null && $this->objectHandler->isValidObjectType($objectType) === true) ?
130 - $generalObjectType : null;
131 - }
132 -
133 - /**
134 127 * @throws Exception
135 128 */
136 129 public function delete(): bool
137 130 {
138 - foreach ($this->objectHandler->getAllObjectTypes() as $objectType) {
131 + $allObjectTypes = $this->objectHandler->getAllObjectTypes();
132 +
133 + foreach ($allObjectTypes as $objectType) {
139 134 $this->removeObject($objectType);
140 135 }
141 136
142 137 return true;
@@ -146,15 +141,17 @@
146 141 * @throws Exception
147 142 */
148 143 public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool
149 144 {
150 - $generalObjectType = $this->getAssignableGeneralObjectType($objectType);
145 + $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
151 146
152 - if ($generalObjectType === null) {
147 + if ($generalObjectType === null
148 + || $this->objectHandler->isValidObjectType($objectType) === false
149 + ) {
153 150 return false;
154 151 }
155 152
156 - $success = $this->database->replace(
153 + $return = $this->database->replace(
157 154 $this->database->getUserGroupToObjectTable(),
158 155 [
159 156 'group_id' => $this->id,
160 157 'group_type' => $this->type,
@@ -163,16 +160,25 @@
163 160 'object_type' => $objectType,
164 161 'from_date' => $fromDate,
165 162 'to_date' => $toDate
166 163 ],
167 - ['%s', '%s', '%s', '%s', '%s', '%s', '%s']
168 - ) !== false;
164 + [
165 + '%s',
166 + '%s',
167 + '%s',
168 + '%s',
169 + '%s',
170 + '%s',
171 + '%s'
172 + ]
173 + );
169 174
170 - if ($success === true) {
175 + if ($return !== false) {
171 176 $this->resetObjectsAfterAssignmentChange();
177 + return true;
172 178 }
173 179
174 - return $success;
180 + return false;
175 181 }
176 182
177 183 /**
178 184 * @throws Exception
@@ -178,15 +184,17 @@
178 184 * @throws Exception
179 185 */
180 186 public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool
181 187 {
182 - $generalObjectType = $this->getAssignableGeneralObjectType($objectType);
188 + $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
183 189
184 - if ($generalObjectType === null) {
190 + if ($generalObjectType === null
191 + || $this->objectHandler->isValidObjectType($objectType) === false
192 + ) {
185 193 return false;
186 194 }
187 195
188 - $objectTypeQuery = " AND `object_type` = '%s' ";
196 + $objectTypeQuery = " AND object_type = '%s' ";
189 197 $values = [
190 198 $this->id,
191 199 $this->type,
192 200 $objectType
@@ -192,23 +200,24 @@
192 200 $objectType
193 201 ];
194 202
195 203 if ($ignoreGeneralType === false) {
196 - $objectTypeQuery = " AND (`object_type` = '%s' OR `general_object_type` = '%s') ";
204 + $objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') ";
197 205 $values[] = $generalObjectType;
198 206 }
199 207
200 - $query = "DELETE FROM `{$this->database->getUserGroupToObjectTable()}`
201 - WHERE `group_id` = %d
202 - AND `group_type` = '%s'
208 + $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
209 + WHERE group_id = %d
210 + AND group_type = '%s'
203 211 $objectTypeQuery";
204 212
205 213 if ($objectId !== null) {
206 - $query .= ' AND `object_id` = %d';
214 + $query .= ' AND object_id = %d';
207 215 $values[] = $objectId;
208 216 }
209 217
210 - $success = $this->database->query($this->database->prepare($query, $values)) !== false;
218 + $query = $this->database->prepare($query, $values);
219 + $success = ($this->database->query($query) !== false);
211 220
212 221 if ($success === true) {
213 222 $this->resetObjectsAfterAssignmentChange();
214 223 }
@@ -220,14 +229,18 @@
220 229 * @return AssignmentInformation[]
221 230 */
222 231 public function getAssignedObjects(string $objectType): array
223 232 {
224 - return $this->assignedObjects[$objectType] ??= $this->assignedObjectsLoader->getAssignedObjects(
225 - $this->type,
226 - $this->id,
227 - $objectType,
228 - $this->ignoreDates
229 - );
233 + if (isset($this->assignedObjects[$objectType]) === false) {
234 + $this->assignedObjects[$objectType] = $this->assignedObjectsLoader->getAssignedObjects(
235 + $this->type,
236 + $this->id,
237 + $objectType,
238 + $this->ignoreDates
239 + );
240 + }
241 +
242 + return $this->assignedObjects[$objectType];
230 243 }
231 244
232 245 /**
233 246 * @throws Exception
@@ -251,43 +264,36 @@
251 264 {
252 265 return $this->removeObject($objectType, '', true);
253 266 }
254 267
255 - /**
256 - * @return array<string, array{0: int|null, 1: int|null}> Time ranges keyed by the object type.
257 - */
258 - public function getDefaultGroupForObjectTypes(): array
268 + public function getDefaultGroupForObjectTypes(): ?array
259 269 {
260 - return $this->defaultTypes ??= $this->loadDefaultGroupForObjectTypes();
261 - }
270 + if ($this->defaultTypes === null) {
271 + $this->defaultTypes = [];
262 272
263 - /**
264 - * @return array<string, array{0: int|null, 1: int|null}>
265 - */
266 - private function loadDefaultGroupForObjectTypes(): array
267 - {
268 - $query = $this->database->prepare(
269 - "SELECT `object_type` AS `objectType`, `from_date` AS `fromDate`, `to_date` AS `toDate`
270 - FROM `{$this->database->getUserGroupToObjectTable()}`
271 - WHERE `group_id` = '%s'
272 - AND `group_type` = '%s'
273 - AND `object_id` = ''",
274 - [
273 + $query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate
274 + FROM {$this->database->getUserGroupToObjectTable()}
275 + WHERE group_id = '%s'
276 + AND group_type = '%s'
277 + AND object_id = ''";
278 +
279 + $parameters = [
275 280 $this->id,
276 281 $this->type
277 - ]
278 - );
282 + ];
279 283
280 - $defaultTypes = [];
284 + $query = $this->database->prepare($query, $parameters);
285 + $results = (array) $this->database->getResults($query);
281 286
282 - foreach ((array) $this->database->getResults($query) as $result) {
283 - $defaultTypes[$result->objectType] = [
284 - ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
285 - ($result->toDate !== null) ? strtotime($result->toDate) : null
286 - ];
287 + foreach ($results as $result) {
288 + $this->defaultTypes[$result->objectType] = [
289 + ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
290 + ($result->toDate !== null) ? strtotime($result->toDate) : null
291 + ];
292 + }
287 293 }
288 294
289 - return $defaultTypes;
295 + return $this->defaultTypes;
290 296 }
291 297
292 298 public function isDefaultGroupForObjectType(string $objectType, ?int &$fromTime = null, ?int &$toTime = null): bool
293 299 {
@@ -292,21 +298,22 @@
292 298 public function isDefaultGroupForObjectType(string $objectType, ?int &$fromTime = null, ?int &$toTime = null): bool
293 299 {
294 300 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
295 301
296 - // The reference values have to be reset even when the group is no default group for the object type
302 + // Reset reference values anyway
297 303 $fromTime = null;
298 304 $toTime = null;
299 305
300 - if (isset($defaultGroupForObjectTypes[$objectType]) === false) {
301 - return false;
306 + if (isset($defaultGroupForObjectTypes[$objectType])) {
307 + $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ?
308 + (int) $defaultGroupForObjectTypes[$objectType][0] : null;
309 + $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ?
310 + (int) $defaultGroupForObjectTypes[$objectType][1] : null;
311 +
312 + return true;
302 313 }
303 314
304 - [$fromTimestamp, $toTimestamp] = $defaultGroupForObjectTypes[$objectType];
305 - $fromTime = ($fromTimestamp !== null) ? (int) $fromTimestamp : null;
306 - $toTime = ($toTimestamp !== null) ? (int) $toTimestamp : null;
307 -
308 - return true;
315 + return false;
309 316 }
310 317
311 318 public function isObjectAssignedToGroup(
312 319 string $objectType,
@@ -312,11 +319,17 @@
312 319 string $objectType,
313 320 int|string|null $objectId,
314 321 ?AssignmentInformation &$assignmentInformation = null
315 322 ): bool {
316 - $assignmentInformation = $this->getAssignedObjects($objectType)[$objectId] ?? null;
323 + $assignmentInformation = null;
324 + $assignedObjects = $this->getAssignedObjects($objectType);
317 325
318 - return $assignmentInformation !== null;
326 + if (isset($assignedObjects[$objectId]) === true) {
327 + $assignmentInformation = $assignedObjects[$objectId];
328 + return true;
329 + }
330 +
331 + return false;
319 332 }
320 333
321 334 /**
322 335 * @throws Exception
@@ -337,15 +350,16 @@
337 350 } catch (MissingObjectMembershipHandlerException) {
338 351 $isMember = false;
339 352 }
340 353
341 - $this->objectMembership[$objectType][$objectId] = ($isMember === true) ? $assignmentInformation : false;
354 + $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
355 + $assignmentInformation : false;
342 356 }
343 357
344 - $membership = $this->objectMembership[$objectType][$objectId];
345 - $assignmentInformation = ($membership instanceof AssignmentInformation) ? $membership : null;
358 + $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ?
359 + $this->objectMembership[$objectType][$objectId] : null;
346 360
347 - return $membership !== false;
361 + return ($this->objectMembership[$objectType][$objectId] !== false);
348 362 }
349 363
350 364 /**
351 365 * @throws Exception
@@ -398,9 +412,16 @@
398 412 * @throws Exception
399 413 */
400 414 public function isLockedRecursive(string $objectType, int|string|null $objectId): bool
401 415 {
402 - return count($this->getRecursiveMembershipForObject($objectType, $objectId)) > 0;
416 + /**
417 + * @var AssignmentInformation $assignmentInformation
418 + */
419 + if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
420 + return (count($assignmentInformation->getRecursiveMembership()) > 0);
421 + }
422 +
423 + return false;
403 424 }
404 425
405 426 /**
406 427 * @throws Exception
@@ -408,10 +429,10 @@
408 429 public function getAssignedObjectsByType(string $objectType): array
409 430 {
410 431 if (isset($this->fullObjectMembership[$objectType]) === false) {
411 432 try {
412 - $membershipHandler = $this->objectHandler->getObjectMembershipHandler($objectType);
413 - $this->fullObjectMembership[$objectType] = $membershipHandler->getFullObjects(
433 + $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
434 + $this->fullObjectMembership[$objectType] = $handler->getFullObjects(
414 435 $this,
415 436 $this->config->lockRecursive(),
416 437 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
417 438 );
@@ -429,8 +450,9 @@
429 450 public function getFullRoles(): array
430 451 {
431 452 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
432 453 }
454 +
433 455
434 456 /**
435 457 * @throws Exception
436 458 */