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
user-access-manager / src / UserGroup / AbstractUserGroup.php

AbstractUserGroup.php in User Access Manager 2.3.17, at src/UserGroup/AbstractUserGroup.php

480 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\UserGroup;
6
7 use Exception;
8 use UserAccessManager\Config\MainConfig;
9 use UserAccessManager\Database\Database;
10 use UserAccessManager\Object\ObjectHandler;
11 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
12 use UserAccessManager\Util\Util;
13 use UserAccessManager\Wrapper\Php;
14 use UserAccessManager\Wrapper\Wordpress;
15
16 abstract class AbstractUserGroup
17 {
18 public const NONE_ROLE = '_none-role_';
19
20 protected ?string $type = null;
21 protected ?string $name = null;
22 protected ?string $description = null;
23 protected string $readAccess = 'group';
24 protected string $writeAccess = 'group';
25 protected bool $ignoreDates = false;
26 protected array $assignedObjects = [];
27 protected array $objectMembership = [];
28 protected array $fullObjectMembership = [];
29 protected ?array $defaultTypes = null;
30
31 /**
32 * @throws UserGroupTypeException
33 */
34 public function __construct(
35 protected Php $php,
36 protected Wordpress $wordpress,
37 protected Database $database,
38 protected MainConfig $config,
39 protected Util $util,
40 protected ObjectHandler $objectHandler,
41 protected AssignedObjectsLoader $assignedObjectsLoader,
42 protected int|string|null $id = null
43 ) {
44 if ($this->type === null) {
45 throw new UserGroupTypeException('User group type must not null.');
46 }
47 }
48
49 public function getId(): int|string|null
50 {
51 return $this->id;
52 }
53
54 public function getType(): ?string
55 {
56 return $this->type;
57 }
58
59 public function getName(): ?string
60 {
61 return $this->name;
62 }
63
64 public function setName(string $name): void
65 {
66 $this->name = $name;
67 }
68
69 public function getDescription(): ?string
70 {
71 return $this->description;
72 }
73
74 public function setDescription(string $description): void
75 {
76 $this->description = $description;
77 }
78
79 public function getReadAccess(): string
80 {
81 return $this->readAccess;
82 }
83
84 public function setReadAccess(string $readAccess): void
85 {
86 $this->readAccess = $readAccess;
87 }
88
89 public function getWriteAccess(): string
90 {
91 return $this->writeAccess;
92 }
93
94 public function setWriteAccess(string $writeAccess): void
95 {
96 $this->writeAccess = $writeAccess;
97 }
98
99 public function setIgnoreDates(bool $ignoreDates): void
100 {
101 if ($this->ignoreDates !== $ignoreDates) {
102 $this->resetObjects();
103 }
104
105 $this->ignoreDates = $ignoreDates;
106 }
107
108 public function getIgnoreDates(): bool
109 {
110 return $this->ignoreDates;
111 }
112
113 protected function resetObjects(): void
114 {
115 $this->assignedObjects = [];
116 $this->objectMembership = [];
117 $this->fullObjectMembership = [];
118 }
119
120 private function resetObjectsAfterAssignmentChange(): void
121 {
122 $this->assignedObjectsLoader->flush();
123 $this->resetObjects();
124 }
125
126 /**
127 * @throws Exception
128 */
129 public function delete(): bool
130 {
131 $allObjectTypes = $this->objectHandler->getAllObjectTypes();
132
133 foreach ($allObjectTypes as $objectType) {
134 $this->removeObject($objectType);
135 }
136
137 return true;
138 }
139
140 /**
141 * @throws Exception
142 */
143 public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool
144 {
145 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
146
147 if ($generalObjectType === null
148 || $this->objectHandler->isValidObjectType($objectType) === false
149 ) {
150 return false;
151 }
152
153 $return = $this->database->replace(
154 $this->database->getUserGroupToObjectTable(),
155 [
156 'group_id' => $this->id,
157 'group_type' => $this->type,
158 'object_id' => $objectId,
159 'general_object_type' => $generalObjectType,
160 'object_type' => $objectType,
161 'from_date' => $fromDate,
162 'to_date' => $toDate
163 ],
164 [
165 '%s',
166 '%s',
167 '%s',
168 '%s',
169 '%s',
170 '%s',
171 '%s'
172 ]
173 );
174
175 if ($return !== false) {
176 $this->resetObjectsAfterAssignmentChange();
177 return true;
178 }
179
180 return false;
181 }
182
183 /**
184 * @throws Exception
185 */
186 public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool
187 {
188 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
189
190 if ($generalObjectType === null
191 || $this->objectHandler->isValidObjectType($objectType) === false
192 ) {
193 return false;
194 }
195
196 $objectTypeQuery = " AND object_type = '%s' ";
197 $values = [
198 $this->id,
199 $this->type,
200 $objectType
201 ];
202
203 if ($ignoreGeneralType === false) {
204 $objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') ";
205 $values[] = $generalObjectType;
206 }
207
208 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
209 WHERE group_id = %d
210 AND group_type = '%s'
211 $objectTypeQuery";
212
213 if ($objectId !== null) {
214 $query .= ' AND object_id = %d';
215 $values[] = $objectId;
216 }
217
218 $query = $this->database->prepare($query, $values);
219 $success = ($this->database->query($query) !== false);
220
221 if ($success === true) {
222 $this->resetObjectsAfterAssignmentChange();
223 }
224
225 return $success;
226 }
227
228 /**
229 * @return AssignmentInformation[]
230 */
231 public function getAssignedObjects(string $objectType): array
232 {
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];
243 }
244
245 /**
246 * @throws Exception
247 */
248 public function addDefaultType(
249 string $objectType,
250 int|string|null $fromTime = null,
251 int|string|null $toTime = null
252 ): bool {
253 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
254 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
255 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
256
257 return $this->addObject($objectType, '', $fromDate, $toDate);
258 }
259
260 /**
261 * @throws Exception
262 */
263 public function removeDefaultType(string $objectType): bool
264 {
265 return $this->removeObject($objectType, '', true);
266 }
267
268 public function getDefaultGroupForObjectTypes(): ?array
269 {
270 if ($this->defaultTypes === null) {
271 $this->defaultTypes = [];
272
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 = [
280 $this->id,
281 $this->type
282 ];
283
284 $query = $this->database->prepare($query, $parameters);
285 $results = (array) $this->database->getResults($query);
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 }
293 }
294
295 return $this->defaultTypes;
296 }
297
298 public function isDefaultGroupForObjectType(string $objectType, ?int &$fromTime = null, ?int &$toTime = null): bool
299 {
300 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
301
302 // Reset reference values anyway
303 $fromTime = null;
304 $toTime = null;
305
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;
313 }
314
315 return false;
316 }
317
318 public function isObjectAssignedToGroup(
319 string $objectType,
320 int|string|null $objectId,
321 ?AssignmentInformation &$assignmentInformation = null
322 ): bool {
323 $assignmentInformation = null;
324 $assignedObjects = $this->getAssignedObjects($objectType);
325
326 if (isset($assignedObjects[$objectId]) === true) {
327 $assignmentInformation = $assignedObjects[$objectId];
328 return true;
329 }
330
331 return false;
332 }
333
334 /**
335 * @throws Exception
336 */
337 public function isObjectMember(
338 string $objectType,
339 int|string|null $objectId,
340 ?AssignmentInformation &$assignmentInformation = null
341 ): bool {
342 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
343 try {
344 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
345 $this,
346 $this->config->lockRecursive(),
347 $objectId,
348 $assignmentInformation
349 );
350 } catch (MissingObjectMembershipHandlerException) {
351 $isMember = false;
352 }
353
354 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
355 $assignmentInformation : false;
356 }
357
358 $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ?
359 $this->objectMembership[$objectType][$objectId] : null;
360
361 return ($this->objectMembership[$objectType][$objectId] !== false);
362 }
363
364 /**
365 * @throws Exception
366 */
367 public function isRoleMember(int|string|null $roleId, ?AssignmentInformation &$assignmentInformation = null): bool
368 {
369 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
370 }
371
372 /**
373 * @throws Exception
374 */
375 public function isUserMember(int|string|null $userId, ?AssignmentInformation &$assignmentInformation = null): bool
376 {
377 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
378 }
379
380 /**
381 * @throws Exception
382 */
383 public function isTermMember(int|string|null $termId, ?AssignmentInformation &$assignmentInformation = null): bool
384 {
385 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
386 }
387
388 /**
389 * @throws Exception
390 */
391 public function isPostMember(int|string|null $postId, ?AssignmentInformation &$assignmentInformation = null): bool
392 {
393 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
394 }
395
396 /**
397 * @throws Exception
398 */
399 public function getRecursiveMembershipForObject(string $objectType, int|string|null $objectId): array
400 {
401 /**
402 * @var AssignmentInformation $assignmentInformation
403 */
404 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
405 return $assignmentInformation->getRecursiveMembership();
406 }
407
408 return [];
409 }
410
411 /**
412 * @throws Exception
413 */
414 public function isLockedRecursive(string $objectType, int|string|null $objectId): bool
415 {
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;
424 }
425
426 /**
427 * @throws Exception
428 */
429 public function getAssignedObjectsByType(string $objectType): array
430 {
431 if (isset($this->fullObjectMembership[$objectType]) === false) {
432 try {
433 $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
434 $this->fullObjectMembership[$objectType] = $handler->getFullObjects(
435 $this,
436 $this->config->lockRecursive(),
437 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
438 );
439 } catch (MissingObjectMembershipHandlerException) {
440 $this->fullObjectMembership[$objectType] = [];
441 }
442 }
443
444 return $this->fullObjectMembership[$objectType];
445 }
446
447 /**
448 * @throws Exception
449 */
450 public function getFullRoles(): array
451 {
452 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
453 }
454
455
456 /**
457 * @throws Exception
458 */
459 public function getFullUsers(): array
460 {
461 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
462 }
463
464 /**
465 * @throws Exception
466 */
467 public function getFullTerms(): array
468 {
469 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
470 }
471
472 /**
473 * @throws Exception
474 */
475 public function getFullPosts(): array
476 {
477 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE);
478 }
479 }
480