Advertisement
Guest User

Untitled

a guest
May 27th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.40 KB | None | 0 0
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /**
  3.  * Sprig Auth driver.
  4.  *
  5.  * @package    Sprig Auth
  6.  * @author     Paul Banks
  7.  */
  8. class Auth_Sprig extends Auth {
  9.  
  10.     /**
  11.      * Checks if a session is active.
  12.      *
  13.      * @param   string   role name
  14.      * @param   array    collection of role names
  15.      * @return  boolean
  16.      */
  17.     public function logged_in($role = NULL)
  18.     {
  19.         $status = FALSE;
  20.  
  21.         // Get the user from the session
  22.         $user = $this->session->get($this->config['session_key']);
  23.        
  24.         if ( ! is_object($user))
  25.         {
  26.             // Attempt auto login
  27.             if ($this->auto_login())
  28.             {
  29.                 // Success, get the user back out of the session
  30.                 $user = $this->session->get($this->config['session_key']);
  31.             }
  32.         }
  33.  
  34.         if (is_object($user) AND $user instanceof Model_User AND $user->loaded())
  35.         {
  36.             // Everything is okay so far
  37.             $status = TRUE;
  38.  
  39.             if ( ! empty($role))
  40.             {
  41.  
  42.                 // If role is an array
  43.                 if (is_array($role))
  44.                 {
  45.                     // Check each role
  46.                     foreach ($role as $role_iteration)
  47.                     {
  48.                         // If the user doesn't have the role
  49.                         if( ! $user->has_role($role_iteration))
  50.                         {
  51.                             // Set the status false and get outta here
  52.                             $status = FALSE;
  53.                             break;
  54.                         }
  55.                     }
  56.                 }
  57.                 else
  58.                 {
  59.                     // Check that the user has the given role
  60.                     $status = $user->has_role($role);
  61.                 }
  62.             }
  63.         }
  64.  
  65.         return $status;
  66.     }
  67.  
  68.     /**
  69.      * Logs a user in.
  70.      *
  71.      * @param   string   username
  72.      * @param   string   password
  73.      * @param   boolean  enable auto-login
  74.      * @return  boolean
  75.      */
  76.     public function _login($user, $password, $remember)
  77.     {
  78.         // Make sure we have a user object
  79.         $user = $this->_get_object($user);
  80.        
  81.         // If the passwords match, perform a login
  82.         if ($user->has_role('login') AND $user->password === $password)
  83.         {
  84.             if ($remember === TRUE)
  85.             {
  86.                 // Create a new autologin token
  87.                 $token = Sprig::factory('user_token');
  88.  
  89.                 // Set token data
  90.                 $token->user = $user->id;
  91.                 $token->expires = time() + $this->config['lifetime'];
  92.                 $token->create();
  93.  
  94.                 // Set the autologin cookie
  95.                 cookie::set('authautologin', $token->token, $this->config['lifetime']);
  96.             }
  97.  
  98.             // Finish the login
  99.             $this->complete_login($user);
  100.  
  101.             return TRUE;
  102.         }
  103.  
  104.         // Login failed
  105.         return FALSE;
  106.     }
  107.  
  108.     /**
  109.      * Forces a user to be logged in, without specifying a password.
  110.      *
  111.      * @param   mixed    username
  112.      * @return  boolean
  113.      */
  114.     public function force_login($user)
  115.     {
  116.         // Make sure we have a user object
  117.         $user = $this->_get_object($user);
  118.  
  119.         // Mark the session as forced, to prevent users from changing account information
  120.         $_SESSION['auth_forced'] = TRUE;
  121.  
  122.         // Run the standard completion
  123.         $this->complete_login($user);
  124.     }
  125.  
  126.     /**
  127.      * Logs a user in, based on the authautologin cookie.
  128.      *
  129.      * @return  boolean
  130.      */
  131.     public function auto_login()
  132.     {
  133.         if ($token = cookie::get('authautologin'))
  134.         {
  135.             // Load the token and user
  136.             $token = Sprig::factory('user_token', array('token' => $token))->load();           
  137.            
  138.             if ($token->loaded() AND $token->user->load() AND $token->user->loaded())
  139.             {
  140.                 if ($token->user_agent === sha1(Request::$user_agent))
  141.                 {
  142.                     // Save the token to create a new unique token
  143.                     $token->update();
  144.  
  145.                     // Set the new token
  146.                     cookie::set('authautologin', $token->token, $token->expires - time());
  147.  
  148.                     // Complete the login with the found data
  149.                     $this->complete_login($token->user);
  150.  
  151.                     // Automatic login was successful
  152.                     return TRUE;
  153.                 }
  154.  
  155.                 // Token is invalid
  156.                 $token->delete();
  157.             }
  158.         }
  159.  
  160.         return FALSE;
  161.     }
  162.  
  163.     /**
  164.      * Log a user out and remove any auto-login cookies.
  165.      *
  166.      * @param   boolean  completely destroy the session
  167.      * @param   boolean  remove all tokens for user
  168.      * @return  boolean
  169.      */
  170.     public function logout($destroy = FALSE, $logout_all = FALSE)
  171.     {
  172.         if ($token = cookie::get('authautologin'))
  173.         {
  174.             // Delete the autologin cookie to prevent re-login
  175.             cookie::delete('authautologin');
  176.            
  177.             // Clear the autologin token from the database
  178.             $token = Sprig::factory('user_token', array('token' => $token))->load();
  179.            
  180.             if ($token->loaded() AND $logout_all)
  181.             {
  182.                 Sprig::factory('user_token', array('user_id' => $token->user->id))->delete();
  183.             }
  184.             elseif ($token->loaded())
  185.             {
  186.                 $token->delete();
  187.             }
  188.         }
  189.  
  190.         return parent::logout($destroy);
  191.     }
  192.  
  193.     /**
  194.      * Get the stored password for a username.
  195.      *
  196.      * @param   mixed   username
  197.      * @return  string
  198.      */
  199.     public function password($user)
  200.     {
  201.         // Make sure we have a user object
  202.         $user = $this->_get_object($user);
  203.  
  204.         return $user->password;
  205.     }
  206.  
  207.     /**
  208.      * Complete the login for a user by incrementing the logins and setting
  209.      * session data: user_id, username, roles
  210.      *
  211.      * @param   object   user model object
  212.      * @return  void
  213.      */
  214.     protected function complete_login($user)
  215.     {
  216.         // Update the number of logins
  217.         $user->logins += 1;
  218.  
  219.         // Set the last login date
  220.         $user->last_login = time();
  221.  
  222.         // Save the user
  223.         $user->update();
  224.  
  225.         return parent::complete_login($user);
  226.     }
  227.    
  228.     /**
  229.      * Convert a unique identifier string to a user object
  230.      *
  231.      * @param mixed $user
  232.      * @return Model_User
  233.      */
  234.     protected function _get_object($user)
  235.     {
  236.         if ( ! is_object($user))
  237.         {
  238.             // Load the user using special factory method
  239.             $user = Model_User::factory($user)->load();        
  240.         }
  241.        
  242.         return $user;
  243.     }
  244.  
  245. } // End Auth_Sprig_Driver
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement