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