2011-03-18 81 views
0

我已經安裝了離子認證,一切都已啓動並正常工作。我唯一的問題是我想改變登錄使用訪問者的用戶名,而不是電子郵件。我改變了ion_auth.php配置文件中的CONFIG選項,它仍然不起作用。是否有額外的步驟我錯過了?無法在Codeigniter Ion Auth中將Identity更改爲'用戶名'?

ion_auth配置

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

登錄()控制器

//log the user in 
function login() 
{ 
    $this->data['title'] = "Login"; 

    //validate form input 
    $this->form_validation->set_rules('email', 'E-mail Address', 'required|valid_email'); 
    $this->form_validation->set_rules('password', 'Password', 'required'); 

    if ($this->form_validation->run() == true) 
    { //check to see if the user is logging in 
     //check for "remember me" 
     $remember = (bool) $this->input->post('remember'); 

     if ($this->ion_auth->login($this->input->post('email'), $this->input->post('password'), $remember)) 
     { //if the login is successful 
      //redirect them back to the home page 
      $this->session->set_flashdata('message', $this->ion_auth->messages()); 
      redirect($this->config->item('base_url'), 'refresh'); 
     } 
     else 
     { //if the login was un-successful 
      //redirect them back to the login page 
      $this->session->set_flashdata('message', $this->ion_auth->errors()); 
      redirect('auth/login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries 
     } 
    } 
    else 
    { //the user is not logging in so display the login page 
     //set the flash data error message if there is one 
     $this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message'); 

     $this->data['email'] = array('name' => 'email', 
      'id' => 'email', 
      'type' => 'text', 
      'value' => $this->form_validation->set_value('email'), 
     ); 
     $this->data['password'] = array('name' => 'password', 
      'id' => 'password', 
      'type' => 'password', 
     ); 

     $this->load->view('auth/login', $this->data); 
    } 
} 

登錄()模型

public function login($identity, $password, $remember=FALSE) 
{ 
    if (empty($identity) || empty($password) || !$this->identity_check($identity)) 
    { 
    return FALSE; 
    } 

    $query = $this->db->select($this->identity_column.', id, password, group_id') 
       ->where($this->identity_column, $identity) 
       ->where('active', 1) 
       ->where($this->ion_auth->_extra_where) 
       ->limit(1) 
       ->get($this->tables['users']); 

    $result = $query->row(); 

    if ($query->num_rows() == 1) 
    { 
    $password = $this->hash_password_db($identity, $password); 

    if ($result->password === $password) 
    { 
     $this->update_last_login($result->id); 

     $group_row = $this->db->select('name')->where('id', $result->group_id)->get($this->tables['groups'])->row(); 

     $session_data = array(
       $this->identity_column => $result->{$this->identity_column}, 
       'id'     => $result->id, //kept for backwards compatibility 
       'user_id'    => $result->id, //everyone likes to overwrite id so we'll use user_id 
       'group_id'    => $result->group_id, 
       'group'    => $group_row->name 
       ); 

     $this->session->set_userdata($session_data); 

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

     return TRUE; 
    } 
    } 

    return FALSE; 
} 
+0

已經將其與電子郵件的工作添加一個索引?張貼選項的代碼,張貼您使用的代碼。 – jondavidjohn 2011-03-18 18:03:08

+0

是的,它可以處理電子郵件。這是默認安裝,默認管理員行通過mysql添加。我也創建了幾個虛擬賬戶。我所做的只是將其更改爲「用戶名」,並嘗試登錄到/ auth/login的默認登錄屏幕。只是給我一個登錄錯誤。我改變的唯一的東西是配置標識選項。我應該根據這種變化創建我自己的登錄控制器嗎? – jkphl 2011-03-21 20:12:59

+0

再次發佈您的代碼。 – jondavidjohn 2011-03-21 20:21:04

回答

1

你爲什麼仍然在控制器處理電子郵件(而不是用戶名)?

0

您需要更改控制器,因爲它仍然從POST中抓取電子郵件並使用它來嘗試登錄。

0

你應該在用戶名欄中的用戶表