2012-12-15 107 views
4

我想在Kohana 3.3.0中使用帶有ORM驅動程序的Auth模塊,但我能做的唯一事情就是在數據庫中插入新用戶。我無法登錄他們。無法登錄Kohana 3.3.0 ORM驗證

我從一個空白的Kohana項目,一個簡單的路徑,數據庫配置文件開始,並且導入了包含在ORM模塊中的auth SQL模式(沒有其他表)。我沒有爲用戶創建新的模型文件。

下面是配置文件我複製到我的應用程序路徑/ config目錄:

<?php defined('SYSPATH') or die('No direct access allowed.'); 

return array(

     'driver'  => 'ORM', 
     'hash_method' => 'sha256', 
     'hash_key'  => 'secretkey', 
     'lifetime'  => 1209600, 
     'session_type' => Session::$default, 
     'session_key' => 'auth_user', 
     'users'  => array() 

); 

現在,這裏是我的簡單的控制器。我嘗試將用戶加載到te數據庫中,然後使用該用戶登錄。

<?php defined('SYSPATH') or die('No direct script access.'); 

class Controller_User extends Controller { 

    public function action_index(){ 

     // Enter a new user manually 
     $user = ORM::factory('user'); 
     $user->username = 'mylogin'; 
     $user->password = 'mypassword'; 
     $user->email = '[email protected]'; 

     try{ 
      $user->save(); 
     } 
     catch(ORM_Validation_Exception $e){ 
      $errors = $e->errors(); 
     } 

     if(isset($errors)){ 
      $this->response->body(var_dump($errors)); 
     }else{ 

      // Login with this user 
      $success = Auth::instance()->login('mylogin','mypassword'); 
      if ($success){ 
       $this->response->body("Welcome !"); 
      }else{ 
       $this->response->body("Not welcome..."); 
      } 
     } 
    } 
} 

此控制器未能登錄。但是當我檢查我的數據庫時,我看到用戶正確保存密碼散列。我忘了我的配置嗎?

回答

7

每個用戶都必須有一個login作用:

... 
$user->save(); 
$user->add('roles', ORM::factory('role')->where('name', '=', 'login')->find()); 
+0

非常感謝你,它完美的作品! –

+0

非常感謝!!!! – Flappy