drzrdc1766788 2015-04-22 17:56
浏览 63

如何使用Yii2 ActiveRecord和ActiveForm保存加密数据

I have a web application allowing users to log in to their (S)FTP servers. The S(FTP) servers credential data is stored into the MySQL database. The passwords are encrypted using PHP MCrypt function and the RIJNDAEL_256 algorithm.

I would like to know is it possible the password field in the edit server form to be empty and the password to be updated in the database only when a new password is written by the user in the ActiveForm field.

Here is my ServersController.php file:

public function actionAdd() {
    $addFtpConnectionForm = new AddFtpConnectionForm();
    if ($addFtpConnectionForm->load(Yii::$app->request->post())) {
        if ($addFtpConnectionForm->addConnection(Yii::$app->user->identity->member_id)) {
            Yii::$app->session->setFlash('alert-success', 'FTP сървърът беше успешно добавен.');
        } else {
            Yii::$app->session->setFlash('alert-danger', 'Възникна грешка при добавянето на FTP сървъра.');
        }
        $this->redirect(Url::toRoute('servers/list'));
    }
    return $this->render('add', [
                'addFtpConnectionForm' => $addFtpConnectionForm,
                'currentConnection' => $this->currentConnection,
                'isConnected' => $this->isConnected,
    ]);
}

public function actionEdit() {
        $serverID = Yii::$app->request->get('id');
        $serverInfo = $this->ftpConnectionsModel->getFtpConnection($serverID);

        if ($serverInfo) {
            if ($serverInfo->load(Yii::$app->request->post())) {
                if ($this->ftpConnectionsModel->editConnection($serverInfo)) {
                    Yii::$app->session->setFlash('alert-success', 'Сървърът беше успешно редактиран.');
                } else {
                    Yii::$app->session->setFlash('alert-danger', 'Възникна грешка при редактирането на сървъра.');
                }
                $this->redirect(Url::toRoute('servers/list'));
            } else {
                return $this->render('edit', [
                            'currentConnection' => $this->currentConnection,
                            'serverInfo' => $serverInfo,
                            'isConnected' => $this->isConnected,
                ]);
            }
        } else {
            Yii::$app->session->setFlash('alert-info', 'Избраният сървър не съществува.');
            $this->redirect(Url::toRoute('servers/list'));
        }
    }

Here are the methods of my FtpConnections.php model:

public function addConnection($memberID, $type, $host, $username, $password, $port, $dir) {
        $this->member_id = $memberID;
        $this->type = $type;
        $this->host = $host;
        $this->username = $username;
        $this->password = MCrypt::mcEncrypt($password, Yii::$app->params['mcrypt']['encryption_key']);
        $this->port = $port;
        $this->dir = $dir;
        return $this->save();
    }

    public function editConnection($serverInfo) {
        return $serverInfo->save();
    }

addConnection method is ok, but how can I encrypt the password in the editConnection model.

And here is my edit server view:

<div class="the-box">
            <?= $this->render('../layouts/partials/flash') ?>
            <?php
            $activeForm = ActiveForm::begin([
                        'options' => ['role' => 'add-ftp'],
                        'enableClientValidation' => true
                    ])
            ?>
            <?= $activeForm->field($serverInfo, 'type')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'type') ?>
            <?= Html::activeDropDownList($serverInfo, 'type', ['FTP' => 'FTP', 'SFTP' => 'SFTP'], ['class' => 'form-control chosen-select', 'data-placeholder' => 'Изберете тип връзка']) ?>
            <?= Html::error($serverInfo, 'type', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'type')->end() ?>

            <?= $activeForm->field($serverInfo, 'host')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'host') ?>
            <?= Html::activeTextInput($serverInfo, 'host', ['class' => 'form-control', 'placeholder' => 'Въведете адрес на сървъра']) ?>
            <?= Html::error($serverInfo, 'host', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'host')->end() ?>

            <?= $activeForm->field($serverInfo, 'port')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'port') ?>
            <?= Html::activeTextInput($serverInfo, 'port', ['class' => 'form-control', 'placeholder' => 'Въведете порт']) ?>
            <?= Html::error($serverInfo, 'port', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'port')->end() ?>

            <?= $activeForm->field($serverInfo, 'username')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'username') ?>
            <?= Html::activeTextInput($serverInfo, 'username', ['class' => 'form-control', 'placeholder' => 'Въведете потребител']) ?>
            <?= Html::error($serverInfo, 'username', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'username')->end() ?>

            <?= $activeForm->field($serverInfo, 'password')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'password') ?>
            <?= Html::activePasswordInput($serverInfo, 'password', ['class' => 'form-control', 'placeholder' => 'Въведете парола']) ?>
            <?= Html::error($serverInfo, 'password', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'password')->end() ?>

            <?= $activeForm->field($serverInfo, 'dir')->begin() ?>
            <?= Html::activeLabel($serverInfo, 'dir') ?>
            <?= Html::activeTextInput($serverInfo, 'dir', ['class' => 'form-control', 'placeholder' => 'Въведете директория']) ?>
            <?= Html::error($serverInfo, 'dir', ['class' => 'help-block']) ?>
            <?= $activeForm->field($serverInfo, 'dir')->end() ?>

            <?= Html::submitButton('Редактирай', ['type' => 'submit', 'class' => 'btn btn-success']) ?>
            <?= Html::resetButton('Изчисти', ['type' => 'reset', 'class' => 'btn btn-danger']) ?>
            <?php ActiveForm::end()
            ?>
        </div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 保护模式-系统加载-段寄存器