Переглянути джерело

Merge branch 'us21-2-user-deletion-cli' into develop

Missingmew 5 роки тому
батько
коміт
8061b69f53
4 змінених файлів з 55 додано та 2 видалено
  1. 4 0
      cli/include/cmdman.h
  2. 1 0
      cli/include/userioman.h
  3. 40 2
      cli/src/cmdman.cpp
  4. 10 0
      cli/src/userioman.cpp

+ 4 - 0
cli/include/cmdman.h

@@ -123,6 +123,9 @@ private:
   CmdRet cmdLogin(vector<string> args);
   const string descSignup = "sign up and login to the server";
   CmdRet cmdSignup(vector<string> args);
+  const string descDeleteme = "delete the user you are currently logged in as "
+                              "(needs to be confirmed with the password)";
+  CmdRet cmdDeleteme(vector<string> args);
 
   /**
    * Method prototypes for commands used internally
@@ -149,6 +152,7 @@ private:
   CmdRet handleLogin(Json::Value);
   CmdRet handleSignup(Json::Value);
   CmdRet handleHead(Json::Value);
+  CmdRet handleDeleteme(Json::Value);
 };
 
 #endif

+ 1 - 0
cli/include/userioman.h

@@ -48,6 +48,7 @@ private:
   void printGetdata(Json::Value root);
   void printListdata(Json::Value root);
   void printHead(Json::Value root);
+  void printDeleteme(Json::Value root);
 
   /**
    * Mutex for synchronized message output

+ 40 - 2
cli/src/cmdman.cpp

@@ -33,6 +33,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   execmap["getdata"] = &CmdMan::cmdGetdata;
   execmap["listdata"] = &CmdMan::cmdListdata;
   execmap["head"] = &CmdMan::cmdHead;
+  execmap["deleteme"] = &CmdMan::cmdDeleteme;
 
   /* initialize description map */
   helpmap["help"] = descHelp;
@@ -44,6 +45,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   helpmap["head"] = descHead;
   helpmap["login"] = descLogin;
   helpmap["signup"] = descSignup;
+  helpmap["deleteme"] = descDeleteme;
 
   /* initialize handle command map */
   handlemap["status"] = &CmdMan::handleStatus;
@@ -58,6 +60,7 @@ CmdMan::CmdMan(FileMan &fm, void (*dpf)(string)) : fileman(fm) {
   handlemap["signup"] = &CmdMan::handleSignup;
   handlemap["listdata"] = &CmdMan::handleListdata;
   handlemap["head"] = &CmdMan::handleHead;
+  handlemap["deleteme"] = &CmdMan::handleDeleteme;
 
   debugprintfunc = dpf;
 }
@@ -289,6 +292,26 @@ CmdMan::CmdRet CmdMan::execute(string cmd, vector<string> args) {
   return (this->*(execmap[cmd]))(args);
 }
 
+CmdMan::CmdRet CmdMan::cmdDeleteme(vector<string> args) {
+  CmdRet retval;
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+  Json::Value root;
+
+  root["command"] = "deleteme";
+
+  if (args.size() < 1) {
+    retval.type = error;
+    root["accept"] = false;
+    root["error"] = "not enough arguments, at least 1 argument required";
+  } else {
+    retval.type = send;
+    root["pass"] = args[0];
+  }
+  retval.msg = root;
+
+  return retval;
+}
+
 /* login and signup commands */
 CmdMan::CmdRet CmdMan::cmdLogin(vector<string> args) {
   CmdRet retval;
@@ -408,9 +431,12 @@ CmdMan::CmdRet CmdMan::handleStatus(Json::Value root) {
 
 CmdMan::CmdRet CmdMan::handleClose(Json::Value root) {
   CmdRet retval;
+  Json::Value output;
   DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
-  retval.type = close;
-  retval.msg = root;
+  output["command"] = "disconnect";
+  output["accept"] = true;
+  retval.type = close | print;
+  retval.msg = output;
 
   return retval;
 }
@@ -716,6 +742,18 @@ CmdMan::CmdRet CmdMan::handleHead(Json::Value root) {
     retval.type = print;
     retval.msg = root;
   }
+  return retval;
+}
 
+CmdMan::CmdRet CmdMan::handleDeleteme(Json::Value root) {
+  CmdRet retval;
+  DEBUGPRINT(string(__PRETTY_FUNCTION__) + " begin");
+
+  if (!root["accept"].asBool()) {
+    retval.type = error;
+  } else {
+    retval.type = close | print;
+  }
+  retval.msg = root;
   return retval;
 }

+ 10 - 0
cli/src/userioman.cpp

@@ -32,6 +32,7 @@ UserIoMan::UserIoMan(char *ipcstring) : IoMan(ipcstring) {
   printmap["putdata"] = &UserIoMan::printPutdata;
   printmap["getdata"] = &UserIoMan::printGetdata;
   printmap["head"] = &UserIoMan::printHead;
+  printmap["deleteme"] = &UserIoMan::printDeleteme;
 }
 
 UserIoMan::~UserIoMan() { delete reader; }
@@ -186,6 +187,15 @@ void UserIoMan::printSignup(Json::Value root) {
     std::cout << "Signup ok. You are now logged in." << std::endl;
 }
 
+void UserIoMan::printDeleteme(Json::Value root) {
+  if (!root["accept"].asBool()) {
+    std::cout << "User deletion failed: " << root["error"].asString()
+              << std::endl;
+  } else
+    std::cout << "User deletion ok. You are now disconnected from the server."
+              << std::endl;
+}
+
 void UserIoMan::printPutdata(Json::Value root) {}
 
 void UserIoMan::printGetdata(Json::Value root) {}