Browse Source

implemented deleteme command

marius.rescheleit 4 years ago
parent
commit
1de7faab9f

+ 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
    *
@@ -120,6 +126,11 @@ private:
    * Executes the getdata command
    */
   Response executeGetdata(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 - 0
daemon/src/JsonCommander.cpp

@@ -11,6 +11,7 @@ JsonCommander::JsonCommander(FileManager &fileManager)
   commandsMap["putdata"] = &JsonCommander::executePutdata;
   commandsMap["get"] = &JsonCommander::executeGet;
   commandsMap["getdata"] = &JsonCommander::executeGetdata;
+  commandsMap["deleteme"] = &JsonCommander::executeDeleteMe;
 }
 
 JsonCommander::~JsonCommander() {}
@@ -310,6 +311,29 @@ JsonCommander::executeGetdata(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;
@@ -345,11 +369,13 @@ 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()) {
     // TODO implement registration
     response.action = closeAndSend;
     response.json["accept"] = false;
     response.json["error"] = "registration is not yet implemented";
+    currentUser = message["user"].asString();
   } else {
     response.action = closeAndSend;
     response.json["accept"] = false;

+ 6 - 2
daemon/src/UserManager.cpp

@@ -44,16 +44,20 @@ void UserManager::addUser(const std::string &name, const std::string &pw) {
   writeToFile(user_map);
 }
 
-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