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

769 lines 20.0 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 namespace UserAccessManager\UserGroup;
16
17 use UserAccessManager\Config\MainConfig;
18 use UserAccessManager\Database\Database;
19 use UserAccessManager\Object\ObjectHandler;
20 use UserAccessManager\ObjectMembership\MissingObjectMembershipHandlerException;
21 use UserAccessManager\ObjectMembership\ObjectMembershipHandlerFactory;
22 use UserAccessManager\Util\Util;
23 use UserAccessManager\Wrapper\Php;
24 use UserAccessManager\Wrapper\Wordpress;
25
26 /**
27 * Class AbstractUserGroup
28 *
29 * @package UserAccessManager\UserGroup
30 */
31 abstract class AbstractUserGroup
32 {
33 const NONE_ROLE = '_none-role_';
34
35 /**
36 * @var Php
37 */
38 protected $php;
39
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 *
142 * @throws UserGroupTypeException
143 */
144 public function __construct(
145 Php $php,
146 Wordpress $wordpress,
147 Database $database,
148 MainConfig $config,
149 Util $util,
150 ObjectHandler $objectHandler,
151 AssignmentInformationFactory $assignmentInformationFactory,
152 $id = null
153 ) {
154 if ($this->type === null) {
155 throw new UserGroupTypeException('User group type must not null.');
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;
166 }
167
168 /*
169 * Primary values.
170 */
171
172 /**
173 * Returns the group _iId.
174 *
175 * @return string
176 */
177 public function getId()
178 {
179 return $this->id;
180 }
181
182 /**
183 * Returns the user group type.
184 *
185 * @return string
186 */
187 public function getType()
188 {
189 return $this->type;
190 }
191
192 /**
193 * Returns the group name.
194 *
195 * @return string
196 */
197 public function getName()
198 {
199 return $this->name;
200 }
201
202 /**
203 * Sets the group name.
204 *
205 * @param string $name The new group name.
206 */
207 public function setName($name)
208 {
209 $this->name = $name;
210 }
211
212 /**
213 * Returns the group description.
214 *
215 * @return string
216 */
217 public function getDescription()
218 {
219 return $this->description;
220 }
221
222 /**
223 * Sets the group description.
224 *
225 * @param string $description The new group description.
226 */
227 public function setDescription($description)
228 {
229 $this->description = $description;
230 }
231
232 /**
233 * Returns the read access.
234 *
235 * @return string
236 */
237 public function getReadAccess()
238 {
239 return $this->readAccess;
240 }
241
242 /**
243 * Sets the read access.
244 *
245 * @param string $readAccess The read access.
246 */
247 public function setReadAccess($readAccess)
248 {
249 $this->readAccess = $readAccess;
250 }
251
252 /**
253 * Returns the write access.
254 *
255 * @return string
256 */
257 public function getWriteAccess()
258 {
259 return $this->writeAccess;
260 }
261
262 /**
263 * Sets the write access.
264 *
265 * @param string $writeAccess The write access.
266 */
267 public function setWriteAccess($writeAccess)
268 {
269 $this->writeAccess = $writeAccess;
270 }
271
272 /**
273 * Sets the ignore dates flag.
274 *
275 * @param bool $ignoreDates
276 */
277 public function setIgnoreDates($ignoreDates)
278 {
279 if ($this->ignoreDates !== $ignoreDates) {
280 $this->resetObjects();
281 }
282
283 $this->ignoreDates = $ignoreDates;
284 }
285
286 /**
287 * Resets the objects
288 */
289 protected function resetObjects()
290 {
291 $this->assignedObjects = [];
292 $this->objectMembership = [];
293 $this->fullObjectMembership = [];
294 }
295
296 /**
297 * Deletes the user group.
298 *
299 * @return bool
300 */
301 public function delete()
302 {
303 $allObjectTypes = $this->objectHandler->getAllObjectTypes();
304
305 foreach ($allObjectTypes as $objectType) {
306 $this->removeObject($objectType);
307 }
308
309 return true;
310 }
311
312 /**
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
321 */
322 public function addObject($objectType, $objectId, $fromDate = null, $toDate = null)
323 {
324 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
325
326 if ($generalObjectType === null
327 || $this->objectHandler->isValidObjectType($objectType) === false
328 ) {
329 return false;
330 }
331
332 $return = $this->database->replace(
333 $this->database->getUserGroupToObjectTable(),
334 [
335 'group_id' => $this->id,
336 'group_type' => $this->type,
337 'object_id' => $objectId,
338 'general_object_type' => $generalObjectType,
339 'object_type' => $objectType,
340 'from_date' => $fromDate,
341 'to_date' => $toDate
342 ],
343 [
344 '%s',
345 '%s',
346 '%s',
347 '%s',
348 '%s',
349 '%s',
350 '%s'
351 ]
352 );
353
354 if ($return !== false) {
355 $this->resetObjects();
356 return true;
357 }
358
359 return false;
360 }
361
362 /**
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
370 */
371 public function removeObject($objectType, $objectId = null, $ignoreGeneralType = false)
372 {
373 $generalObjectType = $this->objectHandler->getGeneralObjectType($objectType);
374
375 if ($generalObjectType === null
376 || $this->objectHandler->isValidObjectType($objectType) === false
377 ) {
378 return false;
379 }
380
381 $objectTypeQuery = " AND object_type = '%s' ";
382 $values = [
383 $this->id,
384 $this->type,
385 $objectType
386 ];
387
388 if ($ignoreGeneralType === false) {
389 $objectTypeQuery = " AND (object_type = '%s' OR general_object_type = '%s') ";
390 $values[] = $generalObjectType;
391 }
392
393 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
394 WHERE group_id = %d
395 AND group_type = '%s'
396 {$objectTypeQuery}";
397
398 if ($objectId !== null) {
399 $query .= ' AND object_id = %d';
400 $values[] = $objectId;
401 }
402
403 $query = $this->database->prepare($query, $values);
404 $success = ($this->database->query($query) !== false);
405
406 if ($success === true) {
407 $this->resetObjects();
408 }
409
410 return $success;
411 }
412
413 /**
414 * Returns the assigned objects.
415 *
416 * @param string $objectType The object type.
417 *
418 * @return AssignmentInformation[]
419 */
420 public function getAssignedObjects($objectType)
421 {
422 if (isset($this->assignedObjects[$objectType]) === false) {
423 $query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate
424 FROM {$this->database->getUserGroupToObjectTable()}
425 WHERE group_id = '%s'
426 AND group_type = '%s'
427 AND object_id != ''
428 AND (general_object_type = '%s' OR object_type = '%s')";
429
430 $parameters = [
431 $this->id,
432 $this->type,
433 $objectType,
434 $objectType
435 ];
436
437 if ($this->ignoreDates === false) {
438 $query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')";
439 $time = $this->wordpress->currentTime('mysql');
440 $parameters = array_merge($parameters, [$time, $time]);
441 }
442
443 $query = $this->database->prepare($query, $parameters);
444 $results = (array)$this->database->getResults($query);
445 $this->assignedObjects[$objectType] = [];
446
447 foreach ($results as $result) {
448 $this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory
449 ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate);
450 }
451 }
452
453 return $this->assignedObjects[$objectType];
454 }
455
456 /**
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
464 */
465 public function addDefaultType($objectType, $fromTime = null, $toTime = null)
466 {
467 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
468 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
469 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
470
471 return $this->addObject($objectType, '', $fromDate, $toDate);
472 }
473
474 /**
475 * Removes the group as default for object type.
476 *
477 * @param string $objectType
478 *
479 * @return bool
480 */
481 public function removeDefaultType($objectType)
482 {
483 return $this->removeObject($objectType, '', true);
484 }
485
486 /**
487 * Returns the object type for which the user group is the default group.
488 *
489 * @return array
490 */
491 public function getDefaultGroupForObjectTypes()
492 {
493 if ($this->defaultTypes === null) {
494 $this->defaultTypes = [];
495
496 $query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate
497 FROM {$this->database->getUserGroupToObjectTable()}
498 WHERE group_id = '%s'
499 AND group_type = '%s'
500 AND object_id = ''";
501
502 $parameters = [
503 $this->id,
504 $this->type
505 ];
506
507 $query = $this->database->prepare($query, $parameters);
508 $results = (array)$this->database->getResults($query);
509
510 foreach ($results as $result) {
511 $this->defaultTypes[$result->objectType] = [
512 ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
513 ($result->toDate !== null) ? strtotime($result->toDate) : null
514 ];
515 }
516 }
517
518 return $this->defaultTypes;
519 }
520
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)
531 {
532 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
533
534 // Reset reference values anyway
535 $fromTime = null;
536 $toTime = null;
537
538 if (isset($defaultGroupForObjectTypes[$objectType])) {
539 $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ?
540 (int)$defaultGroupForObjectTypes[$objectType][0] : null;
541 $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ?
542 (int)$defaultGroupForObjectTypes[$objectType][1] : null;
543
544 return true;
545 }
546
547 return false;
548 }
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 */
559 public function isObjectAssignedToGroup(
560 $objectType,
561 $objectId,
562 &$assignmentInformation = null
563 ) {
564 $assignmentInformation = null;
565 $assignedObjects = $this->getAssignedObjects($objectType);
566
567 if (isset($assignedObjects[$objectId]) === true) {
568 $assignmentInformation = $assignedObjects[$objectId];
569 return true;
570 }
571
572 return false;
573 }
574
575 /**
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
583 */
584 public function isObjectMember(
585 $objectType,
586 $objectId,
587 &$assignmentInformation = null
588 ) {
589 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
590 try {
591 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
592 $this,
593 $this->config->lockRecursive(),
594 $objectId,
595 $assignmentInformation
596 );
597 } catch (MissingObjectMembershipHandlerException $exception) {
598 $isMember = false;
599 }
600
601 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
602 $assignmentInformation : false;
603 }
604
605 $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ?
606 $this->objectMembership[$objectType][$objectId] : null;
607
608 return ($this->objectMembership[$objectType][$objectId] !== false);
609 }
610
611 /**
612 * Checks if the role is a group member.
613 *
614 * @param string $roleId
615 * @param AssignmentInformation $assignmentInformation
616 *
617 * @return bool
618 */
619 public function isRoleMember($roleId, &$assignmentInformation = null)
620 {
621 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
622 }
623
624 /**
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
631 */
632 public function isUserMember($userId, &$assignmentInformation = null)
633 {
634 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
635 }
636
637 /**
638 * Checks if the term is a group member.
639 *
640 * @param int $termId
641 * @param AssignmentInformation $assignmentInformation
642 *
643 * @return bool
644 */
645 public function isTermMember($termId, &$assignmentInformation = null)
646 {
647 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
648 }
649
650 /**
651 * Checks if the post is a group member
652 *
653 * @param int $postId
654 * @param AssignmentInformation $assignmentInformation
655 *
656 * @return bool
657 */
658 public function isPostMember($postId, &$assignmentInformation = null)
659 {
660 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
661 }
662
663 /**
664 * Returns the recursive membership.
665 *
666 * @param string $objectType The object type.
667 * @param string $objectId The object id.
668 *
669 * @return array
670 */
671 public function getRecursiveMembershipForObject($objectType, $objectId)
672 {
673 /**
674 * @var AssignmentInformation $assignmentInformation
675 */
676 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
677 return $assignmentInformation->getRecursiveMembership();
678 }
679
680 return [];
681 }
682
683 /**
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
690 */
691 public function isLockedRecursive($objectType, $objectId)
692 {
693 /**
694 * @var AssignmentInformation $assignmentInformation
695 */
696 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
697 return (count($assignmentInformation->getRecursiveMembership()) > 0);
698 }
699
700 return false;
701 }
702
703 /**
704 * Returns all objects of the given type.
705 *
706 * @param string $objectType The object type.
707 *
708 * @return array
709 */
710 public function getAssignedObjectsByType($objectType)
711 {
712 if (isset($this->fullObjectMembership[$objectType]) === false) {
713 try {
714 $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
715 $this->fullObjectMembership[$objectType] = $handler->getFullObjects(
716 $this,
717 $this->config->lockRecursive(),
718 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
719 );
720 } catch (MissingObjectMembershipHandlerException $exception) {
721 $this->fullObjectMembership[$objectType] = [];
722 }
723 }
724
725 return $this->fullObjectMembership[$objectType];
726 }
727
728 /**
729 * Returns the roles assigned to the group.
730 *
731 * @return array
732 */
733 public function getFullRoles()
734 {
735 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
736 }
737
738
739 /**
740 * Returns the users assigned to the group.
741 *
742 * @return array
743 */
744 public function getFullUsers()
745 {
746 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
747 }
748
749 /**
750 * Returns the terms assigned to the group.
751 *
752 * @return array
753 */
754 public function getFullTerms()
755 {
756 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
757 }
758
759 /**
760 * Returns the posts assigned to the group.
761 *
762 * @return array
763 */
764 public function getFullPosts()
765 {
766 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE);
767 }
768 }
769