2009-11-17 82 views
3

對於熟悉Kohana驗證模塊的用戶,我無法登錄用戶。我可以創建一個用戶罰款,但顯然哈希不匹配。我使用提供的MySql模式來創建數據庫,並使用模塊模型。Kohana驗證模塊無法登錄

這是我創建一個用戶代碼:

public function user_create() 
    { 
     $user = ORM::factory('user'); 
     $user->username = "user"; 

     $this->auth = Auth::instance(); 

     $user->email = "[email protected]"; 
     $user->password = $this->auth->hash_password('admin'); 
     $user->add(ORM::factory('role', 'login')); 
     $user->add(ORM::factory('role', 'admin')); 

     if ($user->save()) 
      { 
       $this->template->title = "user saved"; 
       $this->template->content = "user saved"; 
      } 
     } 

它創建一個具有散列密碼的用戶,並賦予它正確的登錄/管理角色。在數據庫中一切看起來都很好。這是我的登錄代碼。我跳過的檢查,如果用戶登錄的遊戲內的部分。

  $user = $this->input->post('username'); 
     $password = $this->input->post('password'); 

     if(!empty($user)) 
      { 
       $find_user = ORM::factory('user')->where('username', $user)->find(); 
       $username = $find_user->username; 

       $this->auth = Auth::instance(); 

       if($this->auth->login($username, $password)) 
        { 
         $error = "logged in"; 
        } 
       else 
        { 
         $error = "not logged in at all"; 
        } 
      } 

     $this->template->content = new View('admin/login_view'); 
     $this->template->content->user_info = $username . " " . $password; 
     $this->template->title = "Login Admin"; 
     $this->template->content->bind('error', $error); 

它總是返回「完全不登錄」。我確認我輸入了正確的用戶名和密碼,但沒有登錄。我找不到原因。我使用內置的hash_password函數來創建密碼,並且我已經按照文檔進行了操作,但是我無法發現錯誤。任何幫助?

回答

12

當您通過__set()方法設置密碼時,kohana中的Auth模塊自動散列密碼。所以爲了讓你存儲你的密碼就這樣做:

public function user_create() 
    { 
      $user = ORM::factory('user'); 
      $user->username = "user"; 

      $this->auth = Auth::instance(); 

      $user->email = "[email protected]"; 
      $user->password = 'admin'; 
... 

希望有所幫助。如果你想查看auth模塊(models/auth_user.php),你可以看到它的哈希密碼:

public function __set($key, $value) 
{ 
    if ($key === 'password') 
    { 
     // Use Auth to hash the password 
     $value = Auth::instance()->hash_password($value); 
    } 

    parent::__set($key, $value); 
} 
+0

感謝喬恩,那正是我需要的!我應該意識到,當我看着Auth_Model。 – anthony 2009-11-18 17:09:37

+4

爲什麼你不接受這個答案(點擊大勾號)並給喬恩他值得的代表點... – Lukman 2009-11-20 02:51:18

+1

我認爲這個問題應該被標記爲已解決。它回答了我的一些問題,所以+1。 – 2012-08-02 19:06:09