Zend的Acl角色不明确

时间:2012-01-04 12:12:57

标签: php zend-framework

I am doing this in my code 

    <?php
    class Model_LibraryAcl extends Zend_Acl {
        public function __construct() {
            $this->addRole(new Zend_Acl_Role('guests'));
            $this->addRole(new Zend_Acl_Role('users'), 'guests');
            $this->addRole(new Zend_Acl_Role('admins'), 'users');

            $this->add(new Zend_Acl_Resource('library'))
                 ->add(new Zend_Acl_Resource('library:books'), 'library');

            $this->add(new Zend_Acl_Resource('admin'))
                 ->add(new Zend_Acl_Resource('admin:book'), 'admin');

            $this->add(new Zend_Acl_Resource('default'))
                 ->add(new Zend_Acl_Resource('default:authentication'), 'default')
                 ->add(new Zend_Acl_Resource('default:index'), 'default')
                 ->add(new Zend_Acl_Resource('default:error'), 'default');

            $this->allow('guests', 'default:authentication', 'login');
            $this->allow('guests', 'default:error', 'error');

            $this->deny('users', 'default:authentication', 'login');
            $this->allow('users', 'default:index', 'index');
            $this->allow('users', 'default:authentication', 'logout');
            $this->allow('users', 'library:books', array('index', 'list'));

            $this->allow('admins', 'admin:book', array('index', 'add', 'edit', 'delete'));


        }
    }

但我输入用户名和密码登录

后我得到了以下错误
Fatal error: Uncaught exception 'Zend_Acl_Role_Registry_Exception' with message 'Role 'admin' not found' in /var/www/zftutorial/library/Zend/Acl/Role/Registry.php:132 Stack trace: #0 /var/www/zftutorial/library/Zend/Acl.php(837): Zend_Acl_Role_Registry->get('admin') #1 /var/www/zftutorial/application/modules/default/plugins/AccessCheck.php(15): Zend_Acl->isAllowed('admin', 'default:error', 'error') #2 /var/www/zftutorial/library/Zend/Controller/Plugin/Broker.php(309): Plugin_AccessCheck->preDispatch(Object(Zend_Controller_Request_Http)) #3 /var/www/zftutorial/library/Zend/Controller/Front.php(941): Zend_Controller_Plugin_Broker->preDispatch(Object(Zend_Controller_Request_Http)) #4 /var/www/zftutorial/library/Zend/Application/Bootstrap/Bootstrap.php(97): Zend_Controller_Front->dispatch() #5 /var/www/zftutorial/library/Zend/Application.php(366): Zend_Application_Bootstrap_Bootstrap->run() #6 /var/www/zftutorial/public/index.php(26): Zend_Application->run() #7 {main} Next exception 'Zend_Controller_Exception' with message ' in /var/www/zftutorial/library/Zend/Controller/Plugin/Broker.php on line 312

我在这里使用表用户

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `username` (`username`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `users`
--

INSERT INTO `users` (`id`, `username`, `password`, `role`) VALUES
(1, 'john', '123455', 'admin'),
(2, 'harsh', '98935', 'user');

1 个答案:

答案 0 :(得分:5)

您的代码中没有'admin'角色。只有'管理员'角色。

您只有3个角色:

 $this->addRole(new Zend_Acl_Role('guests'));
 $this->addRole(new Zend_Acl_Role('users'), 'guests');
 $this->addRole(new Zend_Acl_Role('admins'), 'users');

在结尾添加's'