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

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

603 lines 16.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-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 (!in_array($objectType, $this->getAllObjectTypes())) {
282 return;
283 }
284
285 if ($objectType == 'user') {
286 $filter = false;
287 }
288
289 if ($filter) {
290 $filterAttr = 'filtered';
291 } else {
292 $filterAttr = 'noneFiltered';
293 }
294
295 if (isset($this->objectUserGroups[$objectType][$filterAttr][$objectId])) {
296 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
297 }
298
299 $objectUserGroups = array();
300
301 $userGroups = $this->getUserGroups(null, $filter);
302
303 $plObject = false;
304
305 if ($objectType != 'user'
306 && $objectType != 'post'
307 && $objectType != 'category'
308 ) {
309 $plObject = true;
310 }
311
312 if (isset($userGroups)) {
313 foreach ($userGroups as $userGroup) {
314 $objectMembership = $userGroup->objectIsMember(
315 $objectType,
316 $objectId,
317 true
318 );
319
320 if ($objectMembership !== false) {
321 if (is_array($objectMembership)) {
322 $userGroup->setRecursive[$objectType][$objectId]
323 = $objectMembership;
324 }
325
326 $objectUserGroups[$userGroup->getId()]
327 = $userGroup;
328 }
329 }
330 }
331
332 //Filter the user groups
333 if ($filter) {
334 $objectUserGroups = $this->_filterUserGroups($objectUserGroups);
335 }
336
337 $this->objectUserGroups[$objectType][$filterAttr][$objectId]
338 = $objectUserGroups;
339
340 return $this->objectUserGroups[$objectType][$filterAttr][$objectId];
341 }
342
343 /**
344 * Unsets the usergroups for objects.
345 *
346 * @return null
347 */
348 public function unsetUserGroupsForObject()
349 {
350 $this->objectUserGroups = array();
351 }
352
353 /**
354 * Checks if the current_user has access to the given post.
355 *
356 * @param string $objectType The object type which should be checked.
357 * @param integer $objectId The id of the object.
358 *
359 * @return boolean
360 */
361 public function checkObjectAccess($objectType, $objectId)
362 {
363 if (!in_array($objectType, $this->getAllObjectTypes())) {
364 return;
365 }
366
367 if (isset($this->objectAccess[$objectType][$objectId])) {
368 return $this->objectAccess[$objectType][$objectId];
369 }
370
371 global $current_user;
372 //Force user infos
373 wp_get_current_user();
374
375 if ($objectType == 'post') {
376 $post = get_post($objectId);
377 $authorId = $post->post_author;
378 } else {
379 $authorId = -1;
380 }
381
382 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
383 $membership = $this->getUserGroupsForObject($objectType, $objectId, false);
384
385 if ($membership == array()
386 || $this->checkUserAccess()
387 || $current_user->ID == $authorId
388 && $uamOptions['authors_has_access_to_own'] == 'true'
389 ) {
390 return $this->objectAccess[$objectType][$objectId] = true;
391 }
392
393 $curIp = explode(".", $_SERVER['REMOTE_ADDR']);
394
395 foreach ($membership as $key => $userGroup) {
396 if ($this->checkUserIp($curIp, $userGroup->getIpRange())
397 || $userGroup->objectIsMember('user', $current_user->ID)
398 ) {
399 return $this->objectAccess[$objectType][$objectId] = true;
400 break;
401 }
402
403 if ($this->getUserAccessManager()->atAdminPanel
404 && $userGroup->getWriteAccess() == 'all'
405 || !$this->getUserAccessManager()->atAdminPanel
406 && $userGroup->getReadAccess() == 'all'
407 ) {
408 unset($membership[$key]);
409 }
410 }
411
412 if ($membership == array()) {
413 return $this->objectAccess[$objectType][$objectId] = true;
414 }
415
416 return $this->objectAccess[$objectType][$objectId] = false;
417 }
418
419
420 /*
421 * Other functions
422 */
423
424 /**
425 * Checks if the given ip matches with the range.
426 *
427 * @param string $curIp The ip of the current user.
428 * @param array $ipRanges The ip ranges.
429 *
430 * @return boolean
431 */
432 public function checkUserIp($curIp, $ipRanges)
433 {
434 if (isset($ipRanges)) {
435 foreach ($ipRanges as $ipRange) {
436 $ipRange = explode("-", $ipRange);
437 $rangeBegin = explode(".", $ipRange[0]);
438
439 if (isset($ipRange[1])) {
440 $rangeEnd = explode(".", $ipRange[1]);
441 } else {
442 $rangeEnd = explode(".", $ipRange[0]);
443 }
444
445 if ($rangeBegin[0] <= $curIp[0]
446 && $curIp[0] <= $rangeEnd[0]
447 && $rangeBegin[1] <= $curIp[1]
448 && $curIp[1] <= $rangeEnd[1]
449 && $rangeBegin[2] <= $curIp[2]
450 && $curIp[2] <= $rangeEnd[2]
451 && $rangeBegin[3] <= $curIp[3]
452 && $curIp[3] <= $rangeEnd[3]
453 ) {
454 return true;
455 }
456 }
457 }
458
459 return false;
460 }
461
462 /**
463 * Return the role of the user.
464 *
465 * @param integer $userId The user id.
466 *
467 * @return string|null
468 */
469 private function _getUserRole($userId)
470 {
471 global $wpdb;
472
473 $curUserdata = get_userdata($userId);
474
475 if (!isset($curUserdata->user_level)) {
476 $curUserdata->user_level = null;
477 }
478
479 if (isset($curUserdata->{$wpdb->prefix . "capabilities"})) {
480 $capabilities = $curUserdata->{$wpdb->prefix . "capabilities"};
481 } else {
482 $capabilities = null;
483 }
484
485 $role = is_array($capabilities) ?
486 array_keys($capabilities) : array('norole');
487
488 return trim($role[0]);
489 }
490
491 /**
492 * Checks if the user is an admin user
493 *
494 * @param integer $userId The user id.
495 *
496 * @return boolean
497 */
498 public function userIsAdmin($userId)
499 {
500 $role = $this->_getUserRole($userId);
501
502 if ($role == 'administrator'
503 || is_super_admin($userId)
504 ) {
505 return true;
506 }
507
508 return false;
509 }
510
511 /**
512 * Checks the user access by user level.
513 *
514 * @return boolean
515 */
516 public function checkUserAccess()
517 {
518 global $current_user;
519 //Force user infos
520 wp_get_current_user();
521
522 $uamOptions = $this->getUserAccessManager()->getAdminOptions();
523
524 $role = $this->_getUserRole($current_user->ID);
525 $orderedRoles = $this->getRolesOrdered();
526
527 if ($orderedRoles[$role] >= $orderedRoles[$uamOptions['full_access_role']]
528 || $role == 'administrator'
529 || is_super_admin($current_user->ID)
530 ) {
531 return true;
532 }
533
534 return false;
535 }
536
537 /**
538 * Returns the roles as assoziative array.
539 *
540 * @return array
541 */
542 public function getRolesOrdered()
543 {
544 $orderedRoles = array(
545 'norole' => 0,
546 'subscriber' => 1,
547 'contributor' => 2,
548 'author' => 3,
549 'editor' => 4,
550 'administrator' => 5
551 );
552
553 return $orderedRoles;
554 }
555
556 /**
557 * Registers object that should be handelt by the user access manager.
558 *
559 * @param array $object The object which you want to register.
560 *
561 * @return boolean
562 */
563 public function registerPlObject($object)
564 {
565 if (!isset($object['name'])
566 || !isset($object['reference'])
567 || !isset($object['getFull'])
568 || !isset($object['getFullObjects'])
569 ) {
570 return false;
571 }
572
573 $this->plObjects[$object['name']] = $object;
574
575 return true;
576 }
577
578 /**
579 * Returns a registerd pluggable object.
580 *
581 * @param string $objectName The name of the object which should be returned.
582 *
583 * @return array
584 */
585 public function getPlObject($objectName)
586 {
587 if (isset($this->plObjects[$objectName])) {
588 return $this->plObjects[$objectName];
589 }
590
591 return array();
592 }
593
594 /**
595 * Returns all registerd pluggable objects.
596 *
597 * @return array
598 */
599 public function getPlObjects()
600 {
601 return $this->plObjects;
602 }
603 }