11.15 php saved
gwoo
Tags add more
auth  
Note
latest version of rdAuth
  1. <?php
  2. /*
  3. * rdAuth Component
  4. *
  5. * @author    gwoo <gwoo@rd11.com>
  6. * @version  1.1.10.3825
  7. * @license  CSFL
  8. */
  9. class rdAuthComponent extends Object {
  10.  
  11.  
  12.     var $components = array('Session');
  13.     /**
  14.     * the name for the cooke.
  15.     *
  16.     * @var string
  17.     * @access public
  18.     */
  19.     var $cookie = '__rd';
  20.  
  21.     /**
  22.     * model that stores the role field
  23.     *
  24.     * @var unknown_type
  25.     * @access private
  26.     */
  27.     var $model = 'User';
  28.     /**
  29.     * the redirect if auth fails.
  30.     *
  31.     * @var string
  32.     * @access public
  33.     */
  34.     var $loginPage = '/users/login';
  35.    
  36.     /**
  37.     * current url.
  38.     *
  39.     * @var string
  40.     * @access private
  41.     */
  42.     var $currentUrl = '/';
  43.    
  44.     /**
  45.     * referring url.
  46.     *
  47.     * @var string
  48.     * @access private
  49.     */
  50.     var $referer = '/';
  51.  
  52.     /**
  53.     * special admin roles
  54.     *
  55.     * @var array
  56.     * @access public
  57.     */
  58.     var $admins = array('Admin', 'Manager');
  59.  
  60.     /**
  61.     * all roles available
  62.     *
  63.     * @var array
  64.     * @access public
  65.     */
  66.     var $roles = array('Admin', 'Manager', 'User');
  67.  
  68.     /**
  69.     * id of the logged in user
  70.     *
  71.     * @var unknown_type
  72.     * @access private
  73.     */
  74.  
  75.     var $id = null;
  76.  
  77.     /**
  78.     * username of the logged in user
  79.     *
  80.     * @var string
  81.     * @access private
  82.     */
  83.     var $username = null;
  84.  
  85.     /**
  86.     * role assigned to the logged in user
  87.     *
  88.     * @var string
  89.     * @access private
  90.     */
  91.     var $role = null;
  92.  
  93.     /**
  94.     * if role passes users set allowed
  95.     *
  96.     * @var boolean
  97.     * @access private
  98.     */
  99.     var $allow = null;
  100.  
  101.     /**
  102.     * Valid if role, username , and id exist.
  103.     *
  104.     * @var string
  105.     * @access public
  106.     */
  107.     var $valid = false;
  108.  
  109.     /**
  110.     * Error messages to be displayed if the user is short of access for the requested action.
  111.     *
  112.     * @var string
  113.     * @access private
  114.     */
  115.     var $errors = null;
  116.  
  117.     /**
  118.     * hash used in basic encryption of session values.
  119.     *
  120.     * @var string
  121.     * @access private
  122.     */
  123.     var $__hash = 'DYhG93b0qyJfIxfs2guVoUubWwvniR2G0FgaC9mi';
  124.        
  125.     /**
  126.     * if admin route then __cakeAdmin equals params['admin']
  127.     *
  128.     * @var string
  129.     * @access private
  130.     */
  131.     var $__cakeAdminnull;
  132.    
  133.     /**
  134.     * Uses callback to automatically check rights
  135.     * @return redirect if check fails
  136.     */
  137.     function startup(&$controller)
  138.     {
  139.         if(isset($controller->params['url']['url'])) {
  140.             $this->currentUrl = '/'.$controller->params['url']['url'];
  141.         }
  142.         if(isset($controller->params['admin'])) {
  143.             $this->__cakeAdmin = $controller->params['admin'];
  144.         }
  145.         $vars = get_class_vars($controller->name.'Controller');
  146.         if(empty($vars['rdAccess'])) {
  147.             $vars = get_class_vars('AppController');
  148.         }
  149.         //setup current session
  150.         $this->set();
  151.                
  152.         if(!empty($vars['rdAccess']))
  153.         {
  154.             if(!strstr($this->currentUrl, $this->loginPage)) {
  155.                 $this->referer = $this->currentUrl;
  156.                 $this->Session->write("{$this->cookie}.referer", $this->referer);
  157.             }
  158.             if($this->check($controller->action, $vars['rdAccess']) === true)
  159.             {            
  160.                 if(!is_null($this->role)) {
  161.                     $roles = array_flip(array_values($this->roles));
  162.                     foreach($this->roles as $role) {
  163.                         if($roles[$this->role] <= $roles[$role]) {
  164.                             $this->{low($role)} = 1;
  165.                         }
  166.                     }
  167.                     $this->valid = 1;
  168.                     $this->allow = 1;
  169.                 }
  170.             }
  171.             else
  172.             {
  173.               $controller->redirect($this->loginPage);
  174.               exit;
  175.             }
  176.         }
  177.     }
  178.  
  179.     function encrypt(&$data = array())
  180.     {
  181.         if(is_array($data)) {
  182.             foreach($data as $key=>$value) {
  183.                 $data[$key] = base64_encode(str_rot13($value.'_'.$this->__hash));
  184.             }
  185.         } else {
  186.             $data = base64_encode(str_rot13($value.'_'.$this->__hash));
  187.         }
  188.     }
  189.  
  190.     function decrypt(&$data = array())
  191.     {
  192.         if(is_array($data)) {
  193.             foreach($data as $key=>$value) {
  194.                 $string = explode('_',str_rot13(base64_decode($value)));
  195.                 if(isset($string[0]))
  196.                 $data[$key] = $string[0];
  197.             }
  198.         } else {
  199.             $string = explode('_',str_rot13(base64_decode($value)));
  200.             if(isset($string[0]))
  201.             $data = $string[0];
  202.         }
  203.     }
  204.  
  205.     /**
  206.     * Function to check the session and return local vars
  207.     *
  208.     * @param string $data used for login method
  209.     * @return errors
  210.     */
  211.     function set($data='')
  212.     {
  213.         foreach($this->roles as $role) {
  214.             $this->{low($role)} = null;
  215.         }
  216.  
  217.         $this->referer = $this->Session->read("{$this->cookie}.referer");
  218.         if(isset($data) && isset($data['id']) && isset($data['username']) && isset($data['role']))
  219.         {
  220.             $this->encrypt($data);
  221.             $this->Session->write("{$this->cookie}", $data);
  222.             $this->Session->del("{$this->cookie}.referer");
  223.         }
  224.         if($this->Session->check("{$this->cookie}.id") && $this->Session->valid("{$this->cookie}.id") &&
  225.             $this->Session->check("{$this->cookie}.username") && $this->Session->valid("{$this->cookie}.username") &&
  226.             $this->Session->check("{$this->cookie}.role") && $this->Session->valid("{$this->cookie}.role"))
  227.         {
  228.             $data = $this->Session->read("{$this->cookie}");
  229.             $this->decrypt($data);
  230.            
  231.             $this->id = $data['id'];
  232.             $this->username = $data['username'];
  233.             $this->role = $data['role'];
  234.         }
  235.         elseif($this->Session->error())
  236.         {
  237.             return $this->Session->error();
  238.         }
  239.         return true;
  240.     }
  241.  
  242.     /**
  243.     * logout method deletes session
  244.     *
  245.     * @return errors
  246.     */
  247.     function logout()
  248.     {
  249.         $this->Session->del("{$this->cookie}.id");
  250.         $this->Session->del("{$this->cookie}.username");
  251.         $this->Session->del("{$this->cookie}.role");
  252.         $this->Session->del("{$this->cookie}");
  253.         if($this->Session->error())
  254.         {
  255.             return $this->Session->error();
  256.         }
  257.     }
  258.    
  259.     /**
  260.     * Function to return the array of set values
  261.     *
  262.     * @param string $type The array to return
  263.     * @return boolean
  264.     */
  265.     function values() {
  266.         $valuesarray('id'=> $this->id, 'username'=> $this->username, 'role'=> $this->role,
  267.                      'valid'=> $this->valid, 'allow'=> $this->allow, 'roles'=> $this->roles);
  268.         foreach($this->roles as $role) {
  269.             if(isset($this->{low($role)})) {
  270.                 $values[low($role)] = 1;
  271.             } else {
  272.                 $values[low($role)] = null;
  273.             }
  274.         }
  275.         return $values;
  276.     }
  277.     /**
  278.     * Function to return the roles used by the class
  279.     * types : (admin|null) admin returns only admins, null returns all roles
  280.     * @param string $type The array to return
  281.     * @return $roles
  282.     */
  283.     function roles($type = null) {
  284.         if(low($type) == 'admin') {
  285.             return array_combine(array_values($this->admins), $this->admins);
  286.         } else {
  287.             return array_combine(array_values($this->roles), $this->roles);
  288.         }
  289.     }
  290.     /**
  291.     * Function to check the access for the action based on the access list
  292.     *
  293.     * @param string $action The action for which we need to check the access
  294.     * @param array $access Access array for the controller's actions
  295.     * @return boolean
  296.     */
  297.     function check($action, $access = null)
  298.     {   
  299.         if(empty($access) || $access === null) {
  300.             return true;
  301.         } else if (is_array($access)) {
  302.             $rights = array();
  303.             if(array_key_exists($action, $access)) {
  304.                 $rights = array_values($access[$action]);
  305.             } else if(array_key_exists(CAKE_ADMIN, $access) && $this->__cakeAdmin){
  306.                 $rights = array_values($access[CAKE_ADMIN]);
  307.             } else {
  308.                 return true;
  309.             }
  310.             if (in_array($this->role, $rights)
  311.                 || in_array($this->id, $rights)
  312.                 || in_array($this->username, $rights)) {
  313.                 return true;
  314.             }
  315.             return false;
  316.         } else if($access === '*' || $access === CAKE_ADMIN) {
  317.             if($this->valid && $this->allow) {
  318.                 return true;
  319.             }
  320.             return false;
  321.         }
  322.         return true;
  323.     }
  324.  
  325.     /**
  326.     * Function to check if a access is authorized on a row in the database
  327.     * your table would have a field that contains the role just like the user table
  328.     *
  329.     * @param string $item The field for which we need to check the access
  330.     * @return boolean
  331.     */
  332.     function perm($item = null)
  333.     {
  334.         if (isset($item) && $this->role)
  335.         {
  336.             if(in_array($this->role, $this->roles))
  337.             {
  338.                 $flipped = array_flip(array_values($this->roles));
  339.                 if(!is_array($item) && isset($flipped[$item]) && $flipped[$item] >= $flipped[$this->role])
  340.                 {
  341.                     return true;
  342.                 }
  343.                 if(is_array($item) && in_array($item, $this->role))
  344.                 {
  345.                     return true;
  346.                 }
  347.             }
  348.         }
  349.         return false;
  350.     }
  351.  
  352.     /**
  353.     * Function to generate the conditons to be used in a query assumes a role field
  354.     * in a model specified by var $model
  355.     *
  356.     * @param array $roles The roles to include in the query
  357.     * @return string $condtions
  358.     */
  359.     function conditions($roles = array())
  360.     {
  361.         if(!is_array($roles)){
  362.             $roles = func_get_args();
  363.         }
  364.         if(!is_array($roles)){
  365.             $roles = array($this->role);
  366.         }
  367.         $combined = array_combine($roles, $roles);
  368.         $roles = am($this->admins, $combined);
  369.  
  370.         $conditions = null;
  371.         $i = 0;
  372.         foreach($roles as $role)
  373.         {
  374.             if($this->perm($role)) {
  375.                 if($i == 0)
  376.                 $conditions .= $this->model.".role = '{$role}'";
  377.                 else
  378.                 $conditions .= ' OR '.$this->model.".role = '{$role}'";
  379.                 $i++;
  380.             }
  381.         }
  382.         return $conditions;
  383.     }
  384.    
  385.     /**
  386.     * Function to store the hits on a particular link in a session relatd to the user
  387.     *
  388.     * @param array $id The unique identifier
  389.     * @example mark a hit on /posts/view/1, use posts.1 as the id
  390.     * @return boolean
  391.     */
  392.     function hit($id, $key = 'hits')
  393.     {
  394.         $hits = array();
  395.         if($this->Session->check("{$this->cookie}.{$key}"))
  396.         {
  397.             $hits = $this->Session->read("{$this->cookie}.{$key}");
  398.         }
  399.         if(!in_array($id, $hits))
  400.         {
  401.             array_push($hits, $id);
  402.             $this->Session->write("{$this->cookie}.{$key}", $hits);
  403.             return true;
  404.         }
  405.         return false;
  406.     }
  407.  
  408.     function clearHits($key = 'hits')
  409.     {
  410.         $this->Session->del("{$this->cookie}.{$key}");
  411.     }
  412. }
  413. ?>
Parsed in 0.452 seconds, using GeSHi 1.0.7.14

Modify this Paste