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

时间:2015-04-22 17:56:35

标签: php forms encryption yii2

我有一个Web应用程序,允许用户登录(S)FTP服务器。 S(FTP)服务器凭据数据存储在MySQL数据库中。密码使用PHP MCrypt函数和 RIJNDAEL_256 算法加密。

我想知道编辑服务器表单中的密码字段是否为空,并且只有在新密码时才能在数据库中更新密码由用户在ActiveForm字段中编写。

这是我的ServersController.php文件:

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'));
        }
    }

以下是我的FtpConnections.php模型的方法:

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方法没问题,但如何在editConnection模型中加密密码。

这是我的编辑服务器视图:

<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 个答案:

没有答案