phpdoctrine-ormzend-framework2bjyauthorize

PHP Zend2 bjyauthorize doctrine Where set default role if no identity is given


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


Solution

  • 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.