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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