2012-01-04 69 views
1
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')); 


     } 
    } 

,但我得到了下面的錯誤我能做些什麼爲登錄時輸入的用戶名和密碼後,的Zend的ACL角色不明確

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'); 

回答

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」在結束

+0

你說得對,我想發生時,處理此異常,但與類Zend_Acl_Role_Registry_Exception趕不工作, 你有什麼主意嗎?! – 2015-05-04 11:29:57