大家好,我是Zend Framework 2的新手,为了更好地了解我的项目,我使用了这个模块(http://samsonasik.wordpress.com/2013/05/29/zend-framework-2-working-with-authenticationservice-and-db-session-save-handler/#comment-5393)),并且我在数据库上添加字段"角色"。
我想问的是,我如何为用户的任何成员制定特定的路线,例如,如果用户的管理员在连接时会自动重定向到路线"管理员",而如果用户的"访问者"会重定向到路由"访问者"???
Thx
/** this function called by indexAction to reduce complexity of function */
protected function authenticate($form, $viewModel)
{
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$dataform = $form->getData();
$this->authService->getAdapter()
->setIdentity($dataform['username'])
->setCredential($dataform['password']);
$result = $this->authService->authenticate();
if ($result->isValid()) {
//authentication success
$resultRow = $this->authService->getAdapter()->getResultRowObject();
$this->authService->getStorage()->write(
array('id' => $resultRow->id,
'username' => $dataform['username'],
'ip_address' => $this->getRequest()->getServer('REMOTE_ADDR'),
'user_agent' => $request->getServer('HTTP_USER_AGENT'))
);
// your userid -> select the role
$role = $this->getRoleUser($resultRow->id);
return $this->redirect()->toRoute('success', array('action' => 'index', 'role'=>$role));
} else {
$viewModel->setVariable('error', 'Login Error');
}
}
}
}
然后进入成功页面,只需使用参数role执行一些操作
不要忘记创建一个函数$role=$this->getRoleUser($resultRow->id)获取用户的角色。
实现角色功能
在编写本文档之前,请查看如何配置和创建模型/数据库:http://framework.zend.com/manual/2.1/en/user-guide/database-and-models.html
protected function getRoleUser($userid){
$table = $this->getServiceLocator()->get('UserModelUserTable');
return $table->find($userid)->current()->role;
}