From da19c46d5eba53a1d867f80923cb95cfae4c0e35 Mon Sep 17 00:00:00 2001 From: eelke Date: Mon, 5 Sep 2022 14:33:51 +0200 Subject: [PATCH] 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. --- pglab/ConnectionConfigurationWidget.cpp | 20 ++++++++++++++++++- pglab/ConnectionConfigurationWidget.h | 4 ++++ pglab/ConnectionController.cpp | 5 +++-- pglab/ConnectionListModel.cpp | 6 +++++- pglablib/ConnectionConfig.cpp | 2 +- pglablib/ConnectionConfig.h | 2 +- ...oved-password-saving-cc6b3d633d1e7c3b.yaml | 6 ++++++ 7 files changed, 39 insertions(+), 6 deletions(-) create mode 100644 releasenotes/notes/improved-password-saving-cc6b3d633d1e7c3b.yaml diff --git a/pglab/ConnectionConfigurationWidget.cpp b/pglab/ConnectionConfigurationWidget.cpp index 34dc744..8690359 100644 --- a/pglab/ConnectionConfigurationWidget.cpp +++ b/pglab/ConnectionConfigurationWidget.cpp @@ -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; +} + diff --git a/pglab/ConnectionConfigurationWidget.h b/pglab/ConnectionConfigurationWidget.h index a67fbf4..2f38102 100644 --- a/pglab/ConnectionConfigurationWidget.h +++ b/pglab/ConnectionConfigurationWidget.h @@ -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 diff --git a/pglab/ConnectionController.cpp b/pglab/ConnectionController.cpp index 0f00e9b..f8815dd 100644 --- a/pglab/ConnectionController.cpp +++ b/pglab/ConnectionController.cpp @@ -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); } diff --git a/pglab/ConnectionListModel.cpp b/pglab/ConnectionListModel.cpp index ebec31d..1b8c892 100644 --- a/pglab/ConnectionListModel.cpp +++ b/pglab/ConnectionListModel.cpp @@ -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(); diff --git a/pglablib/ConnectionConfig.cpp b/pglablib/ConnectionConfig.cpp index 7356108..f324c24 100644 --- a/pglablib/ConnectionConfig.cpp +++ b/pglablib/ConnectionConfig.cpp @@ -272,7 +272,7 @@ QString ConnectionConfig::makeLongDescription() const return result; } -QByteArray ConnectionConfig::encodedPassword() const +const QByteArray& ConnectionConfig::encodedPassword() const { return m_encodedPassword; } diff --git a/pglablib/ConnectionConfig.h b/pglablib/ConnectionConfig.h index ff8c1ff..390202a 100644 --- a/pglablib/ConnectionConfig.h +++ b/pglablib/ConnectionConfig.h @@ -119,7 +119,7 @@ public: bool operator==(QUuid id) const { return m_uuid == id; } QString makeLongDescription() const; - QByteArray encodedPassword() const; + const QByteArray& encodedPassword() const; void setEncodedPassword(const QByteArray &encodedPassword); // void write(QDataStream &out) const; diff --git a/releasenotes/notes/improved-password-saving-cc6b3d633d1e7c3b.yaml b/releasenotes/notes/improved-password-saving-cc6b3d633d1e7c3b.yaml new file mode 100644 index 0000000..16de39e --- /dev/null +++ b/releasenotes/notes/improved-password-saving-cc6b3d633d1e7c3b.yaml @@ -0,0 +1,6 @@ +--- +other: + - | + The way in which editing the password of an existing connection works has been changed to + be consistent with creating a new connection and to allow for clearing the saved password + by unselecting save password. \ No newline at end of file