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>