Changeset 3574 for trunk/modules/auth

Show
Ignore:
Timestamp:
10/10/2008 02:10:55 PM (8 weeks ago)
Author:
Shadowhand
Message:

Added login($array, $redirect) method to Auth_User_Model, similar to ORM::validate(), but used only to log users in.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/modules/auth/classes/model/auth/user.php

    r3520 r3574  
    4141 
    4242        /** 
     43         * Validates login information from an array, and optionally redirects 
     44         * after a successful login. 
     45         * 
     46         * @param  array    values to check 
     47         * @param  string   URI or URL to redirect to 
     48         * @return boolean 
     49         */ 
     50        public function login(array & $array, $redirect = FALSE) 
     51        { 
     52                $array = Validation::factory($array) 
     53                        ->pre_filter('trim') 
     54                        ->add_rules('username', 'required', 'length[4,32]', 'chars[a-zA-Z0-9_.]') 
     55                        ->add_rules('password', 'required', 'length[5,42]'); 
     56 
     57                // Login starts out invalid 
     58                $status = FALSE; 
     59 
     60                if ($array->validate()) 
     61                { 
     62                        if ($this->find($array['username'])) 
     63                        { 
     64                                if (Auth::instance()->login($this, $array['password'])) 
     65                                { 
     66                                        if (is_string($redirect) 
     67                                        { 
     68                                                // Redirect after a successful login 
     69                                                url::redirect($redirect); 
     70                                        } 
     71 
     72                                        // Login is successful 
     73                                        $status = TRUE; 
     74                                } 
     75                                else 
     76                                { 
     77                                        $array->add_error('password', 'invalid'); 
     78                                } 
     79                        } 
     80                        else 
     81                        { 
     82                                $array->add_error('username', 'invalid'); 
     83                        } 
     84                } 
     85 
     86                return $status; 
     87        } 
     88 
     89        /** 
    4390         * Tests if a username exists in the database. This can be used as a 
    4491         * Valdidation rule.