如何在控制器插件内从 Zend_Navigation 检索当前页面

发布于 2024-10-31 22:13:23 字数 2264 浏览 7 评论 0原文

我正在使用控制器插件开发身份验证插件。我在 application.ini 文件中定义导航配置,然后使用该配置和数据库用户记录动态加载 ACL 并将其应用到 Zend_Navigation。该位有效,因为它成功加载菜单并仅显示允许用户查看的页面。

但是,这并不能阻止用户直接转到该页面。我想要做的是确定用户何时访问他们在控制器插件中无权访问的页面,以便我可以将他们的请求重定向到身份验证页面。

我想一定有一个函数可以从 Zend_Navigation 检索当前页面,但我找不到它......所以也许它不存在。

不管怎样,这是我完整的控制器插件。有人看到解决方案吗?

<?php
class Pog_Model_AuthPlugin extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $oRequest)
    {
        /**
         * Load user
         */
        $oAuth = Zend_Auth::getInstance();
        $oDbUsers = new Pog_Model_DbTable_Users();

        if (!$oAuth->hasIdentity())
        {
            $oUser       = $oDbUsers->createRow();
            $oUser->name = "guest";
            $oUser->setReadOnly(true);
        }
        else
        {
            $oUser = $oAuth->getIdentity();
            $oUser->setTable($oDbUsers);
        }

        /**
         * Load ACL
         */    
        $oAcl = new Zend_Acl();
        $oAcl->addRole($oUser->name);


        /**
         * Add current user privileges
         */
        $oPrivileges = $oUser->getPrivileges();
        foreach ($oPrivileges as $oPrivilege)
        {
            if (!$oAcl->has($oPrivilege->resource))
                $oAcl->addResource($oPrivilege->resource);

            $oAcl->allow($oUser->name, $oPrivilege->resource, $oPrivilege->privilege);
        }


        /**
         * Load Navigation view helper
         */
        $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $oNavigation   = $oViewRenderer->view->navigation();


        /**
         * Add remaining Navigation resources
         */
        foreach ($oNavigation->getPages() as $oPage)
        {
            if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource()))
                $oAcl->addResource($oPage->getResource());
        }


        /**
         * Set ACL and Role
         */
        $oNavigation->setAcl($oAcl)->setRole($oUser->name);


        /**
         * Check if use is allowed to be here
         */
        ...MAGIC GOES HERE...
    }
}

I am working on an Authentication Plugin using a Controller Plugin. I define my navigation config within the application.ini file, and then use that and the Database user records to dynamically load the ACL and apply it to Zend_Navigation. This bit works, as it successfully loads the menu and only displays the pages the user is allowed to see.

However, this doesn't stop the user from going to the page directly. What I want to do is identify when the user is going to a page they don't have access to within the Controller Plugin so I can redirect their request to the Authentication page.

I was thinking there must be a function to retrieve the current page from Zend_Navigation, but I can't find it... so maybe it doesn't exist.

Anyway, this is my full Controller Plugin. Anyone see a solution?

<?php
class Pog_Model_AuthPlugin extends Zend_Controller_Plugin_Abstract
{
    public function preDispatch(Zend_Controller_Request_Abstract $oRequest)
    {
        /**
         * Load user
         */
        $oAuth = Zend_Auth::getInstance();
        $oDbUsers = new Pog_Model_DbTable_Users();

        if (!$oAuth->hasIdentity())
        {
            $oUser       = $oDbUsers->createRow();
            $oUser->name = "guest";
            $oUser->setReadOnly(true);
        }
        else
        {
            $oUser = $oAuth->getIdentity();
            $oUser->setTable($oDbUsers);
        }

        /**
         * Load ACL
         */    
        $oAcl = new Zend_Acl();
        $oAcl->addRole($oUser->name);


        /**
         * Add current user privileges
         */
        $oPrivileges = $oUser->getPrivileges();
        foreach ($oPrivileges as $oPrivilege)
        {
            if (!$oAcl->has($oPrivilege->resource))
                $oAcl->addResource($oPrivilege->resource);

            $oAcl->allow($oUser->name, $oPrivilege->resource, $oPrivilege->privilege);
        }


        /**
         * Load Navigation view helper
         */
        $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
        $oNavigation   = $oViewRenderer->view->navigation();


        /**
         * Add remaining Navigation resources
         */
        foreach ($oNavigation->getPages() as $oPage)
        {
            if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource()))
                $oAcl->addResource($oPage->getResource());
        }


        /**
         * Set ACL and Role
         */
        $oNavigation->setAcl($oAcl)->setRole($oUser->name);


        /**
         * Check if use is allowed to be here
         */
        ...MAGIC GOES HERE...
    }
}

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

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

发布评论

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

评论(2

慕烟庭风 2024-11-07 22:13:23

我认为您应该能够获得当前的导航页面,如下所示:

     /**
     * Load Navigation view helper
     */
    $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
    $oNavigation   = $oViewRenderer->view->navigation();

    /*@var $active array */        
    $active = $oNavigation->findActive($oNavigation->getContainer());

    /*@var $activePage Zend_Navigation_Page_Mvc */   
    $activePage =  $active['page'];

    // example of getting page info        
    var_dump($activePage->getLabel(), $activePage->getController(), $activePage->getAction());

希望这会有所帮助。

I think that you should be able to get current navigation page as follows:

     /**
     * Load Navigation view helper
     */
    $oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer');
    $oNavigation   = $oViewRenderer->view->navigation();

    /*@var $active array */        
    $active = $oNavigation->findActive($oNavigation->getContainer());

    /*@var $activePage Zend_Navigation_Page_Mvc */   
    $activePage =  $active['page'];

    // example of getting page info        
    var_dump($activePage->getLabel(), $activePage->getController(), $activePage->getAction());

Hope this helps.

她比我温柔 2024-11-07 22:13:23

这是我使用的解决方案,因为由于某种原因我无法让 Marcin 的解决方案在我的设置中工作。

我做了更多思考,并想到了一个很好的简单解决方案来解决该问题。我没有使用导航模块来查找活动页面,而是自己查找。由于我已经在遍历页面,因此比较控制器和操作是小菜一碟 - 如果它们都匹配,我就有了活动页面!

新的 getPages() foreach 循环如下所示:

$oCurrentPage = null;
foreach ($oNavigation->getPages() as $oPage)
{
    /**
     * Check for Current Page
     */
    if ($oPage->getController() == $oRequest->getControllerName()
      && $oPage->getAction() == $oRequest->getActionName())
        $oCurrentPage = $oPage;


    /**
     * Add Resource, if missing
     */
    if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource()))
        $oAcl->addResource($oPage->getResource());
}

This is the solution I used, since I can't get Marcin's solution working in my setup for some reason.

I did some more thinking and thought of a nice simple solution to the problem. Rather than use the Navigation module to find the Active page, I find it myself. Since I am already iterating through the pages, it's a piece of cake to compare the Controller and Action - if these both match I have my Active page!

The new getPages() foreach loop looks like this:

$oCurrentPage = null;
foreach ($oNavigation->getPages() as $oPage)
{
    /**
     * Check for Current Page
     */
    if ($oPage->getController() == $oRequest->getControllerName()
      && $oPage->getAction() == $oRequest->getActionName())
        $oCurrentPage = $oPage;


    /**
     * Add Resource, if missing
     */
    if (!is_null($oPage->getResource()) && !$oAcl->has($oPage->getResource()))
        $oAcl->addResource($oPage->getResource());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文