I'm learning Zend from Alexander Romanenko video , I have problem about zend_acl
, I've declared database connection in application.ini
:
resources.db.params.dbname = zftutorial
resources.db.params.username = root
resources.db.params.password =
resources.db.params.hostname = localhost
I've added LibraryAcl.php in my models :
<?php
class Model_LibraryAcl extends Zend_Acl
{
public function __construct()
{
$this -> add(new Zend_Acl_Resource('book'));
$this ->add(new Zend_Acl_Resource('add'),'book');
$this ->add(new Zend_Acl_Resource('edit'),'book');
$this->add(new Zend_Acl_Resource('books'));
$this->add(new Zend_Acl_Resource('list'),'books');
$this->addRole(new Zend_Acl_Role('user'));
$this->addRole(new Zend_Acl_Role('admin'),'user');
$this ->allow('user','books','list');
$this->allow('admin','book','edit');
$this->allow('admin','book','add');
}
}
and changed Bootstrap.php :
protected function _initAutoload()
{
$moduleLoader = new Zend_Application_Module_Autoloader(array( 'namespace' => '',
'basePath' => APPLICATION_PATH));
$acl = new Model_LibraryAcl;
$auth = Zend_Auth::getInstance();
$fc = Zend_Controller_Front::getInstance();
$fc->registerPlugin(new Plugin_AccessCheck($acl, $auth));
return $moduleLoader;
}
I've also added AccessCheck.php in plugins :
<?php
class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract
{
private $_acl = null;
private $_auth = null;
public function __construct(Zend_Acl $acl, Zend_Auth $auth){
$this ->_acl = $acl;
$this->_auth = $auth;
}
public function preDispatch(Zend_Controller_Request_Abstract $request)
{
$resource = $request->getControllerName();
$action = $request->getActionName();
$identify = $this->_auth->getStorage()->read();
$role = $identify->role;
if (!$this->_acl->isAllowed($role , $resource, $action)){
$request->setControllerName('authentication')
->setActionName('login');
}
}
}
and last of all , this is my database-zftutorial in localhost:
Database: `zftutorial`<br>
Table structure for table `books`<br>
CREATE TABLE IF NOT EXISTS `books` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(100) NOT NULL,
`author` varchar(500) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
INSERT INTO `books` (`id`, `title`, `author`) VALUES
(1, 'onvane ketab', 'author ketab avali'),
(2, 'onvane ketab dovomi', 'author ketab dovomi'),
(3, 'onvan3', 'author varc3'),
(4, 'onvan4', 'author varc4');<Br>
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`role` varchar(10) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;<br>
INSERT INTO `users` (`id`, `username`, `password`, `role`) VALUES
(1, 'john', 'pass1', 'user'),
(2, 'george', 'pass2', 'admin');
This error occurred :
Notice: Trying to get property of non-object in C:\wamp\www\Zend\workspaces\test\application\plugins\AccessCheck.php on line 17
I think it's because there has not yet been an authentication.
Try this: