Browse Source

Merge branch '32-us21-1-user-deletion-server' into develop

deleteme command only
Tests come later
Jonas Pflanzer 4 years ago
parent
commit
97c8247130

+ 11 - 0
daemon/include/JsonCommander.h

@@ -62,6 +62,12 @@ private:
    */
   const std::string protocolVersion = "0.2";
 
+  /**
+   * Contains the name of the user which uses the current connection.
+   * Is set at checkLogin. Used for deleteUser.
+   */
+  std::string currentUser;
+
   /**
    * Map of all commands
    *
@@ -130,6 +136,11 @@ private:
    * Executes the head command
    */
   Response executeHead(const Json::Value &message);
+
+  /**
+   * Executes the deleteme command
+   */
+  Response executeDeleteMe(const Json::Value &message);
 };
 
 #endif

+ 1 - 1
daemon/include/UserManager.h

@@ -43,7 +43,7 @@ public:
    * @param user the name of the users
    * @param pw password in plain text
    */
-  static void deleteUser(const std::string &name, const std::string &pw);
+  static bool deleteUser(const std::string &name, const std::string &pw);
 
 private:
   /**

+ 26 - 1
daemon/src/JsonCommander.cpp

@@ -13,6 +13,7 @@ JsonCommander::JsonCommander(FileManager &fileManager)
   commandsMap["get"] = &JsonCommander::executeGet;
   commandsMap["getdata"] = &JsonCommander::executeGetdata;
   commandsMap["head"] = &JsonCommander::executeHead;
+  commandsMap["deleteme"] = &JsonCommander::executeDeleteMe;
 }
 
 JsonCommander::~JsonCommander() {}
@@ -403,6 +404,29 @@ JsonCommander::Response JsonCommander::executeHead(const Json::Value &message) {
   return response;
 }
 
+JsonCommander::Response
+JsonCommander::executeDeleteMe(const Json::Value &message) {
+  JsonCommander::Response response;
+  response.json["command"] = "deleteme";
+
+  if (!message["pass"].isString()) {
+    response.action = send;
+    response.json["accept"] = false;
+    response.json["error"] = "incorrect deleteme command request";
+  } else if (UserManager::deleteUser(currentUser, message["pass"].asString())) {
+    // success
+    response.action = closeAndSend;
+    response.json["accept"] = true;
+    response.json["error"] = "";
+  } else {
+    response.action = send;
+    response.json["accept"] = false;
+    response.json["error"] = "wrong password";
+  }
+
+  return response;
+}
+
 JsonCommander::Response JsonCommander::testVersion(const Json::Value &message) {
   JsonCommander::Response response;
   response.json["version"] = this->protocolVersion;
@@ -438,6 +462,7 @@ JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
     response.action = send;
     response.json["accept"] = true;
     response.json["error"] = "";
+    currentUser = message["user"].asString();
   } else if (!message["login"].asBool()) {
     // add user. Check if already exists before
     if (!UserManager::addUser(message["user"].asString(),
@@ -449,8 +474,8 @@ JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
       response.action = send;
       response.json["accept"] = true;
       response.json["error"] = "";
+      currentUser = message["user"].asString();
     }
-
   } else {
     // reject user
     response.action = closeAndSend;

+ 6 - 2
daemon/src/UserManager.cpp

@@ -45,16 +45,20 @@ bool UserManager::addUser(const std::string &name, const std::string &pw) {
   return true;
 }
 
-void UserManager::deleteUser(const std::string &name, const std::string &pw) {
+bool UserManager::deleteUser(const std::string &name, const std::string &pw) {
   // TODO check pw before delete
   std::map<std::string, std::string> user_map;
   readFromFile(user_map);
   auto it = user_map.find(name);
   if (it == user_map.end()) {
-    return;
+    return false;
+  }
+  if (it->second.compare(pw) != 0) {
+    return false;
   }
   user_map.erase(it);
   writeToFile(user_map);
+  return true;
 }
 
 // read content from file into given map