PluginProbe
User Access Manager / 2.2.2
User Access Manager v2.2.2
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 +323 -59 2.3.172.2.2 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 AssignedObjectsLoader $assignedObjectsLoader,
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 = [];
@@ -116,15 +286,11 @@
116 286 $this->objectMembership = [];
117 287 $this->fullObjectMembership = [];
118 288 }
119 289
120 - private function resetObjectsAfterAssignmentChange(): void
121 - {
122 - $this->assignedObjectsLoader->flush();
123 - $this->resetObjects();
124 - }
125 -
126 290 /**
291 + * Deletes the user group.
292 + * @return bool
127 293 * @throws Exception
128 294 */
129 295 public function delete(): bool
130 296 {
@@ -137,11 +303,17 @@
137 303 return true;
138 304 }
139 305
140 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
141 313 * @throws Exception
142 314 */
143 - 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
144 316 {
145 317 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
146 318
147 319 if ($generalObjectType === null
@@ -172,9 +344,9 @@
172 344 ]
173 345 );
174 346
175 347 if ($return !== false) {
176 - $this->resetObjectsAfterAssignmentChange();
348 + $this->resetObjects();
177 349 return true;
178 350 }
179 351
180 352 return false;
@@ -180,11 +352,16 @@
180 352 return false;
181 353 }
182 354
183 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
184 361 * @throws Exception
185 362 */
186 - public function removeObject(string $objectType, $objectId = null, bool $ignoreGeneralType = false): bool
363 + public function removeObject(string $objectType, $objectId = null, $ignoreGeneralType = false): bool
187 364 {
188 365 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
189 366
190 367 if ($generalObjectType === null
@@ -207,9 +384,9 @@
207 384
208 385 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
209 386 WHERE group_id = %d
210 387 AND group_type = '%s'
211 - $objectTypeQuery";
388 + {$objectTypeQuery}";
212 389
213 390 if ($objectId !== null) {
214 391 $query .= ' AND object_id = %d';
215 392 $values[] = $objectId;
@@ -218,9 +395,9 @@
218 395 $query = $this->database->prepare($query, $values);
219 396 $success = ($this->database->query($query) !== false);
220 397
221 398 if ($success === true) {
222 - $this->resetObjectsAfterAssignmentChange();
399 + $this->resetObjects();
223 400 }
224 401
225 402 return $success;
226 403 }
@@ -225,19 +402,43 @@
225 402 return $success;
226 403 }
227 404
228 405 /**
406 + * Returns the assigned objects.
407 + * @param string $objectType The object type.
229 408 * @return AssignmentInformation[]
230 409 */
231 410 public function getAssignedObjects(string $objectType): array
232 411 {
233 412 if (isset($this->assignedObjects[$objectType]) === false) {
234 - $this->assignedObjects[$objectType] = $this->assignedObjectsLoader->getAssignedObjects(
413 + $query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate
414 + FROM {$this->database->getUserGroupToObjectTable()}
415 + WHERE group_id = '%s'
416 + AND group_type = '%s'
417 + AND object_id != ''
418 + AND (general_object_type = '%s' OR object_type = '%s')";
419 +
420 + $parameters = [
421 + $this->id,
235 422 $this->type,
236 - $this->id,
237 423 $objectType,
238 - $this->ignoreDates
239 - );
424 + $objectType
425 + ];
426 +
427 + if ($this->ignoreDates === false) {
428 + $query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')";
429 + $time = $this->wordpress->currentTime('mysql');
430 + $parameters = array_merge($parameters, [$time, $time]);
431 + }
432 +
433 + $query = $this->database->prepare($query, $parameters);
434 + $results = (array) $this->database->getResults($query);
435 + $this->assignedObjects[$objectType] = [];
436 +
437 + foreach ($results as $result) {
438 + $this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory
439 + ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate);
440 + }
240 441 }
241 442
242 443 return $this->assignedObjects[$objectType];
243 444 }
@@ -242,15 +443,17 @@
242 443 return $this->assignedObjects[$objectType];
243 444 }
244 445
245 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
246 452 * @throws Exception
247 453 */
248 - public function addDefaultType(
249 - string $objectType,
250 - int|string|null $fromTime = null,
251 - int|string|null $toTime = null
252 - ): bool {
454 + public function addDefaultType(string $objectType, $fromTime = null, $toTime = null): bool
455 + {
253 456 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
254 457 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
255 458 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
256 459
@@ -257,8 +460,11 @@
257 460 return $this->addObject($objectType, '', $fromDate, $toDate);
258 461 }
259 462
260 463 /**
464 + * Removes the group as default for object type.
465 + * @param string $objectType
466 + * @return bool
261 467 * @throws Exception
262 468 */
263 469 public function removeDefaultType(string $objectType): bool
264 470 {
@@ -264,8 +470,12 @@
264 470 {
265 471 return $this->removeObject($objectType, '', true);
266 472 }
267 473
474 + /**
475 + * Returns the object type for which the user group is the default group.
476 + * @return array
477 + */
268 478 public function getDefaultGroupForObjectTypes(): ?array
269 479 {
270 480 if ($this->defaultTypes === null) {
271 481 $this->defaultTypes = [];
@@ -294,9 +504,16 @@
294 504
295 505 return $this->defaultTypes;
296 506 }
297 507
298 - 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
299 516 {
300 517 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
301 518
302 519 // Reset reference values anyway
@@ -314,12 +531,19 @@
314 531
315 532 return false;
316 533 }
317 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 + */
318 542 public function isObjectAssignedToGroup(
319 543 string $objectType,
320 - int|string|null $objectId,
321 - ?AssignmentInformation &$assignmentInformation = null
544 + $objectId,
545 + &$assignmentInformation = null
322 546 ): bool {
323 547 $assignmentInformation = null;
324 548 $assignedObjects = $this->getAssignedObjects($objectType);
325 549
@@ -331,14 +555,19 @@
331 555 return false;
332 556 }
333 557
334 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
335 564 * @throws Exception
336 565 */
337 566 public function isObjectMember(
338 567 string $objectType,
339 - int|string|null $objectId,
340 - ?AssignmentInformation &$assignmentInformation = null
568 + $objectId,
569 + &$assignmentInformation = null
341 570 ): bool {
342 571 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
343 572 try {
344 573 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
@@ -346,9 +575,9 @@
346 575 $this->config->lockRecursive(),
347 576 $objectId,
348 577 $assignmentInformation
349 578 );
350 - } catch (MissingObjectMembershipHandlerException) {
579 + } catch (MissingObjectMembershipHandlerException $exception) {
351 580 $isMember = false;
352 581 }
353 582
354 583 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
@@ -361,43 +590,63 @@
361 590 return ($this->objectMembership[$objectType][$objectId] !== false);
362 591 }
363 592
364 593 /**
594 + * Checks if the role is a group member.
595 + * @param string $roleId
596 + * @param null $assignmentInformation
597 + * @return bool
365 598 * @throws Exception
366 599 */
367 - public function isRoleMember(int|string|null $roleId, ?AssignmentInformation &$assignmentInformation = null): bool
600 + public function isRoleMember(string $roleId, &$assignmentInformation = null): bool
368 601 {
369 602 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
370 603 }
371 604
372 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
373 610 * @throws Exception
374 611 */
375 - public function isUserMember(int|string|null $userId, ?AssignmentInformation &$assignmentInformation = null): bool
612 + public function isUserMember(int $userId, &$assignmentInformation = null): bool
376 613 {
377 614 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
378 615 }
379 616
380 617 /**
618 + * Checks if the term is a group member.
619 + * @param int $termId
620 + * @param null $assignmentInformation
621 + * @return bool
381 622 * @throws Exception
382 623 */
383 - public function isTermMember(int|string|null $termId, ?AssignmentInformation &$assignmentInformation = null): bool
624 + public function isTermMember(int $termId, &$assignmentInformation = null): bool
384 625 {
385 626 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
386 627 }
387 628
388 629 /**
630 + * Checks if the post is a group member
631 + * @param int $postId
632 + * @param null $assignmentInformation
633 + * @return bool
389 634 * @throws Exception
390 635 */
391 - public function isPostMember(int|string|null $postId, ?AssignmentInformation &$assignmentInformation = null): bool
636 + public function isPostMember(int $postId, &$assignmentInformation = null): bool
392 637 {
393 638 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
394 639 }
395 640
396 641 /**
642 + * Returns the recursive membership.
643 + * @param string $objectType The object type.
644 + * @param int|string $objectId The object id.
645 + * @return array
397 646 * @throws Exception
398 647 */
399 - public function getRecursiveMembershipForObject(string $objectType, int|string|null $objectId): array
648 + public function getRecursiveMembershipForObject(string $objectType, $objectId): array
400 649 {
401 650 /**
402 651 * @var AssignmentInformation $assignmentInformation
403 652 */
@@ -408,11 +657,15 @@
408 657 return [];
409 658 }
410 659
411 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
412 665 * @throws Exception
413 666 */
414 - public function isLockedRecursive(string $objectType, int|string|null $objectId): bool
667 + public function isLockedRecursive(string $objectType, $objectId): bool
415 668 {
416 669 /**
417 670 * @var AssignmentInformation $assignmentInformation
418 671 */
@@ -423,8 +676,11 @@
423 676 return false;
424 677 }
425 678
426 679 /**
680 + * Returns all objects of the given type.
681 + * @param string $objectType The object type.
682 + * @return array
427 683 * @throws Exception
428 684 */
429 685 public function getAssignedObjectsByType(string $objectType): array
430 686 {
@@ -435,9 +691,9 @@
435 691 $this,
436 692 $this->config->lockRecursive(),
437 693 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
438 694 );
439 - } catch (MissingObjectMembershipHandlerException) {
695 + } catch (MissingObjectMembershipHandlerException $exception) {
440 696 $this->fullObjectMembership[$objectType] = [];
441 697 }
442 698 }
443 699
@@ -444,8 +700,10 @@
444 700 return $this->fullObjectMembership[$objectType];
445 701 }
446 702
447 703 /**
704 + * Returns the roles assigned to the group.
705 + * @return array
448 706 * @throws Exception
449 707 */
450 708 public function getFullRoles(): array
451 709 {
@@ -453,8 +711,10 @@
453 711 }
454 712
455 713
456 714 /**
715 + * Returns the users assigned to the group.
716 + * @return array
457 717 * @throws Exception
458 718 */
459 719 public function getFullUsers(): array
460 720 {
@@ -461,8 +721,10 @@
461 721 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
462 722 }
463 723
464 724 /**
725 + * Returns the terms assigned to the group.
726 + * @return array
465 727 * @throws Exception
466 728 */
467 729 public function getFullTerms(): array
468 730 {
@@ -469,8 +731,10 @@
469 731 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
470 732 }
471 733
472 734 /**
735 + * Returns the posts assigned to the group.
736 + * @return array
473 737 * @throws Exception
474 738 */
475 739 public function getFullPosts(): array
476 740 {