Zend framework2 登录后,ZfcUser将角色重定向到不同页面
我在我的项目中使用了ZfcUser和BjyAuthorize。如果用户登录,他将被重定向到我的默认路径 但我想更改它,所以如果角色为a的用户将重定向到页面a,角色为B的用户将重定向到页面BZend framework2 登录后,ZfcUser将角色重定向到不同页面,zend-framework2,zfcuser,bjyauthorize,Zend Framework2,Zfcuser,Bjyauthorize,我在我的项目中使用了ZfcUser和BjyAuthorize。如果用户登录,他将被重定向到我的默认路径 但我想更改它,所以如果角色为a的用户将重定向到页面a,角色为B的用户将重定向到页面B 任何想法,如何实现这一点?我实际上已经这样做了,并在GitHub中创建了合并请求。实际上,这是一个非常小的更改,需要在zfc user/src/ZfcUser/Controlelr/UserController.php中完成。在authenticateAction()中,您需要替换以下内容: return $
任何想法,如何实现这一点?我实际上已经这样做了,并在GitHub中创建了合并请求。实际上,这是一个非常小的更改,需要在zfc user/src/ZfcUser/Controlelr/UserController.php中完成。在authenticateAction()中,您需要替换以下内容:
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
为此:
$route = $this->getOptions()->getLoginRedirectRoute();
if(is_callable($route)) {
$route = $route($this->zfcUserAuthentication()->getIdentity());
}
return $this->redirect()->toRoute($route);
在您的config/autoload/module.zfcuser.global.php文件中,您可以使用回调函数来执行登录路径:
'login_redirect_route' => function(\ZfcUser\Entity\UserInterface $user) {
if($user->getUsername()=='Admin') {
return 'admin';
}
return 'user';
}
由于我必须根据角色检查几件事,我创建了一个简单的模块,而不是用户登录后的默认路由,以及根据角色(可通过bjyautorize获得)将应用程序重定向到正确的url
也许这不是一种优雅的方式,但您不必修改zfcUser代码。假设您在bjyauthorize中有一个“admin”角色,您希望重定向到另一个路由 在您的登录操作中,替换代码:
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
return $this->redirect()->toRoute($this->getOptions()->getLoginRedirectRoute());
}
使用此代码:
if ($this->zfcUserAuthentication()->getAuthService()->hasIdentity()) {
$roles = $this->serviceLocator->get('BjyAuthorize\Provider\Identity\ProviderInterface')->getIdentityRoles();
if (in_array('admin',$roles))
{
return $this->redirect()->toRoute('admin_route');
} else {
return $this->redirect()->toRoute('user_route');
}
}
您可以在登录配置文件后设置重定向
'login_redirect_route' => '/your-url',
您的url
可从所有类型的用户访问,然后创建切换框以匹配您的角色,并将页面重定向到特定于角色的页面
$roles = $this->serviceLocator->get('BjyAuthorize\Provider\Identity\ProviderInterface')->getIdentityRoles();
switch ($roles){
case "admin":
$this->redirect()->toRoute('user_admin');
case "user":
$this->redirect()->toUri('user.html');
}
您必须覆盖
UserController#authenticateAction()
,这要求您也将可调用的zfcuser
覆盖到您的控制器。问题是您还必须从模块中提供视图模板;)这有点棘手,坦率地说,ZfcUser可能需要重写:谢谢你的评论。我已经覆盖了用户控制器
,以添加新操作。但我试图用你的解决方案来解决我的问题。你可以尝试在注册时重定向到一个页面(处理一些操作),然后重定向到正确的用户页面(根据他们的角色)。我已经实现了Sam解决方案。重要的是条目'zfcuser'=>'App\Controller\LoginController',
作为module.config.php
Hi-Marc中的可调用项,正如您在上面的评论中看到的,我已经通过覆盖UserController::authenticateAction()
解决了这个问题。但你的解决方案看起来是个好办法。