adding --change-userpass=<username> option to the core
authorMarcus Eggenberger <egs@quassel-irc.org>
Thu, 12 Mar 2009 17:35:58 +0000 (18:35 +0100)
committerMarcus Eggenberger <egs@quassel-irc.org>
Thu, 12 Mar 2009 17:35:58 +0000 (18:35 +0100)
src/common/main.cpp
src/core/core.cpp
src/core/core.h
src/core/postgresqlstorage.cpp
src/core/postgresqlstorage.h
src/core/sqlitestorage.cpp
src/core/sqlitestorage.h
src/core/storage.h

index 21d066e..97e7100 100644 (file)
@@ -101,6 +101,7 @@ int main(int argc, char **argv) {
   cliParser->addOption("loglevel <level>", 'L', "Loglevel Debug|Info|Warning|Error", "Info");
   cliParser->addOption("select-backend <backendidentifier>", 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("loglevel <level>", 'L', "Loglevel Debug|Info|Warning|Error", "Info");
   cliParser->addOption("select-backend <backendidentifier>", 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 <username>", 0, "Starts an interactive session to change the password of the user identified by username");
 #endif
 
 #ifdef HAVE_KDE
 #endif
 
 #ifdef HAVE_KDE
index 542d425..003f6da 100644 (file)
@@ -151,11 +151,6 @@ void Core::init() {
     exit(0);
   }
 
     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..."));
   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.";
   }
 
     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
   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;
 AbstractSqlMigrationReader *Core::getMigrationReader(Storage *storage) {
   if(!storage)
     return 0;
index d2e698c..fd67fa8 100644 (file)
@@ -429,6 +429,7 @@ private:
   void unregisterStorageBackend(Storage *);
   bool selectBackend(const QString &backend);
   void createUser();
   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);
 
   void saveBackendSettings(const QString &backend, const QVariantMap &settings);
   QVariantMap promptForSettings(const Storage *storage);
 
index d65b4ee..bd1e089 100644 (file)
@@ -151,12 +151,13 @@ UserId PostgreSqlStorage::addUser(const QString &user, const QString &password)
   return uid;
 }
 
   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);
   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) {
 }
 
 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"));
 UserId PostgreSqlStorage::internalUser() {
   QSqlQuery query(logDb());
   query.prepare(queryString("select_internaluser"));
index 5729ac9..de196cd 100644 (file)
@@ -49,9 +49,10 @@ public slots:
   /* User handling */
 
   virtual UserId addUser(const QString &user, const QString &password);
   /* 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 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);
   virtual UserId internalUser();
   virtual void delUser(UserId user);
   virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
index 19710ed..fcc52ad 100644 (file)
@@ -120,7 +120,7 @@ UserId SqliteStorage::addUser(const QString &user, const QString &password) {
   return uid;
 }
 
   return uid;
 }
 
-void SqliteStorage::updateUser(UserId user, const QString &password) {
+bool SqliteStorage::updateUser(UserId user, const QString &password) {
   QSqlDatabase db = logDb();
   db.transaction();
 
   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);
   query.bindValue(":password", cryptedPassword(password));
   lockForWrite();
   safeExec(query);
+  bool success = query.numRowsAffected() != 0;
   db.commit();
   unlock();
   db.commit();
   unlock();
+  return success;
 }
 
 void SqliteStorage::renameUser(UserId user, const QString &newName) {
 }
 
 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"));
 UserId SqliteStorage::internalUser() {
   QSqlQuery query(logDb());
   query.prepare(queryString("select_internaluser"));
index 92654a6..2518250 100644 (file)
@@ -48,11 +48,11 @@ public slots:
   // TODO: Add functions for configuring the backlog handling, i.e. defining auto-cleanup settings etc
 
   /* User handling */
   // 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 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 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);
   virtual UserId internalUser();
   virtual void delUser(UserId user);
   virtual void setUserSetting(UserId userId, const QString &settingName, const QVariant &data);
index f7495f4..b8f5c23 100644 (file)
@@ -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
   //! 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
 
   //! 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;
 
    */
   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
    */
   //! Determine the UserId of the internal user
   /** \return A valid UserId if the password matches the username; 0 else
    */