php - How to save encrypted data using Yii2 ActiveRecord and ActiveForm -
i have web application allowing users log in (s)ftp servers. s(ftp) servers credential data stored mysql database. passwords encrypted using php mcrypt function , rijndael_256 algorithm.
i know possible password field in edit server form empty , password updated in database when new password written user in activeform field.
here 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 methods of 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 ok, how can encrypt password in editconnection model.
and here 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>
Comments
Post a Comment