基于角色的访问控制 - 正确的 MVC 模式

发布于 2024-12-22 11:09:38 字数 650 浏览 2 评论 0原文

半年前开始使用MVC模式,至今仍然存在一些误区。

现在我想在我的应用程序中实现基于角色的访问控制。然而,我的问题不是关于 RBAC 的,而是关于 MVC 的。

我的 RBAC 实现是这样的: 用户->角色->权限 因此每个用户(例如 userA)可以拥有许多角色(例如读者、编辑者、管理员),并且每个角色可以拥有许多权限(读取、更新、删除等)。

MySQL 表

  • users(用户列表)
  • Roles(角色列表)
  • Permissions(权限列表)
  • Roles_permissions(角色列表 -> 权限连接。例如编辑器 -> 更新)
  • users_roles(权限列表 )用户->角色连接。例如用户A->编辑)

现在我的问题是 我应该如何在 MVC 中实现这个? 有一个单独的模型:用户、角色、权限、roles_permissions、users_roles,而不是有一个创建用户、角色、权限、roles_permissions 和 user_roles 的 authManager 类? 这种方式正确吗?有没有更好、也许更优雅的方式?

I started using the MVC pattern a half year ago, and I still have some misunderstandings.

Now I want to implement a role based access control in my application. However, my question is not about RBAC, it is about MVC.

My implementation of RBAC is this:
user->role->permission
so every user (ex. userA) can have many roles (ex. reader, editor, admin), and every role can have many permissions (read, update, delete, etc.).

MySQL tables

  • users (list of users)
  • roles (list of roles)
  • permissions (list of permission)
  • roles_permissions (list of roles->permissions connections. ex. editor->update)
  • users_roles (list of users->roles connections. ex. userA->editor)

Now my question is
How should I implement this in MVC?
Have a separate model for: users, roles, permissions, roles_permissions, users_roles, than have an authManager class that creates users, roles, permission, roles_permissions, and user_roles?
Is this way correct? Is there a better, maybe more elegant way?

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

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

发布评论

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

