2014-06-22 40 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(); 
    } 

我選擇了設置客戶的網頁作爲默認爲我沒有由於錯過了路由檢查而導致管理員被導向到客戶頁面的問題。相反,這會導致客戶混淆。