2

我正在运行 Kohana 3,并且很难理解 Auth 模块,或者即使它是我需要的。基本上我想创建一个具有基本用户名/密码保护的基本用户配置文件站点。

我如何使用我现有的控制器...

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

...并将它们与某种身份验证系统一起使用

4

3 回答 3

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('');
    }
}

简单到可以理解。您可以将其放入控制器中,并拥有所有需要身份验证的控制器来扩展它。

于 2011-05-21T21:56:01.170 回答
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,因此您需要根据自己的目的进行一些调整。

于 2011-05-21T21:19:12.390 回答
1

我提供了一个简短的演练链接,用于在 Kohana 3 中安装和基本使用 Auth 模块

一旦您的 Auth 流程开始工作,您可以通过在 before() 方法中检查登录用户和适当的身份验证角色来保护某些控制器,或者为需要此检查的所有控制器创建一个基本控制器。如果用户没有登录,将他们重定向到登录页面,如果他们没有适当的访问级别(或角色),那么您可以向他们显示“拒绝访问”页面。

于 2011-05-23T15:18:25.040 回答