Zend Framework:可以在没有插件目录的情况下执行 ACL 吗?
我无法理解 ZF 中的 ACL 规则,并且文档不清楚。我对所有网站使用通用的 Zend 库。到目前为止没有问题,但现在每个演示或示例都说您应该将 ACL 类 (acl.php) 作为插件放置在库目录中。 Zend/Library/My/Controller/Plugin/.
我不想这样做,因为它违背了共享公共框架目录的目的。
有没有人做过或有任何关于如何使用每个网站/Web 应用程序的单独 acl.php 类文件来完成 ACL 的想法?
谢谢
I am having trouble understanding the rules to ACL in ZF and the docs aren't clear. I am using a common Zend library for all websites. So far no problem but now every demo or example says that you should place the ACL class (acl.php) in the libraries directory as a plugin. Zend/Library/My/Controller/Plugin/.
I don't want to do this because it defeats the purpose for sharing a common framework directory.
Has anyone done or have any ideas about how to accomplish ACL using individual acl.php class files for each website/web application?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不必将 acl.php 作为插件放置在库目录中。自动加载器将很好地加载该类,Zend_Acl 的技巧只是用您的角色和资源启动该类的实例。
自从我接触 Zend Framwork 以来已经有一段时间了,但我会尽力引导您走向正确的方向。
在引导程序中,创建 Zend_Acl 对象
$acl = new Zend_Acl();
//查看有关如何添加角色和资源的文档
现在在您的 Controller 目录中创建一个 Plugin 文件夹,这将允许您使用 acl 进行身份验证。
在里面创建扩展 Zend_Controller_Plugin_Abstract 的新类,为其提供由自动加载器拾取的正确类名。
将您创建的 acl 存储在注册表中,并在插件中覆盖 preDispatch 方法,从这里您可以访问请求和 acl(来自 zend 注册表),您可以根据需要进行验证。 (有些人将控制器/操作作为其他模型的资源。这是相当自由的。
使用前端控制器注册您的插件。
$frontController->registerPlugin(new My_Controller_Plugin_Acl());
这可能是其他教程所建议的(或其变体),有时可能会有点令人困惑。
You don't have to place the acl.php in the libraries directory as a plugin. The autoloader will load the class just fine, the trick to Zend_Acl is just priming an instance of the class with your roles and resources.
It's been a little while since I touched Zend Framwork but I'll try to steer you in the right direction.
In your bootstrap, create the Zend_Acl object
$acl = new Zend_Acl();
//see documentation on how to add roles and resources
Now create a Plugin folder inside your Controller directory, this will allow you authenticate with your acl.
Inside there create new class that extends Zend_Controller_Plugin_Abstract give it the correct class name to be picked up by the autoloader.
Store the acl you create in the registry and in your plugin override the preDispatch method, from here you have access to the request and the acl (from the zend registry) you can validate as needed. (Some people have controller/action as resources others models. It's quite freeform.
Register your plugin with the front controller.
$frontController->registerPlugin(new My_Controller_Plugin_Acl());
This is probably what the other tutorials are suggesting (or variants of this), it can just be a little confusing sometimes.
您永远不应该将文件添加到您的 Zend 库目录 - 您有推荐此操作的教程链接吗?这些文件应该位于应用程序名称空间下的库目录中,为您提供如下结构:
或位于
application/plugins
、application/controller/helpers
或其他位置,具体取决于你正在采取的方法。编辑:听起来教程建议使用控制器插件,在这种情况下,您需要像
Yourapp_Plugin_Acl
这样的类(将“Yourapp”替换为应用的命名空间)它将位于application/plugins/Acl.php
中。You should never add files to your Zend library directory - do you have any links to tutorials recommending this? The files should either go in the library directory under your application's namespace, giving you a structure like:
or in
application/plugins
,application/controller/helpers
or somewhere else depending on the approach you are taking.Edit: it sounds like a controller plugin is what the tutorial is recommending, in which case you'll want a class like
Yourapp_Plugin_Acl
(replace 'Yourapp' with your app's namespace) which would live atapplication/plugins/Acl.php
.最终,您可以将其放置在您想要的任何位置,只要您的自动加载器配置足够可以找到它。具体如何使用它取决于您想要保护的资源和特权。
但认为您对实例化 ACL 和查询 ACL 感到困惑。
您很可能会在引导期间实例化/填充 ACL 对象,并将其存储在 Bootstrap 注册表或 Zend_Registry 单例中。
如果您的资源是控制器,而您的权限是操作,那么通常会使用查询 ACL 对象的
preDispatch()
插件拦截调度周期。因此,我们实际上正在研究两个不同的类/对象:
一个是 ACL 本身,扩展了
Zend_Acl
。这个可以命名为Application_Model_Acl
并放置在application/models/Acl.php
文件中。另一个是前端控制器插件。这个插件可以命名为
Application_Plugin_Acl
并存储在application/plugins/Acl.php
文件中。
[请注意,这两者都假定我们正在使用应用程序命名空间
Application
。另请注意,这两者都是特定于项目的。]当然,需要为所描述的插件提供 ACL 对象才能完成其工作,因此您的 Bootstrap 可能有这样的方法:
但请记住,这只是使用 ACL 的一种方法。在某些情况下,您的 ACL 可能不仅限于控制器/操作。在这种情况下,您可能还需要将 ACL 对象传递给查询它的其他模型/服务。在这种情况下,您的
Bootstrap
中可能有一个单独的方法来创建 ACL 对象并将其存储在Bootstrap
注册表中。然后您的控制器 - 甚至依赖注入系统 - 可以从那里获取它并将其传递到任何下游模型/服务可能需要它。[你知道,看看我的答案,它与@linead 的答案并没有太大不同。同样的想法,不同的措辞,但他完全先入为主。]
Ultimately, you can place it anywhere you want as long as your autoloader is sufficiently configured to find it. And precisely how you use it depends upon what resources and privileges you are trying to protect.
But think you are confusing instantiating your ACL and querying your ACL.
You will most likely instantiate/populate your ACL object during bootstrap and store it in the
Bootstrap
registry or in the Zend_Registry singleton.If your resources are controllers and your privileges are actions, then it is common to intercept the dispatch cycle with a
preDispatch()
plugin that queries your ACL object.So, we are really looking at two different classes/objects:
One is the ACL itself, extending
Zend_Acl
. This one could be namedApplication_Model_Acl
and placed in the fileapplication/models/Acl.php
.The other is the front controller plugin. This one could be named
Application_Plugin_Acl
and stored in the fileapplication/plugins/Acl.php
[Note that both of these presume that we are using an application namespace
Application
. Also, note that both of these are project-specific.]Of course, the plugin as described needs to be given the ACL object in order to do its job, so your Bootstrap might have a method like this:
But remember, this is only one way to use your ACL. In some cases, your ACL might not be limited to just controllers/actions. In that case, you might need to pass your ACL object to other models/services that query it, as well. In that case, you might have a separate method in your
Bootstrap
to create your ACL object and store it in theBootstrap
registry. Then your controllers - or even a dependency injection system - can grab it from there and pass it through to whatever downstream models/services might need it.[You know, looking at my answer, it's not really different from that of @linead. Same idea, different words, but he totally got in first.]