Browse Source

Fixing some config and user manager bugs

Jonas Pflanzer 4 years ago
parent
commit
a4c8d5e41b

+ 2 - 1
Daemon-Config-Reference.md → daemon/Daemon-Config-Reference.md

@@ -12,5 +12,6 @@ This file must be in the same directory as the binary.
 ```
 port=1234
 interface=lo
-userdatabase=userStorage.text
+userdatabase=user Storage.txt
+filedirectory=./files/
 ```

+ 1 - 1
daemon/include/FileManager.h

@@ -11,7 +11,7 @@
  */
 class FileManager {
 private:
-  const std::string fileDirectory = "./files/";
+  const std::string fileDirectory;
 
   /**
    * file stream for get command

+ 1 - 1
daemon/include/JsonCommander.h

@@ -19,7 +19,7 @@ public:
    * closeAndSend - send the json answer and close the socket
    * close        - close the socket
    */
-  enum Action { send, closeAndSend, close };
+  enum Action { close, send, closeAndSend };
 
   /**
    * Response for commands

+ 1 - 1
daemon/include/UserManager.h

@@ -49,7 +49,7 @@ private:
   /**
    * Name of the file which holds the user data
    */
-  static const std::string filename;
+  static std::string filename;
 
   /**
    * Read data from file and create map.

+ 2 - 1
daemon/src/Config.cpp

@@ -7,7 +7,8 @@ std::map<std::string, std::string> storage;
 bool Config::init(const std::string &filename) {
   std::ifstream ifile(filename);
   std::string line;
-  if (ifile) {
+
+  if (ifile.is_open()) {
     while (getline(ifile, line)) {
       std::stringstream ss(line);
       std::string segment;

+ 3 - 1
daemon/src/FileManager.cpp

@@ -1,6 +1,8 @@
 #include "../include/FileManager.h"
 
-FileManager::FileManager() {}
+#include "../include/Config.h"
+
+FileManager::FileManager() : fileDirectory(Config::getValue("filedirectory")) {}
 
 FileManager::~FileManager() {
   cancelPut();

+ 3 - 0
daemon/src/JsonCommander.cpp

@@ -335,6 +335,9 @@ JsonCommander::Response JsonCommander::checkLogin(const Json::Value &message) {
     response.action = closeAndSend;
     response.json["accept"] = false;
     response.json["error"] = "invalid login request";
+  } else if (message["cancel"].asBool()) {
+    response.action = close;
+
   } else if (message["login"].asBool() &&
              UserManager::isAllowed(message["user"].asString(),
                                     message["pass"].asString())) {

+ 14 - 9
daemon/src/UserManager.cpp

@@ -3,15 +3,20 @@
 // TODO passwords are stored and checked in plain text
 // TODO read userStorage file location from config
 
+// initialize static filename to empty string
+std::string UserManager::filename = "";
+
 void UserManager::init(const std::string &file) {
-  static std::string filename = file;
-  std::ifstream ifile(file);
-  if (!ifile) {
+  filename = file;
+  std::ifstream ifile(filename);
+  if (!ifile.is_open()) {
     // create new file if userStorage does not exist
-    std::ofstream file;
-    file.open("userStorage.txt");
-    file << "cat;tac\n";
-    file.close();
+    std::ofstream ofile;
+    ofile.open(filename);
+    ofile << "user;pass\n";
+    ofile.close();
+    std::cout << "Created \"" << filename << "\" and added the default user"
+              << std::endl;
   }
   ifile.close();
 }
@@ -53,7 +58,7 @@ void UserManager::deleteUser(const std::string &name, const std::string &pw) {
 
 // read content from file into given map
 void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
-  std::ifstream ifile("userStorage.txt");
+  std::ifstream ifile(filename);
   std::string line;
   while (getline(ifile, line)) {
     std::stringstream ss(line);
@@ -70,7 +75,7 @@ void UserManager::readFromFile(std::map<std::string, std::string> &user_map) {
 // write content from map to file
 void UserManager::writeToFile(std::map<std::string, std::string> &user_map) {
   std::ofstream file;
-  file.open("userStorage.txt");
+  file.open(filename);
   for (auto const &x : user_map) {
     file << x.first << ";" << x.second << std::endl;
   }

+ 4 - 1
daemon/src/main.cpp

@@ -10,7 +10,10 @@ using namespace std;
 
 int main(int argc, char *argv[]) {
   // load config int namespace
-  Config::init("config.txt");
+  if (!Config::init("config.txt")) {
+    cerr << "configuration could not be loaded properly" << endl;
+    exit(EXIT_FAILURE);
+  }
 
   const string interface = Config::getValue("interface");
 

+ 1 - 1
daemon/test/CMakeLists.txt

@@ -20,7 +20,7 @@ find_package(GTest REQUIRED)
 include_directories(${Boost_INCLUDE_DIR} ${JSONCPP_INCLUDEDIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR})
 
 # Add test cpp file
-add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp)
+add_executable(jsonCommanderTest test/JsonCommanderTest.cpp src/JsonCommander.cpp src/FileManager.cpp src/base64.cpp src/UserManager.cpp src/Config.cpp)
 target_link_libraries(jsonCommanderTest ${CMAKE_THREAD_LIBS_INIT} ${Boost_LIBRARIES} ${JSONCPP_LIBRARIES} ${GMOCK_LIBRARIES} ${GTEST_LIBRARY} ${GTEST_MAIN_LIBRARY})
 
 add_test(