有没有办法在我的 navigation.xml 中设置允许访问网站不同部分的 acl 角色?

发布于 2024-11-15 02:25:46 字数 3929 浏览 3 评论 0原文

我的引导程序中有这个:

protected function _initAutoload()
{
    $this->_auth = Zend_Auth::getInstance();
    $this->_acl = new Federico_Plugin_Acl($this->_auth);
 ....
}
....
protected function _initNavigation()
{
    $this->bootstrap('view');

    $view = $this->getResource('view');
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation)->setAcl($this->_acl)
                                  ->setRole($this->_auth->getStorage()->read()->role);//I just added this
}

但是我刚刚插入的内容生成了这个:

可捕获的致命错误:传递给 Zend_View_Helper_Navigation_HelperAbstract::setAcl() 的参数 1 必须是 Zend_Acl 的实例,给定的 Federico_Plugin_Acl 的实例,在第 106 行的 /home/fiodorovich/public_html/gisele/application/Bootstrap.php 中调用并定义在/home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php 第 333 行

这是我的 navigation.xml 到目前为止的样子:

<configdata>
<nav>
    <home>
        <label>HOME</label>
        <controller>index</controller>
        <action>index</action>
    </home>
    <about>
        <label>Nosotros</label>
        <module>default</module>
        <controller>about</controller>
        <action>index</action>
    </about>
<admin>
        <label>Admin</label>
        <uri>admin/index</uri>
        <resource>admin</resource>
        <pages>
            <alta>
                <active>0</active>
                <label>Alta Usuario</label>
                <controller>users</controller>
                <action>create</action>
            </alta>    
        </pages>
    </admin>
</nav>

现在,即使是来宾用户也可以看到导航中的项目,尽管他们无法访问,因为它已经在 Acl 类中设置了...我如何在此处传递 acl 角色?

编辑:

//my acl
class Federico_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
private $_acl = null;
private $_auth = null;
const DEFAULT_ROLE = 'guest';

public function __construct($auth)
{
    $this->_auth = $auth;

    $this->_acl = new Zend_Acl();
    $this->_acl->addRole(new Zend_Acl_Role(self::DEFAULT_ROLE));
    $this->_acl->addRole(new Zend_Acl_Role('user'), self::DEFAULT_ROLE);
    $this->_acl->addRole(new Zend_Acl_Role('admin'), 'user');

    $this->_acl->addResource(new Zend_Acl_Resource('index'));
    $this->_acl->addResource(new Zend_Acl_Resource('users'));
    $this->_acl->addResource(new Zend_Acl_Resource('about'));
    $this->_acl->addResource(new Zend_Acl_Resource('gisele'));
    $this->_acl->addResource(new Zend_Acl_Resource('admin'));

    $this->_acl->allow('guest', 'index');
    $this->_acl->allow('guest', 'about');
    $this->_acl->deny('guest', 'gisele');
    $this->_acl->deny('guest', 'users');

    $this->_acl->allow('user', 'users', array('index')); 

    $this->_acl->allow('admin','users');
    $this->_acl->allow('admin','gisele');    
}

public function preDispatch (Zend_Controller_Request_Abstract $request)
{
    if ($this->_auth->hasIdentity()) {
        // user is logged in and we can get role
        $role = $this->_auth->getStorage()->read()->role;  
    } else {
        // guest
        $role = self::DEFAULT_ROLE;
    }
    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if($this->_acl->has($controller)) {
        if(!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('error');
            $request->setControllerName('error');
        }
    }
}
}

I have this in my bootstrap:

protected function _initAutoload()
{
    $this->_auth = Zend_Auth::getInstance();
    $this->_acl = new Federico_Plugin_Acl($this->_auth);
 ....
}
....
protected function _initNavigation()
{
    $this->bootstrap('view');

    $view = $this->getResource('view');
    $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation.xml','nav');
    $navigation = new Zend_Navigation($config);
    $view->navigation($navigation)->setAcl($this->_acl)
                                  ->setRole($this->_auth->getStorage()->read()->role);//I just added this
}

