PluginProbe
User Access Manager / 1.1.3
User Access Manager v1.1.3
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 / UamAccessHandler.class.php

UamAccessHandler.class.php in User Access Manager 1.1.3, at class/UamAccessHandler.class.php

782 lines 21.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * UamAccessHandler.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 access handler 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 UamAccessHandler
29 {
30 protected $userAccessManager = null;
31 protected $objectUserGroups = array();
32 protected $objectAccess = array();
33 protected $userGroups = array(
34 'filtered' => array(),
35 'noneFiltered' => array(),
36 );
37 protected $plObjects = array();
38 protected $objectTypes = array(
39 'post',
40 'page',
41 'attachment',
42 'category',
43 'user',
44 'role'
45 );
46 protected $allObjectTypes = null;
47 protected $sqlResults = array();
48
49 /**
50 * The consturctor
51 *
52 * @param object &$userAccessManager The user access manager object.
53 *
54 * @return null
55 */
56 public function __construct(&$userAccessManager)
57 {
58 $this->userAccessManager = $userAccessManager;
59 }
60
61 /**
62 * Returns the user access manager object.
63 *
64 * @return object
65 */
66 public function &getUserAccessManager()
67 {
68 return $this->userAccessManager;
69 }
70
71 /**
72 * Returns the predfined object types.
73 *
74 * @return array();
75 */
76 public function getObjectTypes()
77 {
78 return $this->objectTypes;
79 }
80
81 /**
82 * Returns all objects types.
83 *
84 * @return array
85 */
86 public function getAllObjectTypes()
87 {
88 if (isset($this->allObjectTypes)) {
89 return $this->allObjectTypes;
90 }
91
92 $plObjects = $this->getPlObjects();
93
94 $this->allObjectTypes = array_merge(
95 $this->objectTypes,
96 array_keys($plObjects)
97 );
98
99 return $this->allObjectTypes;
100 }
101
102 /**
103 * Magic method getter.
104 *
105 * @param string $name The name of the function
106 * @param array $arguments The arguments for the function
107 *
108 * @return mixed
109 */
110 public function __call($name, $arguments)
111 {
112 echo $name;
113 exit;
114
115 $uam = $this->getUserAccessManager();
116
117 $action = '';
118
119 if ($uam->startsWith($name, 'getUserGroupsFor')) {
120 $prefix = 'getUserGroupsFor';
121 } elseif ($uam->startsWith($name, 'checkAccessFor')) {
122 $prefix = 'checkAccessFor';
123 }
124
125 $objectType = str_replace($prefix, '', $name);
126 $objectType = strtolower($objectType);
127
128 $objectId = $arguments[0];
129
130 if ($prefix == 'getUserGroupsFor') {
131 return $this->getUserGroupsForObject(
132 $objectType,
133 $objectId
134 );
135 } elseif ($prefix == 'checkAccessFor') {
136 return $this->checkObjectAccess(
137 $objectType,
138 $objectId
139 );
140 }
141 }
142
143 /**
144 * Filter the user groups of an object if authors_can_add_posts_to_groups
145 * option is enabled
146 *
147 * @param array $userGroups The user groups.
148 *
149 * @return array
150 */
151 private function _filterUserGroups($userGroups)
152 {
153 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
154
155 if ($uamOptions['authors_can_add_posts_to_groups'] == 'true'
156 && !$this->checkUserAccess()
157 && $this->getUserAccessManager()->atAdminPanel()
158 ) {
159 global $current_user;
160 //Force user infos
161 wp_get_current_user();
162
163 $userGroupsForUser
164 = $this->getUserGroupsForObject('user', $current_user->ID);
165
166 foreach ($userGroups as $key => $uamUserGroup) {
167 if (!array_key_exists($uamUserGroup->getId(), $userGroupsForUser)) {
168 unset($userGroups[$key]);
169 }
170 }
171 }
172
173 return $userGroups;
174 }
175
176 /**
177 * Returns all user groups or one requested by the user group id.
178 *
179 * @param integer $userGroupId The id of the single user group
180 * which should be returned.
181 * @param boolean $filter Filter the groups.
182 *
183 * @return array|object
184 */
185 public function getUserGroups($userGroupId = null, $filter = true)
186 {
187 if ($filter) {
188 $filterAttr = 'filtered';
189 } else {
190 $filterAttr = 'noneFiltered';
191 }
192
193 if ($userGroupId === null
194 && $this->userGroups[$filterAttr] != array()
195 ) {
196 return $this->userGroups[$filterAttr];
197 } elseif ($userGroupId !== null
198 && $this->userGroups[$filterAttr] != array()
199 ) {
200 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
201 return $this->userGroups[$filterAttr][$userGroupId];
202 } else {
203 return null;
204 }
205 }
206
207 $this->userGroups[$filterAttr] = array();
208
209 global $wpdb;
210
211 $userGroupsDb = $wpdb->get_results(
212 "SELECT ID
213 FROM " . DB_ACCESSGROUP . "
214 ORDER BY ID", ARRAY_A
215 );
216
217 if (isset($userGroupsDb)) {
218 foreach ($userGroupsDb as $userGroupDb) {
219 $this->userGroups[$filterAttr][$userGroupDb['ID']]
220 = new UamUserGroup(&$this, $userGroupDb['ID']);
221 }
222 }
223
224 //Filter the user groups
225 if ($filter) {
226 $this->userGroups[$filterAttr]
227 = $this->_filterUserGroups($this->userGroups[$filterAttr]);
228 }
229
230 if ($userGroupId == null) {
231 return $this->userGroups[$filterAttr];
232 } elseif ($userGroupId != null) {
233 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
234 return $this->userGroups[$filterAttr][$userGroupId];
235 } else {
236 return null;
237 }
238 }
239 }
240
241 /**
242 * Adds a user group.
243 *
244 * @param object $userGroup The user group which we want to add.
245 *
246 * @return null
247 */
248 public function addUserGroup($userGroup)
249 {
250 $this->getUserGroups();
251 $this->userGroups['noneFiltered'][$userGroup->getId()] = $userGroup;
252 $this->userGroups['filtered'] = array();
253 }
254
255 /**
256 * Deletes a user group.
257 *
258 * @param integer $userGroupId The user group id which we want to delete.
259 *
260 * @return null
261 */
262 public function deleteUserGroup($userGroupId)
263 {
264 if ($this->getUserGroups($userGroupId) != null) {
265 $this->getUserGroups($userGroupId)->delete();
266 unset($this->userGroups['noneFiltered'][$userGroupId]);
267 $this->userGroups['filtered'] = array();
268 }
269 }
270
271 /**
272 * Returns the user groups for the given object.
273 *
274 * @param string $objectType The object type.
275 * @param integer $objectId The id of the object.
276 * @param boolean $filter Filter the groups.
277 *
278 * @return array
279 */
280 public function getUserGroupsForObject($objectType, $objectId, $filter = true)
281 {
282 if (!in_array($objectType, $this->getAllObjectTypes())) {
283 return;
284 }
285
286 if ($objectType == 'user') {
287 $filter = false;
288 }
289
290 if ($filter) {
291 $filterAttr = 'filtered';
292 } else {
293 $filterAttr = 'noneFiltered';
294 }
295
296 if (isset($this->objectUserGroups[$objectType][$filterAttr][$objectId])) {
297 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
298 }
299
300 $objectUserGroups = array();
301
302 $userGroups = $this->getUserGroups(null, $filter);
303
304 $plObject = false;
305
306 if ($objectType != 'user'
307 && $objectType != 'post'
308 && $objectType != 'category'
309 ) {
310 $plObject = true;
311 }
312
313 $curIp = explode(".", $_SERVER['REMOTE_ADDR']);
314
315 if (isset($userGroups)) {
316 foreach ($userGroups as $userGroup) {
317 $objectMembership = $userGroup->objectIsMember(
318 $objectType,
319 $objectId,
320 true
321 );
322
323 if ($objectMembership !== false
324 || $objectType == 'user'
325 && $this->checkUserIp($curIp, $userGroup->getIpRange())
326 ) {
327 if (is_array($objectMembership)) {
328 $userGroup->setRecursive[$objectType][$objectId]
329 = $objectMembership;
330 }
331
332 $objectUserGroups[$userGroup->getId()]
333 = $userGroup;
334 }
335 }
336 }
337
338 //Filter the user groups
339 if ($filter) {
340 $objectUserGroups = $this->_filterUserGroups($objectUserGroups);
341 }
342
343 $this->objectUserGroups[$objectType][$filterAttr][$objectId]
344 = $objectUserGroups;
345
346 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
347 }
348
349 /**
350 * Unsets the usergroups for objects.
351 *
352 * @return null
353 */
354 public function unsetUserGroupsForObject()
355 {
356 $this->objectUserGroups = array();
357 }
358
359 /**
360 * Checks if the current_user has access to the given post.
361 *
362 * @param string $objectType The object type which should be checked.
363 * @param integer $objectId The id of the object.
364 *
365 * @return boolean
366 */
367 public function checkObjectAccess($objectType, $objectId)
368 {
369 if (!in_array($objectType, $this->getAllObjectTypes())) {
370 return;
371 }
372
373 if (isset($this->objectAccess[$objectType][$objectId])) {
374 return $this->objectAccess[$objectType][$objectId];
375 }
376
377 global $current_user;
378 //Force user infos
379 wp_get_current_user();
380
381 if ($objectType == 'post') {
382 $post = get_post($objectId);
383 $authorId = $post->post_author;
384 } else {
385 $authorId = -1;
386 }
387
388 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
389 $membership = $this->getUserGroupsForObject($objectType, $objectId, false);
390
391 if ($membership == array()
392 || $this->checkUserAccess()
393 || $current_user->ID == $authorId
394 && $uamOptions['authors_has_access_to_own'] == 'true'
395 ) {
396 return $this->objectAccess[$objectType][$objectId] = true;
397 }
398
399 $curIp = explode(".", $_SERVER['REMOTE_ADDR']);
400
401 foreach ($membership as $key => $userGroup) {
402 if ($this->checkUserIp($curIp, $userGroup->getIpRange())
403 || $userGroup->objectIsMember('user', $current_user->ID)
404 ) {
405 return $this->objectAccess[$objectType][$objectId] = true;
406 break;
407 }
408
409 if ($this->getUserAccessManager()->atAdminPanel()
410 && $userGroup->getWriteAccess() == 'all'
411 || !$this->getUserAccessManager()->atAdminPanel()
412 && $userGroup->getReadAccess() == 'all'
413 ) {
414 unset($membership[$key]);
415 }
416 }
417
418 if ($membership == array()) {
419 return $this->objectAccess[$objectType][$objectId] = true;
420 }
421
422 return $this->objectAccess[$objectType][$objectId] = false;
423 }
424
425
426 /*
427 * SQL functions.
428 */
429
430 /**
431 * Returns the usergroups for the current user as sql string.
432 *
433 * @return string
434 */
435 private function _getUserGroupsForUserAsSqlString()
436 {
437 if (isset($this->sqlResults['groupsForUser'])) {
438 return $this->sqlResults['groupsForUser'];
439 }
440
441 global $current_user;
442 //Force user infos
443 wp_get_current_user();
444
445 $userUserGroups = $this->getUserGroupsForObject(
446 'user',
447 $current_user->ID,
448 false
449 );
450
451 $userUserGroupArray = array();
452
453 foreach ($userUserGroups as $userUserGroup) {
454 $userUserGroupArray[] = $userUserGroup->getId();
455 }
456
457 if ($userUserGroupArray !== array()) {
458 $userUserGroupString = implode(', ', $userUserGroupArray);
459 } else {
460 $userUserGroupString = "''";
461 }
462
463 $this->sqlResults['groupsForUser'] = $userUserGroupString;
464
465 return $this->sqlResults['groupsForUser'];
466 }
467
468 /**
469 * Returns the categories assigned to the user.
470 *
471 * @return array
472 */
473 public function getCategoriesForUser()
474 {
475 global $wpdb;
476
477 if (isset($this->sqlResults['categoriesAssignedToUser'])) {
478 return $this->sqlResults['categoriesAssignedToUser'];
479 }
480
481 $userUserGroupString = $this->_getUserGroupsForUserAsSqlString();
482
483 $categoriesAssignedToUserSql = "
484 SELECT igc.object_id
485 FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igc
486 WHERE igc.object_type = 'category'
487 AND igc.group_id IN (".$userUserGroupString.")";
488
489 $this->sqlResults['categoriesAssignedToUser']
490 = $wpdb->get_col($categoriesAssignedToUserSql);
491
492 return $this->sqlResults['categoriesAssignedToUser'];
493 }
494
495 /**
496 * Returns the posts assigned to the user.
497 *
498 * @return array
499 */
500 public function getPostsForUser()
501 {
502 global $wpdb;
503
504 if (isset($this->sqlResults['postsAssignedToUser'])) {
505 return $this->sqlResults['postsAssignedToUser'];
506 }
507
508 $userUserGroupString = $this->_getUserGroupsForUserAsSqlString();
509
510 $postAssignedToUserSql = "
511 SELECT igp.object_id
512 FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igp
513 WHERE igp.object_type = 'post'
514 AND igp.group_id IN (".$userUserGroupString.")";
515
516 $this->sqlResults['postsAssignedToUser']
517 = $wpdb->get_col($postAssignedToUserSql);
518
519 return $this->sqlResults['postsAssignedToUser'];
520 }
521
522 /**
523 * Returns the excluded posts.
524 *
525 * @return array
526 */
527 public function getExcludedPosts()
528 {
529 global $wpdb;
530
531 if ($this->checkUserAccess()) {
532 $this->sqlResults['excludedPosts'] = array();
533 }
534
535 if (isset($this->sqlResults['excludedPosts'])) {
536 return $this->sqlResults['excludedPosts'];
537 }
538
539 if ($this->getUserAccessManager()->atAdminPanel()) {
540 $accessType = "write";
541 } else {
542 $accessType = "read";
543 }
544
545 $categoriesAssignedToUser = $this->getCategoriesForUser();
546
547 if ($categoriesAssignedToUser !== array()) {
548 $categoriesAssignedToUserString
549 = implode(', ', $categoriesAssignedToUser);
550 } else {
551 $categoriesAssignedToUserString = "''";
552 }
553
554 $postAssignedToUser = $this->getPostsForUser();
555
556 if ($postAssignedToUser !== array()) {
557 $postAssignedToUserString
558 = implode(', ', $postAssignedToUser);
559 } else {
560 $postAssignedToUserString = "''";
561 }
562
563 $postSql = "SELECT DISTINCT p.ID
564 FROM $wpdb->posts AS p
565 INNER JOIN $wpdb->term_relationships AS tr
566 ON p.ID = tr.object_id
567 INNER JOIN $wpdb->term_taxonomy tt
568 ON tr.term_taxonomy_id = tt.term_taxonomy_id
569 WHERE tt.taxonomy = 'category'
570 AND tt.term_id IN (
571 SELECT gc.object_id
572 FROM ".DB_ACCESSGROUP." iag
573 INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gc
574 ON iag.id = gc.group_id
575 WHERE gc.object_type = 'category'
576 AND iag.".$accessType."_access != 'all'
577 AND gc.object_id NOT IN (".$categoriesAssignedToUserString.")
578 ) AND p.ID NOT IN (".$postAssignedToUserString.")
579 UNION
580 SELECT DISTINCT gp.object_id
581 FROM ".DB_ACCESSGROUP." AS ag
582 INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gp
583 ON ag.id = gp.group_id
584 INNER JOIN $wpdb->term_relationships AS tr
585 ON gp.object_id = tr.object_id
586 INNER JOIN $wpdb->term_taxonomy tt
587 ON tr.term_taxonomy_id = tt.term_taxonomy_id
588 WHERE gp.object_type = 'post'
589 AND ag.".$accessType."_access != 'all'
590 AND gp.object_id NOT IN (".$postAssignedToUserString.")
591 AND tt.term_id NOT IN (".$categoriesAssignedToUserString.")";
592
593 $this->sqlResults['excludedPosts'] = $wpdb->get_col($postSql);
594
595 return $this->sqlResults['excludedPosts'];
596 }
597
598
599 /*
600 * Other functions
601 */
602
603 /**
604 * Checks if the given ip matches with the range.
605 *
606 * @param string $curIp The ip of the current user.
607 * @param array $ipRanges The ip ranges.
608 *
609 * @return boolean
610 */
611 public function checkUserIp($curIp, $ipRanges)
612 {
613 if (isset($ipRanges)) {
614 foreach ($ipRanges as $ipRange) {
615 $ipRange = explode("-", $ipRange);
616 $rangeBegin = explode(".", $ipRange[0]);
617
618 if (isset($ipRange[1])) {
619 $rangeEnd = explode(".", $ipRange[1]);
620 } else {
621 $rangeEnd = explode(".", $ipRange[0]);
622 }
623
624 if ($rangeBegin[0] <= $curIp[0]
625 && $curIp[0] <= $rangeEnd[0]
626 && $rangeBegin[1] <= $curIp[1]
627 && $curIp[1] <= $rangeEnd[1]
628 && $rangeBegin[2] <= $curIp[2]
629 && $curIp[2] <= $rangeEnd[2]
630 && $rangeBegin[3] <= $curIp[3]
631 && $curIp[3] <= $rangeEnd[3]
632 ) {
633 return true;
634 }
635 }
636 }
637
638 return false;
639 }
640
641 /**
642 * Return the role of the user.
643 *
644 * @param integer $userId The user id.
645 *
646 * @return string|null
647 */
648 private function _getUserRole($userId)
649 {
650 global $wpdb;
651
652 $curUserdata = get_userdata($userId);
653
654 if (!isset($curUserdata->user_level)) {
655 $curUserdata->user_level = null;
656 }
657
658 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
659 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
660 } else {
661 $capabilities = null;
662 }
663
664 $role = is_array($capabilities) ?
665 array_keys($capabilities) : array('norole');
666
667 return trim($role[0]);
668 }
669
670 /**
671 * Checks if the user is an admin user
672 *
673 * @param integer $userId The user id.
674 *
675 * @return boolean
676 */
677 public function userIsAdmin($userId)
678 {
679 $role = $this->_getUserRole($userId);
680
681 if ($role == 'administrator'
682 || is_super_admin($userId)
683 ) {
684 return true;
685 }
686
687 return false;
688 }
689
690 /**
691 * Checks the user access by user level.
692 *
693 * @return boolean
694 */
695 public function checkUserAccess()
696 {
697 global $current_user;
698 //Force user infos
699 wp_get_current_user();
700
701 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
702
703 $role = $this->_getUserRole($current_user->ID);
704 $orderedRoles = $this->getRolesOrdered();
705
706 if ($orderedRoles[$role] >= $orderedRoles[$uamOptions['full_access_role']]
707 || $role == 'administrator'
708 || is_super_admin($current_user->ID)
709 ) {
710 return true;
711 }
712
713 return false;
714 }
715
716 /**
717 * Returns the roles as assoziative array.
718 *
719 * @return array
720 */
721 public function getRolesOrdered()
722 {
723 $orderedRoles = array(
724 'norole' => 0,
725 'subscriber' => 1,
726 'contributor' => 2,
727 'author' => 3,
728 'editor' => 4,
729 'administrator' => 5
730 );
731
732 return $orderedRoles;
733 }
734
735 /**
736 * Registers object that should be handelt by the user access manager.
737 *
738 * @param array $object The object which you want to register.
739 *
740 * @return boolean
741 */
742 public function registerPlObject($object)
743 {
744 if (!isset($object['name'])
745 || !isset($object['reference'])
746 || !isset($object['getFull'])
747 || !isset($object['getFullObjects'])
748 ) {
749 return false;
750 }
751
752 $this->plObjects[$object['name']] = $object;
753
754 return true;
755 }
756
757 /**
758 * Returns a registerd pluggable object.
759 *
760 * @param string $objectName The name of the object which should be returned.
761 *
762 * @return array
763 */
764 public function getPlObject($objectName)
765 {
766 if (isset($this->plObjects[$objectName])) {
767 return $this->plObjects[$objectName];
768 }
769
770 return array();
771 }
772
773 /**
774 * Returns all registerd pluggable objects.
775 *
776 * @return array
777 */
778 public function getPlObjects()
779 {
780 return $this->plObjects;
781 }
782 }