Changeset 3090 for trunk/modules/auth

Show
Ignore:
Timestamp:
07/12/2008 10:05:34 AM (5 months ago)
Author:
Shadowhand
Message:

Added role caching back into Auth/ORM/User_Model

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/auth/models/user.php

    r3081 r3090  
    66        protected $has_many = array('user_tokens'); 
    77        protected $has_and_belongs_to_many = array('roles'); 
     8 
     9        // User roles 
     10        protected $has_roles; 
    811 
    912        public function __set($key, $value) 
     
    2225                if ($object === 'role') 
    2326                { 
    24                         // Load a role model 
    25                         $role = ORM::factory('role'); 
     27                        if ( ! $this->loaded) 
     28                                return FALSE; 
    2629 
    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); 
     30                        if ($this->has_roles === NULL) 
     31                        { 
     32                                $this->db->select('id', 'name'); 
    3133 
    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); 
     34                                // Load the roles 
     35                                $this->has_roles = $this->roles->select_list('id', 'name'); 
     36                        } 
     37 
     38                        if (is_string($id) AND ! ctype_digit($id)) 
     39                        { 
     40                                return in_array($id, $this->has_roles); 
     41                        } 
     42                        else 
     43                        { 
     44                                return isset($this->has_roles[$id]); 
     45                        } 
    3746                } 
    3847 
     
    6675        } 
    6776 
     77        /** 
     78         * Resets roles when results are loaded. 
     79         */ 
     80        protected function load_result($array = FALSE) 
     81        { 
     82                parent::load_result($array); 
     83 
     84                if ($array === FALSE) 
     85                { 
     86                        // Reset roles 
     87                        $this->has_roles = NULL; 
     88                } 
     89 
     90                return $this; 
     91        } 
     92 
    6893} // End User_Model