|
@@ -0,0 +1,75 @@
|
|
|
+#include "../include/user_manager.h"
|
|
|
+
|
|
|
+//TODO passwords are stored and checked in plain text
|
|
|
+
|
|
|
+void UserManager::init() {
|
|
|
+ std::ifstream ifile("userStorage.txt");
|
|
|
+ if(!ifile) {
|
|
|
+ std::ofstream file;
|
|
|
+ file.open("userStorage.txt");
|
|
|
+ file << "cat;tac\n";
|
|
|
+ file.close();
|
|
|
+ }
|
|
|
+ ifile.close();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool UserManager::isAllowed(std::string name, std::string pw) {
|
|
|
+ std::map<std::string,std::string> user_map;
|
|
|
+ readFromFile(&user_map);
|
|
|
+ auto it = user_map.find(name);
|
|
|
+ if(it!=user_map.end() && (it->second.compare(pw)==0)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void UserManager::addUser(std::string name, std::string pw) {
|
|
|
+ std::map<std::string,std::string> user_map;
|
|
|
+ readFromFile(&user_map);
|
|
|
+ auto it = user_map.find(name);
|
|
|
+ if(it!=user_map.end()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ user_map.insert(std::pair<std::string,std::string>(name,pw));
|
|
|
+ writeToFile(&user_map);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void UserManager::deleteUser(std::string name, std::string pw) {
|
|
|
+ std::map<std::string,std::string> user_map;
|
|
|
+ readFromFile(&user_map);
|
|
|
+ auto it = user_map.find(name);
|
|
|
+ if(it==user_map.end()) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ user_map.erase(it);
|
|
|
+ writeToFile(&user_map);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void UserManager::readFromFile(std::map<std::string,std::string> *user_map) {
|
|
|
+ std::ifstream ifile("userStorage.txt");
|
|
|
+ std::string line;
|
|
|
+ while(getline(ifile,line)) {
|
|
|
+ std::stringstream ss(line);
|
|
|
+ std::string segment;
|
|
|
+ std::vector<std::string> v;
|
|
|
+ while(std::getline(ss, segment, ';')) {
|
|
|
+ v.push_back(segment);
|
|
|
+ }
|
|
|
+ user_map->insert(std::pair<std::string,std::string>(v.at(0),v.at(1)));
|
|
|
+ }
|
|
|
+ ifile.close();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+void UserManager::writeToFile(std::map<std::string,std::string> *user_map) {
|
|
|
+ std::ofstream file;
|
|
|
+ file.open("userStorage.txt");
|
|
|
+ for(auto const& x : *user_map) {
|
|
|
+ file << x.first << ";" << x.second << std::endl;
|
|
|
+ }
|
|
|
+ file.close();
|
|
|
+}
|