“未授权”放在哪里访问控制逻辑,似乎不适合ACL?

发布于 2024-10-14 04:04:10 字数 164 浏览 3 评论 0原文

我想知道“非授权”访问控制逻辑,例如。只能在顶级待办事项上添加子待办事项。它似乎不适合 ACL,它似乎更适合身份验证/授权访问控制逻辑?

我是否将所有内容都放入我的 ACL 中?我必须以某种方式显示不同的错误消息,例如。 “您无法执行此操作”而不是“您没有足够的权限来执行此操作”...我该怎么做?

I am wondering for "Non-Authorization" Access Control Logic eg. One can only add children todos on top level todos. It doesn't seem to fit into ACL's where it seem to be more for Authentication/Authorization Access Control Logic?

Do I put all into my ACL? I must somehow show different error messages tho, eg. 'You cannot perform this action' instead of 'You don't have sufficient permissions to perform this action' ... how might I do this?

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

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

发布评论

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

评论(2

蓝礼 2024-10-21 04:04:10

您应该在访问列表对象之外处理访问逻辑。我为此目的创建了一个 frontController 插件:

class Soflomo_Controller_Plugin_Access extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup (Zend_Controller_Request_Abstract $request)
    {
        if (!$this->_isAllowed($request)) {
            throw new Zend_Controller_Action_Exception('No permission', 403);
        }
    }

    protected function _isAllowed (Zend_Controller_Request_Abstract $request, $permission = 'view')
    {
        $acl = new Acl(); // Here your logic to fetch a (cached) ACL
        $role = new User(); // Here your logic to fetch role
        $resource = new Page(); // Here your logic to fetch resource

        return $acl->hasRole($role)
            && $acl->has($resource)
            && $acl->isAllowed($role, $resource, $permission);
    }
}

在本例中,我显式检查角色和资源是否存在。这是因为我的数据库中只有角色有权访问资源的 ACL 规则。所有其他情况(当用户没有访问权限时)我将其保留在数据库之外。

当您有不同的逻辑时,检查 $acl->isAllowed($role, $resource, $permission); 并保留 $acl->hasRole 可能就足够了()$acl->has() 输出。

错误处理程序

我抛出一个 Zend_Controller_Action_Exception ,错误代码为 403,但是如果您没有捕获特定代码,它看起来就像一个正常的应用程序错误。因此,我在 ErrorHandler frontController 插件中添加了另一个常量 EXCEPTION_NO_PERMISSION 并将其添加到 switch 语句中:

case 'Zend_Controller_Action_Exception':
if (404 == $exception->getCode()) {
    $error->type = self::EXCEPTION_NO_ACTION;
} elseif (403 == $exception->getCode()) {
    $error->type = self::EXCEPTION_NO_PERMISSION;
} else {
    $error->type = self::EXCEPTION_OTHER;
}
break;

然后您可以在 ErrorController 中获取错误类型 EXCEPTION_NO_PERMISSION

You should handle the access logic outside your access list object. I created a frontController plugin for this purpose:

class Soflomo_Controller_Plugin_Access extends Zend_Controller_Plugin_Abstract
{
    public function dispatchLoopStartup (Zend_Controller_Request_Abstract $request)
    {
        if (!$this->_isAllowed($request)) {
            throw new Zend_Controller_Action_Exception('No permission', 403);
        }
    }

    protected function _isAllowed (Zend_Controller_Request_Abstract $request, $permission = 'view')
    {
        $acl = new Acl(); // Here your logic to fetch a (cached) ACL
        $role = new User(); // Here your logic to fetch role
        $resource = new Page(); // Here your logic to fetch resource

        return $acl->hasRole($role)
            && $acl->has($resource)
            && $acl->isAllowed($role, $resource, $permission);
    }
}

In this case, I explicitly check the existence of a role and resource. This is because I have only the ACL rules in my database where the role has access to the resource. All other cases (when user does not have the access) I leave it out of the database.

When you have a different logic, it is perhaps enough to check for $acl->isAllowed($role, $resource, $permission); and leave the $acl->hasRole() and $acl->has() out.

Error handler

I throw a Zend_Controller_Action_Exception with error code 403, but if you don't catch the specific code, it will look like a normal application error. Therefore I added in the ErrorHandler frontController plugin another constant EXCEPTION_NO_PERMISSION and added this to the switch statement:

case 'Zend_Controller_Action_Exception':
if (404 == $exception->getCode()) {
    $error->type = self::EXCEPTION_NO_ACTION;
} elseif (403 == $exception->getCode()) {
    $error->type = self::EXCEPTION_NO_PERMISSION;
} else {
    $error->type = self::EXCEPTION_OTHER;
}
break;

Then you can fetch the error type EXCEPTION_NO_PERMISSION in your ErrorController.

戏剧牡丹亭 2024-10-21 04:04:10

最常见的方法是处理控制器插件中的逻辑。这在大多数情况下都有效,
但我建议将逻辑存储在您的模型中。

ACL也是一个模型。

任何模型/表单/类都可以通过提供 getResourceId() 方法来实现 Zend_Acl_Resource_Interface
例如

abstract class My_Form_Acl extends Zend_Form {

    public function __construct() {
       // ...
       parent::__construct();
    }

    public function getResourceId()
    {
       return get_class($this);
    }
}

The most common way is to handle the logic in the controller plugin. This works well in most cases,
but I'd recommend to store the logic in your models.

ACL is a model too.

Any model/form/class may implement Zend_Acl_Resource_Interface just by providing getResourceId() method:
e.g.

abstract class My_Form_Acl extends Zend_Form {

    public function __construct() {
       // ...
       parent::__construct();
    }

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