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 / UamAccessHandler.class.php

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

595 lines 16.1 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
48 /**
49 * The consturctor
50 *
51 * @param object &$userAccessManager The user access manager object.
52 *
53 * @return null
54 */
55 public function __construct(&$userAccessManager)
56 {
57 $this->userAccessManager = $userAccessManager;
58 }
59
60 /**
61 * Returns the user access manager object.
62 *
63 * @return object
64 */
65 public function &getUserAccessManager()
66 {
67 return $this->userAccessManager;
68 }
69
70 /**
71 * Returns the predfined object types.
72 *
73 * @return array();
74 */
75 public function getObjectTypes()
76 {
77 return $this->objectTypes;
78 }
79
80 /**
81 * Returns all objects types.
82 *
83 * @return array
84 */
85 public function getAllObjectTypes()
86 {
87 if (isset($this->allObjectTypes)) {
88 return $this->allObjectTypes;
89 }
90
91 $plObjects = $this->getPlObjects();
92
93 $this->allObjectTypes = array_merge(
94 $this->objectTypes,
95 array_keys($plObjects)
96 );
97
98 return $this->allObjectTypes;
99 }
100
101 /**
102 * Magic method getter.
103 *
104 * @param string $name The name of the function
105 * @param array $arguments The arguments for the function
106 *
107 * @return mixed
108 */
109 public function __call($name, $arguments)
110 {
111 echo $name;
112 exit;
113
114 $uam = $this->getUserAccessManager();
115
116 $action = '';
117
118 if ($uam->startsWith($name, 'getUserGroupsFor')) {
119 $prefix = 'getUserGroupsFor';
120 } elseif ($uam->startsWith($name, 'checkAccessFor')) {
121 $prefix = 'checkAccessFor';
122 }
123
124 $objectType = str_replace($prefix, '', $name);
125 $objectType = strtolower($objectType);
126
127 $objectId = $arguments[0];
128
129 if ($prefix == 'getUserGroupsFor') {
130 return $this->getUserGroupsForObject(
131 $objectType,
132 $objectId
133 );
134 } elseif ($prefix == 'checkAccessFor') {
135 return $this->checkObjectAccess(
136 $objectType,
137 $objectId
138 );
139 }
140 }
141
142 /**
143 * Filter the user groups of an object if authors_can_add_posts_to_groups
144 * option is enabled
145 *
146 * @param array $userGroups The user groups.
147 *
148 * @return array
149 */
150 private function _filterUserGroups($userGroups)
151 {
152 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
153
154 if ($uamOptions['authors_can_add_posts_to_groups'] == 'true'
155 && !$this->checkUserAccess()
156 && $this->getUserAccessManager()->atAdminPanel
157 ) {
158 global $current_user;
159 //Force user infos
160 wp_get_current_user();
161
162 $userGroupsForUser
163 = $this->getUserGroupsForObject('user', $current_user->ID);
164
165 foreach ($userGroups as $key => $uamUserGroup) {
166 if (!array_key_exists($uamUserGroup->getId(), $userGroupsForUser)) {
167 unset($userGroups[$key]);
168 }
169 }
170 }
171
172 return $userGroups;
173 }
174
175 /**
176 * Returns all user groups or one requested by the user group id.
177 *
178 * @param integer $userGroupId The id of the single user group
179 * which should be returned.
180 * @param boolean $filter Filter the groups.
181 *
182 * @return array|object
183 */
184 public function getUserGroups($userGroupId = null, $filter = true)
185 {
186 if ($filter) {
187 $filterAttr = 'filtered';
188 } else {
189 $filterAttr = 'noneFiltered';
190 }
191
192 if ($userGroupId === null
193 && $this->userGroups[$filterAttr] != array()
194 ) {
195 return $this->userGroups[$filterAttr];
196 } elseif ($userGroupId !== null
197 && $this->userGroups[$filterAttr] != array()
198 ) {
199 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
200 return $this->userGroups[$filterAttr][$userGroupId];
201 } else {
202 return null;
203 }
204 }
205
206 $this->userGroups[$filterAttr] = array();
207
208 global $wpdb;
209
210 $userGroupsDb = $wpdb->get_results(
211 "SELECT ID
212 FROM " . DB_ACCESSGROUP . "
213 ORDER BY ID", ARRAY_A
214 );
215
216 if (isset($userGroupsDb)) {
217 foreach ($userGroupsDb as $userGroupDb) {
218 $this->userGroups[$filterAttr][$userGroupDb['ID']]
219 = new UamUserGroup(&$this, $userGroupDb['ID']);
220 }
221 }
222
223 //Filter the user groups
224 if ($filter) {
225 $this->userGroups[$filterAttr]
226 = $this->_filterUserGroups($this->userGroups[$filterAttr]);
227 }
228
229 if ($userGroupId == null) {
230 return $this->userGroups[$filterAttr];
231 } elseif ($userGroupId != null) {
232 if (isset($this->userGroups[$filterAttr][$userGroupId])) {
233 return $this->userGroups[$filterAttr][$userGroupId];
234 } else {
235 return null;
236 }
237 }
238 }
239
240 /**
241 * Adds a user group.
242 *
243 * @param object $userGroup The user group which we want to add.
244 *
245 * @return null
246 */
247 public function addUserGroup($userGroup)
248 {
249 $this->getUserGroups();
250 $this->userGroups['noneFiltered'][$userGroup->getId()] = $userGroup;
251 $this->userGroups['filtered'] = array();
252 }
253
254 /**
255 * Deletes a user group.
256 *
257 * @param integer $userGroupId The user group id which we want to delete.
258 *
259 * @return null
260 */
261 public function deleteUserGroup($userGroupId)
262 {
263 if ($this->getUserGroups($userGroupId) != null) {
264 $this->getUserGroups($userGroupId)->delete();
265 unset($this->userGroups['noneFiltered'][$userGroupId]);
266 $this->userGroups['filtered'] = array();
267 }
268 }
269
270 /**
271 * Returns the user groups for the given object.
272 *
273 * @param string $objectType The object type.
274 * @param integer $objectId The id of the object.
275 * @param boolean $filter Filter the groups.
276 *
277 * @return array
278 */
279 public function getUserGroupsForObject($objectType, $objectId, $filter = true)
280 {
281 if ($objectType == 'user') {
282 $filter = false;
283 }
284
285 if ($filter) {
286 $filterAttr = 'filtered';
287 } else {
288 $filterAttr = 'noneFiltered';
289 }
290
291 if (isset($this->objectUserGroups[$objectType][$filterAttr][$objectId])) {
292 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
293 }
294
295 $objectUserGroups = array();
296
297 $userGroups = $this->getUserGroups(null, $filter);
298
299 $plObject = false;
300
301 if ($objectType != 'user'
302 && $objectType != 'post'
303 && $objectType != 'category'
304 ) {
305 $plObject = true;
306 }
307
308 if (isset($userGroups)) {
309 foreach ($userGroups as $userGroup) {
310 $objectMembership = $userGroup->objectIsMember(
311 $objectType,
312 $objectId,
313 true
314 );
315
316 if ($objectMembership !== false) {
317 if (is_array($objectMembership)) {
318 $userGroup->setRecursive[$objectType][$objectId]
319 = $objectMembership;
320 }
321
322 $objectUserGroups[$userGroup->getId()]
323 = $userGroup;
324 }
325 }
326 }
327
328 //Filter the user groups
329 if ($filter) {
330 $objectUserGroups = $this->_filterUserGroups($objectUserGroups);
331 }
332
333 $this->objectUserGroups[$objectType][$filterAttr][$objectId]
334 = $objectUserGroups;
335
336 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
337 }
338
339 /**
340 * Unsets the usergroups for objects.
341 *
342 * @return null
343 */
344 public function unsetUserGroupsForObject()
345 {
346 $this->objectUserGroups = array();
347 }
348
349 /**
350 * Checks if the current_user has access to the given post.
351 *
352 * @param string $objectType The object type which should be checked.
353 * @param integer $objectId The id of the object.
354 *
355 * @return boolean
356 */
357 public function checkObjectAccess($objectType, $objectId)
358 {
359 if (isset($this->objectAccess[$objectType][$objectId])) {
360 return $this->objectAccess[$objectType][$objectId];
361 }
362
363 global $current_user;
364 //Force user infos
365 wp_get_current_user();
366
367 if ($objectType == 'post') {
368 $post = get_post($objectId);
369 $authorId = $post->post_author;
370 } else {
371 $authorId = -1;
372 }
373
374 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
375 $membership = $this->getUserGroupsForObject($objectType, $objectId, false);
376
377 if ($membership == array()
378 || $this->checkUserAccess()
379 || $current_user->ID == $authorId
380 && $uamOptions['authors_has_access_to_own'] == 'true'
381 ) {
382 return $this->objectAccess[$objectType][$objectId] = true;
383 }
384
385 $curIp = explode(".", $_SERVER['REMOTE_ADDR']);
386
387 foreach ($membership as $key => $userGroup) {
388 if ($this->checkUserIp($curIp, $userGroup->getIpRange())
389 || $userGroup->objectIsMember('user', $current_user->ID)
390 ) {
391 return $this->objectAccess[$objectType][$objectId] = true;
392 break;
393 }
394
395 if ($this->getUserAccessManager()->atAdminPanel
396 && $userGroup->getWriteAccess() == 'all'
397 || !$this->getUserAccessManager()->atAdminPanel
398 && $userGroup->getReadAccess() == 'all'
399 ) {
400 unset($membership[$key]);
401 }
402 }
403
404 if ($membership == array()) {
405 return $this->objectAccess[$objectType][$objectId] = true;
406 }
407
408 return $this->objectAccess[$objectType][$objectId] = false;
409 }
410
411
412 /*
413 * Other functions
414 */
415
416 /**
417 * Checks if the given ip matches with the range.
418 *
419 * @param string $curIp The ip of the current user.
420 * @param array $ipRanges The ip ranges.
421 *
422 * @return boolean
423 */
424 public function checkUserIp($curIp, $ipRanges)
425 {
426 if (isset($ipRanges)) {
427 foreach ($ipRanges as $ipRange) {
428 $ipRange = explode("-", $ipRange);
429 $rangeBegin = explode(".", $ipRange[0]);
430
431 if (isset($ipRange[1])) {
432 $rangeEnd = explode(".", $ipRange[1]);
433 } else {
434 $rangeEnd = explode(".", $ipRange[0]);
435 }
436
437 if ($rangeBegin[0] <= $curIp[0]
438 && $curIp[0] <= $rangeEnd[0]
439 && $rangeBegin[1] <= $curIp[1]
440 && $curIp[1] <= $rangeEnd[1]
441 && $rangeBegin[2] <= $curIp[2]
442 && $curIp[2] <= $rangeEnd[2]
443 && $rangeBegin[3] <= $curIp[3]
444 && $curIp[3] <= $rangeEnd[3]
445 ) {
446 return true;
447 }
448 }
449 }
450
451 return false;
452 }
453
454 /**
455 * Return the role of the user.
456 *
457 * @param integer $userId The user id.
458 *
459 * @return string|null
460 */
461 private function _getUserRole($userId)
462 {
463 global $wpdb;
464
465 $curUserdata = get_userdata($userId);
466
467 if (!isset($curUserdata->user_level)) {
468 $curUserdata->user_level = null;
469 }
470
471 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
472 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
473 } else {
474 $capabilities = null;
475 }
476
477 $role = is_array($capabilities) ?
478 array_keys($capabilities) : array('norole');
479
480 return trim($role[0]);
481 }
482
483 /**
484 * Checks if the user is an admin user
485 *
486 * @param integer $userId The user id.
487 *
488 * @return boolean
489 */
490 public function userIsAdmin($userId)
491 {
492 $role = $this->_getUserRole($userId);
493
494 if ($role == 'administrator'
495 || is_super_admin($userId)
496 ) {
497 return true;
498 }
499
500 return false;
501 }
502
503 /**
504 * Checks the user access by user level.
505 *
506 * @return boolean
507 */
508 public function checkUserAccess()
509 {
510 global $current_user;
511 //Force user infos
512 wp_get_current_user();
513
514 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
515
516 $role = $this->_getUserRole($current_user->ID);
517 $orderedRoles = $this->getRolesOrdered();
518
519 if ($orderedRoles[$role] >= $orderedRoles[$uamOptions['full_access_role']]
520 || $role == 'administrator'
521 || is_super_admin($current_user->ID)
522 ) {
523 return true;
524 }
525
526 return false;
527 }
528
529 /**
530 * Returns the roles as assoziative array.
531 *
532 * @return array
533 */
534 public function getRolesOrdered()
535 {
536 $orderedRoles = array(
537 'norole' => 0,
538 'subscriber' => 1,
539 'contributor' => 2,
540 'author' => 3,
541 'editor' => 4,
542 'administrator' => 5
543 );
544
545 return $orderedRoles;
546 }
547
548 /**
549 * Registers object that should be handelt by the user access manager.
550 *
551 * @param array $object The object which you want to register.
552 *
553 * @return boolean
554 */
555 public function registerPlObject($object)
556 {
557 if (!isset($object['name'])
558 || !isset($object['reference'])
559 || !isset($object['getFull'])
560 || !isset($object['getFullObjects'])
561 ) {
562 return false;
563 }
564
565 $this->plObjects[$object['name']] = $object;
566
567 return true;
568 }
569
570 /**
571 * Returns a registerd pluggable object.
572 *
573 * @param string $objectName The name of the object which should be returned.
574 *
575 * @return array
576 */
577 public function getPlObject($objectName)
578 {
579 if (isset($this->plObjects[$objectName])) {
580 return $this->plObjects[$objectName];
581 }
582
583 return array();
584 }
585
586 /**
587 * Returns all registerd pluggable objects.
588 *
589 * @return array
590 */
591 public function getPlObjects()
592 {
593 return $this->plObjects;
594 }
595 }