PluginProbe
User Access Manager / 1.1.1
User Access Manager v1.1.1
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / class / UamUserGroup.class.php

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

967 lines 25.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-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 $uam = $this->getAccessHandler()->getUserAccessManager();
342
343 $action = '';
344
345 if ($uam->startsWith($name, 'add')) {
346 $prefix = 'add';
347 } elseif ($uam->startsWith($name, 'remove')) {
348 $prefix = 'remove';
349 } elseif ($uam->startsWith($name, 'isMember')) {
350 $prefix = 'isMember';
351 }
352
353 $objectType = str_replace($prefix, '', $name);
354 $objectType = strtolower($objectType);
355
356 $objectId = $arguments[0];
357
358 if ($prefix == 'add') {
359 return $this->addObject(
360 $objectType,
361 $objectId
362 );
363 } elseif ($prefix == 'remove') {
364 return $this->removeObject(
365 $objectType,
366 $objectId
367 );
368 } elseif ($prefix == 'ismember') {
369 $withInfo = $arguments[1];
370
371 return $this->objectIsMember(
372 $objectType,
373 $objectId,
374 $withInfo
375 );
376 }
377 }
378
379 /**
380 * Returns the sql query.
381 *
382 * @param string $objectType The object type.
383 * @param string $action The sql action.
384 *
385 * @return string
386 */
387 private function _getSqlQuery($objectType, $action)
388 {
389 $sql = '';
390
391 if ($action == 'select') {
392 $sql = "SELECT object_id as id
393 FROM ".DB_ACCESSGROUP_TO_OBJECT."
394 WHERE group_id = ".$this->getId()."
395 AND object_type = '".$objectType ."'";
396 } elseif ($action == 'delete') {
397 $sql = "DELETE FROM ".DB_ACCESSGROUP_TO_OBJECT."
398 WHERE group_id = ".$this->getId()."
399 AND object_type = '".$objectType ."'";
400 } elseif ($action == 'insert') {
401 $sql = "INSERT INTO ".DB_ACCESSGROUP_TO_OBJECT." (
402 group_id,
403 object_id,
404 object_type
405 ) VALUES (
406 '".$this->getId()."',
407 '%s',
408 '".$objectType."'
409 )";
410 }
411
412 return $sql;
413 }
414
415 /**
416 * Adds a object of the given type.
417 *
418 * @param string $objectType The object type.
419 * @param integer $objectId The object id.
420 *
421 * @return null
422 */
423 public function addObject($objectType, $objectId)
424 {
425 if (!in_array($objectType, $this->getAllObjectTypes())) {
426 return;
427 }
428
429 $this->getAccessHandler()->unsetUserGroupsForObject();
430 $this->getObjectsFromType($objectType);
431
432 $object = new stdClass;
433 $object->id = $objectId;
434
435 $this->objects[$objectType]['real'][$objectId] = $object;
436 $this->objects[$objectType]['full'] = -1;
437
438 $this->_assignedObjects[$objectType][$objectId] = $objectId;
439 }
440
441 /**
442 * Removes a object of the given type.
443 *
444 * @param string $objectType The object type.
445 * @param integer $objectId The object id.
446 *
447 * @return null
448 */
449 public function removeObject($objectType, $objectId)
450 {
451 if (!in_array($objectType, $this->getAllObjectTypes())) {
452 return;
453 }
454
455 $this->getAccessHandler()->unsetUserGroupsForObject();
456 $this->getObjectsFromType($objectType);
457
458 unset($this->objects[$objectType]['real'][$objectId]);
459 $this->objects[$objectType]['full'] = -1;
460
461 unset($this->singleObjects[$objectType][$objectId]);
462 unset($this->_assignedObjects[$objectType][$objectId]);
463 }
464
465 /**
466 * Returns the assigned objects.
467 *
468 * @param string $objectType The object type.
469 *
470 * @return array
471 */
472 private function _getAssignedObjects($objectType)
473 {
474 if ($this->_assignedObjects[$objectType] !== null) {
475 return $this->_assignedObjects[$objectType];
476 }
477
478 global $wpdb;
479
480 $dbObjects = $wpdb->get_results(
481 $this->_getSqlQuery($objectType, 'select')
482 );
483
484 $this->_assignedObjects[$objectType] = array();
485
486 foreach ($dbObjects as $dbObject) {
487 $this->_assignedObjects[$objectType][$dbObject->id]
488 = $dbObject->id;
489 }
490
491 return $this->_assignedObjects[$objectType];
492 }
493
494 /**
495 * Checks if the object is assigned to the group.
496 *
497 * @param string $objectType The object type.
498 * @param integer $objectId The object id.
499 *
500 * @return boolean
501 */
502 private function _isObjectAssignedToGroup($objectType, $objectId)
503 {
504 return array_key_exists(
505 $objectId,
506 $this->_getAssignedObjects($objectType)
507 );
508 }
509
510 /**
511 * Unsets the ojects.
512 *
513 * @param string $objectType The object type.
514 * @param boolean $plusRemove If true also database entrys will remove.
515 *
516 * @return null;
517 */
518 public function unsetObjects($objectType, $plusRemove = false)
519 {
520 if (!in_array($objectType, $this->getAllObjectTypes())) {
521 return;
522 }
523
524 if ($plusRemove) {
525 $this->_deleteObjectsFromDb($objectType);
526 }
527
528 $this->objects[$objectType] = array(
529 'real' => array(),
530 'full' => array(),
531 );
532 }
533
534 /**
535 * Removes all objects from the user group.
536 *
537 * @param string $objectType The object type.
538 *
539 * @return null
540 */
541 private function _deleteObjectsFromDb($objectType)
542 {
543 if (isset($this->id)) {
544 global $wpdb;
545
546 $wpdb->query(
547 $this->_getSqlQuery($objectType, 'delete')
548 );
549 }
550 }
551
552 /**
553 * Checks if the given object is a member of the group.
554 *
555 * @param string $objectType The object type.
556 * @param interger $objectId The id of the object which should be checked.
557 * @param boolean $withInfo If true then we return additional infos.
558 *
559 * @return boolean
560 */
561 public function objectIsMember($objectType, $objectId, $withInfo = false)
562 {
563 if (!in_array($objectType, $this->getAllObjectTypes())) {
564 return;
565 }
566
567 $object = $this->_getSingleObject($objectType, $objectId, 'full');
568
569 if ($object !== null) {
570 if (isset($object->recursiveMember)
571 && $withInfo
572 ) {
573 return $object->recursiveMember;
574 }
575
576 return true;
577 }
578
579 return false;
580 }
581
582 /**
583 * Returns all objects of the given type.
584 *
585 * @param string $objectType The object type.
586 * @param string $type The return type, could be real or full.
587 *
588 * @return array
589 */
590 function getObjectsFromType($objectType, $type = 'real')
591 {
592 if (!in_array($objectType, $this->getAllObjectTypes())) {
593 return;
594 }
595
596 if ($this->id == null) {
597 return array();
598 }
599
600 if ($type != 'real'
601 && $type != 'full'
602 ) {
603 return array();
604 }
605
606 if ($this->objects[$objectType][$type] != -1) {
607 return $this->objects[$objectType][$type];
608 } else {
609 $this->objects[$objectType][$type] = array();
610 }
611
612 global $wpdb;
613
614 $dbObjects = $wpdb->get_results(
615 $this->_getSqlQuery($objectType, 'select')
616 );
617
618 foreach ($dbObjects as $dbObject) {
619 $object = $this->_getSingleObject($objectType, $dbObject->id, $type);
620
621 if ($object !== null) {
622 $this->objects[$objectType][$type][$object->id] = $object;
623 }
624 }
625
626 if ($type == 'full'
627 && $objectType != 'post'
628 && $objectType != 'page'
629 && $objectType != 'attachment'
630 && $objectType != 'role'
631 ) {
632 if ($objectType == 'category') {
633 $this->objects[$objectType][$type]
634 = $this->getFullCategories($this->objects[$objectType][$type]);
635 } elseif ($objectType == 'user') {
636 $this->objects[$objectType][$type] = $this->getFullUsers();
637 } else {
638 $plObject = $this->getAccessHandler()->getPlObject($objectType);
639 $this->objects[$objectType][$type]
640 = $plObject['reference']->{$plObject['getFullObjects']}(
641 $this->objects[$objectType][$type],
642 &$this
643 );
644 }
645 }
646
647 return $this->objects[$objectType][$type];
648 }
649
650 /**
651 * Returns a single object.
652 *
653 * @param string $objectType The object type.
654 * @param interger $objectId The id of the object which should be checked.
655 * @param string $type The return type. Can be real or full.
656 *
657 * @return object
658 */
659 private function _getSingleObject($objectType, $objectId, $type)
660 {
661 if (isset($this->singleObjects[$objectType][$objectId])) {
662 return $this->singleObjects[$objectType][$objectId];
663 }
664
665 $isRecursiveMember = array();
666
667 if ($type == 'full'
668 && $objectType != 'role'
669 ) {
670 if ($objectType == 'post'
671 || $objectType == 'page'
672 || $objectType == 'attachment'
673 ) {
674 $isRecursiveMember = $this->_getFullPost($objectType, $objectId);
675 } else if ($objectType == 'category') {
676 $isRecursiveMember = $this->_getFullCategory($objectId);
677 } else if ($objectType == 'user') {
678 $isRecursiveMember = $this->_getFullUser($objectId);
679 } else {
680 $isRecursiveMember = $this->_getFullPlObject($objectType, $objectId);
681 }
682 }
683
684 if ($this->_isObjectAssignedToGroup($objectType, $objectId)
685 || $isRecursiveMember != array()
686 ) {
687 $object = new stdClass;
688 $object->id = $objectId;
689
690 if ($isRecursiveMember != array()) {
691 $object->recursiveMember = $isRecursiveMember;
692 }
693
694 $this->singleObjects[$objectType][$objectId] = $object;
695 } else {
696 $this->singleObjects[$objectType][$objectId] = null;
697 }
698
699 return $this->singleObjects[$objectType][$objectId];
700 }
701
702
703 /*
704 * Group users functions.
705 */
706
707 /**
708 * Returns a single user.
709 *
710 * @param integer $objectId The object id.
711 *
712 * @return object
713 */
714 private function _getFullUser($objectId)
715 {
716 $isRecursiveMember = array();
717
718 global $wpdb;
719
720 $curUserdata = get_userdata($objectId);
721
722 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
723 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
724 } else {
725 $capabilities = null;
726 }
727
728 $role = is_array($capabilities) ?
729 array_keys($capabilities) : array('norole');
730
731 if (array_key_exists($role[0], $this->getObjectsFromType('role'))
732 ) {
733 $roleObject->name = $role[0];
734
735 $isRecursiveMember = array('role' => array());
736 $isRecursiveMember['role'][] = $roleObject;
737 }
738
739 return $isRecursiveMember;
740 }
741
742 /**
743 * Returns the users in the group
744 *
745 * @return array
746 */
747 public function getFullUsers()
748 {
749 global $wpdb;
750
751 $dbUsers = $wpdb->get_results(
752 "SELECT ID, user_nicename
753 FROM ".$wpdb->users
754 );
755
756 $fullUsers = array();
757
758 if (isset($dbUsers)) {
759 foreach ($dbUsers as $dbUser) {
760 $user = $this->_getSingleObject('user', $dbUser->ID, 'full');
761
762 if ($user !== null) {
763 $fullUsers[$user->id] = $user;
764 }
765 }
766 }
767
768 return $fullUsers;
769 }
770
771
772 /*
773 * Group categories functions.
774 */
775
776 /**
777 * Returns a single category.
778 *
779 * @param integer $objectId The object id.
780 *
781 * @return object
782 */
783 private function _getFullCategory($objectId)
784 {
785 $category = get_category($objectId);
786
787 $isRecursiveMember = array();
788
789 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
790 $uamOptions = $userAccessManager->getAdminOptions();
791
792 if ($uamOptions['lock_recursive'] == 'true') {
793 if (isset($category->parent)
794 && !is_null($category->parent)
795 ) {
796 $parentCategory = $this->_getSingleObject(
797 'category',
798 $category->parent,
799 'full'
800 );
801
802 if ($parentCategory !== null) {
803 $curCategory = get_category($objectId);
804 $parentCategory->name = $curCategory->name;
805
806 if (isset($parentCategory->recursiveMember)) {
807 $isRecursiveMember['category'][]
808 = $parentCategory;
809 } else {
810 $isRecursiveMember['category'][]
811 = $parentCategory;
812 }
813 }
814 }
815 }
816
817 return $isRecursiveMember;
818 }
819
820 /**
821 * Returns the categories in the group
822 *
823 * @param string $categories The real categories.
824 *
825 * @return array
826 */
827 public function getFullCategories($categories)
828 {
829 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
830 $uamOptions = $userAccessManager->getAdminOptions();
831
832 foreach ($categories as $category) {
833 if ($category != null) {
834 if ($uamOptions['lock_recursive'] == 'true') {
835 //We have to remove the filter to get all categories
836 $removeSucc = remove_filter(
837 'get_terms',
838 array(
839 $this->getAccessHandler()->getUserAccessManager(),
840 'showCategory'
841 )
842 );
843
844 if ($removeSucc) {
845 $args = array(
846 'child_of' => $category->id,
847 'hide_empty' => false
848 );
849
850 $categoryChilds = get_categories($args);
851
852 add_filter(
853 'get_terms',
854 array(&$userAccessManager, 'showCategory')
855 );
856
857
858 foreach ($categoryChilds as $categoryChild) {
859 $curCategoryChild = new stdClass();
860 $curCategoryChild->id = $categoryChild->term_id;
861 $curCategoryChild->name = $categoryChild->name;
862
863 $curCategoryChild->recursiveMember
864 = array('category' => array());
865 $curCategoryChild->recursiveMember['category'][]
866 = $curCategoryChild;
867 $categories[$curCategoryChild->id]
868 = $curCategoryChild;
869 }
870 }
871 }
872
873 $categories[$category->id] = $category;
874 }
875 }
876
877 return $categories;
878 }
879
880
881 /*
882 * Group posts functions.
883 */
884
885 /**
886 * Returns the membership of a single post.
887 *
888 * @param string $postType The post type needed for the intern representation.
889 * @param integer $objectId The object id.
890 *
891 * @return object
892 */
893 private function _getFullPost($postType, $objectId)
894 {
895 $post = get_post($objectId);
896
897 $isRecursiveMember = array();
898
899 $userAccessManager = $this->getAccessHandler()->getUserAccessManager();
900 $uamOptions = $userAccessManager->getAdminOptions();
901
902 foreach ($this->getObjectsFromType('category', 'full') as $category) {
903 if (in_category($category->id, $post->ID)) {
904 $categoryObject = get_category($category->id);
905 $category->name = $categoryObject->name;
906
907 $isRecursiveMember['category'][] = $category;
908 }
909 }
910
911 if (($postType == 'page'
912 || $postType == 'attachment')
913 && $uamOptions['lock_recursive'] == 'true'
914 && $post->post_parent != 0
915 ) {
916 $parentPost = $this->_getSingleObject(
917 $postType,
918 $post->post_parent,
919 'full'
920 );
921
922 if ($parentPost !== null) {
923 $postObject = get_post($parentPost->id);
924 $parentPost->name = $postObject->post_title;
925
926 $isRecursiveMember['post'][] = $parentPost;
927 }
928 }
929
930 return $isRecursiveMember;
931 }
932
933
934 /*
935 * Group pluggable objects functions.
936 */
937
938 /**
939 * Returns a the recursive membership for a pluggable object.
940 *
941 * @param string $objectType The pluggable object type.
942 * @param integer $objectId The object id.
943 *
944 * @return array
945 */
946 private function _getFullPlObject($objectType, $objectId)
947 {
948 $isRecursiveMember = array();
949
950 $plObject = $this->getAccessHandler()->getPlObject($objectType);
951
952 if (isset($plObject['reference'])
953 && isset($plObject['getFull'])
954 ) {
955 $plRecMember = $plObject['reference']->{$plObject['getFull']}(
956 $objectId,
957 &$this
958 );
959
960 if (is_array($plRecMember)) {
961 $isRecursiveMember = $plRecMember;
962 }
963 }
964
965 return $isRecursiveMember;
966 }
967 }