From 790dd44a10cd3137ed19d76fbecc8897b711f7b3 Mon Sep 17 00:00:00 2001 From: Raphael Jackstadt Date: Tue, 12 May 2015 19:08:46 +0200 Subject: [PATCH] Configurable Login over Name or Email added `login_with_name` config item renamed `aauth_error_login_failed` to `aauth_error_login_failed_email` in lang file added `aauth_error_login_failed_name` in lang file changed in login function `$email` to `$identifier` --- application/config/aauth.php | 2 + application/language/english/aauth_lang.php | 3 +- application/libraries/Aauth.php | 69 ++++++++++++++++----- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/application/config/aauth.php b/application/config/aauth.php index d353756..df8d7cd 100644 --- a/application/config/aauth.php +++ b/application/config/aauth.php @@ -65,6 +65,8 @@ $config['aauth']['max_login_attempt'] = 10; // to register email verifitaion need? true / false $config['aauth']['verification'] = false; +$config['aauth']['login_with_name'] = false; + // system email. $config['aauth']['email'] = 'admin@admin.com'; $config['aauth']['name'] = 'Emre Akay'; diff --git a/application/language/english/aauth_lang.php b/application/language/english/aauth_lang.php index b3b02ef..2af947b 100644 --- a/application/language/english/aauth_lang.php +++ b/application/language/english/aauth_lang.php @@ -28,7 +28,8 @@ $lang['aauth_error_username_required'] = 'Username required'; // Access errors $lang['aauth_error_no_access'] = 'Sorry, you do not have access to the resource you requested.'; -$lang['aauth_error_login_failed'] = 'E-mail Address and Password do not match.'; +$lang['aauth_error_login_failed_email'] = 'E-mail Address and Password do not match.'; +$lang['aauth_error_login_failed_name'] = 'Username and Password do not match.'; $lang['aauth_error_login_attempts_exceeded'] = 'You have exceeded your login attempts, your account has now been locked.'; $lang['aauth_error_recaptcha_not_correct'] = 'Sorry, the reCAPTCHA text entered was incorrect.'; diff --git a/application/libraries/Aauth.php b/application/libraries/Aauth.php index f147a69..098a23c 100644 --- a/application/libraries/Aauth.php +++ b/application/libraries/Aauth.php @@ -129,7 +129,7 @@ class Aauth { * @param bool $remember * @return bool Indicates successful login. */ - public function login($email, $pass, $remember = FALSE) { + public function login($identifier, $pass, $remember = FALSE) { // Remove cookies first $cookie = array( @@ -141,7 +141,21 @@ class Aauth { $this->CI->input->set_cookie($cookie); - + if( $this->config_vars['login_with_name'] == TRUE){ + if( !$identifier OR strlen($pass) < 5 OR strlen($pass) > $this->config_vars['max'] ) + { + $this->error($this->CI->lang->line('aauth_error_login_failed_name')); + return FALSE; + } + $db_identifier = 'name'; + }else{ + if( !valid_email($identifier) OR strlen($pass) < 5 OR strlen($pass) > $this->config_vars['max'] ) + { + $this->error($this->CI->lang->line('aauth_error_login_failed_email')); + return FALSE; + } + $db_identifier = 'email'; + } /* * * User Verification @@ -150,15 +164,9 @@ class Aauth { * It was causing issues with special characters in passwords * and returning FALSE even if the password matches. */ - if( !valid_email($email) OR strlen($pass) < 5 OR strlen($pass) > $this->config_vars['max'] ) - { - $this->error($this->CI->lang->line('aauth_error_login_failed')); - return FALSE; - } - $query = null; - $query = $this->aauth_db->where('email', $email); + $query = $this->aauth_db->where($db_identifier, $identifier); $query = $this->aauth_db->get($this->config_vars['users']); $row = $query->row(); @@ -171,7 +179,7 @@ class Aauth { //recaptcha login_attempts check $query = null; - $query = $this->aauth_db->where('email', $email); + $query = $this->aauth_db->where($db_identifier, $identifier); $query = $this->aauth_db->get($this->config_vars['users']); $row = $query->row(); if($query->num_rows() > 0 && $this->config_vars['ddos_protection'] && $this->config_vars['recaptcha_active'] && $row->login_attempts >= $this->config_vars['recaptcha_login_attempts']){ @@ -186,7 +194,7 @@ class Aauth { // if user is not verified $query = null; - $query = $this->aauth_db->where('email', $email); + $query = $this->aauth_db->where($db_identifier, $identifier); $query = $this->aauth_db->where('banned', 1); $query = $this->aauth_db->where('verification_code !=', ''); $query = $this->aauth_db->get($this->config_vars['users']); @@ -197,7 +205,7 @@ class Aauth { } // to find user id, create sessions and cookies - $query = $this->aauth_db->where('email', $email); + $query = $this->aauth_db->where($db_identifier, $identifier); $query = $this->aauth_db->get($this->config_vars['users']); if($query->num_rows() == 0){ @@ -208,7 +216,7 @@ class Aauth { $user_id = $query->row()->id; $query = null; - $query = $this->aauth_db->where('email', $email); + $query = $this->aauth_db->where($db_identifier, $identifier); // Database stores pasword hashed password $query = $this->aauth_db->where('pass', $this->hash_password($pass, $user_id)); @@ -589,9 +597,20 @@ class Aauth { $valid = TRUE; - if ($this->user_exsist_by_email($email)) { - $this->error($this->CI->lang->line('aauth_error_email_exists')); - $valid = FALSE; + if($this->config_vars['login_with_name'] == TRUE){ + if (empty($name)){ + $this->error($this->CI->lang->line('aauth_error_username_required')); + $valid = FALSE; + } + if ($this->user_exsist_by_name($name)) { + $this->error($this->CI->lang->line('aauth_error_username_exists')); + $valid = FALSE; + } + }else{ + if ($this->user_exsist_by_email($email)) { + $this->error($this->CI->lang->line('aauth_error_email_exists')); + $valid = FALSE; + } } if (!valid_email($email)){ $this->error($this->CI->lang->line('aauth_error_email_invalid')); @@ -886,6 +905,24 @@ class Aauth { return FALSE; } + /** + * user_exsist_by_name + * Check if user exist by name + * @param $user_id + * + * @return bool + */ + public function user_exsist_by_name( $name ) { + $query = $this->CI->db->where('name', $name); + + $query = $this->CI->db->get($this->config_vars['users']); + + if ($query->num_rows() > 0) + return TRUE; + else + return FALSE; + } + /** * user_exsist_by_email * Check if user exsist by user email