Advertisement
brandinarsenault

OUBIS - membership.class.php

Feb 6th, 2013
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.48 KB | None | 0 0
  1. <?php
  2.  
  3. class Membership
  4. {
  5.    
  6.     private $DBH; //Database Handler
  7.     private $userData; //Users information
  8.    
  9.    
  10.     public function __construct(PDO $DBH)
  11.     {
  12.         $this->DBH = &$DBH;
  13.     }
  14.    
  15.     //check to see if user data is valid
  16.     public function validate_user($username, $password)
  17.     {
  18.        
  19.         $sql = "SELECT id FROM users WHERE username=? AND passwordHash=?";
  20.         $query = $this->DBH->prepare($sql);
  21.         $query->execute(array($username, $password));
  22.        
  23.         if($query->rowCount() <= 0)
  24.         {
  25.             return false;
  26.         }
  27.  
  28.         return true;
  29.     }
  30.    
  31.     public function login($username, $passwordHash)
  32.     {
  33.         if(!$this->validate_user($username, $passwordHash))
  34.         {
  35.             $this->logout();
  36.             return false;
  37.         }
  38.         //get the users data from MySQL
  39.         $sql = "SELECT * FROM users WHERE username=? AND passwordHash=? LIMIT 1";
  40.         $query = $this->DBH->prepare($sql);
  41.         $data = $query->execute(array($username, $passwordHash));
  42.         $user = $query->fetch(PDO::FETCH_ASSOC);
  43.                
  44.         //set up the sessions
  45.         $_SESSION['user'] = $user;
  46.         $_SESSION['user']['loggedIn'] = true;
  47.        
  48.         //set a reference to the session for local use
  49.         $this->userData = &$_SESSION['user'];  
  50.        
  51.        
  52.         //set up perms
  53.         if($this->userData['perms'] != 0)
  54.         {
  55.             //is not superuser
  56.             //explode perms
  57.             $perms = explode(".", $this->userData['perms']);
  58.             unset($this->userData['perms']);
  59.             foreach($perms as $perm)
  60.             {
  61.                 $this->userData['perms'][] = $perm;
  62.             }
  63.         }
  64.        
  65.  
  66.  
  67.         return true;
  68.        
  69.        
  70.     }
  71.    
  72.     public function logout()
  73.     {
  74.         unset ($_SESSION['user']);
  75.         $this->no_user();
  76.         return true;
  77.     }
  78.    
  79.     public function get_field($fieldName)
  80.     {
  81.         if(isset($this->userData[$fieldName]))
  82.         {
  83.             return $this->userData[$fieldName];
  84.         }
  85.        
  86.         //error if it does not exist       
  87.  
  88.         die("Membership Error!");
  89.        
  90.     }
  91.    
  92.     public function no_user()
  93.     {      
  94.  
  95.         if(!isset($_SESSION['user']))
  96.         {
  97.        
  98.             if(isset($_SESSION['user']))
  99.             {
  100.                 unset($_SESSION['user']);
  101.             }
  102.             //set the perms for anonomous users
  103.             $_SESSION['user']['loggedIn'] = false;
  104.             $_SESSION['user']['perms'] = array("1", "0", "0");
  105.            
  106.             $this->userData = &$_SESSION['user'];
  107.            
  108.         }else{
  109.            
  110.             $this->userData = &$_SESSION['user'];
  111.            
  112.         }
  113.     }
  114.    
  115.     public function test_perms($field)
  116.     {
  117.         if(!is_array($this->userData['perms']))
  118.         {
  119.             //is the superuser
  120.             return true;
  121.         }
  122.        
  123.         if(!isset($this->userData['perms'][$field]))
  124.         {
  125.             return false;
  126.         }
  127.        
  128.         if($this->userData['perms'][$field] == 1)
  129.         {
  130.             return true;
  131.         }
  132.         return false;
  133.  
  134.     }
  135.    
  136. }
  137.  
  138. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement