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

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

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

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

有帮助吗?

解决方案

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

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

其他提示

如果您需要向控制器上的所有页面注册用户,则可以在__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,所以你需要为您的目的调整一下。

我已经提供了一个简短演练的链接,用于安装和基本使用 Kohana 3

的AUTH模块

一旦您完成了验证过程,您可以通过检查在之前()方法中的登录用户和正确的身份验证角色来保护某些控制器,或者为需要此检查的所有控制器创建基本控制器。如果未登录用户,将它们重定向到登录页面,如果它们没有正确的访问级别(或角色),则可以向他们展示“访问被拒绝”页面。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top