Skip to content

Commit

Permalink
Configurable Login over Name or Email
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
REJack committed May 12, 2015
1 parent fc24226 commit 790dd44
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
2 changes: 2 additions & 0 deletions application/config/aauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
// 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';
Expand Down
3 changes: 2 additions & 1 deletion application/language/english/aauth_lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

// 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.';

Expand Down
69 changes: 53 additions & 16 deletions application/libraries/Aauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function __construct() {
* @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(
Expand All @@ -141,7 +141,21 @@ public function login($email, $pass, $remember = FALSE) {

$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
Expand All @@ -150,15 +164,9 @@ public function login($email, $pass, $remember = FALSE) {
* 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();

Expand All @@ -171,7 +179,7 @@ public function login($email, $pass, $remember = FALSE) {

//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']){
Expand All @@ -186,7 +194,7 @@ public function login($email, $pass, $remember = FALSE) {

// 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']);
Expand All @@ -197,7 +205,7 @@ public function login($email, $pass, $remember = FALSE) {
}

// 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){
Expand All @@ -208,7 +216,7 @@ public function login($email, $pass, $remember = FALSE) {
$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));
Expand Down Expand Up @@ -589,9 +597,20 @@ public function create_user($email, $pass, $name='') {

$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'));
Expand Down Expand Up @@ -886,6 +905,24 @@ public function is_banned($user_id) {
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
Expand Down

0 comments on commit 790dd44

Please sign in to comment.