【问题标题】:After using amnah authentication the backend crud of user module shows the Yii2 User Module not the User CRUD使用 amnah 身份验证后,用户模块的后端 crud 显示 Yii2 用户模块而不是用户 CRUD
【发布时间】:2015-05-08 04:08:49
【问题描述】:

我们一直在使用 Yii2 Advanced App 开展一个项目,该项目具有自定义引导模板。我已经使用 gii 生成了 crud。所有其他 CRUD 都可以正常工作。但是用户 crud 显示 Yii2 用户模块而不是 CRUD。

我浏览了 amnah 的完整文档,在其他任何地方也找不到任何解决方案。我什至尝试过 Yii2 文档,也没有任何帮助。

这是我的后端配置

<?php
$params = array_merge(
require(__DIR__ . '/../../common/config/params.php'),
require(__DIR__ . '/../../common/config/params-local.php'),
require(__DIR__ . '/params.php'),
require(__DIR__ . '/params-local.php')
);

use \yii\web\Request;
$baseUrl = str_replace('/frontend/web', '', (new Request)->getBaseUrl());

return [
'id' => 'app-backend',
'basePath' => dirname(__DIR__),
'controllerNamespace' => 'backend\controllers',
'defaultRoute' => 'sahasa/index',
'bootstrap' => ['log'],
'components' => [
    'urlManager' => [
        'class' => 'yii\web\UrlManager',
        // Disable index.php
        'showScriptName' => false,
        // Disable r= routes
        'enablePrettyUrl' => true,
        'rules' => array(
                '<controller:\w+>/<id:\d+>' => '<controller>/view',
                '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
                '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
        ),
    ],
    'request' => [
        'baseUrl' => $baseUrl,
    ],
    'user' => [
        'class' => 'amnah\yii2\user\components\User',
    ],
    // 'user' => [
    //     'identityClass' => 'common\models\User',
    //     'enableAutoLogin' => true,
    // ],
    'log' => [
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
                'levels' => ['error', 'warning'],
            ],
        ],
    ],
    'errorHandler' => [
        'errorAction' => 'site/error',
    ],
],
'params' => $params,
'modules' => [
    'user' => [
        'class' => 'amnah\yii2\user\Module',
        // set custom module properties here ...
    ],
    'debug' => [
        'class' => 'yii\debug\Module',
    ],
],
];

这是我转到localhost/app/backend/web/index.php?r=user时得到的结果

我希望它显示类似于此的 CRUD

我被困在那里。如果没有 CRUD,就很难管理用户。 任何帮助表示赞赏。 提前致谢。

【问题讨论】:

  • 正如我从屏幕截图中看到的那样,路径 /user/admin 根据其描述,应该显示 crud 页面。你试过了吗?
  • 是的,我试过了。然后我得到一个#403 Forbidden,你不能执行这个操作(即使我以管理员身份登录)
  • 来自模块文档:Log in as admin using neo/neo (change it!)。我在module demo page 上尝试过,使用此凭据登录后,页面 /user/admin 变得可用

标签: yii2 yii2-advanced-app yii2-user


【解决方案1】:

我认为您也需要重新定义控制器。您需要一个从原始控制器派生的新控制器,以正确处理新的 CRUD 元素。

我想你的控制器在后端并且被命名为user 否则正确更改以下示例(config/main.php 的一部分):

'user' => [
    ...     

    'controllerMap' => [
        'user'  => 'backend\controllers\user', 
    ],
],

【讨论】:

    【解决方案2】:

    您可以将此代码添加到后端的Controller中

    public function init()
    {
         $user_id = Yii::$app->getUser()->id;
         if($user_id){
            $user = \amnah\yii2\user\models\User::findOne($user_id);
             if ($user->can("admin")) {
                 // do something
             }else{
                 throw new HttpException(403, 'You are not allowed to perform this action.');
                }
            }else{
                throw new HttpException(403, 'You are not allowed to perform this action.');
            }
          parent::init();
    }
    

    【讨论】:

      【解决方案3】:

      点击链接/user/admin

      您将为用户获得 CRUD。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-07
        • 2019-06-25
        • 2011-01-10
        • 1970-01-01
        • 2020-10-09
        • 1970-01-01
        • 2012-01-26
        • 2018-03-06
        相关资源
        最近更新 更多