Improve editing of connection password

Previously only a new password was saved if the save password checkbox was checked, Which always
started in the unchecked state. Now when editing existing connection the save password checkbox now
reflects if a password has been saved. Only when the password field is edited the program will update
the saved password. If the save password checkbox is unchecked then clear the save password.
This commit is contained in:
eelke 2022-09-05 14:33:51 +02:00
parent 677302b5a7
commit da19c46d5e
7 changed files with 39 additions and 6 deletions

View file

@ -102,6 +102,7 @@ ConnectionConfigurationWidget::ConnectionConfigurationWidget(
edtPassword = new QLineEdit;
SET_OBJECT_NAME(edtPassword);
edtPassword->setEchoMode(QLineEdit::PasswordEchoOnEdit);
connect(edtPassword, &QLineEdit::textEdited, this, &ConnectionConfigurationWidget::passwordEdited);
cbSavePassword = new QCheckBox;
SET_OBJECT_NAME(cbSavePassword);
@ -212,6 +213,13 @@ void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg)
edtCrl->setText(cfg.sslCrl());
encodedPassword = cfg.encodedPassword();
cbSavePassword->setCheckState(
encodedPassword.isEmpty()
? Qt::CheckState::Unchecked
: Qt::CheckState::Checked
);
passwordChanged = false;
}
ConnectionConfig ConnectionConfigurationWidget::data() const
@ -240,7 +248,12 @@ QString ConnectionConfigurationWidget::group() const
bool ConnectionConfigurationWidget::savePassword() const
{
return cbSavePassword->isChecked() && !edtPassword->text().isEmpty();
return cbSavePassword->isChecked() && !edtPassword->text().isEmpty() && passwordChanged;
}
bool ConnectionConfigurationWidget::clearPassword() const
{
return !cbSavePassword->isChecked();
}
void ConnectionConfigurationWidget::testConnection()
@ -278,3 +291,8 @@ void ConnectionConfigurationWidget::handleTestResult(TestConnectionResult result
cmbDbname->setCurrentText(current);
}
void ConnectionConfigurationWidget::passwordEdited(const QString &)
{
passwordChanged = true;
}

View file

@ -34,7 +34,9 @@ public:
void setData(const ConnectionConfig &cfg);
ConnectionConfig data() const;
QString group() const;
bool savePassword() const;
bool clearPassword() const;
public slots:
void testConnection();
@ -45,6 +47,7 @@ private:
QUuid m_uuid;
QByteArray encodedPassword;
bool passwordChanged;
QLabel *lblGroup;
QComboBox *cmbbxGroup;
@ -77,6 +80,7 @@ private:
QFormLayout *formLayout;
void handleTestResult(TestConnectionResult result);
void passwordEdited(const QString &);
};
#endif // CONNECTIONCONFIGURATIONWIDGET_H

View file

@ -117,9 +117,10 @@ void ConnectionController::saveConnection(ConnectionConfigurationWidget &w)
{
auto cc = w.data();
auto grp = w.group();
if (w.savePassword()) {
if (w.savePassword())
encryptPassword(cc);
}
if (w.clearPassword())
cc.setEncodedPassword({});
m_connectionTreeModel->save(grp, cc);
}

View file

@ -82,7 +82,11 @@ R"__(INSERT OR REPLACE INTO connection
q.bindValue(":sslkey", cc.sslKey());
q.bindValue(":sslrootcert", cc.sslRootCert());
q.bindValue(":sslcrl", cc.sslCrl());
q.bindValue(":password", cc.encodedPassword());
auto& encodedPassword = cc.encodedPassword();
if (encodedPassword.isEmpty())
q.bindValue(":password", QVariant());
else
q.bindValue(":password", encodedPassword);
if (!q.exec()) {
auto sql_error = q.lastError();