From 3db83f14feb212b3e6bbdb6a5fd33ef2d52ad974 Mon Sep 17 00:00:00 2001 From: bartfaik04 Date: Sat, 7 Jun 2025 14:33:00 +0200 Subject: [PATCH] Add users from configfile --- configs/ncsambawatcher.config.default | 3 ++- src/main.cpp | 2 ++ src/usermanager.h | 30 ++++++++++++++++++++++----- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/configs/ncsambawatcher.config.default b/configs/ncsambawatcher.config.default index 22c7d0e..ab252ea 100644 --- a/configs/ncsambawatcher.config.default +++ b/configs/ncsambawatcher.config.default @@ -1 +1,2 @@ -NEXTCLOUD_CONTAINER_NAME=nextcloud \ No newline at end of file +NEXTCLOUD_CONTAINER_NAME=nextcloud +NEXTCLOUD_USERS=username1 username2 username3 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 60e7691..1f29e15 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,6 +106,8 @@ void scannerThreadFunc() int main() { + manager.tryAddUsersFromConfig(cfm); + std::thread readingThread(readingThreadFunc); std::thread scannerThread(scannerThreadFunc); diff --git a/src/usermanager.h b/src/usermanager.h index 799c90b..4067ef7 100644 --- a/src/usermanager.h +++ b/src/usermanager.h @@ -7,6 +7,7 @@ #include #include #include +#include #include "definitions.h" #include "configfilemanager.h" @@ -25,28 +26,30 @@ public: addUser(splitString(line, '|').at(USER_LOG_LOCATION)); } - void addUser(std::string &user) + void addUser(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 0) { users[user] = false; + std::cout << "User added the list: " << user << std::endl; } } - void removeUser(std::string &user) + void removeUser(const std::string &user) { std::lock_guard lock(mtx); users.erase(user); + std::cout << "User removed the list: " << user << std::endl; } - bool isContains(std::string &user) + bool isContains(const std::string &user) { std::lock_guard lock(mtx); return users.count(user) == 1; } - void setUserFlagged(std::string &user) + void setUserFlagged(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 1) @@ -55,7 +58,7 @@ public: } } - void setUserUnflagged(std::string &user) + void setUserUnflagged(const std::string &user) { std::lock_guard lock(mtx); if (users.count(user) == 1) @@ -116,6 +119,23 @@ public: return false; } + + void tryAddUsersFromConfig(configfilemanager &cfm) + { + try + { + std::vector alluser = splitString(cfm.at("NEXTCLOUD_USERS"), ' '); + + for (const std::string& user : alluser) + { + addUser(user); + } + } + catch (std::exception e) + { + std::cerr << "No user added from configuration file" << std::endl; + } + } }; #endif // _USERMAN_H \ No newline at end of file