【问题标题】:Role not found error找不到角色错误
【发布时间】:2013-08-17 03:05:52
【问题描述】:

我正在尝试使用 BjyAuthorize 设置 ZfcUser。尝试访问受保护的路由“/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

我的数据库表是使用 bjyauthorize 存储库中提供的 schema.sql 创建的,其中包含以下代码:

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

;

有条目:

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

我不得不修改我的 bjyauthorize.global.php 文件,因为“role_id_field”和“parent_role_field”值与提供的 sql 模式不匹配。它看起来像这样:

<?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')),
            ),
        ),
    ),
);

我的 application.config.php 看起来像这样:

<?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',
        ),
    ),
);

模块“用户”是我的自定义 ZfcUser 实体。

任何想法问题出在哪里?我对 ZF2 和 zfc 还很陌生,所以感谢您的帮助!

编辑:我应该注意,当我没有登录时,我在尝试访问 /user 时正确地得到了 403,当我登录时我收到了这条消息。我在尝试访问任何路由时收到它(无效路由除外,我收到 404)

【问题讨论】:

    标签: zend-framework zend-framework2 zfcuser bjyauthorize


    【解决方案1】:

    基于第一个评论,以下sql有效, parent_id也存在同样的问题,你也需要改一下:

    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;
    

    【讨论】:

      【解决方案2】:

      问题似乎与提供的 sql 架构有关。在 user_role_linker 表中,将 role_id 字段更改为 VARCHAR。对该表进行条目时,user_id 应该对应用户的数字id,而role_id 应该是user_role 表中“roleId”字段的文本id。

      【讨论】:

        猜你喜欢
        • 2013-01-17
        • 2022-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-19
        • 1970-01-01
        • 2020-07-13
        • 1970-01-01
        相关资源
        最近更新 更多