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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?