ZF2 - 使用zfc-rbac进行自定义路由重定向

时间:2014-06-22 15:12:56

标签: zend-framework2

我正在使用ZfcRbac并需要添加自定义重定向策略。我在这里阅读了re-direct strategy documents文件,但我不能100%确定如何为我的用例实现替代策略:

  1. 当用户尝试在管理区域中输入路线时,如果他们尚未登录,我目前会重定向回管理员登录页面。

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

  3. 我的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%肯定如何去做。

1 个答案:

答案 0 :(得分: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();
    }

我选择将客户页面设置为默认值,因为由于错过了路径检查,管理员被定向到客户页面没有任何问题。另一种方式可能会使客户感到困惑。