Could not create navigation based on Zend_Acl

2019-08-20 23:09发布

问题:

I am trying to learn to create navigation with Zend_Acl. But the navigation only displays for admin and no one else.

I have read through my code and I tried to trace the code that comes with ZendFramework. But I am stuck and I can't figure out what I am doing wrong.

Here is my acl class:

class Application_Model_LibraryACL extends Zend_Acl
{
    public function __construct()
    { 
            $this->add(new Zend_Acl_Resource( 'guestbook' ) );
            $this->add( new Zend_Acl_Resource( 'index' ) );

            $this->add(new Zend_Acl_Resource( 'error' ) );
            $this->add(new Zend_Acl_Resource( 'authentication' ) );
            $this->add(new Zend_Acl_Resource( 'login' ), 'authentication' );
            $this->add(new Zend_Acl_Resource( 'logout' ), 'authentication' );

            $this->addRole( new Zend_Acl_Role( 'guest' ) );
            $this->addRole( new Zend_Acl_Role( 'member' ), 'guest' );
            $this->addRole( new Zend_Acl_Role( 'admin' ), 'member' );

            $this->allow( 'guest', 'error', 'error' );
            $this->allow( 'guest', 'index', 'index' );
            $this->allow( 'guest', 'authentication', array( 'login', 'logout' ) );
            $this->allow( 'member', 'guestbook', 'sign' );
            $this->allow( 'admin' );
    } 
}

Here is the xml file that defines the navigation:

<?xml version="1.0" encoding="utf-8"?>
<config>
    <nav>
            <home>
                    <label>Home</label>
                    <controller>index</controller>
                    <action>index</action>
                    <resource>index</resource>
            </home>

            <logout>
                    <label>Logout</label>
                    <controller>authentication</controller>
                    <action>logout</action>
                    <resource>logout</resource>
            </logout>

            <login>
                    <label>Login</label>
                    <controller>authentication</controller>
                    <action>login</action>
                    <resource>login</resource>
            </login>

            <guestbook>
                    <label>Guestbook</label>
                    <resource>guestbook</resource>
                    <uri></uri>
                    <pages>
                            <list>
                                    <label>List</label>
                                    <controller>guestbook</controller>
                                    <action>index</action>
                                    <resource>guestbook</resource>
                            </list>
                            <sign>
                                    <label>Sign</label>
                                    <controller>guestbook</controller>
                                    <action>sign</action>
                                    <resource>guestbook</resource>
                            </sign>
                    </pages>
            </guestbook>


    </nav>

And here is the code in the bootstrap file that sets up the navigation:

$navContainerConfig = new Zend_Config_Xml( APPLICATION_PATH . '/configs/navigation.xml', 'nav' );
$navContainer = new Zend_Navigation( $navContainerConfig );
$view->navigation( $navContainer )->setAcl( $this->acl )->setRole( Zend_Registry::get( 'role' ) );

$this->acl holds the acl object and zend registry holds the role of the logged in user.

please ask any question you might have. I have been completely stuck for over 3 days.

回答1:

What I see and concerns me are two things.

First is a resource named "guestbook" is used three times. I don't think this is your problem but you should fix that.

Second, and your problem, the third argument in allow are $privileges. I'm not certain about what I'm saying now but navigation does not require privileges. Most certainly not the ones you've provided, though.

So, try just this:

$this->allow( 'guest', 'error' );
$this->allow( 'guest', 'index' );
$this->allow( 'guest', 'authentication');
$this->allow( 'member', 'guestbook' );
$this->allow( 'admin' );