模型中的 Zend ACL 与 Zend Navigation

发布于 2024-11-15 02:47:32 字数 341 浏览 9 评论 0原文

我喜欢将 ACL 绑定到模型的想法,如下所述:

但是如何将其与 Zend Navigation 结合起来?渲染站点地图时,我必须实例化所有模型。

是否有人实际上在至少中等规模的站点上使用过这种方法,并且可以分享他解决性能问题的经验?

I like the idea of binding ACL to models like described here:

But how can I combine this with Zend Navigation? When rendering the sitemap, I'd have to instantiate all the models.

Did someone actually used this approach on at least medium scale site and can share his experience on solving performance issues?

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

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

发布评论

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

评论(2

骄兵必败 2024-11-22 02:47:32

您可以将 acl 实例直接传递给导航

$view->navigation ( $navContainer )
     ->setAcl ( $this->_acl )
     ->setRole ( Zend_Registry::get ( 'role' ) );

这是我在 boostrap initAutoload 方法中初始化 acl 的方式

$this->_acl = new Model_LibraryAcl ();
$fc = Zend_Controller_Front::getInstance ();
$fc->registerPlugin ( new Plugin_AccessCheck ( $this->_acl ) );

我使用导航 xml 代码来定义我的导航,例如

<all>
    <label>All</label>
    <resource>default:programme</resource>
    <module>default</module>
    <controller>programme</controller>
    <action>list</action>
    <privilege>index</privilege>
</all>

这定义了列表操作的链接。它可以被所有用户访问,所以

$this->allow('guests', 'default:programme', array('index','list'));

这是来自我的库 acl 文件,该文件派生自 Zend_Acl。有关更多详细信息,请观看此视频教程Zend Framework 1.8 教程 8 zend_navigation 和 zend_acl

You can pass an acl instance straight to the navigation

$view->navigation ( $navContainer )
     ->setAcl ( $this->_acl )
     ->setRole ( Zend_Registry::get ( 'role' ) );

This is how I init the acl in my boostrap initAutoload method

$this->_acl = new Model_LibraryAcl ();
$fc = Zend_Controller_Front::getInstance ();
$fc->registerPlugin ( new Plugin_AccessCheck ( $this->_acl ) );

I use navigation xml code to define my navigation, e.g.

<all>
    <label>All</label>
    <resource>default:programme</resource>
    <module>default</module>
    <controller>programme</controller>
    <action>list</action>
    <privilege>index</privilege>
</all>

This defines the link for the list action. It can be accessed by all users, so

$this->allow('guests', 'default:programme', array('index','list'));

This is from my library acl file that is derived from Zend_Acl. For more detailed information go through this video tutorial Zend Framework 1.8 tutorial 8 zend_navigation and zend_acl

夕色琉璃 2024-11-22 02:47:32

我知道您不久前问过这个问题,但我认为分享我的代码会很有趣,因为我也遇到了同样的问题。

基本上,我想出的是一种新的 Xml 文件解析方法,这样我就可以直接将我的模型实例化到我的导航 Xml 配置文件中,以便将我的权限正确添加到我的 ACL 树中。

首先让我们看一下我的 Xml 菜单:

<?xml version="1.0" encoding="UTF-8" ?>
<configdata>
    <nav>
        <dashboard>
            <label>Dashboard</label>
            <controller>index</controller>
            <action>index</action>
            <class>icon-dashboard</class>
                    <resource>Model_Dashboard_Dashboard</resource>
        </dashboard>
        <members>
            <label>Members</label>
            <controller>members</controller>
            <action>index</action>
                    <resource>Model_Members_Members</resource>
            <class>icon-members</class>
            <pages>
                <members-list>
                    <label>Members list</label>
                    <controller>members</controller>
                    <action>list</action>
                    <resource>Model_Members_List</resource>
                    <privilege>list</privilege>
                </members-list>
            </pages>
        </members>
        <charts>
            <label>Charts</label>
            <controller>charts</controller>
            <action>index</action>
                    <resource>Model_Charts_Charts</resource>
            <class>icon-charts</class>
        </charts>
        <documents>
            <label>Documents</label>
            <controller>documents</controller>
            <action>index</action>
                    <resource>Model_Documents_Documents</resource>
            <class>icon-documents</class>
        </documents>
    </nav>
</configdata>

这里有趣的是 resource 属性,它们实际上都是代表我的模型的类。

现在,您可能在 Zend 文档中注意到:

注意:返回类型

读入Zend_Config_Xml的配置数据是
始终以字符串形式返回。将数据从字符串转换为其他数据
类型留给开发人员来满足他们的特定需求。

这意味着我的模型将被铸成字符串......真糟糕!为了防止这种行为,我使用了这个将资源字符串转换为对象的函数:

public static function convertNavigationAclToObject($config)
{
    foreach ($config as $key => $value) {
        if (is_string($value) AND $key === "resource") {
            $config[$key] = new $value;
            break;
        } elseif (is_array($value)) {
            $config[$key] = self::convertNavigationAclToObject($value);
        }
    }
    return $config;
}

该函数允许我递归地将所有值转换为对象,因此在模型中同时设置权限(允许、拒绝... - <代码>setAcl())。

最后,我分三个步骤实例化我的导航:

  1. 从 XML 文件获取配置
  2. 将资源字符串转换为对象
  3. 实例化 Zend_Navigation

在您的引导程序中:

$config = new Zend_Config_Xml(APPLICATION_PATH . /modules/default/views/navigation/navigation.xml', 'nav');
$pages = My_Utils::convertNavigationAclToObject($config->toArray());
$container = new Zend_Navigation($pages);

希望它能有所帮助;)

I know you asked this question a while ago, but I thought it would be interesting to share my code since I've faced the same problem.

Basically, what I came up with, is a new parsing method for Xml file, so I can directly instantiate my models into my navigation Xml configuration file, so that my privileges are correctly added to my ACL tree.

Let's take a look at my Xml menu first:

<?xml version="1.0" encoding="UTF-8" ?>
<configdata>
    <nav>
        <dashboard>
            <label>Dashboard</label>
            <controller>index</controller>
            <action>index</action>
            <class>icon-dashboard</class>
                    <resource>Model_Dashboard_Dashboard</resource>
        </dashboard>
        <members>
            <label>Members</label>
            <controller>members</controller>
            <action>index</action>
                    <resource>Model_Members_Members</resource>
            <class>icon-members</class>
            <pages>
                <members-list>
                    <label>Members list</label>
                    <controller>members</controller>
                    <action>list</action>
                    <resource>Model_Members_List</resource>
                    <privilege>list</privilege>
                </members-list>
            </pages>
        </members>
        <charts>
            <label>Charts</label>
            <controller>charts</controller>
            <action>index</action>
                    <resource>Model_Charts_Charts</resource>
            <class>icon-charts</class>
        </charts>
        <documents>
            <label>Documents</label>
            <controller>documents</controller>
            <action>index</action>
                    <resource>Model_Documents_Documents</resource>
            <class>icon-documents</class>
        </documents>
    </nav>
</configdata>

What is interesting here is the resource attributes, all of them are actually classes that represent my models.

Now, you probably noticed in the Zend documentation:

Note: Return Type

Configuration data read into Zend_Config_Xml are
always returned as strings. Conversion of data from strings to other
types is left to developers to suit their particular needs.

which means that my models will be casted into string... bummer! To prevent this behavior, I used this function that transform resources string into objects:

public static function convertNavigationAclToObject($config)
{
    foreach ($config as $key => $value) {
        if (is_string($value) AND $key === "resource") {
            $config[$key] = new $value;
            break;
        } elseif (is_array($value)) {
            $config[$key] = self::convertNavigationAclToObject($value);
        }
    }
    return $config;
}

This function allow me to recursively converts all my values into object, and therefore set the privileges at the same time (allow, deny... in your models - setAcl()).

Finally, I instantiate my navigation in three steps:

  1. Get the config from a XML file
  2. Convert resource string into object
  3. Instantiate Zend_Navigation

In your bootstrap:

$config = new Zend_Config_Xml(APPLICATION_PATH . /modules/default/views/navigation/navigation.xml', 'nav');
$pages = My_Utils::convertNavigationAclToObject($config->toArray());
$container = new Zend_Navigation($pages);

Hope it can help ;)

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