Advertisement
pbowers

UserSpice possible function add to to US_helper.php

Sep 4th, 2016
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. // hasPermission()
  2. // Parameters:
  3. //   $permissions: can be either a scalar or an array of values from permissions.id (1=User, 2=Admin usually in US)
  4. //   $user_id: allows to ask for a given user; if not provided the current user will be assumed
  5. //   $allow_master_user: user_id=1 (Admin) is a "special" user in US - under normal circumstances this user is part
  6. //     of ALL permission groups
  7. // Return value:
  8. //   return TRUE if the specified user is in (one of) the permission groups
  9. //   return FALSE otherwise
  10. function hasPermission($permissions, $user_id=null, $allow_master_user=true) {
  11.     global $user;
  12.     $db = DB::getInstance();
  13.  
  14.   // decide which user_id we're working on
  15.     if (!$user_id)
  16.         $user_id = $user->data()->id; // self
  17.  
  18.     //Grant access if master user
  19.     if ($allow_master_user && $user_id == 1)
  20.         return true;
  21.  
  22.     foreach((array)$permissions as $perm){
  23.         #echo "DEBUG: Looking for user_id=$user_id and perm=$perm<br />\n";
  24.         $query = $db->query("SELECT id FROM user_permission_matches  WHERE user_id = ? AND permission_id = ?",array($user_id,$perm));
  25.         if ($query->count() > 0) {
  26.             #echo "DEBUG: count=".$query->count()."<br />\n";
  27.             return true;
  28.         }
  29.     }
  30.     return false;
  31. }
  32.  
  33. function checkPermissions($permissions) {
  34.     return hasPermissions($permissions[0]);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement