PluginProbe
User Access Manager / 1.2.6.4
User Access Manager v1.2.6.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.6.4, at class/UamUserGroup.class.php

1,119 lines 32.9 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-2013 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 object
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 $aRole = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
824 $sRole = $aRole[0];
825 $aObjects = $this->getObjectsFromType('role');
826
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 return $aIsRecursiveMember;
836 }
837
838 /**
839 * Returns the users in the group
840 *
841 * @return array
842 */
843 public function getFullUsers()
844 {
845 $sCacheKey = 'getFullUsers';
846 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
847 $aFullUsers = $oUserAccessManager->getFromCache($sCacheKey);
848
849 if ($aFullUsers === null) {
850 /**
851 * @var wpdb $wpdb
852 */
853 global $wpdb;
854
855 $aDbUsers = $wpdb->get_results(
856 "SELECT ID, user_nicename
857 FROM ".$wpdb->users
858 );
859
860 $aFullUsers = array();
861
862 if (isset($aDbUsers)) {
863 foreach ($aDbUsers as $oDbUser) {
864 $oUser = $this->_getSingleObject('user', $oDbUser->ID, 'full');
865
866 if ($oUser !== null) {
867 $aFullUsers[$oUser->id] = $oUser;
868 }
869 }
870 }
871
872 $oUserAccessManager->addToCache($sCacheKey, $aFullUsers);
873 }
874
875 return $aFullUsers;
876 }
877
878
879 /*
880 * Group categories functions.
881 */
882
883 /**
884 * Returns a single category.
885 *
886 * @param integer $iObjectId The object _iId.
887 *
888 * @return object
889 */
890 protected function _getFullCategory($iObjectId)
891 {
892 $oCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
893
894 $aIsRecursiveMember = array();
895
896 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
897 $aUamOptions = $oUserAccessManager->getAdminOptions();
898
899 if ($aUamOptions['lock_recursive'] == 'true') {
900 if (isset($oCategory->parent)
901 && !is_null($oCategory->parent)
902 ) {
903 $oParentCategory = $this->_getSingleObject(
904 'category',
905 $oCategory->parent,
906 'full'
907 );
908
909 if ($oParentCategory !== null) {
910 $oCurCategory = $this->getAccessHandler()->getUserAccessManager()->getCategory($iObjectId);
911 $oParentCategory->name = $oCurCategory->name;
912 $aIsRecursiveMember['category'][] = $oParentCategory;
913 }
914 }
915 }
916
917 return $aIsRecursiveMember;
918 }
919
920 /**
921 * Returns the categories in the group
922 *
923 * @param array $aCategories The real categories.
924 *
925 * @return array
926 */
927 public function getFullCategories($aCategories)
928 {
929 $aUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
930 $aUamOptions = $aUserAccessManager->getAdminOptions();
931
932 foreach ($aCategories as $oCategory) {
933 if ($oCategory != null) {
934 if ($aUamOptions['lock_recursive'] == 'true') {
935 //We have to remove the filter to get all categories
936 $blRemoveSuccess = remove_filter(
937 'get_terms',
938 array(
939 $this->getAccessHandler()->getUserAccessManager(),
940 'showCategory'
941 )
942 );
943
944 if ($blRemoveSuccess) {
945 $aArgs = array(
946 'child_of' => $oCategory->id,
947 'hide_empty' => false
948 );
949
950 $aCategoryChildren = get_categories($aArgs);
951
952 add_filter(
953 'get_terms',
954 array($aUserAccessManager, 'showCategory')
955 );
956
957
958 foreach ($aCategoryChildren as $oCategoryChild) {
959 $oCurCategoryChild = new stdClass();
960 $oCurCategoryChild->id = $oCategoryChild->term_id;
961 $oCurCategoryChild->name = $oCategoryChild->name;
962
963 $oCurCategoryChild->recursiveMember = array('category' => array());
964 $oCurCategoryChild->recursiveMember['category'][] = $oCurCategoryChild;
965 $aCategories[$oCurCategoryChild->id] = $oCurCategoryChild;
966 }
967 }
968 }
969
970 $aCategories[$oCategory->id] = $oCategory;
971 }
972 }
973
974 return $aCategories;
975 }
976
977
978 /*
979 * Group posts functions.
980 */
981
982 /**
983 * Checks if the give post in the give category.
984 *
985 * @param integer $iPostId The post _iId.
986 * @param integer $iCategoryId The category _iId.
987 *
988 * @return boolean
989 */
990 protected function _isPostInCategory($iPostId, $iCategoryId)
991 {
992 if ($this->_aObjectsInCategory === null) {
993 $this->_aObjectsInCategory = array();
994 $sCacheKey = '_isPostInCategory';
995 $oUserAccessManager = $this->getAccessHandler()->getUserAccessManager();
996 $aObjectsInCategory = $oUserAccessManager->getFromCache($sCacheKey);
997
998 if ($aObjectsInCategory === null) {
999 /**
1000 * @var wpdb $wpdb
1001 */
1002 global $wpdb;
1003
1004 $aDbObjects = $wpdb->get_results(
1005 "SELECT tr.object_id AS objectId, tt.term_id AS categoryId
1006 FROM ".$wpdb->term_relationships." AS tr,
1007 ".$wpdb->term_taxonomy." AS tt
1008 WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
1009 AND tt.taxonomy = 'category'"
1010 );
1011
1012 foreach ($aDbObjects as $oDbObject) {
1013 if (!isset($this->_aObjectsInCategory[$oDbObject->objectId])) {
1014 $this->_aObjectsInCategory[$oDbObject->objectId] = array();
1015 }
1016
1017 $this->_aObjectsInCategory[$oDbObject->objectId][$oDbObject->categoryId] = $oDbObject->categoryId;
1018 }
1019
1020 $oUserAccessManager->addToCache($sCacheKey, $this->_aObjectsInCategory);
1021 } else {
1022 $this->_aObjectsInCategory = $aObjectsInCategory;
1023 }
1024 }
1025
1026 return (isset($this->_aObjectsInCategory[$iPostId])
1027 && isset($this->_aObjectsInCategory[$iPostId][$iCategoryId]));
1028 }
1029
1030 /**
1031 * Returns the membership of a single post.
1032 *
1033 * @param string $sPostType The post type needed for the intern representation.
1034 * @param integer $iObjectId The object _iId.
1035 *
1036 * @return object
1037 */
1038 protected function _getFullPost($sPostType, $iObjectId)
1039 {
1040 $aIsRecursiveMember = array();
1041 $oPost = $this->getAccessHandler()->getUserAccessManager()->getPost($iObjectId);
1042 $aUamOptions = $this->getAccessHandler()->getUserAccessManager()->getAdminOptions();
1043
1044 foreach ($this->getObjectsFromType('category', 'full') as $oCategory) {
1045 if ($this->_isPostInCategory($oPost->ID, $oCategory->id)) {
1046 $oCategoryObject = $this->getAccessHandler()->getUserAccessManager()->getCategory($oCategory->id);
1047 $oCategory->name = $oCategoryObject->name;
1048
1049 $aIsRecursiveMember['category'][] = $oCategory;
1050 }
1051 }
1052
1053 if ($oPost->post_parent == 0
1054 && $oPost->post_type == 'post'
1055 && $this->getAccessHandler()->getUserAccessManager()->getWpOption('show_on_front') == 'page'
1056 && $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts') != $iObjectId
1057 ) {
1058 $iParentId = $this->getAccessHandler()->getUserAccessManager()->getWpOption('page_for_posts');
1059 } else {
1060 $iParentId = $oPost->post_parent;
1061 }
1062
1063 if ($iParentId != 0
1064 && $aUamOptions['lock_recursive'] == 'true'
1065 ) {
1066 $oParent = $this->getAccessHandler()->getUserAccessManager()->getPost($iParentId);
1067
1068 $oParentPost = $this->_getSingleObject(
1069 $oParent->post_type,
1070 $iParentId,
1071 'full'
1072 );
1073
1074 if ($oParentPost !== null) {
1075 $oPostObject = $this->getAccessHandler()->getUserAccessManager()->getPost($oParentPost->id);
1076 $oParentPost->name = $oPostObject->post_title;
1077
1078 $aIsRecursiveMember[$oParent->post_type][] = $oParentPost;
1079 }
1080 }
1081
1082 return $aIsRecursiveMember;
1083 }
1084
1085
1086 /*
1087 * Group pluggable _aObjects functions.
1088 */
1089
1090 /**
1091 * Returns a the recursive membership for a pluggable object.
1092 *
1093 * @param string $sObjectType The pluggable object type.
1094 * @param integer $iObjectId The object _iId.
1095 *
1096 * @return array
1097 */
1098 protected function _getFullPlObject($sObjectType, $iObjectId)
1099 {
1100 $blIsRecursiveMember = array();
1101
1102 $oPlObject = $this->getAccessHandler()->getPlObject($sObjectType);
1103
1104 if (isset($oPlObject['reference'])
1105 && isset($oPlObject['getFull'])
1106 ) {
1107 $aPlRecMember = $oPlObject['reference']->{$oPlObject['getFull']}(
1108 $iObjectId,
1109 $this
1110 );
1111
1112 if (is_array($aPlRecMember)) {
1113 $blIsRecursiveMember = $aPlRecMember;
1114 }
1115 }
1116
1117 return $blIsRecursiveMember;
1118 }
1119 }