douchai7891 2014-11-19 13:49
浏览 28
已采纳

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

I'm pretty new to Zend 2, Doctrine and Stuff

In my Project i would like to implement BjyAuthorize module from zend 2 via doctrine. I already have done some stuff - i successfully implement and configured everything except the default role, if no identitiy is given (new user is visting, or after logout for example).

role and user classes are the blueprints from BjyAuthorize

this is my identity provider class which is defined in my bjyauthorize.global.php

'identity_provider' => 'Application\Provider\Identity\IdentityProvider',

code:

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

the role provider is successfully set to

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

The only thing is missing now is that i want to set a default role (from db, the role "guest") if a new user is visiting the page. after all reading and googling i can't find any hint where and how to set the default role.

I already tried the method "getDefaultRole" in my IdentityProvider, but this method seems not to be fired.

I only see now to fetch the default role in my "getIdentityRoles" if no identity is set.

But to archiv this i have to get the doctrine entity manager and more stuff to include - is this the only way?

edit: In the "byauthorize.global.php" i can see following lines:

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

but i do not know where i i have to define the default role in the role provider ... :-/

kindly regards

  • 写回答

1条回答 默认 最新

  • doulianxi0587 2014-11-19 17:10
    关注

    The 'default_role' setting is only used by the shipped AuthenticationIdentityProvider of BjyAuthorize (and its factory).

    When implementing your own IdentityProvider, then you simply must implement BjyAuthorize\Provider\Identity\ProviderInterface#getIdentityRoles() so that it falls back to an identity of your choice when none is given.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败