Changeset 3081

Show
Ignore:
Timestamp:
07/11/2008 01:37:30 PM (5 months ago)
Author:
Shadowhand
Message:

Updated Auth/ORM models to work with new ORM syntax.

Location:
trunk/modules/auth
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/auth/libraries/drivers/Auth/ORM.php

    r2747 r3081  
    3737                // Checks if a user is logged in and valid 
    3838                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) 
    4040                { 
    4141                        // Everything is okay so far 
     
    4545                        { 
    4646                                // Check that the user has the given role 
    47                                 $status = $_SESSION['auth_user']->has_role($role); 
     47                                $status = $_SESSION['auth_user']->has('role', $role); 
    4848                        } 
    4949                } 
     
    6060 
    6161                // 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) 
    6363                { 
    6464                        if ($remember === TRUE) 
     
    186186 
    187187                // Regenerate session_id 
    188                 $this->session->regenerate(); 
     188                // $this->session->regenerate(); 
    189189 
    190190                // Store session data 
  • trunk/modules/auth/models/role.php

    r1614 r3081  
    88         * Allows finding roles by name. 
    99         */ 
    10         public function where_key($id = NULL) 
     10        public function unique_key($id) 
    1111        { 
    1212                if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) 
     
    1818        } 
    1919 
    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 id 
    26                 $where = array($this->class.'_id' => $this->object->id); 
    27  
    28                 // Related table name 
    29                 $table = $this->related_table('users'); 
    30  
    31                 if ($return = parent::delete()) 
    32                 { 
    33                         // Delete the many<>many relationships for users<>roles 
    34                         self::$db 
    35                                 ->where($where) 
    36                                 ->delete($table); 
    37                 } 
    38  
    39                 return $return; 
    40         } 
    41  
    4220} // End Role_Model 
  • trunk/modules/auth/models/user.php

    r3000 r3081  
    44 
    55        // Relationships 
    6         protected $has_many = array('tokens'); 
     6        protected $has_many = array('user_tokens'); 
    77        protected $has_and_belongs_to_many = array('roles'); 
    8  
    9         // User roles 
    10         protected $roles = NULL; 
    11  
    12         public function __get($key) 
    13         { 
    14                 // Allow roles to be fetched as a simple array 
    15                 if ($key === 'roles') 
    16                 { 
    17                         // Force the roles to load if they are empty 
    18                         ($this->roles === NULL) and $this->has_role('login'); 
    19  
    20                         return $this->roles; 
    21                 } 
    22  
    23                 return parent::__get($key); 
    24         } 
    258 
    269        public function __set($key, $value) 
     
    3518        } 
    3619 
    37         /** 
    38          * Overloading the has_role method, for optimization. 
    39          */ 
    40         public function has_role($role) 
     20        public function has($object, $id = NULL) 
    4121        { 
    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'); 
    4526 
    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); 
    5231 
    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); 
    6137                } 
    6238 
    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); 
    7640        } 
    7741 
     
    7943         * Tests if a username exists in the database. 
    8044         * 
    81          * @param   string   username to check 
    82          * @return  bool 
     45         * @param   mixed    id to check 
     46         * @return  boolean 
    8347         */ 
    84         public function username_exists($name) 
     48        public function username_exists($id) 
    8549        { 
    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); 
    8753        } 
    8854 
     
    9056         * Allows a model to be loaded by username or email address. 
    9157         */ 
    92         protected function where_key($id = NULL) 
     58        public function unique_key($id) 
    9359        { 
    9460                if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id)) 
     
    9763                } 
    9864 
    99                 return parent::where_key($id); 
     65                return parent::unique_key($id); 
    10066        } 
    10167 
  • trunk/modules/auth/models/user_token.php

    r1667 r3081  
    1212         * Handles garbage collection and deleting of expired objects. 
    1313         */ 
    14         public function __construct($id = FALSE) 
     14        public function __construct($id = NULL) 
    1515        { 
    1616                parent::__construct($id); 
     
    2525                } 
    2626 
    27                 if ($this->object->id != 0 AND $this->object->expires < $this->now) 
     27                if ($this->expires < $this->now) 
    2828                { 
    2929                        // This object has expired 
     
    3838        public function save() 
    3939        { 
    40                 if ($this->object->id == 0) 
     40                if ($this->loaded === FALSE) 
    4141                { 
    4242                        // Set the created time, token, and hash of the user agent 
     
    5959        { 
    6060                // 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); 
    6362 
    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; 
    7564        } 
    7665 
     
    9079 
    9180                        // 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) 
    9382                        { 
    9483                                // A unique token has been found 
     
    9887        } 
    9988 
     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 
    100102} // End User Token