however the insert I just did generated this:

Catchable fatal error: Argument 1 passed to Zend_View_Helper_Navigation_HelperAbstract::setAcl() must be an instance of Zend_Acl, instance of Federico_Plugin_Acl given, called in /home/fiodorovich/public_html/gisele/application/Bootstrap.php on line 106 and defined in /home/fiodorovich/library/ZendFramework/library/Zend/View/Helper/Navigation/HelperAbstract.php on line 333

And this is what my navigation.xml looks like so far:

<configdata>
<nav>
    <home>
        <label>HOME</label>
        <controller>index</controller>
        <action>index</action>
    </home>
    <about>
        <label>Nosotros</label>
        <module>default</module>
        <controller>about</controller>
        <action>index</action>
    </about>
<admin>
        <label>Admin</label>
        <uri>admin/index</uri>
        <resource>admin</resource>
        <pages>
            <alta>
                <active>0</active>
                <label>Alta Usuario</label>
                <controller>users</controller>
                <action>create</action>
            </alta>    
        </pages>
    </admin>
</nav>

Right now, even the guests users can see that items in the nav, althogh they can't access since that's already set up in the Acl class... how do I pass the acl roles in here?

EDIT:

//my acl
class Federico_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
private $_acl = null;
private $_auth = null;
const DEFAULT_ROLE = 'guest';

public function __construct($auth)
{
    $this->_auth = $auth;

    $this->_acl = new Zend_Acl();
    $this->_acl->addRole(new Zend_Acl_Role(self::DEFAULT_ROLE));
    $this->_acl->addRole(new Zend_Acl_Role('user'), self::DEFAULT_ROLE);
    $this->_acl->addRole(new Zend_Acl_Role('admin'), 'user');

    $this->_acl->addResource(new Zend_Acl_Resource('index'));
    $this->_acl->addResource(new Zend_Acl_Resource('users'));
    $this->_acl->addResource(new Zend_Acl_Resource('about'));
    $this->_acl->addResource(new Zend_Acl_Resource('gisele'));
    $this->_acl->addResource(new Zend_Acl_Resource('admin'));

    $this->_acl->allow('guest', 'index');
    $this->_acl->allow('guest', 'about');
    $this->_acl->deny('guest', 'gisele');
    $this->_acl->deny('guest', 'users');

    $this->_acl->allow('user', 'users', array('index')); 

    $this->_acl->allow('admin','users');
    $this->_acl->allow('admin','gisele');    
}

public function preDispatch (Zend_Controller_Request_Abstract $request)
{
    if ($this->_auth->hasIdentity()) {
        // user is logged in and we can get role
        $role = $this->_auth->getStorage()->read()->role;  
    } else {
        // guest
        $role = self::DEFAULT_ROLE;
    }
    $action = $request->getActionName();
    $controller = $request->getControllerName();
    if($this->_acl->has($controller)) {
        if(!$this->_acl->isAllowed($role, $controller, $action)) {
            $request->setActionName('error');
            $request->setControllerName('error');
        }
    }
}
}

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

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

发布评论

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

评论(1

缘字诀 2024-11-22 02:25:46

获取 Zend_View 实例(在引导程序中、在操作助手中,无论在哪里对您来说更容易),然后:

$view->navigation()
    ->setAcl(Zend_Acl $acl)
    ->setRole(Zend_Acl_Role $role);

基本上,必须明确向导航视图助手提供有关 ACL 和当前角色的知识。

Get the Zend_View instance (in your bootstrap, in an action helper, wherever it's easier for you) and then:

$view->navigation()
    ->setAcl(Zend_Acl $acl)
    ->setRole(Zend_Acl_Role $role);

Basically, the navigation view helper must explicitly be given knowledge about the ACL and current role.

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