bjyoungblood / BjyAuthorize

MIT License
276 stars 161 forks source link

Strange behaviour in role assigning #298

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hi, I don't understand what is happening in my application which runes with these modules :

        'ZendDeveloperTools',
        'DoctrineModule',
        'DoctrineORMModule',
        'ZfcBase',
        'ZfcUser',
        'ZfcUserDoctrineORM',
        'BjyAuthorize',

I have 3 roles defined in my database:

        'role_providers' => [
            \BjyAuthorize\Provider\Role\ZendDb::class => [
                'table' => 'role',
                'identifier_field_name' => 'roleId',
                'role_id_field' => 'id',
                'parent_role_field' => 'parent_id',
            ],
        ],
INSERT INTO `role` (`id`, `parent_id`, `roleId`) VALUES
(1, NULL, 'guest'),
(2, NULL, 'user'),
(3, NULL, 'admin');

The user table is well set with ORM:

    /**
     * @var \Doctrine\Common\Collections\Collection
     * @ORM\ManyToMany(targetEntity="Role")
     * @ORM\JoinTable(name="user_role_linker",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
     * )
     */
    protected $roles;

With its getter and setter.

When I register a new account, the account appears in the database. In my Module.php I have a listener attached to ''register.post" to set a default role for my user like this :

$em->attach('ZfcUser\Service\User', 'register.post', function ($e) {
            $user = $e->getParam('user');  // User account object

            $sm = $this->app->getServiceManager();
            $om = $sm->get('doctrine.entitymanager.orm_default');

            $userRole = $om->getRepository('LMS\Model\Role')->findOneBy(array('roleId' => 'user'));
            $user->addRole($userRole);

            $om->flush();
        });

So this is what happens :

When I create the FIRST user, he appears in the DB, the listener receives the event and creates a new row in user_role_linker which corresponds to what I'm doing in PHP. BUT the role given to the user is the role number 3 (admin) and not the number 2 (user). Even when I signout/sign in, his role remains the number 3.

When I create the second user his role is number 2.

AND the 3rd user has no role set in the application although his entry in user_role_linker is set to the role number 2.

I would be happy if someone could point out my mistake.

Thank you.

ghost commented 8 years ago

Ok I messed up my bjyauth config file and the role/identity providers. So I guess it had two roles/identity providers which were in conflict or something. It looks like this now :

        'identity_provider' => 'BjyAuthorize\Provider\Identity\AuthenticationIdentityProvider',

        'role_providers' => array(
            'BjyAuthorize\Provider\Role\ObjectRepositoryProvider' => array(
                'object_manager' => 'doctrine.entitymanager.orm_default',
                'role_entity_class' => 'LMS\Model\Role',
            ),
        ),

(found in zfcuserdoctrineorm module.config.php)