评论(4

Spring初心 2024-12-29 11:09:38

基本上,我会坚持使用许多现有的 Kohana ACL 库之一,而不是编写自己的库(或者至少尝试它们,看看它们是否适合您的需求)。

您可能想检查此线程(Wouter A1、A2 和 ACL 模块) - http://forum.kohanaframework.org/discussion/1988/releases-a1-authentication-acl-acl-for-kohana-a2-object-level-authorization/p1
它正在不断更新和维护,并且也可用于 3.2 版本。

如果你觉得 Wouter 模块很复杂,你还可以查看 Vendo ACL 模块,它非常简单,消除了很多复杂性 - https ://github.com/vendo/acl
如何使用它的示例 - http://forum.kohanaframework .org/discussion/9517/getting-started-with-vendo-acl/p1

Basically I'd stick with one of many already existing Kohana ACL libraries instead of writing your own (or at least try them to see if they fit to your needs).

You may want to check this thread (Wouter A1, A2 and ACL modules) - http://forum.kohanaframework.org/discussion/1988/releases-a1-authentication-acl-acl-for-kohana-a2-object-level-authorization/p1
It's being constantly updated and maintained and it's available for 3.2 version as well.

If you feel Wouter modules are complicated, you can also check Vendo ACL module which is very simple and removes a lot of complications - https://github.com/vendo/acl
Examples how to use it - http://forum.kohanaframework.org/discussion/9517/getting-started-with-vendo-acl/p1

情栀口红 2024-12-29 11:09:38

您通常需要为此使用 ACL 库/类,因为您正在描述的是 ACL。我不知道 Kohana,但通过快速谷歌我找到了这个 Kohana ACL 库。 https://github.com/synapsestudios/kohana-acl

但基本上你确实需要模型管理 ACL 库中的单独实体,例如用户、角色和权限。然后与控制器或其他库中的 ACL-api 通信,以确定对应用程序特定部分的访问权限。

You'll typically want to use an ACL library/class for this since it's ACL you are describing. I don't know Kohana but from a quick google i've found this Kohana ACL library. https://github.com/synapsestudios/kohana-acl

But basically you'll indeed need models to manage your separate entities in the ACL libraries like users, roles and permissions. Then talk to the ACL-api in your controllers or other libraries to determine access to particular parts of your app.

眼趣 2024-12-29 11:09:38

我复制/粘贴 KohanaPHP 的主应用程序控制器的代码,假设我们已经包含 Zend_ACL。

请注意,我拥有基于用户的权限,而不是基于组的权限...尽管这可以轻松编辑。

<?php

defined('SYSPATH') OR exit('No direct script access.');

class Controller_Application extends Controller_Template
{

    protected static $acl;
    public $template = 'default';

    public function before()
    {
        parent::before();
        session_start();
        self::$acl = new Zend_Acl();
        $this->set_permissions($_SESSION['userid']);
    }

    protected function check_access($resource, $privilege, $redirect = TRUE)
    {
        $permission = (self::$acl->has($resource) AND self::$acl->isAllowed($_SESSION['userid'], $resource, $privilege));
        if (!$permission AND $redirect)
            $this->request->redirect('user/denied');
        elseif (!$permission AND !$redirect)
            return FALSE;
        elseif ($permission AND !$redirect)
            return TRUE;
    }

    protected function set_permissions($user_id)
    {
        $result = DB::select()
            ->from('permissions')
            ->where('user_id', '=', $user_id)
            ->execute()
            ->as_array();
        self::$acl->addRole(new Zend_Acl_Role($user_id));
        foreach ($result AS $permission)
        {
            if (!self::$acl->has($permission['resource']))
                self::$acl->add(new Zend_Acl_Resource($permission['resource']));
            self::$acl->allow($user_id, $permission['resource'], $permission['privilege']);
        }
    }
}

?>

然后我检查控制器中的访问,如下所示:$this->check_access('events', 'add');

I'm copy/pasting the code of KohanaPHP's main application controller assuming that we have Zend_ACL already included.

Please note I have user-based permissions, not group-based one... Though this can be easily edited.

<?php

defined('SYSPATH') OR exit('No direct script access.');

class Controller_Application extends Controller_Template
{

    protected static $acl;
    public $template = 'default';

    public function before()
    {
        parent::before();
        session_start();
        self::$acl = new Zend_Acl();
        $this->set_permissions($_SESSION['userid']);
    }

    protected function check_access($resource, $privilege, $redirect = TRUE)
    {
        $permission = (self::$acl->has($resource) AND self::$acl->isAllowed($_SESSION['userid'], $resource, $privilege));
        if (!$permission AND $redirect)
            $this->request->redirect('user/denied');
        elseif (!$permission AND !$redirect)
            return FALSE;
        elseif ($permission AND !$redirect)
            return TRUE;
    }

    protected function set_permissions($user_id)
    {
        $result = DB::select()
            ->from('permissions')
            ->where('user_id', '=', $user_id)
            ->execute()
            ->as_array();
        self::$acl->addRole(new Zend_Acl_Role($user_id));
        foreach ($result AS $permission)
        {
            if (!self::$acl->has($permission['resource']))
                self::$acl->add(new Zend_Acl_Resource($permission['resource']));
            self::$acl->allow($user_id, $permission['resource'], $permission['privilege']);
        }
    }
}

?>

Then I check access in controllers like this: $this->check_access('events', 'add');.

谈场末日恋爱 2024-12-29 11:09:38

我知道这条线索很冷,但一个新项目已经出现:

PHP-RBAC 是一个 PHP 分层 NIST 2 级标准基于角色的访问控制,并且非常成熟。这也是一个 OWASP 项目。

我希望您喜欢 http://phprbac.net

它在 jframework 中的使用方式是标准方式将 RBAC 纳入 MVC 模式。

I know the trail is cold, but a new project has popped up :

PHP-RBAC is a PHP Hierarchical NIST Level 2 Standard Role Based Access Control and is pretty mature. It is also an OWASP project.

I hope you enjoy it at http://phprbac.net

it is used in jframework in a way that is the standard way of incorporating RBAC in a MVC pattern.

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