PHP Zend2 bjyauthorize doctrine如果没有给出标识,则设置默认角色

时间:2014-11-19 13:49:14

标签: php doctrine-orm zend-framework2 bjyauthorize

我对Zend 2,Doctrine and Stuff

很新

在我的项目中,我想通过doctrine从zend 2实现BjyAuthorize模块。 我已经做了一些事情 - 我成功地实现并配置了除默认角色之外的所有内容,如果没有给出相同的身份(新用户是visting,或者例如在注销后)。

角色和用户类是BjyAuthorize的蓝图

这是我的身份提供者类,在我的bjyauthorize.global.php

中定义
'identity_provider' => 'Application\Provider\Identity\IdentityProvider',

代码:     

namespace Application\Provider\Identity;

use BjyAuthorize\Provider\Identity\ProviderInterface;
use Zend\Authentication\AuthenticationService;

class IdentityProvider implements ProviderInterface
{
//    public function getDefaultRole()
//    {
//        $aTest = "test";
//        return new Debug();
//    }

    public function getIdentityRoles()
    {
        $oIdentity = $this->getIdentity();

        $aRoles = [];
        if(!empty($oIdentity))
        {
            $aRoles = $oIdentity->getRoles();
        }

        return $aRoles;
    }

    protected $authService;

    public function __construct(AuthenticationService $authService)
    {
        $this->authService = $authService;
    }

    public function getAdapter()
    {
        return $this->authService->getAdapter();
    }

    public function getStorage()
    {
        return $this->authService->getStorage();
    }

    public function getIdentity()
    {
        return $this->authService->getIdentity();
    }

    public function clearIdentity()
    {
        return $this->authService->clearIdentity();
    }
}

角色提供程序已成功设置为

'role_providers' => [
    // this will load roles from
    // the 'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' service
    "BjyAuthorize\Provider\Role\ObjectRepositoryProvider" => [
        // class name of the entity representing the role
        'role_entity_class' => 'Application\Tables\Role',
        // service name of the object manager
        'object_manager'    => 'doctrine.entitymanager.orm_default',
    ],
],

现在唯一缺少的是,如果新用户正在访问该页面,我想设置一个默认角色(来自db,角色" guest")。在阅读和谷歌搜索之后,我无法找到任何提示,以及如何设置默认角色。

我已经尝试了方法" getDefaultRole"在我的IdentityProvider中,但似乎不会触发此方法。

我现在只看到在我的" getIdentityRoles"中获取默认角色如果没有设置身份。

但是为了归档这个我必须得到主义实体经理和更多的东西 - 这是唯一的方法吗?

编辑: 在" byauthorize.global.php"我可以看到以下几行:

// set the 'guest' role as default (must be defined in a role provider)
'default_role' => 'guest',

但我不知道在哪里我必须在角色提供者中定义默认角色......: - /

亲切地问候

1 个答案:

答案 0 :(得分:1)

'default_role'设置仅供BjyAuthorize(和AuthenticationIdentityProvider)附带的its factory使用。

在实施您自己的IdentityProvider时,您只需实施BjyAuthorize\Provider\Identity\ProviderInterface#getIdentityRoles(),以便在没有给出时将其恢复为您选择的身份。