Zend framework2 ZF2-使用zfc rbac进行自定义路由重定向
我正在使用ZfcRbac,需要添加一个自定义的重新定向策略。我已经阅读了这里的文档,但不能100%确定如何为我的用例实施替代策略: 当用户试图在管理员区域中输入路由时,如果他们尚未登录,我当前会重定向回管理员登录页面 当客户登录到他们的帐户页面时,我需要将他们重新定向到客户登录页面。目前,它们被重新定向到管理员登录页面 我的Module.php文件包含以下内容:Zend framework2 ZF2-使用zfc rbac进行自定义路由重定向,zend-framework2,Zend Framework2,我正在使用ZfcRbac,需要添加一个自定义的重新定向策略。我已经阅读了这里的文档,但不能100%确定如何为我的用例实施替代策略: 当用户试图在管理员区域中输入路由时,如果他们尚未登录,我当前会重定向回管理员登录页面 当客户登录到他们的帐户页面时,我需要将他们重新定向到客户登录页面。目前,它们被重新定向到管理员登录页面 我的Module.php文件包含以下内容: public function onBootstrap(EventInterface $e) { $t = $e-&g
public function onBootstrap(EventInterface $e)
{
$t = $e->getTarget();
$t->getEventManager()->attach(
$t->getServiceManager()->get('ZfcRbac\View\Strategy\RedirectStrategy')
);
}
<?php
return [
'zfc_rbac' => [
'protection_policy' => \ZfcRbac\Guard\GuardInterface::POLICY_ALLOW,
'guards' => [
'ZfcRbac\Guard\RouteGuard' => [
//ADMIN ACCOUNT GUARDS
'user' => ['admin-master'],
'user/login' => ['guest'],
'user/logout' => ['admin-master', 'merchant-worker', 'guest'],
'user/register' => ['admin-master', 'merchant-admin', 'guest'],
'user/change-password' => ['admin-master', 'merchant-worker'],
'user/forgot-password' => ['guest'],
//CUSTOMER ACCOUNT GUARDS
'customer' => ['customer'],
]
],
'identity_provider' => \RoleBasedUser\Service\AuthenticationService::class,
'role_provider' => [
'ZfcRbac\Role\ObjectRepositoryRoleProvider' => [
'object_manager' => 'doctrine.entitymanager.orm_default',
'class_name' => 'RoleBasedUser\Entity\HierarchicalRole',
'role_name_property' => 'name'
]
],
'redirect_strategy' => [
'redirect_when_connected' => true,
'redirect_to_route_connected' => 'home',
'redirect_to_route_disconnected' => 'user/login',
'append_previous_uri' => true,
'previous_uri_query_key' => 'redirectTo'
],
]
];
我的环球旅行社有:
public function onBootstrap(EventInterface $e)
{
$t = $e->getTarget();
$t->getEventManager()->attach(
$t->getServiceManager()->get('ZfcRbac\View\Strategy\RedirectStrategy')
);
}
<?php
return [
'zfc_rbac' => [
'protection_policy' => \ZfcRbac\Guard\GuardInterface::POLICY_ALLOW,
'guards' => [
'ZfcRbac\Guard\RouteGuard' => [
//ADMIN ACCOUNT GUARDS
'user' => ['admin-master'],
'user/login' => ['guest'],
'user/logout' => ['admin-master', 'merchant-worker', 'guest'],
'user/register' => ['admin-master', 'merchant-admin', 'guest'],
'user/change-password' => ['admin-master', 'merchant-worker'],
'user/forgot-password' => ['guest'],
//CUSTOMER ACCOUNT GUARDS
'customer' => ['customer'],
]
],
'identity_provider' => \RoleBasedUser\Service\AuthenticationService::class,
'role_provider' => [
'ZfcRbac\Role\ObjectRepositoryRoleProvider' => [
'object_manager' => 'doctrine.entitymanager.orm_default',
'class_name' => 'RoleBasedUser\Entity\HierarchicalRole',
'role_name_property' => 'name'
]
],
'redirect_strategy' => [
'redirect_when_connected' => true,
'redirect_to_route_connected' => 'home',
'redirect_to_route_disconnected' => 'user/login',
'append_previous_uri' => true,
'previous_uri_query_key' => 'redirectTo'
],
]
];
为了实现这一点,我相信我需要编写一个自定义策略,但是我不能100%确定如何进行。一旦我了解了ZfcRbac,这其实很容易解决。我所做的是创建我自己的RedirectStrategy::MyRedirectStrategy并更新配置全局 配置全局:路由数组和指向的路由。这本可以做得更广泛,包括各种路由和重定向,但我只需要另外一个
'redirect_to_route_disconnected' => [
'routes' => [
'user','other1','other2'
],
'redirect' => 'user/login'
],
'redirect_to_route_connected' => [
'routes' => [
'user','other1','other2'
],
'redirect' => 'user'
],
默认设置适用于客户页面,备选页面适用于我的管理员
我在扩展策略中添加了以下检查:
if ($event->getRouteMatch()) {
$routeMatch = $event->getRouteMatch()->getMatchedRouteName();
$redirect_to_route_disconnected = $this->sl->get('config')['zfc_rbac']['redirect_to_route_disconnected'];
$redirect_to_route_connected = $this->sl->get('config')['zfc_rbac']['redirect_to_route_connected'];
} else {
$routeMatch = '';
$redirect_to_route_disconnected = array();
$redirect_to_route_connected = array();
}
然后添加了一个检查和设置:
if ($this->authenticationService->hasIdentity()) {
if (!$this->options->getRedirectWhenConnected()) {
return;
}
if (in_array($routeMatch,$redirect_to_route_connected['routes'])) {
$this->options->setRedirectToRouteConnected($redirect_to_route_connected['redirect']);
}
$redirectRoute = $this->options->getRedirectToRouteConnected();
} else {
if (in_array($routeMatch,$redirect_to_route_disconnected['routes'])) {
$this->options->setRedirectToRouteDisconnected($redirect_to_route_disconnected['redirect']);
}
$redirectRoute = $this->options->getRedirectToRouteDisconnected();
}
我选择将客户页面设置为默认页面,因为我不介意管理员因为错过了路线检查而被引导到客户页面。反之则会让客户感到困惑