PluginProbe
User Access Manager / 1.2.6.0
User Access Manager v1.2.6.0
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.2.6.0, at class/UamAccessHandler.class.php

836 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 * 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-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 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 $_oUserAccessManager = null;
31 protected $_aObjectUserGroups = array();
32 protected $_aObjectAccess = array();
33 protected $_aUserGroups = array(
34 'filtered' => array(),
35 'noneFiltered' => array(),
36 );
37 protected $_aPlObjects = array();
38 protected $_aObjectTypes = array(
39 'category',
40 'user',
41 'role',
42 );
43 protected $_aPostableTypes = array(
44 'post',
45 'page',
46 'attachment',
47 );
48 protected $_aPostableTypesMap = array();
49 protected $_aAllObjectTypes = null;
50 protected $_aAllObjectTypesMap = null;
51 protected $_aSqlResults = array();
52 protected $_aValidObjectTypes = array();
53
54 /**
55 * The constructor
56 *
57 * @param UserAccessManager $oUserAccessManager The user access manager object.
58 */
59 public function __construct(UserAccessManager &$oUserAccessManager)
60 {
61 $this->_oUserAccessManager = $oUserAccessManager;
62
63 $this->_aPostableTypes = array_merge($this->_aPostableTypes, get_post_types(array('publicly_queryable' => true), 'names'));
64 $this->_aPostableTypes = array_unique($this->_aPostableTypes);
65
66 $this->_aPostableTypesMap = array_flip($this->_aPostableTypes);
67
68 $this->_aObjectTypes = array_merge($this->_aPostableTypes, $this->_aObjectTypes);
69 add_action( 'registered_post_type', array( &$this, 'registered_post_type'), 10, 2);
70 }
71
72 /**
73 * used for adding custom post types using the registered_post_type hook
74 * @see http://wordpress.org/support/topic/modifying-post-type-using-the-registered_post_type-hook
75 *
76 * @param string $post_type The string for the new post_type
77 * @param stdClass $oArgs The array of arguments used to create the post_type
78 *
79 */
80 public function registered_post_type($post_type, $oArgs)
81 {
82 if ($oArgs->publicly_queryable) {
83 $this->_aPostableTypes[] = $oArgs->name;
84 $this->_aPostableTypes = array_unique($this->_aPostableTypes);
85 $this->_aPostableTypesMap = array_flip($this->_aPostableTypes);
86 $this->_aObjectTypes = array_merge($this->_aPostableTypes, $this->_aObjectTypes);
87 $this->_aAllObjectTypes = null;
88 $this->_aAllObjectTypesMap = null;
89 $this->_aValidObjectTypes = null;
90 }
91 }
92
93 /**
94 * Checks if type is postable.
95 *
96 * @param string $sType
97 *
98 * @return bool
99 */
100 public function isPostableType($sType)
101 {
102 return isset($this->_aPostableTypesMap[$sType]);
103 }
104
105 /**
106 * Returns the user access manager object.
107 *
108 * @return UserAccessManager
109 */
110 public function &getUserAccessManager()
111 {
112 return $this->_oUserAccessManager;
113 }
114
115 /**
116 * Returns the predefined object types.
117 *
118 * @return array
119 */
120 public function getObjectTypes()
121 {
122 return $this->_aObjectTypes;
123 }
124
125 /**
126 * Returns the predefined object types.
127 *
128 * @return array;
129 */
130 public function getPostableTypes()
131 {
132 return $this->_aPostableTypes;
133 }
134
135 /**
136 * Returns all objects types.
137 *
138 * @return array
139 */
140 public function getAllObjectTypes()
141 {
142 if ($this->_aAllObjectTypes === null) {
143 $aPlObjects = $this->getPlObjects();
144
145 $this->_aAllObjectTypes = array_merge(
146 $this->_aObjectTypes,
147 array_keys($aPlObjects)
148 );
149 }
150
151 return $this->_aAllObjectTypes;
152 }
153
154 /**
155 * Returns all objects types as map.
156 *
157 * @return array
158 */
159 public function getAllObjectTypesMap()
160 {
161 if ($this->_aAllObjectTypesMap === null) {
162 $this->_aAllObjectTypesMap = array_flip($this->getAllObjectTypes());
163 }
164
165 return $this->_aAllObjectTypesMap;
166 }
167
168 /**
169 * Magic method getter.
170 *
171 * @param string $sName The name of the function
172 * @param array $aArguments The arguments for the function
173 *
174 * @return mixed
175 */
176 public function __call($sName, $aArguments)
177 {
178 $oUserAccessManager = $this->getUserAccessManager();
179
180 if ($oUserAccessManager->startsWith($sName, 'getUserGroupsFor')) {
181 $sPrefix = 'getUserGroupsFor';
182 } elseif ($oUserAccessManager->startsWith($sName, 'checkAccessFor')) {
183 $sPrefix = 'checkAccessFor';
184 }
185
186 if (isset($sPrefix)) {
187 $sObjectType = str_replace($sPrefix, '', $sName);
188 $sObjectType = strtolower($sObjectType);
189
190 $iObjectId = $aArguments[0];
191
192 if ($sPrefix == 'getUserGroupsFor') {
193 return $this->getUserGroupsForObject($sObjectType, $iObjectId);
194 } elseif ($sPrefix == 'checkAccessFor') {
195 return $this->checkObjectAccess($sObjectType, $iObjectId);
196 }
197 }
198
199 return null;
200 }
201
202 /**
203 * Filter the user groups of an object if authors_can_add_posts_to_groups
204 * option is enabled
205 *
206 * @param UamUserGroup[] $aUserGroups The user groups.
207 *
208 * @return array
209 */
210 protected function _filterUserGroups($aUserGroups)
211 {
212 $aUamOptions = $this->getUserAccessManager()->getAdminOptions();
213
214 if ($aUamOptions['authors_can_add_posts_to_groups'] == 'true'
215 && !$this->checkUserAccess('manage_user_groups')
216 && $this->getUserAccessManager()->atAdminPanel()
217 ) {
218 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
219 $aUserGroupsForUser = $this->getUserGroupsForObject('user', $oCurrentUser->ID);
220
221 foreach ($aUserGroups as $sKey => $oUamUserGroup) {
222 if (!isset($aUserGroupsForUser[$oUamUserGroup->getId()])) {
223 unset($aUserGroups[$sKey]);
224 }
225 }
226 }
227
228 return $aUserGroups;
229 }
230
231 /**
232 * Checks if the object type is a valid one.
233 *
234 * @param string $sObjectType The object type to check.
235 *
236 * @return boolean
237 */
238 public function isValidObjectType($sObjectType)
239 {
240 if (!isset($this->_aValidObjectTypes[$sObjectType])) {
241 $aObjectTypesMap = $this->getAllObjectTypesMap();
242
243 if (isset($aObjectTypesMap[$sObjectType])) {
244 $this->_aValidObjectTypes[$sObjectType] = true;
245 } else {
246 $this->_aValidObjectTypes[$sObjectType] = false;
247 }
248 }
249
250 return $this->_aValidObjectTypes[$sObjectType];
251 }
252
253 /**
254 * Returns all user groups or one requested by the user group id.
255 *
256 * @param integer $iUserGroupId The id of the single user group which should be returned.
257 * @param boolean $blFilter Filter the groups.
258 *
259 * @return UamUserGroup[]|UamUserGroup
260 */
261 public function getUserGroups($iUserGroupId = null, $blFilter = true)
262 {
263 if ($blFilter) {
264 $sFilterAttr = 'filtered';
265 } else {
266 $sFilterAttr = 'noneFiltered';
267 }
268
269 if ($iUserGroupId === null
270 && $this->_aUserGroups[$sFilterAttr] != array()
271 ) {
272 return $this->_aUserGroups[$sFilterAttr];
273 } elseif ($iUserGroupId !== null
274 && $this->_aUserGroups[$sFilterAttr] != array()
275 ) {
276 if (isset($this->_aUserGroups[$sFilterAttr][$iUserGroupId])) {
277 return $this->_aUserGroups[$sFilterAttr][$iUserGroupId];
278 } else {
279 return null;
280 }
281 }
282
283 $this->_aUserGroups[$sFilterAttr] = array();
284
285 /**
286 * @var wpdb $wpdb
287 */
288 global $wpdb;
289
290 $aUserGroupsDb = $wpdb->get_results(
291 "SELECT ID
292 FROM " . DB_ACCESSGROUP . "
293 ORDER BY ID", ARRAY_A
294 );
295
296 if (isset($aUserGroupsDb)) {
297 foreach ($aUserGroupsDb as $aUserGroupDb) {
298 $this->_aUserGroups[$sFilterAttr][$aUserGroupDb['ID']] = new UamUserGroup($this, $aUserGroupDb['ID']);
299 }
300 }
301
302 //Filter the user groups
303 if ($blFilter) {
304 $this->_aUserGroups[$sFilterAttr] = $this->_filterUserGroups($this->_aUserGroups[$sFilterAttr]);
305 }
306
307 if ($iUserGroupId == null) {
308 if (isset($this->_aUserGroups[$sFilterAttr])) {
309 return $this->_aUserGroups[$sFilterAttr];
310 }
311
312 return array();
313 } else {
314 if (isset($this->_aUserGroups[$sFilterAttr][$iUserGroupId])) {
315 return $this->_aUserGroups[$sFilterAttr][$iUserGroupId];
316 }
317
318 return null;
319 }
320 }
321
322 /**
323 * Adds a user group.
324 *
325 * @param UamUserGroup $oUserGroup The user group which we want to add.
326 *
327 * @return null
328 */
329 public function addUserGroup($oUserGroup)
330 {
331 $this->getUserGroups();
332 $this->_aUserGroups['noneFiltered'][$oUserGroup->getId()] = $oUserGroup;
333 $this->_aUserGroups['filtered'] = array();
334 }
335
336 /**
337 * Deletes a user group.
338 *
339 * @param integer $iUserGroupId The user group _iId which we want to delete.
340 *
341 * @return null
342 */
343 public function deleteUserGroup($iUserGroupId)
344 {
345 if ($this->getUserGroups($iUserGroupId) != null) {
346 $this->getUserGroups($iUserGroupId)->delete();
347 unset($this->_aUserGroups['noneFiltered'][$iUserGroupId]);
348 $this->_aUserGroups['filtered'] = array();
349 }
350 }
351
352 /**
353 * Returns the user groups for the given object.
354 *
355 * @param string $sObjectType The object type.
356 * @param integer $iObjectId The _iId of the object.
357 * @param boolean $blFilter Filter the groups.
358 *
359 * @return UamUserGroup[]
360 */
361 public function getUserGroupsForObject($sObjectType, $iObjectId, $blFilter = true)
362 {
363 if (!$this->isValidObjectType($sObjectType)) {
364 return array();
365 }
366
367 if ($sObjectType == 'user') {
368 $blFilter = false;
369 }
370
371 if ($blFilter) {
372 $sFilterAttr = 'filtered';
373 } else {
374 $sFilterAttr = 'noneFiltered';
375 }
376
377 if (isset($this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId])) {
378 return $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId];
379 }
380
381 $sCacheKey = 'getUserGroupsForObject|'.$sObjectType.'|'.$sFilterAttr.'|'.$iObjectId;
382 $oUserAccessManager = $this->getUserAccessManager();
383 $aObjectUserGroups = $oUserAccessManager->getFromCache($sCacheKey);
384
385 if ($aObjectUserGroups !== null) {
386 $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId] = $aObjectUserGroups;
387 } else {
388 $aObjectUserGroups = array();
389 $aUserGroups = $this->getUserGroups(null, $blFilter);
390
391 $aCurIp = explode(".", $_SERVER['REMOTE_ADDR']);
392
393 if (isset($aUserGroups)) {
394 foreach ($aUserGroups as $oUserGroup) {
395 $mObjectMembership = $oUserGroup->objectIsMember($sObjectType, $iObjectId, true);
396
397 if ($mObjectMembership !== false
398 || $sObjectType == 'user' && $this->checkUserIp($aCurIp, $oUserGroup->getIpRange())
399 ) {
400 if (is_array($mObjectMembership)) {
401 $oUserGroup->setRecursiveMembership($sObjectType, $iObjectId, $mObjectMembership);
402 }
403
404 $aObjectUserGroups[$oUserGroup->getId()] = $oUserGroup;
405 }
406 }
407 }
408
409 //Filter the user groups
410 if ($blFilter) {
411 $aObjectUserGroups = $this->_filterUserGroups($aObjectUserGroups);
412 }
413
414 $oUserAccessManager->addToCache($sCacheKey, $aObjectUserGroups);
415 }
416
417 $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId] = $aObjectUserGroups;
418 return $this->_aObjectUserGroups[$sObjectType][$sFilterAttr][$iObjectId];
419 }
420
421 /**
422 * Unset the user groups for _aObjects.
423 *
424 * @return null
425 */
426 public function unsetUserGroupsForObject()
427 {
428 $this->_aObjectUserGroups = array();
429 }
430
431 /**
432 * Checks if the current_user has access to the given post.
433 *
434 * @param string $sObjectType The object type which should be checked.
435 * @param integer $iObjectId The _iId of the object.
436 *
437 * @return boolean
438 */
439 public function checkObjectAccess($sObjectType, $iObjectId)
440 {
441 if (!$this->isValidObjectType($sObjectType)) {
442 return true;
443 }
444
445 if (isset($this->_aObjectAccess[$sObjectType][$iObjectId])) {
446 return $this->_aObjectAccess[$sObjectType][$iObjectId];
447 }
448
449 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
450
451 if ($this->isPostableType($sObjectType)) {
452 $oPost = $this->getUserAccessManager()->getPost($iObjectId);
453 $sAuthorId = $oPost->post_author;
454 } else {
455 $sAuthorId = -1;
456 }
457
458 $aUamOptions = $this->getUserAccessManager()->getAdminOptions();
459 $aMembership = $this->getUserGroupsForObject($sObjectType, $iObjectId, false);
460
461 if ($aMembership == array()
462 || $this->checkUserAccess('manage_user_groups')
463 || $oCurrentUser->ID == $sAuthorId
464 && $aUamOptions['authors_has_access_to_own'] == 'true'
465 ) {
466 return $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
467 }
468
469 $aCurIp = explode(".", $_SERVER['REMOTE_ADDR']);
470
471 foreach ($aMembership as $sKey => $oUserGroup) {
472 if ($this->checkUserIp($aCurIp, $oUserGroup->getIpRange())
473 || $oUserGroup->objectIsMember('user', $oCurrentUser->ID)
474 ) {
475 return $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
476 }
477
478 if ($this->getUserAccessManager()->atAdminPanel() && $oUserGroup->getWriteAccess() == 'all'
479 || !$this->getUserAccessManager()->atAdminPanel() && $oUserGroup->getReadAccess() == 'all'
480 ) {
481 unset($aMembership[$sKey]);
482 }
483 }
484
485 if ($aMembership == array()) {
486 return $this->_aObjectAccess[$sObjectType][$iObjectId] = true;
487 }
488
489 return $this->_aObjectAccess[$sObjectType][$iObjectId] = false;
490 }
491
492
493 /*
494 * SQL functions.
495 */
496
497 /**
498 * Returns the user groups for the current user as sql string.
499 *
500 * @return string
501 */
502 protected function _getUserGroupsForUserAsSqlString()
503 {
504 if (isset($this->_aSqlResults['groupsForUser'])) {
505 return $this->_aSqlResults['groupsForUser'];
506 }
507
508 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
509 $aUserUserGroups = $this->getUserGroupsForObject('user', $oCurrentUser->ID, false);
510 $aUserUserGroupIds = array();
511
512 foreach ($aUserUserGroups as $oUserUserGroup) {
513 $aUserUserGroupIds[] = $oUserUserGroup->getId();
514 }
515
516 if ($aUserUserGroupIds !== array()) {
517 $sUserUserGroups = implode(', ', $aUserUserGroupIds);
518 } else {
519 $sUserUserGroups = "''";
520 }
521
522 $this->_aSqlResults['groupsForUser'] = $sUserUserGroups;
523 return $this->_aSqlResults['groupsForUser'];
524 }
525
526 /**
527 * Returns the categories assigned to the user.
528 *
529 * @return array
530 */
531 public function getCategoriesForUser()
532 {
533 /**
534 * @var wpdb $wpdb
535 */
536 global $wpdb;
537
538 if (isset($this->_aSqlResults['categoriesAssignedToUser'])) {
539 return $this->_aSqlResults['categoriesAssignedToUser'];
540 }
541
542 $sUserUserGroups = $this->_getUserGroupsForUserAsSqlString();
543
544 $sCategoriesAssignedToUserSql = "
545 SELECT igc.object_id
546 FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igc
547 WHERE igc.object_type = 'category'
548 AND igc.group_id IN (".$sUserUserGroups.")";
549
550 $this->_aSqlResults['categoriesAssignedToUser'] = $wpdb->get_col($sCategoriesAssignedToUserSql);
551 return $this->_aSqlResults['categoriesAssignedToUser'];
552 }
553
554 /**
555 * Returns the posts assigned to the user.
556 *
557 * @return array
558 */
559 public function getPostsForUser()
560 {
561 /**
562 * @var wpdb $wpdb
563 */
564 global $wpdb;
565
566 if (isset($this->_aSqlResults['postsAssignedToUser'])) {
567 return $this->_aSqlResults['postsAssignedToUser'];
568 }
569
570 $sUserUserGroup = $this->_getUserGroupsForUserAsSqlString();
571 $sPostableTypes = "'".implode("','", $this->getPostableTypes())."'";
572
573 $sPostAssignedToUserSql = "
574 SELECT igp.object_id
575 FROM ".DB_ACCESSGROUP_TO_OBJECT." AS igp
576 WHERE igp.object_type IN (".$sPostableTypes.")
577 AND igp.group_id IN (".$sUserUserGroup.")";
578
579 $this->_aSqlResults['postsAssignedToUser'] = $wpdb->get_col($sPostAssignedToUserSql);
580 return $this->_aSqlResults['postsAssignedToUser'];
581 }
582
583 /**
584 * Returns the excluded posts.
585 *
586 * @return array
587 */
588 public function getExcludedPosts()
589 {
590 /**
591 * @var wpdb $wpdb
592 */
593 global $wpdb;
594
595 if ($this->checkUserAccess('manage_user_groups')) {
596 $this->_aSqlResults['excludedPosts'] = array();
597 }
598
599 if (isset($this->_aSqlResults['excludedPosts'])) {
600 return $this->_aSqlResults['excludedPosts'];
601 }
602
603 if ($this->getUserAccessManager()->atAdminPanel()) {
604 $sAccessType = "write";
605 } else {
606 $sAccessType = "read";
607 }
608
609 $aCategoriesAssignedToUser = $this->getCategoriesForUser();
610
611 if ($aCategoriesAssignedToUser !== array()) {
612 $sCategoriesAssignedToUser = implode(', ', $aCategoriesAssignedToUser);
613 } else {
614 $sCategoriesAssignedToUser = "''";
615 }
616
617 $aPostAssignedToUser = $this->getPostsForUser();
618
619 if ($aPostAssignedToUser !== array()) {
620 $sPostAssignedToUser = implode(', ', $aPostAssignedToUser);
621 } else {
622 $sPostAssignedToUser = "''";
623 }
624
625 $sPostSql = "SELECT DISTINCT p.ID
626 FROM $wpdb->posts AS p
627 INNER JOIN $wpdb->term_relationships AS tr
628 ON p.ID = tr.object_id
629 INNER JOIN $wpdb->term_taxonomy tt
630 ON tr.term_taxonomy_id = tt.term_taxonomy_id
631 WHERE tt.taxonomy = 'category'
632 AND tt.term_id IN (
633 SELECT gc.object_id
634 FROM ".DB_ACCESSGROUP." iag
635 INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gc
636 ON iag.id = gc.group_id
637 WHERE gc.object_type = 'category'
638 AND iag.".$sAccessType."_access != 'all'
639 AND gc.object_id NOT IN (".$sCategoriesAssignedToUser.")
640 ) AND p.ID NOT IN (".$sPostAssignedToUser.")
641 UNION
642 SELECT DISTINCT gp.object_id
643 FROM ".DB_ACCESSGROUP." AS ag
644 INNER JOIN ".DB_ACCESSGROUP_TO_OBJECT." AS gp
645 ON ag.id = gp.group_id
646 INNER JOIN $wpdb->term_relationships AS tr
647 ON gp.object_id = tr.object_id
648 INNER JOIN $wpdb->term_taxonomy tt
649 ON tr.term_taxonomy_id = tt.term_taxonomy_id
650 WHERE gp.object_type = 'post'
651 AND ag.".$sAccessType."_access != 'all'
652 AND gp.object_id NOT IN (".$sPostAssignedToUser.")
653 AND tt.term_id NOT IN (".$sCategoriesAssignedToUser.")";
654
655 $this->_aSqlResults['excludedPosts'] = $wpdb->get_col($sPostSql);
656 return $this->_aSqlResults['excludedPosts'];
657 }
658
659
660 /*
661 * Other functions
662 */
663
664 /**
665 * Checks if the given ip matches with the range.
666 *
667 * @param array $aCurIp The ip of the current user.
668 * @param array $aIpRanges The ip ranges.
669 *
670 * @return boolean
671 */
672 public function checkUserIp($aCurIp, $aIpRanges)
673 {
674 if (isset($aIpRanges)) {
675 foreach ($aIpRanges as $aIpRange) {
676 $aIpRange = explode("-", $aIpRange);
677 $aRangeBegin = explode(".", $aIpRange[0]);
678
679 if (isset($aIpRange[1])) {
680 $aRangeEnd = explode(".", $aIpRange[1]);
681 } else {
682 $aRangeEnd = explode(".", $aIpRange[0]);
683 }
684
685 if ($aRangeBegin[0] <= $aCurIp[0] && $aCurIp[0] <= $aRangeEnd[0]
686 && $aRangeBegin[1] <= $aCurIp[1] && $aCurIp[1] <= $aRangeEnd[1]
687 && $aRangeBegin[2] <= $aCurIp[2] && $aCurIp[2] <= $aRangeEnd[2]
688 && $aRangeBegin[3] <= $aCurIp[3] && $aCurIp[3] <= $aRangeEnd[3]
689 ) {
690 return true;
691 }
692 }
693 }
694
695 return false;
696 }
697
698 /**
699 * Return the role of the user.
700 *
701 * @param integer $iUserId The user _iId.
702 *
703 * @return string|null
704 */
705 protected function _getUserRole($iUserId)
706 {
707 /**
708 * @var wpdb $wpdb
709 */
710 global $wpdb;
711
712 $oUserData = get_userdata($iUserId);
713
714 if (!empty($oUserData->user_level) && !isset($oUserData->user_level)) {
715 $oUserData->user_level = null;
716 }
717
718 if (isset($oUserData->{$wpdb->prefix . "capabilities"})) {
719 $aCapabilities = $oUserData->{$wpdb->prefix . "capabilities"};
720 } else {
721 $aCapabilities = array();
722 }
723
724 $aRole = (is_array($aCapabilities) && count($aCapabilities) > 0) ? array_keys($aCapabilities) : array('norole');
725 return trim($aRole[0]);
726 }
727
728 /**
729 * Checks if the user is an admin user
730 *
731 * @param integer $iUserId The user _iId.
732 *
733 * @return boolean
734 */
735 public function userIsAdmin($iUserId)
736 {
737 $sRole = $this->_getUserRole($iUserId);
738
739 if ($sRole == 'administrator' || is_super_admin($iUserId)) {
740 return true;
741 }
742
743 return false;
744 }
745
746 /**
747 * Checks the user access by user level.
748 *
749 * @param bool|string $sAllowedCapability If true check also for the capability.
750 *
751 * @return boolean
752 */
753 public function checkUserAccess($sAllowedCapability = false)
754 {
755 $oCurrentUser = $this->getUserAccessManager()->getCurrentUser();
756 $aUamOptions = $this->getUserAccessManager()->getAdminOptions();
757
758 $sRole = $this->_getUserRole($oCurrentUser->ID);
759 $aOrderedRoles = $this->getRolesOrdered();
760
761 if (isset($aOrderedRoles[$sRole])
762 && $aOrderedRoles[$sRole] >= $aOrderedRoles[$aUamOptions['full_access_role']]
763 || $sRole == 'administrator' || is_super_admin($oCurrentUser->ID)
764 || ($sAllowedCapability && $oCurrentUser->has_cap($sAllowedCapability))
765 ) {
766 return true;
767 }
768
769 return false;
770 }
771
772 /**
773 * Returns the roles as associative array.
774 *
775 * @return array
776 */
777 public function getRolesOrdered()
778 {
779 $aOrderedRoles = array(
780 'norole' => 0,
781 'subscriber' => 1,
782 'contributor' => 2,
783 'author' => 3,
784 'editor' => 4,
785 'administrator' => 5
786 );
787
788 return $aOrderedRoles;
789 }
790
791 /**
792 * Registers object that should be handelt by the user access manager.
793 *
794 * @param array $oObject The object which you want to register.
795 *
796 * @return boolean
797 */
798 public function registerPlObject($oObject)
799 {
800 if (!isset($oObject['name']) || !isset($oObject['reference'])
801 || !isset($oObject['getFull']) || !isset($oObject['getFullObjects'])
802 ) {
803 return false;
804 }
805
806 $this->_aPlObjects[$oObject['name']] = $oObject;
807
808 return true;
809 }
810
811 /**
812 * Returns a registered pluggable object.
813 *
814 * @param string $sObjectName The name of the object which should be returned.
815 *
816 * @return array
817 */
818 public function getPlObject($sObjectName)
819 {
820 if (isset($this->_aPlObjects[$sObjectName])) {
821 return $this->_aPlObjects[$sObjectName];
822 }
823
824 return array();
825 }
826
827 /**
828 * Returns all registered pluggable objects.
829 *
830 * @return array
831 */
832 public function getPlObjects()
833 {
834 return $this->_aPlObjects;
835 }
836 }