2014-06-22 43 views
0

我正在使用ZfcRbac并需要添加自定义重定向策略。我已阅读这里的文档re-direct strategy documents,但我不是100%确定如何实施替代stratergy我用例:ZF2-使用zfc-rbac进行自定义路由重定向

  1. 当用户试图在管理方面我目前重定向到管理员进入路径登录页面,如果他们还没有登录。

  2. 当客户登录到他们的帐户页面时,我需要将他们重定向到客户登录页面。目前他们被重定向到管理员登录页面。

我Module.php文件有这个::

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' 
    ], 
] 
]; 

为了使这项工作,我相信我需要写一个自定义stratergy但是,我不是100%确定如何去做这件事。

回答

0

这实际上很容易解决,一旦我得到了ZfcRbac我的头。我所做的就是创建自己的RedirectStrategy :: MyRedirectStrategy并更新配置全局。

config global:路由数组和路由指向。这可能是更广泛的,包括各种路线和重定向,但我只需要一个额外的。

 '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(); 
    } 

我选择了设置客户的网页作为默认为我没有由于错过了路由检查而导致管理员被导向到客户页面的问题。相反,这会导致客户混淆。