如何设置模块特定的ACL资源?

发布于 2024-11-30 19:25:22 字数 985 浏览 3 评论 0原文

Zend 的 Acl 允许仅在控制器中创建资源

但是,控制器可能位于任何模块上,而不仅仅是默认模块上。所以,我在互联网上读了一些文章,我找到了一篇并尝试了这个:

$acl -> addResource(new Zend_Acl_Resource("admin"));
$acl -> addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl -> addResource(new Zend_Acl_Resource("admin:posts"), "admin");

但是,当我浏览到相应的页面时,我仍然收到有关资源 page not found 或 posts 的错误未找到。

如何设置模块特定的ACL资源?

更新:

$acl -> addrole(new Zend_Acl_Role('guest'))
    -> addrole(new Zend_Acl_ROle('admin'), 'admin');

$acl -> addResource(new Zend_Acl_Resource("page")); //controller with same name in admin module exists

$acl -> addResource(new Zend_Acl_Resource("admin"));
$acl -> addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl -> addResource(new Zend_Acl_Resource("admin:posts"), "admin");

$acl -> deny(null, null);
$acl -> allow('user', 'page', 'view');
$acl -> allow('admin', null);

Zend's Acl allows the resource to be created in controller only

But, the controllers might be on any module not just only the default module. So, I read some article off the internet, I found one and tried this:

$acl -> addResource(new Zend_Acl_Resource("admin"));
$acl -> addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl -> addResource(new Zend_Acl_Resource("admin:posts"), "admin");

But, still, when i browse to the respective pages, I get errors regarding resouce page not found, or posts not found.

How to setup Module Specific ACL Resource?

Update:

$acl -> addrole(new Zend_Acl_Role('guest'))
    -> addrole(new Zend_Acl_ROle('admin'), 'admin');

$acl -> addResource(new Zend_Acl_Resource("page")); //controller with same name in admin module exists

$acl -> addResource(new Zend_Acl_Resource("admin"));
$acl -> addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl -> addResource(new Zend_Acl_Resource("admin:posts"), "admin");

$acl -> deny(null, null);
$acl -> allow('user', 'page', 'view');
$acl -> allow('admin', null);

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

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

发布评论

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

评论(1

梦太阳 2024-12-07 19:25:22

我猜您发布的代码是某种伪代码,因为它无效。尽管如此,这个答案应该可以完美地工作。

$acl->addrole(new Zend_Acl_Role('guest'))
    ->addrole(new Zend_Acl_Role('admin'), 'guest');

$acl->addResource(new Zend_Acl_Resource("page"));
$acl->addResource(new Zend_Acl_Resource("admin"));
$acl->addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl->addResource(new Zend_Acl_Resource("admin:posts"), "admin");

$acl->deny();
$acl->allow('guest', 'page', 'view');
$acl->allow('admin');

// Lets run some quick tests...
var_dump($acl->isAllowed('guest', 'page', 'view')); // true
var_dump($acl->isAllowed('guest', 'page', 'edit')); // false
var_dump($acl->isAllowed('guest', 'admin:page', 'view')); // false

var_dump($acl->isAllowed('admin', 'page', 'view')); // true
var_dump($acl->isAllowed('admin', 'page', 'edit')); // false
var_dump($acl->isAllowed('admin', 'admin:page', 'view')); // true

I'm guessing the code you posted was some sort of pseudo code, as it wasn't valid. Still, this answer should work perfectly fine as is.

$acl->addrole(new Zend_Acl_Role('guest'))
    ->addrole(new Zend_Acl_Role('admin'), 'guest');

$acl->addResource(new Zend_Acl_Resource("page"));
$acl->addResource(new Zend_Acl_Resource("admin"));
$acl->addResource(new Zend_Acl_Resource("admin:page"), "admin");
$acl->addResource(new Zend_Acl_Resource("admin:posts"), "admin");

$acl->deny();
$acl->allow('guest', 'page', 'view');
$acl->allow('admin');

// Lets run some quick tests...
var_dump($acl->isAllowed('guest', 'page', 'view')); // true
var_dump($acl->isAllowed('guest', 'page', 'edit')); // false
var_dump($acl->isAllowed('guest', 'admin:page', 'view')); // false

var_dump($acl->isAllowed('admin', 'page', 'view')); // true
var_dump($acl->isAllowed('admin', 'page', 'edit')); // false
var_dump($acl->isAllowed('admin', 'admin:page', 'view')); // true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文