| | 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 | /** |