Role not found error

3.6k views Asked by At

I'm trying to setup ZfcUser with BjyAuthorize. I'm getting the following error when trying to access the guarded route "/user".

Fatal error: Uncaught exception 'Zend\Permissions\Acl\Exception\InvalidArgumentException' with message 'Role '3' not found' in /home/brian/dev/ptapp/app/vendor/zendframework/zendframework/library/Zend/Permissions/Acl/Role/Registry.php:106 

Stack trace: 

#0 /home/brian/dev/ptapp/app/vendor/zendframework/zendframework/library/Zend/Permissions/Acl/Role/Registry.php(67): Zend\Permissions\Acl\Role\Registry->get('3') 

#1 /home/brian/dev/ptapp/app/vendor/zendframework/zendframework/library/Zend/Permissions/Acl/Acl.php(112): Zend\Permissions\Acl\Role\Registry->add(Object(Zend\Permissions\Acl\Role\GenericRole), Array) 

#2 /home/brian/dev/ptapp/app/vendor/bjyoungblood/bjy-authorize/src/BjyAuthorize/Service/Authorize.php(277): Zend\Permissions\Acl\Acl->addRole('bjyauthorize-id...', Array) 

#3 /home/brian/dev/ptapp/app/vendor/bjyoungblood/bjy-authorize/src/BjyAuthorize/Service/Authorize.php(90): BjyAuthorize\Service\Authorize->load() 

#4 /home/brian/dev/ptapp/app/vendor/bjyoungblood/bjy-authorize/src/BjyAuthorize/Service/Authorize.php(239) in /home/brian/dev/ptapp/app/vendor/zendframework/zendframework/library/Zend/Permissions/Acl/Role/Registry.php on line 69

My database tables were created with the provided schema.sql in the bjyauthorize repository, which contains the following code:

CREATE TABLE IF NOT EXISTS `user_role` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `roleId` varchar(255) NOT NULL,
  `is_default` tinyint(1) NOT NULL,
  `parent_id` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `user_role_linker` (
  `user_id` int(11) unsigned NOT NULL,
  `role_id` int(11) NOT NULL,
  PRIMARY KEY (`user_id`,`role_id`),
  KEY `role_id` (`role_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

;

With entries:

user_role

id  |   roleId  |   is_default  |   parent_id
---------------------------------------------
1       admin           0           therapist
2       therapist       0           patient
3       patient         0           user
4       guest           1           NULL
5       user            0           NULL

user_role_linker

user_id     role_id
-------------------
    3           3

I had to modify my bjyauthorize.global.php file because the "role_id_field" and the "parent_role_field" value didn't match up with sql schema provided. It looks like this:

<?php

return array(
    'bjyauthorize' => array(

        'default_role' => 'guest',

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

        'role_providers' => array(
            'BjyAuthorize\Provider\Role\ZendDb' => array(
                'table'             => 'user_role',
                'role_id_field'     => 'roleId',
                'parent_role_field' => 'parent_id',
            ),
        ),

        'guards' => array(
            'BjyAuthorize\Guard\Route' => array(
                array('route' => 'zfcuser', 'roles' => array('user')),
                array('route' => 'zfcuser/logout', 'roles' => array('user')),
                array('route' => 'zfcuser/login', 'roles' => array('guest')),
                array('route' => 'zfcuser/register', 'roles' => array('guest')),
                // Below is the default index action used by the ZendSkeletonApplication
                array('route' => 'home', 'roles' => array('guest', 'user')),
            ),
        ),
    ),
);

And my application.config.php looks like this:

<?php
return array(
    'modules' => array(
        'Application',
        'ZfcBase',
        'ZfcUser',
        'User',
        'BjyAuthorize',
    ),

    'module_listener_options' => array(
        'module_paths' => array(
            './module',
            './vendor',
        ),

        'config_glob_paths' => array(
            'config/autoload/{,*.}{global,local}.php',
        ),
    ),
);

Where the module "User" Is my custom ZfcUser Entity.

Any ideas where the problem is? I'm pretty new to ZF2 and zfc so thanks for your help!

EDIT: I should note that when I'm not logged in, I correctly get a 403 when trying to access /user, it's when I'm logged in that I receive this message. I receive it when trying to access any route (except invalid routes, I get a 404)

2

There are 2 answers

0
Brian On BEST ANSWER

The problem seems to be with the provided sql schema. In the user_role_linker table, change the role_id field to a VARCHAR. When making entries to this table, user_id should correspond to the user's numerical id, but the role_id should be the text id of the "roleId" field in the user_role table.

0
user2851823 On

Based on first Comment, following sql works, the same problem exists on parent_id, you need to change it, too:

CREATE  TABLE IF NOT EXISTS `user_role` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `role_id` VARCHAR(255) NOT NULL,
  `is_default` TINYINT(1) NOT NULL DEFAULT 0,
  `parent_id` VARCHAR(255) NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `unique_role` (`role_id` ASC),
  INDEX `idx_parent_id` (`parent_id` ASC),
  CONSTRAINT `fk_parent_id` FOREIGN KEY (`parent_id`) REFERENCES `user_role` (`role_id`) ON DELETE SET NULL
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_unicode_ci;

CREATE  TABLE IF NOT EXISTS `user_role_linker` (
  `user_id` INT UNSIGNED NOT NULL,
  `role_id` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`user_id`, `role_id`),
  INDEX `idx_role_id` (`role_id` ASC),
  INDEX `idx_user_id` (`user_id` ASC),
  CONSTRAINT `fk_role_id` FOREIGN KEY (`role_id`) REFERENCES `user_role` (`role_id`) ON DELETE CASCADE,
  CONSTRAINT `fk_user_id` FOREIGN KEY (`user_id`) REFERENCES `user` (`user_id`) ON DELETE CASCADE
) ENGINE = InnoDB DEFAULT CHARACTER SET = utf8 COLLATE = utf8_unicode_ci;