Changeset 2261 for trunk/modules/auth

Show
Ignore:
Timestamp:
03/10/2008 12:18:12 AM (9 months ago)
Author:
Shadowhand
Message:

Updated Auth User_Model:

  • Better Auth handling
  • Better role handling
Files:
1 modified

Legend:

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

    r1766 r2261  
    88 
    99        // User roles 
    10         protected $roles = array(); 
    11  
    12         public function __construct($id = FALSE) 
    13         { 
    14                 parent::__construct($id); 
    15  
    16                 if ($this->object->id != 0) 
    17                 { 
    18                         // Preload the roles, so that we can optimize has_role 
    19                         foreach($this->find_related_roles() as $role) 
    20                         { 
    21                                 $this->roles[$role->id] = $role->name; 
    22                         } 
    23                 } 
    24         } 
     10        protected $roles = NULL; 
    2511 
    2612        public function __get($key) 
    2713        { 
    28                 // Allow roles to be fetched as array(id => name) 
     14                // Allow roles to be fetched as a simple array 
    2915                if ($key === 'roles') 
     16                { 
     17                        // Force the roles to load if they are empty 
     18                        ($this->roles === NULL) and $this->has_role('login'); 
     19 
    3020                        return $this->roles; 
     21                } 
    3122 
    3223                return parent::__get($key); 
     
    3930                if ($key === 'password') 
    4031                { 
    41                         if ($auth === NULL) 
    42                         { 
    43                                 // Load Auth, attempting to use the controller copy 
    44                                 $auth = isset(Kohana::instance()->auth) ? Kohana::instance()->auth : new Auth(); 
    45                         } 
    46  
    4732                        // Use Auth to hash the password 
    48                         $value = $auth->hash_password($value); 
     33                        $value = Auth::instance()->hash_password($value); 
    4934                } 
    5035 
     
    6045                if (is_object($role)) 
    6146                        return parent::has_role($role); 
     47 
     48                if ($this->roles === NULL) 
     49                { 
     50                        // Make the roles into an array. This serves a dual purpose 
     51                        // of preventing the roles from being re-queried unnecessarily 
     52                        // as well as optimizing has_role() calls. 
     53                        $this->roles = array(); 
     54 
     55                        if ($this->id > 0) 
     56                        { 
     57                                foreach($this->find_related_roles() as $r) 
     58                                { 
     59                                        // Load all the user roles 
     60                                        $this->roles[$r->id] = $r->name; 
     61                                } 
     62                        } 
     63                } 
    6264 
    6365                // Make sure the role name is a string