Advertisement
Guest User

has_access

a guest
Aug 24th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.28 KB | None | 0 0
  1. <?
  2. public static function has_access($class = null, $method = null, $role = 'make_magic')
  3. {
  4.     //static::check() + check if user is a staff member
  5.     if ( ! static::is_staff())
  6.     {
  7.         return false;
  8.     }
  9.     //static::$account created at login <ORM object>
  10.     if (in_array(static::$account->name, Config::get('cms.superadmins', array()), true))
  11.     {
  12.         return true;
  13.     }
  14.     $namespace = Inflector::get_namespace($class);
  15.     $class     = Inflector::denamespace($class);
  16.     empty($namespace) and $namespace = 'Main\\';
  17.     $resource = $namespace.'::'.$class.'@'.$method;
  18.  
  19.     //Check cache
  20.     if (isset(static::$roles_cache[$resource][$role]))
  21.     {
  22.         return static::$roles_cache[$resource][$role];
  23.     }
  24.  
  25.     //Does the role $role is defined in given namespace?
  26.     $role = Model_Site_Role::find()->where('namespace', $namespace)->and_where('name', $role)->get_one();
  27.     if (empty($role))
  28.     {
  29.         return false;
  30.     }
  31.  
  32.     //Find user's access information for given resource
  33.     $access = Model_Site_Access::find()->where('account_id', static::$account->id)->and_where('resource', $resource)->get_one();
  34.     //Chceck if user has required role
  35.     if (in_array($role->id, $access->roles, true))
  36.     {
  37.         static::$roles_cache[$resource][$role] = true;
  38.  
  39.         return true;
  40.     }
  41.     static::$roles_cache[$resource][$role] = false;
  42.  
  43.     return false;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement