无法使用Yii 2-usuario查看后端用户管理页面

cld4siwp  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(145)

我的用户模块使用Yii2-usuario
我运行了“first step“中“Creating the first Administrator during a migration”(在迁移过程中创建第一个管理员)一节下的迁移,只将new \Da\User\Model\User()更改为new \app\models\user\Model\User(),如下所示

$user = new \app\models\user\Model\User([
             'scenario' => 'create', 
             'email' => "admin@admin.com", 
             'firstname' => 'first',
             'lastname' => 'last',
             'password' => "verysecret"  // >6 characters!
         ]);

它正确地填充了我的表。但是当我登录到后端并试图查看https://localhost/bla/backend/web/user/admin/index时,我得到了一个403禁止错误
在我的后端main.php中,我有这个

'components' => [
    ....
    'authManager' => [
       'class' => 'Da\User\Component\AuthDbManagerComponent',
       'defaultRoles' => ['guest'], 
    ],
],
'modules' => [
        'user' => [
            'class' => Da\User\Module::class,
            'enableEmailConfirmation' => true,
            'enableRegistration' => false,
            'maxPasswordAge' => 90,
            'enableGdprCompliance' => false,
             'classMap' => [
                 'User' => 'app\models\user\Model\User', 
             ], 
            'viewPath' => '@app/views/user', 
            'controllerMap' => [
                //disable for backend
                'profile' => [
                    'class' => Da\User\Controller\ProfileController::class,
                    'as access' => [
                        'class' => yii\filters\AccessControl::class,
                        'rules' => [['allow' => false]],
                    ],
                ],
                'recovery' => [
                    'class' => Da\User\Controller\RecoveryController::class,
                    'as access' => [
                        'class' => yii\filters\AccessControl::class,
                        'rules' => [['allow' => false]],
                    ],
                ],
                'Registration' => [
                    'class' => Da\User\Controller\RegistrationController::class,
                    'as access' => [
                        'class' => yii\filters\AccessControl::class,
                        'rules' => [['allow' => false]],
                    ],
                ],
                'Settings' => [
                    'class' => Da\User\Controller\SettingsController::class,
                    'as access' => [
                        'class' => yii\filters\AccessControl::class,
                        'rules' => [['allow' => false]],
                    ],
                ],

                'migrate' => [
                    'class' => \yii\console\controllers\MigrateController::class,
                    'migrationNamespaces' => [
                        'Da\User\Migration',
                    ],
                    'migrationPath' => [
                        '@app/migrations',
                        '@yii/rbac/migrations', 
                    ],
                ],
            ],
        ],

我的backend\models\user\Model中的User模型看起来像这样

use Da\User\Model\User as BaseUser;

class User extends BaseUser
{
     public static function tableName()
     {
         return '{{%admin}}';
      }
    ...
    ...
    ..
 }

the list的RBAC和管理操作不工作。我得到一个403。
你知道我错过了什么或者做错了什么吗谢谢。

zvokhttg

zvokhttg1#

在为迁移code的第一步提供的链接中,写入了next
在安装扩展并配置好一切之后,你需要用所有与用户相关的东西来设置你的应用程序,例如。
只有在完成here中提到所有安装步骤后,您才需要运行此迁移但仍然无法运行,因为将填充默认user表,该表是通过初始迁移创建此程序包将创建自己用户表,而且在安装步骤中有一个注意
注意:如果您使用的是Yii 2的高级应用程序模板,在开始使用数据库之前,请确保您已经删除了m130524_201442_init.php迁移文件,该文件来自默认安装,它位于%PROJECT_DIR%/console/migrations/m130524_201442_init.php路径。

步骤1

在您的情况下,我将执行yii migrate/down 2,这将恢复最后2个迁移(初始化迁移包含用户表描述)。仅在您没有添加更多迁移的情况下:)

Total 2 migrations to be reverted:
        m190124_110200_add_verification_token_column_to_user_table
        m130524_201442_init

如果迁移失败,您可以运行几个SQL查询

drop table user;
delete from migration where version='m130524_201442_init';

然后删除m130524_201442_init.phpm190124_110200_add_verification_token_column_to_user_table.php第二个是可选的,但由您决定)文件

步骤2

之后,根据文档,您需要同时运行rbac + Yii 2 Usuario迁移,如本说明中所述
注意事项:您仍然需要通过执行./yii migrate --migrationPath=@yii/rbac/migrations来应用Yii 2 RBAC迁移。记住您必须首先配置AuthManager组件。另外,命名空间迁移是在Yii 2. 0. 10中引入的,所以在使用它们之前请考虑更新您的框架安装版本。如果您使用的是2.0.10之前的Yii 2版本,您必须复制位于vendor/2amigos/yii 2-usuario/src/User/Migration的迁移文件,删除其命名空间并将其添加到您的@app/migrations文件夹中。
但在此之前,您需要将下面的代码从backend/config/main.php移动到%PROJECT_DIR%/console/config/main.php

'controllerMap' => [
    'migrate' => [
        'class' => \yii\console\controllers\MigrateController::class,
        'migrationNamespaces' => [
            'Da\User\Migration',
        ],
        'migrationPath' => [
            '@app/migrations',
            '@yii/rbac/migrations', 
        ],
    ],
]

并将authManager添加到rbac控制台配置中

'authManager' => [
    'class' => 'Da\User\Component\AuthDbManagerComponent',
],

最后,您console/config/main.php应该与下面类似

return [
    // ....
    'controllerMap' => [
          // ...
          'migrate' => [
            'class' => \yii\console\controllers\MigrateController::class,
            'migrationPath' => [
                '@app/migrations',
                '@yii/rbac/migrations', // Just in case you forgot to run it on console (see next note)
            ],
            'migrationNamespaces' => [
                'Da\User\Migration',
            ],
         ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'Da\User\Component\AuthDbManagerComponent',
        ],
        // ...
    ],
    // ...
];

步骤3

运行迁移
./yii migrate --migrationPath=@yii/rbac/migrations

Total 13 new migrations to be applied:
        Da\User\Migration\m000000_000001_create_user_table
        Da\User\Migration\m000000_000002_create_profile_table
        Da\User\Migration\m000000_000003_create_social_account_table
        Da\User\Migration\m000000_000004_create_token_table
        Da\User\Migration\m000000_000005_add_last_login_at
        Da\User\Migration\m000000_000006_add_two_factor_fields
        Da\User\Migration\m000000_000007_enable_password_expiration
        Da\User\Migration\m000000_000008_add_last_login_ip
        Da\User\Migration\m000000_000009_add_gdpr_consent_fields
        m140506_102106_rbac_init
        m170907_052038_rbac_add_index_on_auth_assignment_user_id
        m180523_151638_rbac_updates_indexes_without_prefix
        m200409_110543_rbac_update_mssql_trigger

步骤4

在不更改示例中的模型的情况下,从first steps创建并运行迁移。

步骤5

backend/config/main.phpfrontend/config/main.php中删除user配置

'user' => [
    'identityClass' => 'common\models\User',
    'enableAutoLogin' => true,
    'identityCookie' => ['name' => '_identity-backend', 'httpOnly' => true],
],

使用适当管理员选项将authManageruser module应用于各自配置文件此数组应包含与您在步骤4中执行迁移中相同角色名称

'modules' => [
    'user' => [
        'class' => Da\User\Module::class,
        'administrators' => ['admin']
    ],
],
'authManager' => [
    'class' => 'Da\User\Component\AuthDbManagerComponent',
],

最后,您的前端后端配置文件应该与以下内容类似

return [
    // ...
    'modules' => [
        'user' => [
            'class' => Da\User\Module::class,
            'administrators' => ['admin']
        ],
    ],
    'components' => [
        'authManager' => [
            'class' => 'Da\User\Component\AuthDbManagerComponent',
        ],
        // ....
    ]
    // ...
];

步骤6

我最喜欢的步骤:)
http://yourapp/index.php?r=user/admin访问并输入您的迁移凭据。尽情享受吧!

相关问题