dqo88037 2013-08-25 19:27
浏览 42
已采纳

YII提出的安全Ajax请求

i'm currently writing a Application based on YII.
My action for index:

  public function actionIndex() {
    $data = array();
    $data['server'] = Server::model()->findByPk(1);
    $data['dataProvider'] = new CActiveDataProvider('ServerUserPermission', array('criteria' => array('condition' => 'serverID=:id', 'params' => array(':id' => 1))));
    $this->render('index', $data);
}

my ajax action:

public function actionAddPermission($server) {
    if(Util::checkServerPower($server, Permission::MODIFY_SERVER)) {
        $perm = new ServerUserPermission;
        $perm->userID = 1;
        $perm->serverID = $server;
        $perm->power = 10;
        try {
            if ($perm->save()) {
                echo "OK";
            } else {
                echo Util::print_r($perm->getErrors());
            }
        } catch (Exception $e) {
            echo 'Critical Error Code: ' . $e->getCode();
        }
    } else {
        echo 'No Permissions';
    }
}

My view links to the addPermission action by using a button:

echo CHtml::ajaxButton("Insert New Player", array('addPermission', 'server' => $server->serverID), array('success'=>'refresh')); 

My function Util::checkServerPower(...) checks the current User of the Application. Consequence: Ajax requests in YII are handled by an Guest AuthWeb User, but i need to check whether the User is actually allowed to add permissions or not. I currently cannot think of a secured solution to protect malicious data send by other guests or not. Is it somehow possible to get the (server-side) userID of the Ajax-call?

Thanks anyway sincerly

  • 写回答

1条回答 默认 最新

  • dongyangben6144 2013-08-26 07:43
    关注

    I would do it by using the built in access control and extending CWebUser. It might seem lengthy but I think it's a clean solution. (We already have Yii::app()->user->isGuest and the like, so why not check all permissions here?)

    1) Activate the access control filter.

    (In one controller or in /components/controller.php for all your controllers at once)

    public function filters()
    {
        return array( 'accessControl' ); // Tell Yii to use access rules for this controller
    }
    

    2) Add an access rule

    In the concerned controller. (Sorry, I didn't bother with your index-action.)

    public function accessRules()
    {
        return array(
            [
                'allow', 
                'actions'=>['AddPermission'], 
                'expression'=>'$user->has(Permission::MODIFY_SERVER)'
            ],
            ['deny'],  // Deny everything else. 
        );
    }
    

    3) Extend CWebUser

    // components/WebUser.php
    class WebUser extends CWebUser {
        public function has( $permission)
        {
                // Check database for permissions using Yii::app()->user->id
                ...
        }
    }
    

    4) Configure your app to use your new WebUser instead of CWebUser

    // config/main.php
    'components'=>[
        'user'=>[
            'class' => 'WebUser',
        ],
    ],
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题