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

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

942 lines 25.3 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-2010 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 $accessHandler = null;
31 protected $id = null;
32 protected $groupName = null;
33 protected $groupDesc = null;
34 protected $readAccess = null;
35 protected $writeAccess = null;
36 protected $ipRange = null;
37 protected $roles = array();
38 protected $objects = null;
39 protected $singleObjects = null;
40 private $_assignedObjects = null;
41 protected $plObjects = array();
42
43 /**
44 * Consturtor
45 *
46 * @param object &$uamAccessHandler The access handler object.
47 * @param integer $id The id of the user group.
48 *
49 * @return null
50 */
51 public function __construct(&$uamAccessHandler, $id = null)
52 {
53 $this->accessHandler = $uamAccessHandler;
54
55 //Create default values for the objects.
56 foreach ($this->getAllObjectTypes() as $objectType) {
57 $this->objects[$objectType] = array(
58 'real' => -1,
59 'full' => -1
60 );
61 $this->_assignedObjects[$objectType] = null;
62 }
63
64 if ($id !== null) {
65 global $wpdb;
66
67 $this->id = $id;
68
69 $dbUsergroup = $wpdb->get_row(
70 "SELECT *
71 FROM ".DB_ACCESSGROUP."
72 WHERE ID = ".$this->getId()."
73 LIMIT 1",
74 ARRAY_A
75 );
76
77 $this->groupName = $dbUsergroup['groupname'];
78 $this->groupDesc = $dbUsergroup['groupdesc'];
79 $this->readAccess = $dbUsergroup['read_access'];
80 $this->writeAccess = $dbUsergroup['write_access'];
81 $this->ipRange = $dbUsergroup['ip_range'];
82 }
83 }
84
85 /**
86 * Returns the user access handler object.
87 *
88 * @return object
89 */
90 public function &getAccessHandler()
91 {
92 return $this->accessHandler;
93 }
94
95 /**
96 * Deletes the user group.
97 *
98 * @return null
99 */
100 public function delete()
101 {
102 if ($this->id == null) {
103 return false;
104 }
105
106 global $wpdb;
107
108 $wpdb->query(
109 "DELETE FROM " . DB_ACCESSGROUP . "
110 WHERE ID = $this->id LIMIT 1"
111 );
112
113 foreach ($this->getAllObjectTypes() as $objectType) {
114 $this->_deleteObjectsFromDb($objectType);
115 }
116 }
117
118 /**
119 * Saves the user group.
120 *
121 * @return null;
122 */
123 public function save()
124 {
125 global $wpdb;
126
127 if ($this->id == null) {
128 $wpdb->query(
129 "INSERT INTO " . DB_ACCESSGROUP . " (
130 ID,
131 groupname,
132 groupdesc,
133 read_access,
134 write_access,
135 ip_range
136 )
137 VALUES (
138 NULL,
139 '" . $this->groupName . "',
140 '" . $this->groupDesc . "',
141 '" . $this->readAccess . "',
142 '" . $this->writeAccess . "',
143 '" . $this->ipRange . "'
144 )"
145 );
146
147 $this->id = $wpdb->insert_id;
148 } else {
149 $wpdb->query(
150 "UPDATE " . DB_ACCESSGROUP . "
151 SET groupname = '" . $this->groupName . "',
152 groupdesc = '" . $this->groupDesc . "',
153 read_access = '" . $this->readAccess . "',
154 write_access = '" . $this->writeAccess . "',
155 ip_range = '" . $this->ipRange . "'
156 WHERE ID = " . $this->id
157 );
158
159 foreach ($this->getAllObjectTypes() as $objectType) {
160 //Load to object
161 $this->getObjectsFromType($objectType);
162 //Delete form database
163 $this->_deleteObjectsFromDb($objectType);
164 }
165 }
166
167 foreach ($this->getAllObjectTypes() as $objectType) {
168 foreach ($this->getObjectsFromType($objectType) as $key => $object) {
169 $sql = sprintf($this->_getSqlQuery($objectType, 'insert'), trim($key));
170 $wpdb->query($sql);
171 }
172 }
173 }
174
175
176 /*
177 * Primary values.
178 */
179
180 /**
181 * Returns the group id.
182 *
183 * @return integer
184 */
185 public function getId()
186 {
187 return $this->id;
188 }
189
190 /**
191 * Returns the group name.
192 *
193 * @return string
194 */
195 public function getGroupName()
196 {
197 return $this->groupName;
198 }
199
200 /**
201 * Sets the group name.
202 *
203 * @param string $groupName The new group name.
204 *
205 * @return null
206 */
207 public function setGroupName($groupName)
208 {
209 $this->groupName = $groupName;
210 }
211
212 /**
213 * Returns the group description.
214 *
215 * @return string
216 */
217 public function getGroupDesc()
218 {
219 return $this->groupDesc;
220 }
221
222 /**
223 * Sets the group description.
224 *
225 * @param string $groupDesc The new group description.
226 *
227 * @return null
228 */
229 public function setGroupDesc($groupDesc)
230 {
231 $this->groupDesc = $groupDesc;
232 }
233
234 /**
235 * Returns the read access.
236 *
237 * @return string
238 */
239 public function getReadAccess()
240 {
241 return $this->readAccess;
242 }
243
244 /**
245 * Sets the read access.
246 *
247 * @param string $readAccess The read access.
248 *
249 * @return null
250 */
251 public function setReadAccess($readAccess)
252 {
253 $this->readAccess = $readAccess;
254 }
255
256 /**
257 * Returns the write access.
258 *
259 * @return string
260 */
261 public function getWriteAccess()
262 {
263 return $this->writeAccess;
264 }
265
266 /**
267 * Sets the write access.
268 *
269 * @param string $writeAccess The wirte access.
270 *
271 * @return null
272 */
273 public function setWriteAccess($writeAccess)
274 {
275 $this->writeAccess = $writeAccess;
276 }
277
278 /**
279 * Returns the ip range.
280 *
281 * @param string $type The return type.
282 *
283 * @return array
284 */
285 public function getIpRange($type = null)
286 {
287 if ($type == 'string') {
288 return $this->ipRange;
289 }
290
291 $ipRange = explode(';', $this->ipRange);
292
293 if ($ipRange[0] == null) {
294 return null;
295 }
296
297 return $ipRange;
298 }
299
300 /**
301 * Sets the ip range.
302 *
303 * @param string $ipRange The new ip range.
304 *
305 * @return null
306 */
307 public function setIpRange($ipRange)
308 {
309 if (is_array($ipRange)) {
310 $ipRange = implode(';', $ipRange);
311 }
312
313 $this->ipRange = $ipRange;
314 }
315
316 /**
317 * Returns all objects types.
318 *
319 * @return array
320 */
321 public function getAllObjectTypes()
322 {
323 return $this->getAccessHandler()->getAllObjectTypes();
324 }
325
326
327 /*
328 * Meta functions.
329 */
330
331 /**
332 * Magic method getter.
333 *
334 * @param string $name The name of the function
335 * @param array $arguments The arguments for the function
336 *
337 * @return mixed
338 */
339 public function __call($name, $arguments)
340 {
341 echo "NAME:".$name;
342 exit;
343
344 $uam = $this->getAccessHandler()->getUserAccessManager();
345
346 $action = '';
347
348 if ($uam->startsWith($name, 'add')) {
349 $prefix = 'add';
350 } elseif ($uam->startsWith($name, 'remove')) {
351 $prefix = 'remove';
352 } elseif ($uam->startsWith($name, 'isMember')) {
353 $prefix = 'isMember';
354 }
355
356 $objectType = str_replace($prefix, '', $name);
357 $objectType = strtolower($objectType);
358
359 $objectId = $arguments[0];
360
361 if ($prefix == 'add') {
362 return $this->addObject(
363 $objectType,
364 $objectId
365 );
366 } elseif ($prefix == 'remove') {
367 return $this->removeObject(
368 $objectType,
369 $objectId
370 );
371 } elseif ($prefix == 'ismember') {
372 $withInfo = $arguments[1];
373
374 return $this->objectIsMember(
375 $objectType,
376 $objectId,
377 $withInfo
378 );
379 }
380 }
381
382 /**
383 * Returns the sql query.
384 *
385 * @param string $objectType The object type.
386 * @param string $action The sql action.
387 *
388 * @return string
389 */
390 private function _getSqlQuery($objectType, $action)
391 {
392 $sql = '';
393
394 if ($action == 'select') {
395 $sql = "SELECT object_id as id
396 FROM ".DB_ACCESSGROUP_TO_OBJECT."
397 WHERE group_id = ".$this->getId()."
398 AND object_type = '".$objectType ."'";
399 } elseif ($action == 'delete') {
400 $sql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT."
401 WHERE group_id = ".$this->getId()."
402 AND object_type = '".$objectType ."'";
403 } elseif ($action == 'insert') {
404 $sql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." (
405 group_id,
406 object_id,
407 object_type
408 ) VALUES (
409 '".$this->getId()."',
410 '%s',
411 '".$objectType."'
412 )";
413 }
414
415 return $sql;
416 }
417
418 /**
419 * Adds a object of the given type.
420 *
421 * @param string $objectType The object type.
422 * @param integer $objectId The object id.
423 *
424 * @return null
425 */
426 public function addObject($objectType, $objectId)
427 {
428 $this->getAccessHandler()->unsetUserGroupsForObject();
429 $this->getObjectsFromType($objectType);
430
431 $object = new stdClass;
432 $object->id = $objectId;
433
434 $this->objects[$objectType]['real'][$objectId] = $object;
435 $this->objects[$objectType]['full'] = -1;
436
437 $this->_assignedObjects[$objectType][$objectId] = $objectId;
438 }
439
440 /**
441 * Removes a object of the given type.
442 *
443 * @param string $objectType The object type.
444 * @param integer $objectId The object id.
445 *
446 * @return null
447 */
448 public function removeObject($objectType, $objectId)
449 {
450 $this->getAccessHandler()->unsetUserGroupsForObject();
451 $this->getObjectsFromType($objectType);
452
453 unset($this->objects[$objectType]['real'][$objectId]);
454 $this->objects[$objectType]['full'] = -1;
455
456 unset($this->singleObjects[$objectType][$objectId]);
457 unset($this->_assignedObjects[$objectType][$objectId]);
458 }
459
460 /**
461 * Returns the assigned objects.
462 *
463 * @param string $objectType The object type.
464 *
465 * @return array
466 */
467 private function _getAssignedObjects($objectType)
468 {
469 if ($this->_assignedObjects[$objectType] !== null) {
470 return $this->_assignedObjects[$objectType];
471 }
472
473 global $wpdb;
474
475 $dbObjects = $wpdb->get_results(
476 $this->_getSqlQuery($objectType, 'select')
477 );
478
479 $this->_assignedObjects[$objectType] = array();
480
481 foreach ($dbObjects as $dbObject) {
482 $this->_assignedObjects[$objectType][$dbObject->id]
483 = $dbObject->id;
484 }
485
486 return $this->_assignedObjects[$objectType];
487 }
488
489 /**
490 * Checks if the object is assigned to the group.
491 *
492 * @param string $objectType The object type.
493 * @param integer $objectId The object id.
494 *
495 * @return boolean
496 */
497 private function _isObjectAssignedToGroup($objectType, $objectId)
498 {
499 return array_key_exists(
500 $objectId,
501 $this->_getAssignedObjects($objectType)
502 );
503 }
504
505 /**
506 * Unsets the ojects.
507 *
508 * @param string $objectType The object type.
509 * @param boolean $plusRemove If true also database entrys will remove.
510 *
511 * @return null;
512 */
513 public function unsetObjects($objectType, $plusRemove = false)
514 {
515 if ($plusRemove) {
516 $this->_deleteObjectsFromDb($objectType);
517 }
518
519 $this->objects[$objectType] = array(
520 'real' => array(),
521 'full' => array(),
522 );
523 }
524
525 /**
526 * Removes all objects from the user group.
527 *
528 * @param string $objectType The object type.
529 *
530 * @return null
531 */
532 private function _deleteObjectsFromDb($objectType)
533 {
534 if (isset($this->id)) {
535 global $wpdb;
536
537 $wpdb->query(
538 $this->_getSqlQuery($objectType, 'delete')
539 );
540 }
541 }
542
543 /**
544 * Checks if the given object is a member of the group.
545 *
546 * @param string $objectType The object type.
547 * @param interger $objectId The id of the object which should be checked.
548 * @param boolean $withInfo If true then we return additional infos.
549 *
550 * @return boolean
551 */
552 public function objectIsMember($objectType, $objectId, $withInfo = false)
553 {
554 $object = $this->_getSingleObject($objectType, $objectId, 'full');
555
556 if ($object !== null) {
557 if (isset($object->recursiveMember)
558 && $withInfo
559 ) {
560 return $object->recursiveMember;
561 }
562
563 return true;
564 }
565
566 return false;
567 }
568
569 /**
570 * Returns all objects of the given type.
571 *
572 * @param string $objectType The object type.
573 * @param string $type The return type, could be real or full.
574 *
575 * @return array
576 */
577 function getObjectsFromType($objectType, $type = 'real')
578 {
579 if ($this->id == null) {
580 return array();
581 }
582
583 if ($type != 'real'
584 && $type != 'full'
585 ) {
586 return array();
587 }
588
589 if ($this->objects[$objectType][$type] != -1) {
590 return $this->objects[$objectType][$type];
591 } else {
592 $this->objects[$objectType][$type] = array();
593 }
594
595 global $wpdb;
596
597 $dbObjects = $wpdb->get_results(
598 $this->_getSqlQuery($objectType, 'select')
599 );
600
601 foreach ($dbObjects as $dbObject) {
602 $object = $this->_getSingleObject($objectType, $dbObject->id, $type);
603
604 if ($object !== null) {
605 $this->objects[$objectType][$type][$object->id] = $object;
606 }
607 }
608
609 if ($type == 'full'
610 && $objectType != 'post'
611 && $objectType != 'page'
612 && $objectType != 'attachment'
613 && $objectType != 'role'
614 ) {
615 if ($objectType == 'category') {
616 $this->objects[$objectType][$type]
617 = $this->getFullCategories($this->objects[$objectType][$type]);
618 } elseif ($objectType == 'user') {
619 $this->objects[$objectType][$type] = $this->getFullUsers();
620 } else {
621 $plObject = $this->getAccessHandler()->getPlObject($objectType);
622 $this->objects[$objectType][$type]
623 = $plObject['reference']->{$plObject['getFullObjects']}(
624 $this->objects[$objectType][$type],
625 &$this
626 );
627 }
628 }
629
630 return $this->objects[$objectType][$type];
631 }
632
633 /**
634 * Returns a single object.
635 *
636 * @param string $objectType The object type.
637 * @param interger $objectId The id of the object which should be checked.
638 * @param string $type The return type. Can be real or full.
639 *
640 * @return object
641 */
642 private function _getSingleObject($objectType, $objectId, $type)
643 {
644 if (isset($this->singleObjects[$objectType][$objectId])) {
645 return $this->singleObjects[$objectType][$objectId];
646 }
647
648 $isRecursiveMember = array();
649
650 if ($type == 'full'
651 && $objectType != 'role'
652 ) {
653 if ($objectType == 'post'
654 || $objectType == 'page'
655 || $objectType == 'attachment'
656 ) {
657 $isRecursiveMember = $this->_getFullPost($objectType, $objectId);
658 } else if ($objectType == 'category') {
659 $isRecursiveMember = $this->_getFullCategory($objectId);
660 } else if ($objectType == 'user') {
661 $isRecursiveMember = $this->_getFullUser($objectId);
662 } else {
663 $isRecursiveMember = $this->_getFullPlObject($objectType, $objectId);
664 }
665 }
666
667 if ($this->_isObjectAssignedToGroup($objectType, $objectId)
668 || $isRecursiveMember != array()
669 ) {
670 $object = new stdClass;
671 $object->id = $objectId;
672
673 if ($isRecursiveMember != array()) {
674 $object->recursiveMember = $isRecursiveMember;
675 }
676
677 $this->singleObjects[$objectType][$objectId] = $object;
678 } else {
679 $this->singleObjects[$objectType][$objectId] = null;
680 }
681
682 return $this->singleObjects[$objectType][$objectId];
683 }
684
685
686 /*
687 * Group users functions.
688 */
689
690 /**
691 * Returns a single user.
692 *
693 * @param integer $objectId The object id.
694 *
695 * @return object
696 */
697 private function _getFullUser($objectId)
698 {
699 $isRecursiveMember = array();
700
701 global $wpdb;
702
703 $curUserdata = get_userdata($objectId);
704
705 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
706 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
707 } else {
708 $capabilities = null;
709 }
710
711 $role = is_array($capabilities) ?
712 array_keys($capabilities) : array('norole');
713
714 if (array_key_exists($role[0], $this->getObjectsFromType('role'))
715 ) {
716 $roleObject->name = $role[0];
717
718 $isRecursiveMember = array('role' => array());
719 $isRecursiveMember['role'][] = $roleObject;
720 }
721
722 return $isRecursiveMember;
723 }
724
725 /**
726 * Returns the users in the group
727 *
728 * @return array
729 */
730 public function getFullUsers()
731 {
732 global $wpdb;
733
734 $dbUsers = $wpdb->get_results(
735 "SELECT ID, user_nicename
736 FROM ".$wpdb->users
737 );
738
739 $fullUsers = array();
740
741 if (isset($dbUsers)) {
742 foreach ($dbUsers as $dbUser) {
743 $user = $this->_getSingleObject('user', $dbUser->ID, 'full');
744
745 if ($user !== null) {
746 $fullUsers[$user->id] = $user;
747 }
748 }
749 }
750
751 return $fullUsers;
752 }
753
754
755 /*
756 * Group categories functions.
757 */
758
759 /**
760 * Returns a single category.
761 *
762 * @param integer $objectId The object id.
763 *
764 * @return object
765 */
766 private function _getFullCategory($objectId)
767 {
768 $category = get_category($objectId);
769
770 $isRecursiveMember = array();
771
772 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
773 $uamOptions = $userAccessManager->getAdminOptions();
774
775 if ($uamOptions['lock_recursive'] == 'true') {
776 if (isset($category->parent)
777 && !is_null($category->parent)
778 ) {
779 $parentCategory = $this->_getSingleObject(
780 'category',
781 $category->parent,
782 'full'
783 );
784
785 if ($parentCategory !== null) {
786 $curCategory = get_category($objectId);
787 $parentCategory->name = $curCategory->name;
788
789 if (isset($parentCategory->recursiveMember)) {
790 $isRecursiveMember['category'][]
791 = $parentCategory;
792 } else {
793 $isRecursiveMember['category'][]
794 = $parentCategory;
795 }
796 }
797 }
798 }
799
800 return $isRecursiveMember;
801 }
802
803 /**
804 * Returns the categories in the group
805 *
806 * @param string $categories The real categories.
807 *
808 * @return array
809 */
810 public function getFullCategories($categories)
811 {
812 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
813 $uamOptions = $userAccessManager->getAdminOptions();
814
815 foreach ($categories as $category) {
816 if ($category != null) {
817 if ($uamOptions['lock_recursive'] == 'true') {
818 //We have to remove the filter to get all categories
819 $removeSucc = remove_filter(
820 'get_terms',
821 array(
822 $this->getAccessHandler()->getUserAccessManager(),
823 'showCategory'
824 )
825 );
826
827 if ($removeSucc) {
828 $args = array(
829 'child_of' => $category->id,
830 'hide_empty' => false
831 );
832
833 $categoryChilds = get_categories($args);
834
835 add_filter(
836 'get_terms',
837 array(&$userAccessManager, 'showCategory')
838 );
839
840
841 foreach ($categoryChilds as $categoryChild) {
842 $curCategoryChild = new stdClass();
843 $curCategoryChild->id = $categoryChild->term_id;
844 $curCategoryChild->name = $categoryChild->name;
845
846 $curCategoryChild->recursiveMember
847 = array('category' => array());
848 $curCategoryChild->recursiveMember['category'][]
849 = $curCategoryChild;
850 $categories[$curCategoryChild->id]
851 = $curCategoryChild;
852 }
853 }
854 }
855
856 $categories[$category->id] = $category;
857 }
858 }
859
860 return $categories;
861 }
862
863
864 /*
865 * Group posts functions.
866 */
867
868 /**
869 * Returns the membership of a single post.
870 *
871 * @param string $postType The post type needed for the intern representation.
872 * @param integer $objectId The object id.
873 *
874 * @return object
875 */
876 private function _getFullPost($postType, $objectId)
877 {
878 $post = get_post($objectId);
879
880 $isRecursiveMember = array();
881
882 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
883 $uamOptions = $userAccessManager->getAdminOptions();
884
885 foreach ($this->getObjectsFromType('category', 'full') as $category) {
886 if (in_category($category->id, $post->ID)) {
887 $categoryObject = get_category($category->id);
888 $category->name = $categoryObject->name;
889
890 $isRecursiveMember['category'][] = $category;
891 }
892 }
893
894 if (($postType == 'page'
895 || $postType == 'attachment')
896 && $uamOptions['lock_recursive'] == 'true'
897 && $post->post_parent != 0
898 ) {
899 $parentPost = $this->_getSingleObject(
900 $postType,
901 $post->post_parent,
902 'full'
903 );
904
905 if ($parentPost !== null) {
906 $postObject = get_post($parentPost->id);
907 $parentPost->name = $postObject->post_title;
908
909 $isRecursiveMember['post'][] = $parentPost;
910 }
911 }
912
913 return $isRecursiveMember;
914 }
915
916
917 /*
918 * Group pluggable objects functions.
919 */
920
921 /**
922 * Returns a the recursive membership for a pluggable object.
923 *
924 * @param string $objectType The pluggable object type.
925 * @param integer $objectId The object id.
926 *
927 * @return array
928 */
929 private function _getFullPlObject($objectType, $objectId)
930 {
931 $isRecursiveMember = array();
932
933 $plObject = $this->getAccessHandler()->getPlObject($objectType);
934 $plRecMember = $plObject['reference']->{$plObject['getFull']}($objectId, &$this);
935
936 if (is_array($plRecMember)) {
937 $isRecursiveMember = $plRecMember;
938 }
939
940 return $isRecursiveMember;
941 }
942 }