使用 Kohana 制作用户档案

发布于 2024-11-08 13:25:22 字数 323 浏览 0 评论 0原文

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

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

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

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

I'm running Kohana 3, and having a hard time understanding the Auth module, or even if it's what I need. Basically I want to create a basic user profile site with basic username/password protection.

How do I take my existing controllers...

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

...and use them with some sort of authentication system

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

看透却不说透 2024-11-15 13:25:22

对于 Kohana 3,您需要在之前进行签入,而不是像 JIStone 建议的那样__construct

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

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

For Kohana 3 you'll want to do your check in before and not __construct like JIStone suggests.

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

Simple enough to understand. You can put this into a controller and have all the controllers that need authentication to extend that.

树深时见影 2024-11-15 13:25:22

如果您要求用户注册控制器上的所有页面,您可以在 __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 ,因此您需要根据您的目的进行一些调整。

If you will be requiring a user to be registered for all pages on the controller you can put a check in your __construct() statement:

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

This is the code we use, but it is Kohana 2, not 3, so you will need to adjust a bit for your purposes.

恬淡成诗 2024-11-15 13:25:22

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

一旦您的身份验证流程正常运行,您可以通过在 before() 方法中检查登录用户和正确的身份验证角色来保护某些控制器,或者为以下内容创建一个基本控制器:你所有的控制器将需要这项检查。如果用户未登录,请将他们重定向到登录页面,如果他们没有适当的访问级别(或角色),那么您可以向他们显示“拒绝访问”页面。

I have provided a link to a short walkthrough for the installation and basic usage of the Auth Module in Kohana 3

Once you have your Auth process working, you can protect certain controllers by checking for a logged in user and proper authentication role in your before() method, or create a base controller for all your controllers that will need this check. If the user is not logged in, redirect them to the login page, if they do not have the proper access level (or role), then you can show them an "Access Denied" page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文