PluginProbe
User Access Manager / 2.1.11
User Access Manager v2.1.11
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 +368 -95 2.3.13 → 2.1.11 View file →
@@ -1,103 +1,281 @@
1 1 <?php
2 -
3 -declare(strict_types=1);
4 -
2 +/**
3 + * AbstractUserGroup.php
4 + *
5 + * The AbstractUserGroup class file.
6 + *
7 + * PHP versions 5
8 + *
9 + * @author Alexander Schneider <[email protected]>
10 + * @copyright 2008-2017 Alexander Schneider
11 + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
12 + * @version SVN: $id$
13 + * @link http://wordpress.org/extend/plugins/user-access-manager/
14 + */
5 15 namespace UserAccessManager\UserGroup;
6 16
7 -use Exception;
8 17 use UserAccessManager\Config\MainConfig;
9 18 use UserAccessManager\Database\Database;
10 19 use UserAccessManager\Object\ObjectHandler;
11 20 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
21 +use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory;
12 22 use UserAccessManager\Util\Util;
13 23 use UserAccessManager\Wrapper\Php;
14 24 use UserAccessManager\Wrapper\Wordpress;
15 25
26 +/**
27 + * Class AbstractUserGroup
28 + *
29 + * @package UserAccessManager\UserGroup
30 + */
16 31 abstract class AbstractUserGroup
17 32 {
18 - public const NONE_ROLE = '_none-role_';
33 + const NONE_ROLE = '_none-role_';
19 34
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;
35 + /**
36 + * @var Php
37 + */
38 + protected $php;
30 39
31 40 /**
41 + * @var Wordpress
42 + */
43 + protected $wordpress;
44 +
45 + /**
46 + * @var Database
47 + */
48 + protected $database;
49 +
50 + /**
51 + * @var MainConfig
52 + */
53 + protected $config;
54 +
55 + /**
56 + * @var Util
57 + */
58 + protected $util;
59 +
60 + /**
61 + * @var ObjectHandler
62 + */
63 + protected $objectHandler;
64 +
65 + /**
66 + * @var ObjectMembershipHandlerFactory
67 + */
68 + protected $membershipHandlerFactory;
69 +
70 + /**
71 + * @var AssignmentInformationFactory
72 + */
73 + protected $assignmentInformationFactory;
74 +
75 + /**
76 + * @var string
77 + */
78 + protected $id = null;
79 +
80 + /**
81 + * @var string
82 + */
83 + protected $type = null;
84 +
85 + /**
86 + * @var string
87 + */
88 + protected $name = null;
89 +
90 + /**
91 + * @var string
92 + */
93 + protected $description = null;
94 +
95 + /**
96 + * @var string
97 + */
98 + protected $readAccess = 'group';
99 +
100 + /**
101 + * @var string
102 + */
103 + protected $writeAccess = 'group';
104 +
105 + /**
106 + * @var bool
107 + */
108 + protected $ignoreDates = false;
109 +
110 + /**
111 + * @var array
112 + */
113 + protected $assignedObjects = [];
114 +
115 + /**
116 + * @var array
117 + */
118 + protected $objectMembership = [];
119 +
120 + /**
121 + * @var array
122 + */
123 + protected $fullObjectMembership = [];
124 +
125 + /**
126 + * @var array|null
127 + */
128 + protected $defaultTypes = null;
129 +
130 + /**
131 + * AbstractUserGroup constructor.
132 + *
133 + * @param Php $php
134 + * @param Wordpress $wordpress
135 + * @param Database $database
136 + * @param MainConfig $config
137 + * @param Util $util
138 + * @param ObjectHandler $objectHandler
139 + * @param AssignmentInformationFactory $assignmentInformationFactory
140 + * @param null|string $id
141 + *
32 142 * @throws UserGroupTypeException
33 143 */
34 144 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 AssignmentInformationFactory $assignmentInformationFactory,
42 - protected int|string|null $id = null
145 + Php $php,
146 + Wordpress $wordpress,
147 + Database $database,
148 + MainConfig $config,
149 + Util $util,
150 + ObjectHandler $objectHandler,
151 + AssignmentInformationFactory $assignmentInformationFactory,
152 + $id = null
43 153 ) {
44 154 if ($this->type === null) {
45 155 throw new UserGroupTypeException('User group type must not null.');
46 156 }
157 +
158 + $this->php = $php;
159 + $this->wordpress = $wordpress;
160 + $this->database = $database;
161 + $this->config = $config;
162 + $this->util = $util;
163 + $this->objectHandler = $objectHandler;
164 + $this->assignmentInformationFactory = $assignmentInformationFactory;
165 + $this->id = $id;
47 166 }
48 167
49 - public function getId(): int|string|null
168 + /*
169 + * Primary values.
170 + */
171 +
172 + /**
173 + * Returns the group _iId.
174 + *
175 + * @return string
176 + */
177 + public function getId()
50 178 {
51 179 return $this->id;
52 180 }
53 181
54 - public function getType(): ?string
182 + /**
183 + * Returns the user group type.
184 + *
185 + * @return string
186 + */
187 + public function getType()
55 188 {
56 189 return $this->type;
57 190 }
58 191
59 - public function getName(): ?string
192 + /**
193 + * Returns the group name.
194 + *
195 + * @return string
196 + */
197 + public function getName()
60 198 {
61 199 return $this->name;
62 200 }
63 201
64 - public function setName(string $name): void
202 + /**
203 + * Sets the group name.
204 + *
205 + * @param string $name The new group name.
206 + */
207 + public function setName($name)
65 208 {
66 209 $this->name = $name;
67 210 }
68 211
69 - public function getDescription(): ?string
212 + /**
213 + * Returns the group description.
214 + *
215 + * @return string
216 + */
217 + public function getDescription()
70 218 {
71 219 return $this->description;
72 220 }
73 221
74 - public function setDescription(string $description): void
222 + /**
223 + * Sets the group description.
224 + *
225 + * @param string $description The new group description.
226 + */
227 + public function setDescription($description)
75 228 {
76 229 $this->description = $description;
77 230 }
78 231
79 - public function getReadAccess(): string
232 + /**
233 + * Returns the read access.
234 + *
235 + * @return string
236 + */
237 + public function getReadAccess()
80 238 {
81 239 return $this->readAccess;
82 240 }
83 241
84 - public function setReadAccess(string $readAccess): void
242 + /**
243 + * Sets the read access.
244 + *
245 + * @param string $readAccess The read access.
246 + */
247 + public function setReadAccess($readAccess)
85 248 {
86 249 $this->readAccess = $readAccess;
87 250 }
88 251
89 - public function getWriteAccess(): string
252 + /**
253 + * Returns the write access.
254 + *
255 + * @return string
256 + */
257 + public function getWriteAccess()
90 258 {
91 259 return $this->writeAccess;
92 260 }
93 261
94 - public function setWriteAccess(string $writeAccess): void
262 + /**
263 + * Sets the write access.
264 + *
265 + * @param string $writeAccess The write access.
266 + */
267 + public function setWriteAccess($writeAccess)
95 268 {
96 269 $this->writeAccess = $writeAccess;
97 270 }
98 271
99 - public function setIgnoreDates(bool $ignoreDates): void
272 + /**
273 + * Sets the ignore dates flag.
274 + *
275 + * @param bool $ignoreDates
276 + */
277 + public function setIgnoreDates($ignoreDates)
100 278 {
101 279 if ($this->ignoreDates !== $ignoreDates) {
102 280 $this->resetObjects();
103 281 }
@@ -104,15 +282,13 @@
104 282
105 283 $this->ignoreDates = $ignoreDates;
106 284 }
107 285
108 - public function getIgnoreDates(): bool
286 + /**
287 + * Resets the objects
288 + */
289 + protected function resetObjects()
109 290 {
110 - return $this->ignoreDates;
111 - }
112 -
113 - protected function resetObjects(): void
114 - {
115 291 $this->assignedObjects = [];
116 292 $this->objectMembership = [];
117 293 $this->fullObjectMembership = [];
118 294 }
@@ -117,11 +293,13 @@
117 293 $this->fullObjectMembership = [];
118 294 }
119 295
120 296 /**
121 - * @throws Exception
297 + * Deletes the user group.
298 + *
299 + * @return bool
122 300 */
123 - public function delete(): bool
301 + public function delete()
124 302 {
125 303 $allObjectTypes = $this->objectHandler->getAllObjectTypes();
126 304
127 305 foreach ($allObjectTypes as $objectType) {
@@ -131,11 +309,18 @@
131 309 return true;
132 310 }
133 311
134 312 /**
135 - * @throws Exception
313 + * Adds a object of the given type.
314 + *
315 + * @param string $objectType The object type.
316 + * @param string $objectId The object id.
317 + * @param string $fromDate From date.
318 + * @param string $toDate To date.
319 + *
320 + * @return bool
136 321 */
137 - public function addObject(string $objectType, int|string|null $objectId, $fromDate = null, $toDate = null): bool
322 + public function addObject($objectType, $objectId, $fromDate = null, $toDate = null)
138 323 {
139 324 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
140 325
141 326 if ($generalObjectType === null
@@ -174,11 +359,17 @@
174 359 return false;
175 360 }
176 361
177 362 /**
178 - * @throws Exception
363 + * Removes a object of the given type.
364 + *
365 + * @param string $objectType The object type.
366 + * @param string $objectId The object id.
367 + * @param bool $ignoreGeneralType
368 + *
369 + * @return bool
179 370 */
180 - public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool
371 + public function removeObject($objectType, $objectId = null, $ignoreGeneralType = false)
181 372 {
182 373 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
183 374
184 375 if ($generalObjectType === null
@@ -201,9 +392,9 @@
201 392
202 393 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
203 394 WHERE group_id = %d
204 395 AND group_type = '%s'
205 - $objectTypeQuery";
396 + {$objectTypeQuery}";
206 397
207 398 if ($objectId !== null) {
208 399 $query .= ' AND object_id = %d';
209 400 $values[] = $objectId;
@@ -219,11 +410,15 @@
219 410 return $success;
220 411 }
221 412
222 413 /**
414 + * Returns the assigned objects.
415 + *
416 + * @param string $objectType The object type.
417 + *
223 418 * @return AssignmentInformation[]
224 419 */
225 - public function getAssignedObjects(string $objectType): array
420 + public function getAssignedObjects($objectType)
226 421 {
227 422 if (isset($this->assignedObjects[$objectType]) === false) {
228 423 $query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate
229 424 FROM {$this->database->getUserGroupToObjectTable()}
@@ -245,9 +440,9 @@
245 440 $parameters = array_merge($parameters, [$time, $time]);
246 441 }
247 442
248 443 $query = $this->database->prepare($query, $parameters);
249 - $results = (array) $this->database->getResults($query);
444 + $results = (array)$this->database->getResults($query);
250 445 $this->assignedObjects[$objectType] = [];
251 446
252 447 foreach ($results as $result) {
253 448 $this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory
@@ -258,15 +453,18 @@
258 453 return $this->assignedObjects[$objectType];
259 454 }
260 455
261 456 /**
262 - * @throws Exception
457 + * Marks the group as default for the object type.
458 + *
459 + * @param string $objectType
460 + * @param null|int $fromTime
461 + * @param null|int $toTime
462 + *
463 + * @return bool
263 464 */
264 - public function addDefaultType(
265 - string $objectType,
266 - int|string|null $fromTime = null,
267 - int|string|null $toTime = null
268 - ): bool {
465 + public function addDefaultType($objectType, $fromTime = null, $toTime = null)
466 + {
269 467 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
270 468 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
271 469 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
272 470
@@ -273,16 +471,25 @@
273 471 return $this->addObject($objectType, '', $fromDate, $toDate);
274 472 }
275 473
276 474 /**
277 - * @throws Exception
475 + * Removes the group as default for object type.
476 + *
477 + * @param string $objectType
478 + *
479 + * @return bool
278 480 */
279 - public function removeDefaultType(string $objectType): bool
481 + public function removeDefaultType($objectType)
280 482 {
281 483 return $this->removeObject($objectType, '', true);
282 484 }
283 485
284 - public function getDefaultGroupForObjectTypes(): ?array
486 + /**
487 + * Returns the object type for which the user group is the default group.
488 + *
489 + * @return array
490 + */
491 + public function getDefaultGroupForObjectTypes()
285 492 {
286 493 if ($this->defaultTypes === null) {
287 494 $this->defaultTypes = [];
288 495
@@ -297,9 +504,9 @@
297 504 $this->type
298 505 ];
299 506
300 507 $query = $this->database->prepare($query, $parameters);
301 - $results = (array) $this->database->getResults($query);
508 + $results = (array)$this->database->getResults($query);
302 509
303 510 foreach ($results as $result) {
304 511 $this->defaultTypes[$result->objectType] = [
305 512 ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
@@ -310,9 +517,18 @@
310 517
311 518 return $this->defaultTypes;
312 519 }
313 520
314 - public function isDefaultGroupForObjectType(string $objectType, ?int &$fromTime = null, ?int &$toTime = null): bool
521 + /**
522 + * Checks if the group is the default one for the given object type.
523 + *
524 + * @param string $objectType
525 + * @param null|int $fromTime
526 + * @param null|int $toTime
527 + *
528 + * @return bool
529 + */
530 + public function isDefaultGroupForObjectType($objectType, &$fromTime = null, &$toTime = null)
315 531 {
316 532 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
317 533
318 534 // Reset reference values anyway
@@ -320,11 +536,11 @@
320 536 $toTime = null;
321 537
322 538 if (isset($defaultGroupForObjectTypes[$objectType])) {
323 539 $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ?
324 - (int) $defaultGroupForObjectTypes[$objectType][0] : null;
540 + (int)$defaultGroupForObjectTypes[$objectType][0] : null;
325 541 $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ?
326 - (int) $defaultGroupForObjectTypes[$objectType][1] : null;
542 + (int)$defaultGroupForObjectTypes[$objectType][1] : null;
327 543
328 544 return true;
329 545 }
330 546
@@ -330,13 +546,22 @@
330 546
331 547 return false;
332 548 }
333 549
550 + /**
551 + * Checks if the object is assigned to the group.
552 + *
553 + * @param string $objectType The object type.
554 + * @param string $objectId The object id.
555 + * @param AssignmentInformation|null $assignmentInformation The assignment information object.
556 + *
557 + * @return bool
558 + */
334 559 public function isObjectAssignedToGroup(
335 - string $objectType,
336 - int|string|null $objectId,
337 - ?AssignmentInformation &$assignmentInformation = null
338 - ): bool {
560 + $objectType,
561 + $objectId,
562 + &$assignmentInformation = null
563 + ) {
339 564 $assignmentInformation = null;
340 565 $assignedObjects = $this->getAssignedObjects($objectType);
341 566
342 567 if (isset($assignedObjects[$objectId]) === true) {
@@ -347,15 +572,21 @@
347 572 return false;
348 573 }
349 574
350 575 /**
351 - * @throws Exception
576 + * Returns a single object.
577 + *
578 + * @param string $objectType The object type.
579 + * @param string $objectId The id of the object which should be checked.
580 + * @param AssignmentInformation $assignmentInformation The assignment information
581 + *
582 + * @return bool
352 583 */
353 584 public function isObjectMember(
354 - string $objectType,
355 - int|string|null $objectId,
356 - ?AssignmentInformation &$assignmentInformation = null
357 - ): bool {
585 + $objectType,
586 + $objectId,
587 + &$assignmentInformation = null
588 + ) {
358 589 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
359 590 try {
360 591 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
361 592 $this,
@@ -362,9 +593,9 @@
362 593 $this->config->lockRecursive(),
363 594 $objectId,
364 595 $assignmentInformation
365 596 );
366 - } catch (MissingObjectMembershipHandlerException) {
597 + } catch (MissingObjectMembershipHandlerException $exception) {
367 598 $isMember = false;
368 599 }
369 600
370 601 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
@@ -377,43 +608,68 @@
377 608 return ($this->objectMembership[$objectType][$objectId] !== false);
378 609 }
379 610
380 611 /**
381 - * @throws Exception
612 + * Checks if the role is a group member.
613 + *
614 + * @param string $roleId
615 + * @param AssignmentInformation $assignmentInformation
616 + *
617 + * @return bool
382 618 */
383 - public function isRoleMember(int|string|null $roleId, ?AssignmentInformation &$assignmentInformation = null): bool
619 + public function isRoleMember($roleId, &$assignmentInformation = null)
384 620 {
385 621 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
386 622 }
387 623
388 624 /**
389 - * @throws Exception
625 + * Checks if the user is a group member.
626 + *
627 + * @param int $userId The user id.
628 + * @param AssignmentInformation $assignmentInformation The assignment information.
629 + *
630 + * @return bool
390 631 */
391 - public function isUserMember(int|string|null $userId, ?AssignmentInformation &$assignmentInformation = null): bool
632 + public function isUserMember($userId, &$assignmentInformation = null)
392 633 {
393 634 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
394 635 }
395 636
396 637 /**
397 - * @throws Exception
638 + * Checks if the term is a group member.
639 + *
640 + * @param int $termId
641 + * @param AssignmentInformation $assignmentInformation
642 + *
643 + * @return bool
398 644 */
399 - public function isTermMember(int|string|null $termId, ?AssignmentInformation &$assignmentInformation = null): bool
645 + public function isTermMember($termId, &$assignmentInformation = null)
400 646 {
401 647 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
402 648 }
403 649
404 650 /**
405 - * @throws Exception
651 + * Checks if the post is a group member
652 + *
653 + * @param int $postId
654 + * @param AssignmentInformation $assignmentInformation
655 + *
656 + * @return bool
406 657 */
407 - public function isPostMember(int|string|null $postId, ?AssignmentInformation &$assignmentInformation = null): bool
658 + public function isPostMember($postId, &$assignmentInformation = null)
408 659 {
409 660 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
410 661 }
411 662
412 663 /**
413 - * @throws Exception
664 + * Returns the recursive membership.
665 + *
666 + * @param string $objectType The object type.
667 + * @param string $objectId The object id.
668 + *
669 + * @return array
414 670 */
415 - public function getRecursiveMembershipForObject(string $objectType, int|string|null $objectId): array
671 + public function getRecursiveMembershipForObject($objectType, $objectId)
416 672 {
417 673 /**
418 674 * @var AssignmentInformation $assignmentInformation
419 675 */
@@ -424,11 +680,16 @@
424 680 return [];
425 681 }
426 682
427 683 /**
428 - * @throws Exception
684 + * Returns true if the requested object is locked recursive.
685 + *
686 + * @param string $objectType The object type.
687 + * @param string $objectId The object id.
688 + *
689 + * @return bool
429 690 */
430 - public function isLockedRecursive(string $objectType, int|string|null $objectId): bool
691 + public function isLockedRecursive($objectType, $objectId)
431 692 {
432 693 /**
433 694 * @var AssignmentInformation $assignmentInformation
434 695 */
@@ -439,11 +700,15 @@
439 700 return false;
440 701 }
441 702
442 703 /**
443 - * @throws Exception
704 + * Returns all objects of the given type.
705 + *
706 + * @param string $objectType The object type.
707 + *
708 + * @return array
444 709 */
445 - public function getAssignedObjectsByType(string $objectType): array
710 + public function getAssignedObjectsByType($objectType)
446 711 {
447 712 if (isset($this->fullObjectMembership[$objectType]) === false) {
448 713 try {
449 714 $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
@@ -451,9 +716,9 @@
451 716 $this,
452 717 $this->config->lockRecursive(),
453 718 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
454 719 );
455 - } catch (MissingObjectMembershipHandlerException) {
720 + } catch (MissingObjectMembershipHandlerException $exception) {
456 721 $this->fullObjectMembership[$objectType] = [];
457 722 }
458 723 }
459 724
@@ -460,11 +725,13 @@
460 725 return $this->fullObjectMembership[$objectType];
461 726 }
462 727
463 728 /**
464 - * @throws Exception
729 + * Returns the roles assigned to the group.
730 + *
731 + * @return array
465 732 */
466 - public function getFullRoles(): array
733 + public function getFullRoles()
467 734 {
468 735 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
469 736 }
470 737
@@ -469,27 +736,33 @@
469 736 }
470 737
471 738
472 739 /**
473 - * @throws Exception
740 + * Returns the users assigned to the group.
741 + *
742 + * @return array
474 743 */
475 - public function getFullUsers(): array
744 + public function getFullUsers()
476 745 {
477 746 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
478 747 }
479 748
480 749 /**
481 - * @throws Exception
750 + * Returns the terms assigned to the group.
751 + *
752 + * @return array
482 753 */
483 - public function getFullTerms(): array
754 + public function getFullTerms()
484 755 {
485 756 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
486 757 }
487 758
488 759 /**
489 - * @throws Exception
760 + * Returns the posts assigned to the group.
761 + *
762 + * @return array
490 763 */
491 - public function getFullPosts(): array
764 + public function getFullPosts()
492 765 {
493 766 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE);
494 767 }
495 768 }