PluginProbe
User Access Manager / 2.2.18
User Access Manager v2.2.18
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.2.18, at src/UserGroup/AbstractUserGroup.php

744 lines 20.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 */
15
16 declare(strict_types=1);
17
18 namespace UserAccessManager\UserGroup;
19
20 use Exception;
21 use UserAccessManager\Config\MainConfig;
22 use UserAccessManager\Database\Database;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
25 use UserAccessManager\Util\Util;
26 use UserAccessManager\Wrapper\Php;
27 use UserAccessManager\Wrapper\Wordpress;
28
29 /**
30 * Class AbstractUserGroup
31 *
32 * @package UserAccessManager\UserGroup
33 */
34 abstract class AbstractUserGroup
35 {
36 const NONE_ROLE = '_none-role_';
37
38 /**
39 * @var Php
40 */
41 protected $php;
42
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
138 * @throws UserGroupTypeException
139 */
140 public function __construct(
141 Php $php,
142 Wordpress $wordpress,
143 Database $database,
144 MainConfig $config,
145 Util $util,
146 ObjectHandler $objectHandler,
147 AssignmentInformationFactory $assignmentInformationFactory,
148 $id = null
149 ) {
150 if ($this->type === null) {
151 throw new UserGroupTypeException('User group type must not null.');
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;
162 }
163
164 /*
165 * Primary values.
166 */
167
168 /**
169 * Returns the group id.
170 * @return int|string|null
171 */
172 public function getId()
173 {
174 return $this->id;
175 }
176
177 /**
178 * Returns the user group type.
179 * @return string
180 */
181 public function getType(): ?string
182 {
183 return $this->type;
184 }
185
186 /**
187 * Returns the group name.
188 * @return string
189 */
190 public function getName(): ?string
191 {
192 return $this->name;
193 }
194
195 /**
196 * Sets the group name.
197 * @param string $name The new group name.
198 */
199 public function setName(string $name)
200 {
201 $this->name = $name;
202 }
203
204 /**
205 * Returns the group description.
206 * @return string
207 */
208 public function getDescription(): ?string
209 {
210 return $this->description;
211 }
212
213 /**
214 * Sets the group description.
215 * @param string $description The new group description.
216 */
217 public function setDescription(string $description)
218 {
219 $this->description = $description;
220 }
221
222 /**
223 * Returns the read access.
224 * @return string
225 */
226 public function getReadAccess(): string
227 {
228 return $this->readAccess;
229 }
230
231 /**
232 * Sets the read access.
233 * @param string $readAccess The read access.
234 */
235 public function setReadAccess(string $readAccess)
236 {
237 $this->readAccess = $readAccess;
238 }
239
240 /**
241 * Returns the write access.
242 * @return string
243 */
244 public function getWriteAccess(): string
245 {
246 return $this->writeAccess;
247 }
248
249 /**
250 * Sets the write access.
251 * @param string $writeAccess The write access.
252 */
253 public function setWriteAccess(string $writeAccess)
254 {
255 $this->writeAccess = $writeAccess;
256 }
257
258 /**
259 * Sets the ignore dates flag.
260 * @param bool $ignoreDates
261 */
262 public function setIgnoreDates(bool $ignoreDates)
263 {
264 if ($this->ignoreDates !== $ignoreDates) {
265 $this->resetObjects();
266 }
267
268 $this->ignoreDates = $ignoreDates;
269 }
270
271 /**
272 * Return the ignore dates flag.
273 * @return bool
274 */
275 public function getIgnoreDates(): bool
276 {
277 return $this->ignoreDates;
278 }
279
280 /**
281 * Resets the objects
282 */
283 protected function resetObjects()
284 {
285 $this->assignedObjects = [];
286 $this->objectMembership = [];
287 $this->fullObjectMembership = [];
288 }
289
290 /**
291 * Deletes the user group.
292 * @return bool
293 * @throws Exception
294 */
295 public function delete(): bool
296 {
297 $allObjectTypes = $this->objectHandler->getAllObjectTypes();
298
299 foreach ($allObjectTypes as $objectType) {
300 $this->removeObject($objectType);
301 }
302
303 return true;
304 }
305
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
313 * @throws Exception
314 */
315 public function addObject(string $objectType, $objectId, $fromDate = null, $toDate = null): bool
316 {
317 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
318
319 if ($generalObjectType === null
320 || $this->objectHandler->isValidObjectType($objectType) === false
321 ) {
322 return false;
323 }
324
325 $return = $this->database->replace(
326 $this->database->getUserGroupToObjectTable(),
327 [
328 'group_id' => $this->id,
329 'group_type' => $this->type,
330 'object_id' => $objectId,
331 'general_object_type' => $generalObjectType,
332 'object_type' => $objectType,
333 'from_date' => $fromDate,
334 'to_date' => $toDate
335 ],
336 [
337 '%s',
338 '%s',
339 '%s',
340 '%s',
341 '%s',
342 '%s',
343 '%s'
344 ]
345 );
346
347 if ($return !== false) {
348 $this->resetObjects();
349 return true;
350 }
351
352 return false;
353 }
354
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
361 * @throws Exception
362 */
363 public function removeObject(string $objectType, $objectId = null, $ignoreGeneralType = false): bool
364 {
365 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
366
367 if ($generalObjectType === null
368 || $this->objectHandler->isValidObjectType($objectType) === false
369 ) {
370 return false;
371 }
372
373 $objectTypeQuery = " AND object_type = '%s' ";
374 $values = [
375 $this->id,
376 $this->type,
377 $objectType
378 ];
379
380 if ($ignoreGeneralType === false) {
381 $objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') ";
382 $values[] = $generalObjectType;
383 }
384
385 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
386 WHERE group_id = %d
387 AND group_type = '%s'
388 {$objectTypeQuery}";
389
390 if ($objectId !== null) {
391 $query .= ' AND object_id = %d';
392 $values[] = $objectId;
393 }
394
395 $query = $this->database->prepare($query, $values);
396 $success = ($this->database->query($query) !== false);
397
398 if ($success === true) {
399 $this->resetObjects();
400 }
401
402 return $success;
403 }
404
405 /**
406 * Returns the assigned objects.
407 * @param string $objectType The object type.
408 * @return AssignmentInformation[]
409 */
410 public function getAssignedObjects(string $objectType): array
411 {
412 if (isset($this->assignedObjects[$objectType]) === false) {
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,
422 $this->type,
423 $objectType,
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 }
441 }
442
443 return $this->assignedObjects[$objectType];
444 }
445
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
452 * @throws Exception
453 */
454 public function addDefaultType(string $objectType, $fromTime = null, $toTime = null): bool
455 {
456 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
457 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
458 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
459
460 return $this->addObject($objectType, '', $fromDate, $toDate);
461 }
462
463 /**
464 * Removes the group as default for object type.
465 * @param string $objectType
466 * @return bool
467 * @throws Exception
468 */
469 public function removeDefaultType(string $objectType): bool
470 {
471 return $this->removeObject($objectType, '', true);
472 }
473
474 /**
475 * Returns the object type for which the user group is the default group.
476 * @return array
477 */
478 public function getDefaultGroupForObjectTypes(): ?array
479 {
480 if ($this->defaultTypes === null) {
481 $this->defaultTypes = [];
482
483 $query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate
484 FROM {$this->database->getUserGroupToObjectTable()}
485 WHERE group_id = '%s'
486 AND group_type = '%s'
487 AND object_id = ''";
488
489 $parameters = [
490 $this->id,
491 $this->type
492 ];
493
494 $query = $this->database->prepare($query, $parameters);
495 $results = (array) $this->database->getResults($query);
496
497 foreach ($results as $result) {
498 $this->defaultTypes[$result->objectType] = [
499 ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
500 ($result->toDate !== null) ? strtotime($result->toDate) : null
501 ];
502 }
503 }
504
505 return $this->defaultTypes;
506 }
507
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
516 {
517 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
518
519 // Reset reference values anyway
520 $fromTime = null;
521 $toTime = null;
522
523 if (isset($defaultGroupForObjectTypes[$objectType])) {
524 $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ?
525 (int) $defaultGroupForObjectTypes[$objectType][0] : null;
526 $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ?
527 (int) $defaultGroupForObjectTypes[$objectType][1] : null;
528
529 return true;
530 }
531
532 return false;
533 }
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 */
542 public function isObjectAssignedToGroup(
543 string $objectType,
544 $objectId,
545 &$assignmentInformation = null
546 ): bool {
547 $assignmentInformation = null;
548 $assignedObjects = $this->getAssignedObjects($objectType);
549
550 if (isset($assignedObjects[$objectId]) === true) {
551 $assignmentInformation = $assignedObjects[$objectId];
552 return true;
553 }
554
555 return false;
556 }
557
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 $assignmentInformation The assignment information
563 * @return bool
564 * @throws Exception
565 */
566 public function isObjectMember(
567 string $objectType,
568 $objectId,
569 &$assignmentInformation = null
570 ): bool {
571 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
572 try {
573 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
574 $this,
575 $this->config->lockRecursive(),
576 $objectId,
577 $assignmentInformation
578 );
579 } catch (MissingObjectMembershipHandlerException $exception) {
580 $isMember = false;
581 }
582
583 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
584 $assignmentInformation : false;
585 }
586
587 $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ?
588 $this->objectMembership[$objectType][$objectId] : null;
589
590 return ($this->objectMembership[$objectType][$objectId] !== false);
591 }
592
593 /**
594 * Checks if the role is a group member.
595 * @param int|string $roleId
596 * @param null $assignmentInformation
597 * @return bool
598 * @throws Exception
599 */
600 public function isRoleMember($roleId, &$assignmentInformation = null): bool
601 {
602 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
603 }
604
605 /**
606 * Checks if the user is a group member.
607 * @param int|string $userId The user id.
608 * @param null|AssignmentInformation $assignmentInformation The assignment information.
609 * @return bool
610 * @throws Exception
611 */
612 public function isUserMember($userId, &$assignmentInformation = null): bool
613 {
614 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
615 }
616
617 /**
618 * Checks if the term is a group member.
619 * @param int|string $termId
620 * @param null|AssignmentInformation $assignmentInformation
621 * @return bool
622 * @throws Exception
623 */
624 public function isTermMember($termId, &$assignmentInformation = null): bool
625 {
626 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
627 }
628
629 /**
630 * Checks if the post is a group member
631 * @param int|string $postId
632 * @param null|AssignmentInformation $assignmentInformation
633 * @return bool
634 * @throws Exception
635 */
636 public function isPostMember($postId, &$assignmentInformation = null): bool
637 {
638 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
639 }
640
641 /**
642 * Returns the recursive membership.
643 * @param string $objectType The object type.
644 * @param int|string $objectId The object id.
645 * @return array
646 * @throws Exception
647 */
648 public function getRecursiveMembershipForObject(string $objectType, $objectId): array
649 {
650 /**
651 * @var AssignmentInformation $assignmentInformation
652 */
653 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
654 return $assignmentInformation->getRecursiveMembership();
655 }
656
657 return [];
658 }
659
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
665 * @throws Exception
666 */
667 public function isLockedRecursive(string $objectType, $objectId): bool
668 {
669 /**
670 * @var AssignmentInformation $assignmentInformation
671 */
672 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
673 return (count($assignmentInformation->getRecursiveMembership()) > 0);
674 }
675
676 return false;
677 }
678
679 /**
680 * Returns all objects of the given type.
681 * @param string $objectType The object type.
682 * @return array
683 * @throws Exception
684 */
685 public function getAssignedObjectsByType(string $objectType): array
686 {
687 if (isset($this->fullObjectMembership[$objectType]) === false) {
688 try {
689 $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
690 $this->fullObjectMembership[$objectType] = $handler->getFullObjects(
691 $this,
692 $this->config->lockRecursive(),
693 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
694 );
695 } catch (MissingObjectMembershipHandlerException $exception) {
696 $this->fullObjectMembership[$objectType] = [];
697 }
698 }
699
700 return $this->fullObjectMembership[$objectType];
701 }
702
703 /**
704 * Returns the roles assigned to the group.
705 * @return array
706 * @throws Exception
707 */
708 public function getFullRoles(): array
709 {
710 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
711 }
712
713
714 /**
715 * Returns the users assigned to the group.
716 * @return array
717 * @throws Exception
718 */
719 public function getFullUsers(): array
720 {
721 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
722 }
723
724 /**
725 * Returns the terms assigned to the group.
726 * @return array
727 * @throws Exception
728 */
729 public function getFullTerms(): array
730 {
731 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
732 }
733
734 /**
735 * Returns the posts assigned to the group.
736 * @return array
737 * @throws Exception
738 */
739 public function getFullPosts(): array
740 {
741 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE);
742 }
743 }
744