From b040ef84cdc254a0b1f083db3151f2724e45d210 Mon Sep 17 00:00:00 2001 From: Marcus Eggenberger Date: Thu, 12 Mar 2009 18:35:58 +0100 Subject: [PATCH] adding --change-userpass= option to the core --- src/common/main.cpp | 1 + src/core/core.cpp | 53 ++++++++++++++++++++++++++++++---- src/core/core.h | 1 + src/core/postgresqlstorage.cpp | 16 +++++++++- src/core/postgresqlstorage.h | 3 +- src/core/sqlitestorage.cpp | 21 +++++++++++++- src/core/sqlitestorage.h | 4 +-- src/core/storage.h | 9 +++++- 8 files changed, 97 insertions(+), 11 deletions(-) diff --git a/src/common/main.cpp b/src/common/main.cpp index 21d066ef..97e71004 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -101,6 +101,7 @@ int main(int argc, char **argv) { cliParser->addOption("loglevel ", 'L', "Loglevel Debug|Info|Warning|Error", "Info"); cliParser->addOption("select-backend ", 0, "Starts an interactive session and switches your current storage backend to the new one. Attempts a merge if the new backend is uninitialized and the old backend supports migration. Otherwise prompts for new user credentials!"); cliParser->addSwitch("add-user", 0, "Starts an interactive session to add a new core user"); + cliParser->addOption("change-userpass ", 0, "Starts an interactive session to change the password of the user identified by username"); #endif #ifdef HAVE_KDE diff --git a/src/core/core.cpp b/src/core/core.cpp index 542d4250..003f6daf 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -151,11 +151,6 @@ void Core::init() { exit(0); } - if(Quassel::isOptionSet("add-user")) { - createUser(); - exit(0); - } - if(!_configured) { if(!_storageBackends.count()) { qWarning() << qPrintable(tr("Could not initialize any storage backend! Exiting...")); @@ -167,6 +162,16 @@ void Core::init() { qWarning() << "Core is currently not configured! Please connect with a Quassel Client for basic setup."; } + if(Quassel::isOptionSet("add-user")) { + createUser(); + exit(0); + } + + if(Quassel::isOptionSet("change-userpass")) { + changeUserPass(Quassel::optionValue("change-userpass")); + exit(0); + } + connect(&_server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); connect(&_v6server, SIGNAL(newConnection()), this, SLOT(incomingConnection())); if(!startListening()) exit(1); // TODO make this less brutal @@ -808,6 +813,44 @@ void Core::createUser() { } } +void Core::changeUserPass(const QString &username) { + QTextStream out(stdout); + QTextStream in(stdin); + UserId userId = _storage->getUserId(username); + if(!userId.isValid()) { + out << "User " << username << " does not exist." << endl; + return; + } + + out << "Change password for user: " << username << endl; + + disableStdInEcho(); + out << "New Password: "; + out.flush(); + QString password = in.readLine().trimmed(); + out << endl; + out << "Repeat Password: "; + out.flush(); + QString password2 = in.readLine().trimmed(); + out << endl; + enableStdInEcho(); + + if(password != password2) { + qWarning() << "Passwords don't match!"; + return; + } + if(password.isEmpty()) { + qWarning() << "Password is empty!"; + return; + } + + if(_storage->updateUser(userId, password)) { + out << "Password changed successfuly!" << endl; + } else { + qWarning() << "Failed to change password!"; + } +} + AbstractSqlMigrationReader *Core::getMigrationReader(Storage *storage) { if(!storage) return 0; diff --git a/src/core/core.h b/src/core/core.h index d2e698c7..fd67fa8a 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -429,6 +429,7 @@ private: void unregisterStorageBackend(Storage *); bool selectBackend(const QString &backend); void createUser(); + void changeUserPass(const QString &username); void saveBackendSettings(const QString &backend, const QVariantMap &settings); QVariantMap promptForSettings(const Storage *storage); diff --git a/src/core/postgresqlstorage.cpp b/src/core/postgresqlstorage.cpp index d65b4ee0..bd1e089e 100644 --- a/src/core/postgresqlstorage.cpp +++ b/src/core/postgresqlstorage.cpp @@ -151,12 +151,13 @@ UserId PostgreSqlStorage::addUser(const QString &user, const QString &password) return uid; } -void PostgreSqlStorage::updateUser(UserId user, const QString &password) { +bool PostgreSqlStorage::updateUser(UserId user, const QString &password) { QSqlQuery query(logDb()); query.prepare(queryString("update_userpassword")); query.bindValue(":userid", user.toInt()); query.bindValue(":password", cryptedPassword(password)); safeExec(query); + return query.numRowsAffected() != 0; } void PostgreSqlStorage::renameUser(UserId user, const QString &newName) { @@ -182,6 +183,19 @@ UserId PostgreSqlStorage::validateUser(const QString &user, const QString &passw } } +UserId PostgreSqlStorage::getUserId(const QString &user) { + QSqlQuery query(logDb()); + query.prepare(queryString("select_userid")); + query.bindValue(":username", user); + safeExec(query); + + if(query.first()) { + return query.value(0).toInt(); + } else { + return 0; + } +} + UserId PostgreSqlStorage::internalUser() { QSqlQuery query(logDb()); query.prepare(queryString("select_internaluser")); diff --git a/src/core/postgresqlstorage.h b/src/core/postgresqlstorage.h index 5729ac95..de196cd6 100644 --- a/src/core/postgresqlstorage.h +++ b/src/core/postgresqlstorage.h @@ -49,9 +49,10 @@ public slots: /* User handling */ virtual UserId addUser(const QString &user, const QString &password); - virtual void updateUser(UserId user, const QString &password); + virtual bool updateUser(UserId user, const QString &password); virtual void renameUser(UserId user, const QString &newName); virtual UserId validateUser(const QString &user, const QString &password); + virtual UserId getUserId(const QString &username); virtual UserId internalUser(); virtual void delUser(UserId user); virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data); diff --git a/src/core/sqlitestorage.cpp b/src/core/sqlitestorage.cpp index 19710edf..fcc52adf 100644 --- a/src/core/sqlitestorage.cpp +++ b/src/core/sqlitestorage.cpp @@ -120,7 +120,7 @@ UserId SqliteStorage::addUser(const QString &user, const QString &password) { return uid; } -void SqliteStorage::updateUser(UserId user, const QString &password) { +bool SqliteStorage::updateUser(UserId user, const QString &password) { QSqlDatabase db = logDb(); db.transaction(); @@ -130,8 +130,10 @@ void SqliteStorage::updateUser(UserId user, const QString &password) { query.bindValue(":password", cryptedPassword(password)); lockForWrite(); safeExec(query); + bool success = query.numRowsAffected() != 0; db.commit(); unlock(); + return success; } void SqliteStorage::renameUser(UserId user, const QString &newName) { @@ -167,6 +169,23 @@ UserId SqliteStorage::validateUser(const QString &user, const QString &password) } } +UserId SqliteStorage::getUserId(const QString &username) { + QSqlQuery query(logDb()); + query.prepare(queryString("select_userid")); + query.bindValue(":username", username); + + lockForRead(); + safeExec(query); + + if(query.first()) { + unlock(); + return query.value(0).toInt(); + } else { + unlock(); + return 0; + } +} + UserId SqliteStorage::internalUser() { QSqlQuery query(logDb()); query.prepare(queryString("select_internaluser")); diff --git a/src/core/sqlitestorage.h b/src/core/sqlitestorage.h index 92654a61..25182507 100644 --- a/src/core/sqlitestorage.h +++ b/src/core/sqlitestorage.h @@ -48,11 +48,11 @@ public slots: // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc /* User handling */ - virtual UserId addUser(const QString &user, const QString &password); - virtual void updateUser(UserId user, const QString &password); + virtual bool updateUser(UserId user, const QString &password); virtual void renameUser(UserId user, const QString &newName); virtual UserId validateUser(const QString &user, const QString &password); + virtual UserId getUserId(const QString &username); virtual UserId internalUser(); virtual void delUser(UserId user); virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data); diff --git a/src/core/storage.h b/src/core/storage.h index f7495f45..b8f5c232 100644 --- a/src/core/storage.h +++ b/src/core/storage.h @@ -101,8 +101,9 @@ public slots: //! Update a core user's password. /** \param user The user's id * \param password The user's new password + * \return true on success. */ - virtual void updateUser(UserId user, const QString &password) = 0; + virtual bool updateUser(UserId user, const QString &password) = 0; //! Rename a user /** \param user The user's id @@ -117,6 +118,12 @@ public slots: */ virtual UserId validateUser(const QString &user, const QString &password) = 0; + //! Check if a user with given username exists. Do not use for login purposes! + /** \param username The username to validate + * \return A valid UserId if the user exists; 0 else + */ + virtual UserId getUserId(const QString &username) = 0; + //! Determine the UserId of the internal user /** \return A valid UserId if the password matches the username; 0 else */ -- 2.20.1