Changeset 3081 for trunk/modules/auth
- Timestamp:
- 07/11/2008 01:37:30 PM (5 months ago)
- Location:
- trunk/modules/auth
- Files:
-
- 4 modified
-
libraries/drivers/Auth/ORM.php (modified) (4 diffs)
-
models/role.php (modified) (2 diffs)
-
models/user.php (modified) (5 diffs)
-
models/user_token.php (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/auth/libraries/drivers/Auth/ORM.php
r2747 r3081 37 37 // Checks if a user is logged in and valid 38 38 if ( ! empty($_SESSION['auth_user']) AND is_object($_SESSION['auth_user']) 39 AND ($_SESSION['auth_user'] instanceof User_Model) AND $_SESSION['auth_user']-> id> 0)39 AND ($_SESSION['auth_user'] instanceof User_Model) AND $_SESSION['auth_user']->primary_key_value > 0) 40 40 { 41 41 // Everything is okay so far … … 45 45 { 46 46 // Check that the user has the given role 47 $status = $_SESSION['auth_user']->has _role($role);47 $status = $_SESSION['auth_user']->has('role', $role); 48 48 } 49 49 } … … 60 60 61 61 // If the passwords match, perform a login 62 if ($user->has _role('login') AND $user->password === $password)62 if ($user->has('role', 'login') AND $user->password === $password) 63 63 { 64 64 if ($remember === TRUE) … … 186 186 187 187 // Regenerate session_id 188 $this->session->regenerate();188 // $this->session->regenerate(); 189 189 190 190 // Store session data -
trunk/modules/auth/models/role.php
r1614 r3081 8 8 * Allows finding roles by name. 9 9 */ 10 public function where_key($id = NULL)10 public function unique_key($id) 11 11 { 12 12 if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) … … 18 18 } 19 19 20 /**21 * Removes all user<>role relationships for this object when deleted.22 */23 public function delete()24 {25 // Set WHERE before deleting, to access the object id26 $where = array($this->class.'_id' => $this->object->id);27 28 // Related table name29 $table = $this->related_table('users');30 31 if ($return = parent::delete())32 {33 // Delete the many<>many relationships for users<>roles34 self::$db35 ->where($where)36 ->delete($table);37 }38 39 return $return;40 }41 42 20 } // End Role_Model -
trunk/modules/auth/models/user.php
r3000 r3081 4 4 5 5 // Relationships 6 protected $has_many = array(' tokens');6 protected $has_many = array('user_tokens'); 7 7 protected $has_and_belongs_to_many = array('roles'); 8 9 // User roles10 protected $roles = NULL;11 12 public function __get($key)13 {14 // Allow roles to be fetched as a simple array15 if ($key === 'roles')16 {17 // Force the roles to load if they are empty18 ($this->roles === NULL) and $this->has_role('login');19 20 return $this->roles;21 }22 23 return parent::__get($key);24 }25 8 26 9 public function __set($key, $value) … … 35 18 } 36 19 37 /** 38 * Overloading the has_role method, for optimization. 39 */ 40 public function has_role($role) 20 public function has($object, $id = NULL) 41 21 { 42 // Don't mess with these calls, they are too complex 43 if (is_object($role)) 44 return parent::has_role($role); 22 if ($object === 'role') 23 { 24 // Load a role model 25 $role = ORM::factory('role'); 45 26 46 if ($this->roles === NULL) 47 { 48 // Make the roles into an array. This serves a dual purpose 49 // of preventing the roles from being re-queried unnecessarily 50 // as well as optimizing has_role() calls. 51 $this->roles = array(); 27 // Load JOIN info 28 $join_table = $role->join_table($this->table_name); 29 $join_col1 = $role->foreign_key(NULL, $join_table); 30 $join_col2 = $role->foreign_key(TRUE); 52 31 53 if ($this->id > 0) 54 { 55 foreach ($this->find_related_roles() as $r) 56 { 57 // Load all the user roles 58 $this->roles[$r->id] = $r->name; 59 } 60 } 32 return (bool) $this->db 33 ->join($role->table_name, $join_col1, $join_col2) 34 ->where($role->unique_key($id), $id) 35 ->where($this->foreign_key(NULL, $join_table), $this->object[$this->primary_key]) 36 ->count_records($join_table); 61 37 } 62 38 63 // Make sure the role name is a string 64 $role = (string) $role; 65 66 if (ctype_digit($role)) 67 { 68 // Find by id 69 return isset($this->roles[$role]); 70 } 71 else 72 { 73 // Find by name 74 return in_array($role, $this->roles); 75 } 39 return parent::has($object, $id); 76 40 } 77 41 … … 79 43 * Tests if a username exists in the database. 80 44 * 81 * @param string usernameto check82 * @return bool 45 * @param mixed id to check 46 * @return boolean 83 47 */ 84 public function username_exists($ name)48 public function username_exists($id) 85 49 { 86 return (bool) self::$db->where($this->where_key($name), $name)->count_records('users'); 50 return (bool) $this->db 51 ->where($this->unique_key($id), $id) 52 ->count_records($this->table_name); 87 53 } 88 54 … … 90 56 * Allows a model to be loaded by username or email address. 91 57 */ 92 p rotected function where_key($id = NULL)58 public function unique_key($id) 93 59 { 94 60 if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) … … 97 63 } 98 64 99 return parent:: where_key($id);65 return parent::unique_key($id); 100 66 } 101 67 -
trunk/modules/auth/models/user_token.php
r1667 r3081 12 12 * Handles garbage collection and deleting of expired objects. 13 13 */ 14 public function __construct($id = FALSE)14 public function __construct($id = NULL) 15 15 { 16 16 parent::__construct($id); … … 25 25 } 26 26 27 if ($this-> object->id != 0 AND $this->object->expires < $this->now)27 if ($this->expires < $this->now) 28 28 { 29 29 // This object has expired … … 38 38 public function save() 39 39 { 40 if ($this-> object->id == 0)40 if ($this->loaded === FALSE) 41 41 { 42 42 // Set the created time, token, and hash of the user agent … … 59 59 { 60 60 // Delete all expired tokens 61 self::$db->where('expires <', $this->now)->delete($this->table); 62 } 61 $this->db->where('expires <', $this->now)->delete($this->table_name); 63 62 64 /** 65 * Allows loading by token string. 66 */ 67 protected function where_key($id) 68 { 69 if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) 70 { 71 return 'token'; 72 } 73 74 return parent::where_key($id); 63 return $this; 75 64 } 76 65 … … 90 79 91 80 // Make sure the token does not already exist 92 if ( count(self::$db->select('id')->where('token', $token)->get($this->table)) === 0)81 if ($this->db->select('id')->where('token', $token)->get($this->table)->count() === 0) 93 82 { 94 83 // A unique token has been found … … 98 87 } 99 88 89 /** 90 * Allows loading by token string. 91 */ 92 public function unique_key($id) 93 { 94 if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) 95 { 96 return 'token'; 97 } 98 99 return parent::unique_key($id); 100 } 101 100 102 } // End User Token
