PluginProbe
User Access Manager / 2.1.2
User Access Manager v2.1.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
user-access-manager / src / UserGroup / AbstractUserGroup.php

AbstractUserGroup.php in User Access Manager 2.1.2, at src/UserGroup/AbstractUserGroup.php

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