PluginProbe
User Access Manager / 1.2.8
User Access Manager v1.2.8
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.php

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

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