我可以通过用户名或电子邮件将Ion auth设置为登录

问题描述 投票:4回答:8

我知道我可以在配置中通过用户名设置离线验证,我也知道我可以通过配置中的电子邮件将其设置为登录。

有没有一种简单的方法可以将其设置为自动使用?

php codeigniter codeigniter-2 ion-auth
8个回答
5
投票

如果通过自动,你的意思是尝试一个,然后另一个,看看是否给出有效的回报:

登录发生在ion_auth_model行:899

->where($this->identity_column, $this->db->escape_str($identity))

所以你可以改变它来做一个“或”并尝试两个列。您需要在整个模型中执行此操作,因为不仅要考虑实际登录,还有用户拥有另一个用户的用户名的电子邮件(不太可能)的潜在问题


5
投票

它可能只有几个代码行:让我们假设这是你的ion_auth配置文件:

$config['identity'] = 'username'; // A database column which is used to login with

你必须运行两次登录功能。如果第一次尝试(使用用户名)没有成功,您可以更改标识符并重试:

$ this-> ion_auth_model-> identity_column =“email”;

无需修改模型或库或自定义查询


1
投票

我最近分叉了Ion Auth并进行了必要的增强,以便在配置中进行选择。叉子位于这里:

https://github.com/zepernick/CodeIgniter-Ion-Auth

我提出了一个拉取请求,要求将其包含在Ion Auth代码库中,但目前尚未被接受。关于它是否使代码变得复杂,存在一些争论。请给他们留言,让他们知道你想要这个功能,如果它对你有用。

https://github.com/benedmunds/CodeIgniter-Ion-Auth/pull/746


1
投票

无需编辑ion_auth_model,您可以执行以下操作:

考虑到你已经有这个配置:

 $config['identity'] = 'username';

你在你的控制器上有这个:

// log the user in
public function login()
{
...
    if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
    {
    //if the login is successful

您可以让它检查,然后如果它不成功,将email设置为标识列并检查它:

// log the user in
public function login()
{
...

    // check for username
    $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);
    if(! $login) { // username is not successful
        $this->ion_auth_model->identity_column = 'email';
        // check for email
        $login = $this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember);
    }

    if( $login ) {
         // successful
    } else {
         // both failed
    }

这样做的优点是:由于您没有更改核心文件,因此与ionAuth的任何新更新更兼容。这样做的缺点是它必须对数据库进行双重查询。


Auth控制器代码修改自:ionAuth Auth Controller Example

关于ionAuth Repo的讨论:


0
投票

在'identity'ion_auth配置中使用'email'然后在ion_auth_model第866行$ query之后添加此代码

if($query->num_rows() == 0){
    $query = $this->db->select($this->identity_column . ', username, email, id, password, active, last_login')
          ->where('username', $this->db->escape_str($identity))
          ->limit(1)
          ->get($this->tables['users']);
}

0
投票

我认为更简单的方法是检查$ identity var是否是电子邮件。如果它不是电子邮件,则将列设置为“用户名”。像这样的东西:

$check_column = valid_email($identity) ? $this->identity_column : 'username';   
$query = $this->db->select('username, email, id, password, active, last_login')
    ->where($check_column, $this->db->escape_str($identity))
    ->limit(1)
    ->get($this->tables['users']);

在这种情况下,您需要加载email_helper。

适合我。


0
投票

把它放在你的控制器上

if ($this->form_validation->run() !== FALSE) {
    $remember = (bool) $this->input->post('remember');
    $this->ion_auth_model->identity_column = 'username/email';

    if ($this->ion_auth->login($this->input->post('username'), $this->input->post('password'), $remember))
    {
        $this->session->set_flashdata('message', $this->ion_auth->messages());
    }
    redirect('auth/login');
}

编辑ion_auth_model.php。找到函数login()并使用下面的代码更新代码。

public function login($identity, $password, $remember=FALSE)
    {
        $this->trigger_events('pre_login');

        if (empty($identity) || empty($password))
        {
            $this->set_error('login_unsuccessful');
            return FALSE;
        }

        $this->trigger_events('extra_where');

        //just add this (starting this line)
        if ($this->identity_column == "username/email")
        {
            $fieldname = explode('/', $this->identity_column);
            $query = $this->db->select($fieldname[0] . ', username, email, id, password, active, last_login')
                          ->where($fieldname[0], $identity)
                          ->limit(1)
                          ->get($this->tables['users']);

            $this->identity_column = $fieldname[0];

            if ($query->num_rows() === 0) {
                $query = $this->db->select($fieldname[1] . ', username, email, id, password, active, last_login')
                          ->where($fieldname[1], $identity)
                          ->limit(1)
                          ->get($this->tables['users']);

                $this->identity_column = $fieldname[1];
            }
        }
        else
        {
            $query = $this->db->select($this->identity_column . ', username, email, id, password, active, last_login')
                          ->where($this->identity_column, $identity)
                          ->limit(1)
                          ->get($this->tables['users']);
        }
        //up to this line   

        if($this->is_time_locked_out($identity))
        {
            //Hash something anyway, just to take up time
            $this->hash_password($password);

            $this->trigger_events('post_login_unsuccessful');
            $this->set_error('login_timeout');

            return FALSE;
        }

        if ($query->num_rows() === 1)
        {
            $user = $query->row();

            $password = $this->hash_password_db($user->id, $password);

            if ($password === TRUE)
            {
                if ($user->active == 0)
                {
                    $this->trigger_events('post_login_unsuccessful');
                    $this->set_error('login_unsuccessful_not_active');

                    return FALSE;
                }

                $this->set_session($user);

                $this->update_last_login($user->id);

                $this->clear_login_attempts($identity);

                if ($remember && $this->config->item('remember_users', 'ion_auth'))
                {
                    $this->remember_user($user->id);
                }

                $this->trigger_events(array('post_login', 'post_login_successful'));
                $this->set_message('login_successful');

                return TRUE;
            }
        }

        //Hash something anyway, just to take up time
        $this->hash_password($password);

        $this->increase_login_attempts($identity);

        $this->trigger_events('post_login_unsuccessful');
        $this->set_error('login_unsuccessful');

        return FALSE;
    }

0
投票

您无需修改​​核心代码即可完成此操作。如果存在有效的电子邮件,只需动态更改标识列即可。注意:ion_auth_model不是ion_auth

public function check_login()
{
    if (!$this->input->is_ajax_request()) {
        exit('No direct script access allowed');
    }
    $this->form_validation->set_rules('username', str_replace(':', '', $this->lang->line('login_identity_label')), 'required');
    $this->form_validation->set_rules('password', str_replace(':', '', $this->lang->line('login_password_label')), 'required');
    if ($this->form_validation->run() == false) {
        $this->form_validation->json_errors();
    }
    $identity = $this->input->post('username');
    if ($this->form_validation->valid_email($identity)) {
        $this->ion_auth_model->identity_column = 'email';
    } else {
        $this->ion_auth_model->identity_column = 'username';
    }
    if ($this->ion_auth->login($identity, $this->input->post('password'), false)) {
        encode_response('success');
    } else {
        encode_response('error', $this->ion_auth->errors());
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.