【问题标题】:How Do I disable the "gii" code generator for non admin user?如何为非管理员用户禁用“gii”代码生成器?
【发布时间】:2015-04-30 04:14:58
【问题描述】:

我正在为我的项目使用 Yii 1.13 框架,我需要“gii”代码生成器,但我想将它限制为仅限管理员用户,我该如何实现?

【问题讨论】:

  • gii 是一种开发工具,通常不应提供给经过身份验证的用户。查看实施 .htaccess 以限制对生产服务器的所有访问,或从实时部署中删除功能。

标签: php yii yii-components


【解决方案1】:

按照以下步骤操作:-

  1. system.gii 复制 gii 模块,即 framework/gii
  2. 将其粘贴到项目的 protected/modules 文件夹中。
  3. gii 模块中的 GiiModule.php 中进行以下更改。

改变这个

public function beforeControllerAction($controller, $action)
    {   

      if(parent::beforeControllerAction($controller, $action))
        {
            $route=$controller->id.'/'.$action->id;
            if(!$this->allowIp(Yii::app()->request->userHostAddress) && $route!=='default/error')
                throw new CHttpException(403,"You are not allowed to access this page.");

           $publicPages=array(
                'default/login',
                'default/error',
            );

           if(Yii::app()->user->isGuest  && !in_array($route,$publicPages))
                   Yii::app()->user->loginRequired();
            // check your admin conditions here
           elseif(!isset(Yii::app()->user->isAdmin) || !Yii::app()->user->isAdmin)
                   throw new CHttpException(403,"You are not allowed to access this page.");
            else
                return true;
          }
        return false;
    }
  1. 在你的 config/main.php

    'modules' => array( 'gii'=>array( 'class'=>'application.modules.gii.GiiModule', 'password'=> Your password, 'ipFilters'=>array('127.0.0.1','::1'), ), ),

注意:-我没有测试过。但它可能会让您知道如何进行。

【讨论】:

    【解决方案2】:

    您可以通过IP限制用户或根据Gii工具的documentation选择密码

    return array(
        ......
        'modules'=>array(
            'gii'=>array(
                'class'=>'system.gii.GiiModule',
                'password'=>'pick up a password here',
                // 'ipFilters'=>array(...a list of IPs...),
            ),
        ),
    );
    

    【讨论】:

    • 这不是 OP 所要求的。即使未经身份验证的用户知道密码,他也可以使用 Gii 工具。
    • @Touqeer Shafi 我知道基于 IP 的过滤器,但是如果普通用户知道他可以轻松访问的密码,我想阻止普通用户访问“gii”甚至登录页面
    • @GaneshGhalame 普通用户如何知道 gii 工具的密码,因为此密码已保存在您的配置文件中?
    • @Touqeer Shafi,我的问题是禁用模块,即使普通用户知道或不知道密码他可以访问“gii”登录页面,为什么系统要显示他们的偶数登录页面逻辑上未授权时。
    • @GaneshGhalame Gii 工具仅为开发目的而创建,不建议您在生产服务器上使用它。所以只需从有意义的配置文件中的模块数组中删除它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-11-04
    • 1970-01-01
    相关资源
    最近更新 更多