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

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