2011-05-21 50 views
2

我正在運行Kohana 3,並且很難理解Auth模塊,或者即使它是我需要的。基本上我想創建一個基本的用戶配置文件站點,具有基本的用戶名/密碼保護。用Kohana製作用戶配置文件

我如何把我現有的控制器......

class Controller_Profile extends Controller 
{ 
    function action_index($user_id) 
    { 
     // User should already be authenticated by here I think 
    } 
} 

...與某種認證系統

回答

6

對於Kohana 3,你會想要做你的支票before而不是__construct像JIStone建議的那樣。

public function before() 
{ 
    parent::before(); 

    // This automatically checks for an auto login cookie (thanks kemo). 
    if (! Auth::instance()->logged_in()) 
    { 
     // Redirect to a login page (or somewhere else). 
     $this->request->redirect(''); 
    } 
} 

夠簡單的理解。你可以把它放到一個控制器中,讓所有需要驗證的控制器擴展它。

+0

Auth :: logged_in()將嘗試自動登錄:) – Kemo 2011-05-22 07:49:23

+0

因此,它確實kemo,我從來不知道這一點。我編輯了我的答案。 – 2011-05-22 11:29:17

1

的使用他們,如果你將需要用戶註冊的所有網頁在控制器上,你可以把支票在__construct()聲明:

function __construct() 
{ 
    //Check roles for Access!!!! 
    parent::__construct(); 
    $this->load_user(); 
    if(! $this->is_registered) 
    { 
     if(request::is_ajax()) 
      die('This ajax call cannot be completed due to permission issues.'); 
     // this will redirect from the login page back to this page 
     $this->session->set('requested_url', url::current()); 
     url::redirect('user/login'); 
    } 
} 

這是我們使用的代碼,但它是Kohana的2,不是3,所以你需要調整爲你的目的。

1

我提供了一個鏈接到一個簡短演練的安裝及Auth Module in Kohana 3

基本用法一旦你驗證過程中的工作,你可以通過檢查一個登錄用戶和正確的驗證作用保護某些控制器在你的before()方法中,或爲所有需要此檢查的控制器創建一個基本控制器。如果用戶沒有登錄,將他們重定向到登錄頁面,如果他們沒有適當的訪問級別(或角色),那麼你可以向他們展示一個「訪問被拒絕」頁面。

相關問題