|
@@ -1,10 +1,12 @@
|
|
#include "../include/user_manager.h"
|
|
#include "../include/user_manager.h"
|
|
|
|
|
|
//TODO passwords are stored and checked in plain text
|
|
//TODO passwords are stored and checked in plain text
|
|
|
|
+//TODO read userStorage file location from config
|
|
|
|
|
|
void UserManager::init() {
|
|
void UserManager::init() {
|
|
std::ifstream ifile("userStorage.txt");
|
|
std::ifstream ifile("userStorage.txt");
|
|
if(!ifile) {
|
|
if(!ifile) {
|
|
|
|
+ // create new file if userStorage does not exist
|
|
std::ofstream file;
|
|
std::ofstream file;
|
|
file.open("userStorage.txt");
|
|
file.open("userStorage.txt");
|
|
file << "cat;tac\n";
|
|
file << "cat;tac\n";
|
|
@@ -18,6 +20,7 @@ bool UserManager::isAllowed(std::string name, std::string pw) {
|
|
std::map<std::string,std::string> user_map;
|
|
std::map<std::string,std::string> user_map;
|
|
readFromFile(&user_map);
|
|
readFromFile(&user_map);
|
|
auto it = user_map.find(name);
|
|
auto it = user_map.find(name);
|
|
|
|
+ // check if user exists and pw is equal
|
|
if(it!=user_map.end() && (it->second.compare(pw)==0)) {
|
|
if(it!=user_map.end() && (it->second.compare(pw)==0)) {
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
@@ -29,6 +32,7 @@ void UserManager::addUser(std::string name, std::string pw) {
|
|
std::map<std::string,std::string> user_map;
|
|
std::map<std::string,std::string> user_map;
|
|
readFromFile(&user_map);
|
|
readFromFile(&user_map);
|
|
auto it = user_map.find(name);
|
|
auto it = user_map.find(name);
|
|
|
|
+ // if user exists, do nothing
|
|
if(it!=user_map.end()) {
|
|
if(it!=user_map.end()) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -38,6 +42,7 @@ void UserManager::addUser(std::string name, std::string pw) {
|
|
|
|
|
|
|
|
|
|
void UserManager::deleteUser(std::string name, std::string pw) {
|
|
void UserManager::deleteUser(std::string name, std::string pw) {
|
|
|
|
+ // TODO check pw before delete
|
|
std::map<std::string,std::string> user_map;
|
|
std::map<std::string,std::string> user_map;
|
|
readFromFile(&user_map);
|
|
readFromFile(&user_map);
|
|
auto it = user_map.find(name);
|
|
auto it = user_map.find(name);
|
|
@@ -48,7 +53,7 @@ void UserManager::deleteUser(std::string name, std::string pw) {
|
|
writeToFile(&user_map);
|
|
writeToFile(&user_map);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+// read content from file into given map
|
|
void UserManager::readFromFile(std::map<std::string,std::string> *user_map) {
|
|
void UserManager::readFromFile(std::map<std::string,std::string> *user_map) {
|
|
std::ifstream ifile("userStorage.txt");
|
|
std::ifstream ifile("userStorage.txt");
|
|
std::string line;
|
|
std::string line;
|
|
@@ -64,7 +69,7 @@ void UserManager::readFromFile(std::map<std::string,std::string> *user_map) {
|
|
ifile.close();
|
|
ifile.close();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
|
|
+// write content from map to file
|
|
void UserManager::writeToFile(std::map<std::string,std::string> *user_map) {
|
|
void UserManager::writeToFile(std::map<std::string,std::string> *user_map) {
|
|
std::ofstream file;
|
|
std::ofstream file;
|
|
file.open("userStorage.txt");
|
|
file.open("userStorage.txt");
|