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

767 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->insert(
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 = ($ignoreGeneralType === false) ?
382 " AND (general_object_type = '%s' OR object_type = '%s') " : " AND object_type = '%s' ";
383
384 $query = "DELETE FROM {$this->database->getUserGroupToObjectTable()}
385 WHERE group_id = %d
386 AND group_type = '%s'
387 {$objectTypeQuery}";
388
389 $values = [
390 $this->id,
391 $this->type,
392 $generalObjectType,
393 $objectType
394 ];
395
396 if ($objectId !== null) {
397 $query .= ' AND object_id = %d';
398 $values[] = $objectId;
399 }
400
401 $query = $this->database->prepare($query, $values);
402 $success = ($this->database->query($query) !== false);
403
404 if ($success === true) {
405 $this->resetObjects();
406 }
407
408 return $success;
409 }
410
411 /**
412 * Returns the assigned objects.
413 *
414 * @param string $objectType The object type.
415 *
416 * @return AssignmentInformation[]
417 */
418 public function getAssignedObjects($objectType)
419 {
420 if (isset($this->assignedObjects[$objectType]) === false) {
421 $query = "SELECT object_id AS id, object_type AS objectType, from_date AS fromDate, to_date AS toDate
422 FROM {$this->database->getUserGroupToObjectTable()}
423 WHERE group_id = '%s'
424 AND group_type = '%s'
425 AND object_id != ''
426 AND (general_object_type = '%s' OR object_type = '%s')";
427
428 $parameters = [
429 $this->id,
430 $this->type,
431 $objectType,
432 $objectType
433 ];
434
435 if ($this->ignoreDates === false) {
436 $query .= " AND (from_date IS NULL OR from_date <= '%s') AND (to_date IS NULL OR to_date >= '%s')";
437 $time = $this->wordpress->currentTime('mysql');
438 $parameters = array_merge($parameters, [$time, $time]);
439 }
440
441 $query = $this->database->prepare($query, $parameters);
442 $results = (array)$this->database->getResults($query);
443 $this->assignedObjects[$objectType] = [];
444
445 foreach ($results as $result) {
446 $this->assignedObjects[$objectType][$result->id] = $this->assignmentInformationFactory
447 ->createAssignmentInformation($result->objectType, $result->fromDate, $result->toDate);
448 }
449 }
450
451 return $this->assignedObjects[$objectType];
452 }
453
454 /**
455 * Marks the group as default for the object type.
456 *
457 * @param string $objectType
458 * @param null|int $fromTime
459 * @param null|int $toTime
460 *
461 * @return bool
462 */
463 public function addDefaultType($objectType, $fromTime = null, $toTime = null)
464 {
465 $fromDate = ($fromTime !== null) ? gmdate('Y-m-d H:i:s', $fromTime) : null;
466 $toTime = ($toTime !== null && $toTime <= $fromTime) ? $fromTime + 1 : $toTime;
467 $toDate = ($toTime !== null) ? gmdate('Y-m-d H:i:s', $toTime) : null;
468
469 return $this->addObject($objectType, '', $fromDate, $toDate);
470 }
471
472 /**
473 * Removes the group as default for object type.
474 *
475 * @param string $objectType
476 *
477 * @return bool
478 */
479 public function removeDefaultType($objectType)
480 {
481 return $this->removeObject($objectType, '', true);
482 }
483
484 /**
485 * Returns the object type for which the user group is the default group.
486 *
487 * @return array
488 */
489 public function getDefaultGroupForObjectTypes()
490 {
491 if ($this->defaultTypes === null) {
492 $this->defaultTypes = [];
493
494 $query = "SELECT object_type AS objectType, from_date AS fromDate, to_date AS toDate
495 FROM {$this->database->getUserGroupToObjectTable()}
496 WHERE group_id = '%s'
497 AND group_type = '%s'
498 AND object_id = ''";
499
500 $parameters = [
501 $this->id,
502 $this->type
503 ];
504
505 $query = $this->database->prepare($query, $parameters);
506 $results = (array)$this->database->getResults($query);
507
508 foreach ($results as $result) {
509 $this->defaultTypes[$result->objectType] = [
510 ($result->fromDate !== null) ? strtotime($result->fromDate) : null,
511 ($result->toDate !== null) ? strtotime($result->toDate) : null
512 ];
513 }
514 }
515
516 return $this->defaultTypes;
517 }
518
519 /**
520 * Checks if the group is the default one for the given object type.
521 *
522 * @param string $objectType
523 * @param null|int $fromTime
524 * @param null|int $toTime
525 *
526 * @return bool
527 */
528 public function isDefaultGroupForObjectType($objectType, &$fromTime = null, &$toTime = null)
529 {
530 $defaultGroupForObjectTypes = $this->getDefaultGroupForObjectTypes();
531
532 // Reset reference values anyway
533 $fromTime = null;
534 $toTime = null;
535
536 if (isset($defaultGroupForObjectTypes[$objectType])) {
537 $fromTime = $defaultGroupForObjectTypes[$objectType][0] !== null ?
538 (int)$defaultGroupForObjectTypes[$objectType][0] : null;
539 $toTime = $defaultGroupForObjectTypes[$objectType][1] !== null ?
540 (int)$defaultGroupForObjectTypes[$objectType][1] : null;
541
542 return true;
543 }
544
545 return false;
546 }
547
548 /**
549 * Checks if the object is assigned to the group.
550 *
551 * @param string $objectType The object type.
552 * @param string $objectId The object id.
553 * @param AssignmentInformation|null $assignmentInformation The assignment information object.
554 *
555 * @return bool
556 */
557 public function isObjectAssignedToGroup(
558 $objectType,
559 $objectId,
560 &$assignmentInformation = null
561 ) {
562 $assignmentInformation = null;
563 $assignedObjects = $this->getAssignedObjects($objectType);
564
565 if (isset($assignedObjects[$objectId]) === true) {
566 $assignmentInformation = $assignedObjects[$objectId];
567 return true;
568 }
569
570 return false;
571 }
572
573 /**
574 * Returns a single object.
575 *
576 * @param string $objectType The object type.
577 * @param string $objectId The id of the object which should be checked.
578 * @param AssignmentInformation $assignmentInformation The assignment information
579 *
580 * @return bool
581 */
582 public function isObjectMember(
583 $objectType,
584 $objectId,
585 &$assignmentInformation = null
586 ) {
587 if (isset($this->objectMembership[$objectType][$objectId]) === false) {
588 try {
589 $isMember = $this->objectHandler->getObjectMembershipHandler($objectType)->isMember(
590 $this,
591 $this->config->lockRecursive(),
592 $objectId,
593 $assignmentInformation
594 );
595 } catch (MissingObjectMembershipHandlerException $exception) {
596 $isMember = false;
597 }
598
599 $this->objectMembership[$objectType][$objectId] = ($isMember === true) ?
600 $assignmentInformation : false;
601 }
602
603 $assignmentInformation = ($this->objectMembership[$objectType][$objectId] instanceof AssignmentInformation) ?
604 $this->objectMembership[$objectType][$objectId] : null;
605
606 return ($this->objectMembership[$objectType][$objectId] !== false);
607 }
608
609 /**
610 * Checks if the role is a group member.
611 *
612 * @param string $roleId
613 * @param AssignmentInformation $assignmentInformation
614 *
615 * @return bool
616 */
617 public function isRoleMember($roleId, &$assignmentInformation = null)
618 {
619 return $this->isObjectMember(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, $roleId, $assignmentInformation);
620 }
621
622 /**
623 * Checks if the user is a group member.
624 *
625 * @param int $userId The user id.
626 * @param AssignmentInformation $assignmentInformation The assignment information.
627 *
628 * @return bool
629 */
630 public function isUserMember($userId, &$assignmentInformation = null)
631 {
632 return $this->isObjectMember(ObjectHandler::GENERAL_USER_OBJECT_TYPE, $userId, $assignmentInformation);
633 }
634
635 /**
636 * Checks if the term is a group member.
637 *
638 * @param int $termId
639 * @param AssignmentInformation $assignmentInformation
640 *
641 * @return bool
642 */
643 public function isTermMember($termId, &$assignmentInformation = null)
644 {
645 return $this->isObjectMember(ObjectHandler::GENERAL_TERM_OBJECT_TYPE, $termId, $assignmentInformation);
646 }
647
648 /**
649 * Checks if the post is a group member
650 *
651 * @param int $postId
652 * @param AssignmentInformation $assignmentInformation
653 *
654 * @return bool
655 */
656 public function isPostMember($postId, &$assignmentInformation = null)
657 {
658 return $this->isObjectMember(ObjectHandler::GENERAL_POST_OBJECT_TYPE, $postId, $assignmentInformation);
659 }
660
661 /**
662 * Returns the recursive membership.
663 *
664 * @param string $objectType The object type.
665 * @param string $objectId The object id.
666 *
667 * @return array
668 */
669 public function getRecursiveMembershipForObject($objectType, $objectId)
670 {
671 /**
672 * @var AssignmentInformation $assignmentInformation
673 */
674 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
675 return $assignmentInformation->getRecursiveMembership();
676 }
677
678 return [];
679 }
680
681 /**
682 * Returns true if the requested object is locked recursive.
683 *
684 * @param string $objectType The object type.
685 * @param string $objectId The object id.
686 *
687 * @return bool
688 */
689 public function isLockedRecursive($objectType, $objectId)
690 {
691 /**
692 * @var AssignmentInformation $assignmentInformation
693 */
694 if ($this->isObjectMember($objectType, $objectId, $assignmentInformation) === true) {
695 return (count($assignmentInformation->getRecursiveMembership()) > 0);
696 }
697
698 return false;
699 }
700
701 /**
702 * Returns all objects of the given type.
703 *
704 * @param string $objectType The object type.
705 *
706 * @return array
707 */
708 public function getAssignedObjectsByType($objectType)
709 {
710 if (isset($this->fullObjectMembership[$objectType]) === false) {
711 try {
712 $handler = $this->objectHandler->getObjectMembershipHandler($objectType);
713 $this->fullObjectMembership[$objectType] = $handler->getFullObjects(
714 $this,
715 $this->config->lockRecursive(),
716 ($objectType === $this->objectHandler->getGeneralObjectType($objectType)) ? null : $objectType
717 );
718 } catch (MissingObjectMembershipHandlerException $exception) {
719 $this->fullObjectMembership[$objectType] = [];
720 }
721 }
722
723 return $this->fullObjectMembership[$objectType];
724 }
725
726 /**
727 * Returns the roles assigned to the group.
728 *
729 * @return array
730 */
731 public function getFullRoles()
732 {
733 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
734 }
735
736
737 /**
738 * Returns the users assigned to the group.
739 *
740 * @return array
741 */
742 public function getFullUsers()
743 {
744 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_USER_OBJECT_TYPE);
745 }
746
747 /**
748 * Returns the terms assigned to the group.
749 *
750 * @return array
751 */
752 public function getFullTerms()
753 {
754 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_TERM_OBJECT_TYPE);
755 }
756
757 /**
758 * Returns the posts assigned to the group.
759 *
760 * @return array
761 */
762 public function getFullPosts()
763 {
764 return $this->getAssignedObjectsByType(ObjectHandler::GENERAL_POST_OBJECT_TYPE);
765 }
766 }
767