PluginProbe
User Access Manager / 1.2.7.4
User Access Manager v1.2.7.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 / class / UamUserGroup.class.php

UamUserGroup.class.php in User Access Manager 1.2.7.4, at class/UamUserGroup.class.php

1,120 lines 33.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UamUserGroup.class.php
4 *
5 * The UamUserGroup class file.
6 *
7 * PHP versions 5
8 *
9 * @category UserAccessManager
10 * @package UserAccessManager
11 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
12 * @copyright 2008-2016 Alexander Schneider
13 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
14 * @version SVN: $Id$
15 * @link http://wordpress.org/extend/plugins/user-access-manager/
16 */
17
18 /**
19 * The user group class.
20 *
21 * @category UserAccessManager
22 * @package UserAccessManager
23 * @author Alexander Schneider <alexanderschneider85@gmail.com>
24 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
25 * @link http://wordpress.org/extend/plugins/user-access-manager/
26 */
27
28 class UamUserGroup
29 {
30 protected $_oAccessHandler = null;
31 protected $_iId = null;
32 protected $_sGroupName = null;
33 protected $_sGroupDesc = null;
34 protected $_sReadAccess = null;
35 protected $_sWriteAccess = null;
36 protected $_sIpRange = null;
37 protected $_aRoles = array();
38 protected $_aObjects = null;
39 protected $_aSingleObjects = null;
40 protected $_aAssignedObjects = null;
41 protected $_aPlObjects = array();
42 protected $_aSetRecursive = array();
43 protected $_aValidObjectTypes = array();
44 protected $_aObjectsInCategory = null;
45 protected $_aObjectIsMember = array();
46
47 /**
48 * Constructor
49 *
50 * @param UamAccessHandler $oUamAccessHandler The access handler object.
51 * @param integer $iId The id of the user group.
52 */
53 public function __construct(UamAccessHandler &$oUamAccessHandler, $iId = null)
54 {
55 $this->_oAccessHandler = $oUamAccessHandler;
56
57 if ($iId !== null) {
58 /**
59 * @var wpdb $wpdb
60 */
61 global $wpdb;
62
63 $this->_iId = $iId;
64
65 $aDbUserGroup = $wpdb->get_row(
66 "SELECT *
67 FROM ".DB_ACCESSGROUP."
68 WHERE ID = ".$this->getId()."
69 LIMIT 1",
70 ARRAY_A
71 );
72
73 $this->_sGroupName = $aDbUserGroup['groupname'];
74 $this->_sGroupDesc = $aDbUserGroup['groupdesc'];
75 $this->_sReadAccess = $aDbUserGroup['read_access'];
76 $this->_sWriteAccess = $aDbUserGroup['write_access'];
77 $this->_sIpRange = $aDbUserGroup['ip_range'];
78 }
79
80 //Create default values for the objects.
81 foreach ($this->getAllObjectTypes() as $sObjectType) {
82 $this->_aAssignedObjects[$sObjectType] = null;
83 $this->_aObjects[$sObjectType] = array(
84 'real' => -1,
85 'full' => -1
86 );
87
88 if ($iId !== null) {
89 $this->getObjectsFromType($sObjectType);
90 }
91 }
92 }
93
94 /**
95 * Returns the user access handler object.
96 *
97 * @return UamAccessHandler
98 */
99 public function &getAccessHandler()
100 {
101 return $this->_oAccessHandler;
102 }
103
104 /**
105 * Deletes the user group.
106 *
107 * @return boolean
108 */
109 public function delete()
110 {
111 if ($this->_iId == null) {
112 return false;
113 }
114
115 /**
116 * @var wpdb $wpdb
117 */
118 global $wpdb;
119
120 $wpdb->query(
121 "DELETE FROM " . DB_ACCESSGROUP . "
122 WHERE ID = $this->_iId LIMIT 1"
123 );
124
125 foreach ($this->getAllObjectTypes() as $sObjectType) {
126 $this->_deleteObjectsFromDb($sObjectType);
127 }
128
129 return true;
130 }
131
132 /**
133 * Saves the user group.
134 *
135 * @param boolean $blRemoveOldAssignments If false we will not remove old entries.
136 *
137 * @return null
138 */
139 public function save($blRemoveOldAssignments = true)
140 {
141 /**
142 * @var wpdb $wpdb
143 */
144 global $wpdb;
145
146 if ($this->_iId == null) {
147 $wpdb->query(
148 "INSERT INTO " . DB_ACCESSGROUP . " (
149 ID,
150 groupname,
151 groupdesc,
152 read_access,
153 write_access,
154 ip_range
155 )
156 VALUES (
157 NULL,
158 '" . $this->_sGroupName . "',
159 '" . $this->_sGroupDesc . "',
160 '" . $this->_sReadAccess . "',
161 '" . $this->_sWriteAccess . "',
162 '" . $this->_sIpRange . "'
163 )"
164 );
165
166 $this->_iId = $wpdb->insert_id;
167 } else {
168 $wpdb->query(
169 "UPDATE " . DB_ACCESSGROUP . "
170 SET groupname = '" . $this->_sGroupName . "',
171 groupdesc = '" . $this->_sGroupDesc . "',
172 read_access = '" . $this->_sReadAccess . "',
173 write_access = '" . $this->_sWriteAccess . "',
174 ip_range = '" . $this->_sIpRange . "'
175 WHERE ID = " . $this->_iId
176 );
177
178 if ($blRemoveOldAssignments === true) {
179 foreach ($this->getAllObjectTypes() as $sObjectType) {
180 //Delete form database
181 $this->_deleteObjectsFromDb($sObjectType);
182 }
183 }
184 }
185
186 foreach ($this->getAllObjectTypes() as $sObjectType) {
187 $aKeys = $this->_getAssignedObjects($sObjectType);
188
189 if (count($aKeys) > 0) {
190 $sSql = $this->_getSqlQuery($sObjectType, 'insert', $aKeys);
191 $wpdb->query($sSql);
192 }
193 }
194 }
195
196
197 /*
198 * Primary values.
199 */
200
201 /**
202 * Returns the group _iId.
203 *
204 * @return integer
205 */
206 public function getId()
207 {
208 return $this->_iId;
209 }
210
211 /**
212 * Returns the group name.
213 *
214 * @return string
215 */
216 public function getGroupName()
217 {
218 return $this->_sGroupName;
219 }
220
221 /**
222 * Sets the group name.
223 *
224 * @param string $sGroupName The new group name.
225 *
226 * @return null
227 */
228 public function setGroupName($sGroupName)
229 {
230 $this->_sGroupName = $sGroupName;
231 }
232
233 /**
234 * Returns the group description.
235 *
236 * @return string
237 */
238 public function getGroupDesc()
239 {
240 return $this->_sGroupDesc;
241 }
242
243 /**
244 * Sets the group description.
245 *
246 * @param string $sGroupDesc The new group description.
247 *
248 * @return null
249 */
250 public function setGroupDesc($sGroupDesc)
251 {
252 $this->_sGroupDesc = $sGroupDesc;
253 }
254
255 /**
256 * Returns the read access.
257 *
258 * @return string
259 */
260 public function getReadAccess()
261 {
262 return $this->_sReadAccess;
263 }
264
265 /**
266 * Sets the read access.
267 *
268 * @param string $sReadAccess The read access.
269 *
270 * @return null
271 */
272 public function setReadAccess($sReadAccess)
273 {
274 $this->_sReadAccess = $sReadAccess;
275 }
276
277 /**
278 * Returns the write access.
279 *
280 * @return string
281 */
282 public function getWriteAccess()
283 {
284 return $this->_sWriteAccess;
285 }
286
287 /**
288 * Sets the write access.
289 *
290 * @param string $sWriteAccess The write access.
291 *
292 * @return null
293 */
294 public function setWriteAccess($sWriteAccess)
295 {
296 $this->_sWriteAccess = $sWriteAccess;
297 }
298
299 /**
300 * Returns the ip range.
301 *
302 * @param string $sType The return type.
303 *
304 * @return array|string
305 */
306 public function getIpRange($sType = null)
307 {
308 if ($sType == 'string') {
309 return $this->_sIpRange;
310 }
311
312 $aIpRange = explode(';', $this->_sIpRange);
313
314 if ($aIpRange[0] == null) {
315 return null;
316 }
317
318 return $aIpRange;
319 }
320
321 /**
322 * Sets the ip range.
323 *
324 * @param string|array $mIpRange The new ip range.
325 *
326 * @return null
327 */
328 public function setIpRange($mIpRange)
329 {
330 if (is_array($mIpRange)) {
331 $mIpRange = implode(';', $mIpRange);
332 }
333
334 $this->_sIpRange = $mIpRange;
335 }
336
337 /**
338 * Returns all _aObjects types.
339 *
340 * @return array
341 */
342 public function getAllObjectTypes()
343 {
344 return $this->getAccessHandler()->getAllObjectTypes();
345 }
346
347 /**
348 * Checks if the object type is a valid one.
349 *
350 * @param string $sObjectType The object type to check.
351 *
352 * @return boolean
353 */
354 public function isValidObjectType($sObjectType)
355 {
356 return $this->getAccessHandler()->isValidObjectType($sObjectType);
357 }
358
359 /**
360 * Sets the recursive object membership.
361 *
362 * @param string $sObjectType The object type.
363 * @param integer $iObjectId The object id.
364 * @param array $aObjectMembership The membership.
365 *
366 * @return null
367 */
368 public function setRecursiveMembership($sObjectType, $iObjectId, $aObjectMembership)
369 {
370 if (!isset($this->_aSetRecursive[$sObjectType])) {
371 $this->_aSetRecursive[$sObjectType] = array();
372 }
373
374 $this->_aSetRecursive[$sObjectType][$iObjectId] = $aObjectMembership;
375 }
376
377 /**
378 * Returns the recursive membership.
379 *
380 * @param string $sObjectType The object type.
381 * @param integer $iObjectId The object id.
382 * @param string $sCurObjectType The current object type.
383 *
384 * @return array
385 */
386 public function getRecursiveMembershipForObjectType($sObjectType, $iObjectId, $sCurObjectType)
387 {
388 if (isset($this->_aSetRecursive[$sObjectType])
389 && isset($this->_aSetRecursive[$sObjectType][$iObjectId])
390 && isset($this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType])
391 ) {
392 return $this->_aSetRecursive[$sObjectType][$iObjectId][$sCurObjectType];
393 }
394
395 return array();
396 }
397
398 /**
399 * Returns true if the requested object is locked recursive.
400 *
401 * @param string $sObjectType The object type.
402 * @param integer $iObjectId The object id.
403 *
404 * @return boolean
405 */
406 public function isLockedRecursive($sObjectType, $iObjectId)
407 {
408 if (isset($this->_aSetRecursive[$sObjectType]) && isset($this->_aSetRecursive[$sObjectType][$iObjectId])) {
409 return true;
410 }
411
412 return false;
413 }
414
415
416 /*
417 * Meta functions.
418 */
419
420 /**
421 * Magic method getter.
422 *
423 * @param string $sName The name of the function
424 * @param array $aArguments The arguments for the function
425 *
426 * @return mixed
427 */
428 public function __call($sName, $aArguments)
429 {
430 $oUam = $this->getAccessHandler()->getUserAccessManager();
431
432 $sPrefix = '';
433
434 if ($oUam->startsWith($sName, 'add')) {
435 $sPrefix = 'add';
436 } elseif ($oUam->startsWith($sName, 'remove')) {
437 $sPrefix = 'remove';
438 } elseif ($oUam->startsWith($sName, 'isMember')) {
439 $sPrefix = 'isMember';
440 }
441
442 $sObjectType = str_replace($sPrefix, '', $sName);
443 $sObjectType = strtolower($sObjectType);
444
445 $iObjectId = $aArguments[0];
446
447 if ($sPrefix == 'add') {
448 return $this->addObject(
449 $sObjectType,
450 $iObjectId
451 );
452 } elseif ($sPrefix == 'remove') {
453 return $this->removeObject(
454 $sObjectType,
455 $iObjectId
456 );
457 } elseif ($sPrefix == 'ismember') {
458 $blWithInfo = $aArguments[1];
459
460 return $this->objectIsMember(
461 $sObjectType,
462 $iObjectId,
463 $blWithInfo
464 );
465 }
466
467 return null;
468 }
469
470 /**
471 * Returns the sql query.
472 *
473 * @param string $sObjectType The object type.
474 * @param string $sAction The sql action.
475 * @param array $aKeys The keys for the insert query.
476 *
477 * @return string
478 */
479 protected function _getSqlQuery($sObjectType, $sAction, $aKeys = array())
480 {
481 $sSql = '';
482
483 if ($sAction == 'select') {
484 $sSql = "SELECT object_id as id
485 FROM ".DB_ACCESSGROUP_TO_OBJECT."
486 WHERE group_id = ".$this->getId()."
487 AND object_type = '".$sObjectType ."'";
488 } elseif ($sAction == 'delete') {
489 $sSql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT."
490 WHERE group_id = ".$this->getId()."
491 AND object_type = '".$sObjectType ."'";
492 } elseif ($sAction == 'insert') {
493 $sSql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." (
494 group_id,
495 object_id,
496 object_type
497 ) VALUES ";
498
499 foreach ($aKeys as $sKey) {
500 $sKey = trim($sKey);
501 $sSql .= "('".$this->getId()."', '".$sKey."', '".$sObjectType."'), ";
502 }
503
504 $sSql = rtrim($sSql, ', ');
505 $sSql .= " ON DUPLICATE KEY UPDATE group_id = group_id ";
506 }
507
508 return $sSql;
509 }
510
511 /**
512 * Adds a object of the given type.
513 *
514 * @param string $sObjectType The object type.
515 * @param integer $iObjectId The object _iId.
516 *
517 * @return null
518 */
519 public function addObject($sObjectType, $iObjectId)
520 {
521 if (!$this->isValidObjectType($sObjectType)) {
522 return;
523 }
524
525 $this->getAccessHandler()->unsetUserGroupsForObject();
526 $this->getObjectsFromType($sObjectType);
527
528 $oObject = new stdClass();
529 $oObject->iId = $iObjectId;
530
531 $this->_aObjects[$sObjectType]['real'][$iObjectId] = $oObject;
532 $this->_aObjects[$sObjectType]['full'] = -1;
533
534 $this->_aAssignedObjects[$sObjectType][$iObjectId] = $iObjectId;
535 }
536
537 /**
538 * Removes a object of the given type.
539 *
540 * @param string $sObjectType The object type.
541 * @param integer $sObjectId The object _iId.
542 *
543 * @return null
544 */
545 public function removeObject($sObjectType, $sObjectId)
546 {
547 if (!$this->isValidObjectType($sObjectType)) {
548 return;
549 }
550
551 $this->getAccessHandler()->unsetUserGroupsForObject();
552 $this->getObjectsFromType($sObjectType);
553
554 unset($this->_aObjects[$sObjectType]['real'][$sObjectId]);
555 $this->_aObjects[$sObjectType]['full'] = -1;
556
557 unset($this->_aSingleObjects[$sObjectType][$sObjectId]);
558 unset($this->_aAssignedObjects[$sObjectType][$sObjectId]);
559 }
560
561 /**
562 * Returns the assigned _aObjects.
563 *
564 * @param string $sObjectType The object type.
565 *
566 * @return array
567 */
568 protected function _getAssignedObjects($sObjectType)
569 {
570 if ($this->_aAssignedObjects[$sObjectType] !== null) {
571 return $this->_aAssignedObjects[$sObjectType];
572 }
573
574 $sCacheKey = '_getAssignedObjects|'.$sObjectType.'|'.$this->getId();
575 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
576 $aAssignedObjects = $oUserAccessManager->getFromCache($sCacheKey);
577
578 if ($aAssignedObjects !== null) {
579 $this->_aAssignedObjects[$sObjectType] = $aAssignedObjects;
580 } else {
581 /**
582 * @var wpdb $wpdb
583 */
584 global $wpdb;
585
586 $aDbObjects = $wpdb->get_results(
587 $this->_getSqlQuery($sObjectType, 'select')
588 );
589
590 $this->_aAssignedObjects[$sObjectType] = array();
591
592 foreach ($aDbObjects as $oDbObject) {
593 $this->_aAssignedObjects[$sObjectType][$oDbObject->id]
594 = $oDbObject->id;
595 }
596
597 $oUserAccessManager->addToCache($sCacheKey, $this->_aAssignedObjects[$sObjectType]);
598 }
599
600 return $this->_aAssignedObjects[$sObjectType];
601 }
602
603 /**
604 * Checks if the object is assigned to the group.
605 *
606 * @param string $sObjectType The object type.
607 * @param integer $iObjectId The object _iId.
608 *
609 * @return boolean
610 */
611 protected function _isObjectAssignedToGroup($sObjectType, $iObjectId)
612 {
613 $aAssignedObjects = $this->_getAssignedObjects($sObjectType);
614 return isset($aAssignedObjects[$iObjectId]);
615 }
616
617 /**
618 * Unset the objects.
619 *
620 * @param string $sObjectType The object type.
621 * @param boolean $blPlusRemove If true also database entries will remove.
622 *
623 * @return null;
624 */
625 public function unsetObjects($sObjectType, $blPlusRemove = false)
626 {
627 if (!$this->isValidObjectType($sObjectType)) {
628 return;
629 }
630
631 if ($blPlusRemove) {
632 $this->_deleteObjectsFromDb($sObjectType);
633 }
634
635 $this->_aAssignedObjects[$sObjectType] = array();
636 $this->getAccessHandler()->getUserAccessManager()->flushCache();
637
638 $this->_aObjects[$sObjectType] = array(
639 'real' => array(),
640 'full' => array(),
641 );
642 }
643
644 /**
645 * Removes all _aObjects from the user group.
646 *
647 * @param string $sObjectType The object type.
648 *
649 * @return null
650 */
651 protected function _deleteObjectsFromDb($sObjectType)
652 {
653 if (isset($this->_iId)) {
654 /**
655 * @var wpdb $wpdb
656 */
657 global $wpdb;
658
659 $sQuery = $this->_getSqlQuery($sObjectType, 'delete');
660 $wpdb->query($sQuery);
661 }
662 }
663
664 /**
665 * Checks if the given object is a member of the group.
666 *
667 * @param string $sObjectType The object type.
668 * @param integer $iObjectId The _iId of the object which should be checked.
669 * @param boolean $blWithInfo If true then we return additional info.
670 *
671 * @return boolean|array
672 */
673 public function objectIsMember($sObjectType, $iObjectId, $blWithInfo = false)
674 {
675 $sCacheKey = $sObjectType.'|'.$iObjectId;
676 $sCacheKey .= ($blWithInfo) ? '|wi' : '|ni';
677
678 if (!isset($this->_aObjectIsMember[$sCacheKey])) {
679 $this->_aObjectIsMember[$sCacheKey] = false;
680
681 if ($this->isValidObjectType($sObjectType)) {
682 $oObject = $this->_getSingleObject($sObjectType, $iObjectId, 'full');
683
684 if ($oObject !== null) {
685 if ($blWithInfo && isset($oObject->recursiveMember)) {
686 $this->_aObjectIsMember[$sCacheKey] = $oObject->recursiveMember;
687 } else {
688 $this->_aObjectIsMember[$sCacheKey] = true;
689 }
690 }
691 }
692 }
693
694 return $this->_aObjectIsMember[$sCacheKey];
695 }
696
697 /**
698 * Returns all objects of the given type.
699 *
700 * @param string $sObjectType The object type.
701 * @param string $sType The return type, could be real or full.
702 *
703 * @return array
704 */
705 function getObjectsFromType($sObjectType, $sType = 'real')
706 {
707 if (!$this->isValidObjectType($sObjectType)) {
708 return null;
709 }
710
711 if ($this->_iId == null || $sType != 'real' && $sType != 'full') {
712 return array();
713 }
714
715 if (isset($this->_aObjects[$sObjectType])
716 && isset($this->_aObjects[$sObjectType][$sType])
717 && $this->_aObjects[$sObjectType][$sType] != -1
718 ) {
719 return $this->_aObjects[$sObjectType][$sType];
720 } else {
721 $this->_aObjects[$sObjectType][$sType] = array();
722 }
723
724 $aObjectIds = $this->_getAssignedObjects($sObjectType);
725
726 foreach ($aObjectIds as $sObjectId) {
727 $oObject = $this->_getSingleObject($sObjectType, $sObjectId, $sType);
728
729 if ($oObject !== null) {
730 $this->_aObjects[$sObjectType][$sType][$oObject->id] = $oObject;
731 }
732 }
733
734 if ($sType == 'full' && $this->getAccessHandler()->isPostableType($sObjectType) && $sObjectType != 'role') {
735 if ($sObjectType == 'category') {
736 $this->_aObjects[$sObjectType][$sType] = $this->getFullCategories($this->_aObjects[$sObjectType][$sType]);
737 } elseif ($sObjectType == 'user') {
738 $this->_aObjects[$sObjectType][$sType] = $this->getFullUsers();
739 } else {
740 $oPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
741 $this->_aObjects[$sObjectType][$sType] = $oPlObject['reference']->{$oPlObject['getFullObjects']}(
742 $this->_aObjects[$sObjectType][$sType],
743 $this
744 );
745 }
746 }
747
748 return $this->_aObjects[$sObjectType][$sType];
749 }
750
751 /**
752 * Returns a single object.
753 *
754 * @param string $sObjectType The object type.
755 * @param integer $iObjectId The _iId of the object which should be checked.
756 * @param string $sType The return type. Can be real or full.
757 *
758 * @return object
759 */
760 protected function _getSingleObject($sObjectType, $iObjectId, $sType)
761 {
762 if (!isset($this->_aSingleObjects[$sObjectType])
763 || !isset($this->_aSingleObjects[$sObjectType][$iObjectId])
764 ) {
765 $this->_aSingleObjects[$sObjectType][$iObjectId] = null;
766 $aIsRecursiveMember = array();
767
768 if ($sType == 'full' && $sObjectType != 'role') {
769 if ($sObjectType == 'category') {
770 $aIsRecursiveMember = $this->_getFullCategory($iObjectId);
771 } elseif ($sObjectType == 'user') {
772 $aIsRecursiveMember = $this->_getFullUser($iObjectId);
773 } elseif ($this->getAccessHandler()->isPostableType($sObjectType)) {
774 $aIsRecursiveMember = $this->_getFullPost($sObjectType, $iObjectId);
775 } else {
776 $aIsRecursiveMember = $this->_getFullPlObject($sObjectType, $iObjectId);
777 }
778 }
779
780 if ($aIsRecursiveMember != array()
781 || $this->_isObjectAssignedToGroup($sObjectType, $iObjectId)
782 ) {
783 $oObject = new stdClass();
784 $oObject->id = $iObjectId;
785
786 if ($aIsRecursiveMember != array()) {
787 $oObject->recursiveMember = $aIsRecursiveMember;
788 }
789
790 $this->_aSingleObjects[$sObjectType][$iObjectId] = $oObject;
791 }
792 }
793
794 return $this->_aSingleObjects[$sObjectType][$iObjectId];
795 }
796
797
798 /*
799 * Group users functions.
800 */
801
802 /**
803 * Returns a single user.
804 *
805 * @param integer $iObjectId The object _iId.
806 *
807 * @return array
808 */
809 protected function _getFullUser($iObjectId)
810 {
811 $aIsRecursiveMember = array();
812
813 global $wpdb;
814
815 $oCurUserData = get_userdata($iObjectId);
816
817 if (isset($oCurUserData->{$wpdb->prefix . "capabilities"})) {
818 $aCapabilities = $oCurUserData->{$wpdb->prefix . "capabilities"};
819 } else {
820 $aCapabilities = array();
821 }
822
823 $aRoles = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
824 $aObjects = $this->getObjectsFromType('role');
825
826 foreach ($aRoles as $sRole) {
827 if (isset($aObjects[$sRole])) {
828 $oRoleObject = new stdClass();
829 $oRoleObject->name = $sRole;
830
831 $aIsRecursiveMember = array('role' => array());
832 $aIsRecursiveMember['role'][] = $oRoleObject;
833 }
834 }
835
836 return $aIsRecursiveMember;
837 }
838
839 /**
840 * Returns the users in the group
841 *
842 * @return array
843 */
844 public function getFullUsers()
845 {
846 $sCacheKey = 'getFullUsers';
847 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
848 $aFullUsers = $oUserAccessManager->getFromCache($sCacheKey);
849
850 if ($aFullUsers === null) {
851 /**
852 * @var wpdb $wpdb
853 */
854 global $wpdb;
855
856 $aDbUsers = $wpdb->get_results(
857 "SELECT ID, user_nicename
858 FROM ".$wpdb->users
859 );
860
861 $aFullUsers = array();
862
863 if (isset($aDbUsers)) {
864 foreach ($aDbUsers as $oDbUser) {
865 $oUser = $this->_getSingleObject('user', $oDbUser->ID, 'full');
866
867 if ($oUser !== null) {
868 $aFullUsers[$oUser->id] = $oUser;
869 }
870 }
871 }
872
873 $oUserAccessManager->addToCache($sCacheKey, $aFullUsers);
874 }
875
876 return $aFullUsers;
877 }
878
879
880 /*
881 * Group categories functions.
882 */
883
884 /**
885 * Returns a single category.
886 *
887 * @param integer $iObjectId The object _iId.
888 *
889 * @return object
890 */
891 protected function _getFullCategory($iObjectId)
892 {
893 $oCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
894
895 $aIsRecursiveMember = array();
896
897 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
898 $aUamOptions = $oUserAccessManager->getAdminOptions();
899
900 if ($aUamOptions['lock_recursive'] == 'true') {
901 if (isset($oCategory->parent)
902 && !is_null($oCategory->parent)
903 ) {
904 $oParentCategory = $this->_getSingleObject(
905 'category',
906 $oCategory->parent,
907 'full'
908 );
909
910 if ($oParentCategory !== null) {
911 $oCurCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
912 $oParentCategory->name = $oCurCategory->name;
913 $aIsRecursiveMember['category'][] = $oParentCategory;
914 }
915 }
916 }
917
918 return $aIsRecursiveMember;
919 }
920
921 /**
922 * Returns the categories in the group
923 *
924 * @param array $aCategories The real categories.
925 *
926 * @return array
927 */
928 public function getFullCategories($aCategories)
929 {
930 $aUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
931 $aUamOptions = $aUserAccessManager->getAdminOptions();
932
933 foreach ($aCategories as $oCategory) {
934 if ($oCategory != null) {
935 if ($aUamOptions['lock_recursive'] == 'true') {
936 //We have to remove the filter to get all categories
937 $blRemoveSuccess = remove_filter(
938 'get_terms',
939 array(
940 $this->getAccessHandler()->getUserAccessManager(),
941 'showCategory'
942 )
943 );
944
945 if ($blRemoveSuccess) {
946 $aArgs = array(
947 'child_of' => $oCategory->id,
948 'hide_empty' => false
949 );
950
951 $aCategoryChildren = get_categories($aArgs);
952
953 add_filter(
954 'get_terms',
955 array($aUserAccessManager, 'showCategory')
956 );
957
958
959 foreach ($aCategoryChildren as $oCategoryChild) {
960 $oCurCategoryChild = new stdClass();
961 $oCurCategoryChild->id = $oCategoryChild->term_id;
962 $oCurCategoryChild->name = $oCategoryChild->name;
963
964 $oCurCategoryChild->recursiveMember = array('category' => array());
965 $oCurCategoryChild->recursiveMember['category'][] = $oCurCategoryChild;
966 $aCategories[$oCurCategoryChild->id] = $oCurCategoryChild;
967 }
968 }
969 }
970
971 $aCategories[$oCategory->id] = $oCategory;
972 }
973 }
974
975 return $aCategories;
976 }
977
978
979 /*
980 * Group posts functions.
981 */
982
983 /**
984 * Checks if the give post in the give category.
985 *
986 * @param integer $iPostId The post _iId.
987 * @param integer $iCategoryId The category _iId.
988 *
989 * @return boolean
990 */
991 protected function _isPostInCategory($iPostId, $iCategoryId)
992 {
993 if ($this->_aObjectsInCategory === null) {
994 $this->_aObjectsInCategory = array();
995 $sCacheKey = '_isPostInCategory';
996 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
997 $aObjectsInCategory = $oUserAccessManager->getFromCache($sCacheKey);
998
999 if ($aObjectsInCategory === null) {
1000 /**
1001 * @var wpdb $wpdb
1002 */
1003 global $wpdb;
1004
1005 $aDbObjects = $wpdb->get_results(
1006 "SELECT tr.object_id AS objectId, tt.term_id AS categoryId
1007 FROM ".$wpdb->term_relationships." AS tr,
1008 ".$wpdb->term_taxonomy." AS tt
1009 WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
1010 AND tt.taxonomy = 'category'"
1011 );
1012
1013 foreach ($aDbObjects as $oDbObject) {
1014 if (!isset($this->_aObjectsInCategory[$oDbObject->objectId])) {
1015 $this->_aObjectsInCategory[$oDbObject->objectId] = array();
1016 }
1017
1018 $this->_aObjectsInCategory[$oDbObject->objectId][$oDbObject->categoryId] = $oDbObject->categoryId;
1019 }
1020
1021 $oUserAccessManager->addToCache($sCacheKey, $this->_aObjectsInCategory);
1022 } else {
1023 $this->_aObjectsInCategory = $aObjectsInCategory;
1024 }
1025 }
1026
1027 return (isset($this->_aObjectsInCategory[$iPostId])
1028 && isset($this->_aObjectsInCategory[$iPostId][$iCategoryId]));
1029 }
1030
1031 /**
1032 * Returns the membership of a single post.
1033 *
1034 * @param string $sPostType The post type needed for the intern representation.
1035 * @param integer $iObjectId The object _iId.
1036 *
1037 * @return object
1038 */
1039 protected function _getFullPost($sPostType, $iObjectId)
1040 {
1041 $aIsRecursiveMember = array();
1042 $oPost = $this->getAccessHandler()->getUserAccessManager()->getPost($iObjectId);
1043 $aUamOptions = $this->getAccessHandler()->getUserAccessManager()->getAdminOptions();
1044
1045 foreach ($this->getObjectsFromType('category', 'full') as $oCategory) {
1046 if ($this->_isPostInCategory($oPost->ID, $oCategory->id)) {
1047 $oCategoryObject = $this->getAccessHandler()->getUserAccessManager()->getCategory($oCategory->id);
1048 $oCategory->name = $oCategoryObject->name;
1049
1050 $aIsRecursiveMember['category'][] = $oCategory;
1051 }
1052 }
1053
1054 if ($oPost->post_parent == 0
1055 && $oPost->post_type == 'post'
1056 && $this->getAccessHandler()->getUserAccessManager()->getWpOption('show_on_front') == 'page'
1057 && $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts') != $iObjectId
1058 ) {
1059 $iParentId = $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts');
1060 } else {
1061 $iParentId = $oPost->post_parent;
1062 }
1063
1064 if ($iParentId != 0
1065 && $aUamOptions['lock_recursive'] == 'true'
1066 ) {
1067 $oParent = $this->getAccessHandler()->getUserAccessManager()->getPost($iParentId);
1068
1069 $oParentPost = $this->_getSingleObject(
1070 $oParent->post_type,
1071 $iParentId,
1072 'full'
1073 );
1074
1075 if ($oParentPost !== null) {
1076 $oPostObject = $this->getAccessHandler()->getUserAccessManager()->getPost($oParentPost->id);
1077 $oParentPost->name = $oPostObject->post_title;
1078
1079 $aIsRecursiveMember[$oParent->post_type][] = $oParentPost;
1080 }
1081 }
1082
1083 return $aIsRecursiveMember;
1084 }
1085
1086
1087 /*
1088 * Group pluggable _aObjects functions.
1089 */
1090
1091 /**
1092 * Returns a the recursive membership for a pluggable object.
1093 *
1094 * @param string $sObjectType The pluggable object type.
1095 * @param integer $iObjectId The object _iId.
1096 *
1097 * @return array
1098 */
1099 protected function _getFullPlObject($sObjectType, $iObjectId)
1100 {
1101 $blIsRecursiveMember = array();
1102
1103 $oPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
1104
1105 if (isset($oPlObject['reference'])
1106 && isset($oPlObject['getFull'])
1107 ) {
1108 $aPlRecMember = $oPlObject['reference']->{$oPlObject['getFull']}(
1109 $iObjectId,
1110 $this
1111 );
1112
1113 if (is_array($aPlRecMember)) {
1114 $blIsRecursiveMember = $aPlRecMember;
1115 }
1116 }
1117
1118 return $blIsRecursiveMember;
1119 }
1120 }