我目前正在开展一个需要ACL的项目,问题是'角色'列未被识别。我已经在“用户”中预定义了所有角色。表
AuthPlugin文件
$auth = Zend_Auth::getInstance();
// If user is not logged in and is not requesting login page
// - redirect to login page.
if (!$auth->hasIdentity()
&& $request->getControllerName() != $loginController
&& $request->getActionName() != $loginAction) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoSimpleAndExit($loginAction, $loginController);
}
// User is logged in or on login page.
if ($auth->hasIdentity()) {
$registry = Zend_Registry::getInstance();
$acl = $registry->get('acl');
$identity = $auth->getIdentity();
$isAllowed = $acl->isAllowed($identity->role,
$request->getControllerName(),
$request->getActionName());
if (!$isAllowed) {
$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('Redirector');
$redirector->gotoUrlAndExit('/');
}
}
}
}
ACL配置
<?php
$acl = new Zend_Acl();
$roles = array('Administrator', 'User');
$controllers = array('cart', 'index', 'user', 'purchase');
foreach ($roles as $role) {
$acl->addRole(new Zend_Acl_Role($role));
}
foreach ($controllers as $controller) {
$acl->add(new Zend_Acl_Resource($controller));
}
$acl->allow('Administrator');
$acl->allow('User');
$acl->deny('User', 'Administrator');
$registry = Zend_Registry::getInstance();
$registry->set('acl', $acl);
控制器
public function indexAction()
{
$form = new Application_Form_Login();
$request = $this->getRequest();
if ($request->isPost()) {
if ($form->isValid($request->getPost())) {
if ($this->_process($form->getValues())) {
$this->_helper->redirector('index', 'index');
}
}
}
$this->view->form = $form;
}
protected function _process($values)
{
$adapter = $this->_getAuthAdapter();
$adapter->setIdentity($values['username']);
$adapter->setCredential($values['password']);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
$user = $adapter->getResultRowObject();
$auth->getStorage()->write($user);
return true;
}
return false;
}
protected function _getAuthAdapter()
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('users')
->setIdentityColumn('username')
->setIdentity($username)
->setCredentialColumn('password')
->setCredentialTreatment('MD5(?)');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
$data = $authAdapter->getResultRowObject(null, 'password'); // without password
$auth->getStorage()->write($data);
return $authAdapter;
}
当我尝试登录时,我收到一条错误消息
Message: A value for the identity was not provided prior to authentication with Zend_Auth_Adapter_DbTable.
请告诉我一种可以获得“角色”的方法。来自用户表。