-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.cpp
171 lines (157 loc) · 4.95 KB
/
User.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// Created by Christo on 3/5/2019.
//
#pragma once
#include "User.h"
#include "globalFunction.h"
//--------------------Constructors-----------------------
/**
* \brief a Constructor used to create or retreive a user from the program files, If the username
* is not located in the files, it will store it.
* @param name is the username of the user.
* @param isregistered is a boolean used to specify if the user has registered before in the program.
*/
User::User(std::string name, bool isregistered) {
if(isregistered){
userName = name;
userFilePath =name+"_user";
password = getPassword(true);
}else{
std::cout<<"\nEnter your password:";
std::getline(std::cin,password);
userName=name;
//The userFilePath is set as the name of the user + _user
userFilePath = name+"_user";
userFile.open(userFilePath);
if(!userFile){
userFile.open(userFilePath, std::ios::out | std::ios::app);
userFile<<name+"_user"<<std::endl;
userFile<<password+"_password"<<std::endl;
userFile.close();
}
userFile.close();
}
refreshUserInfo();
}
/**
* \brief default User Constructor.
*/
User::User() {}
//-----------------Getters and Setters-----------------------
std::vector<std::string > User::getDatabases() {
std::vector<std::string> toRet;
refreshUserInfo();
for(std::string info : userInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_database")){
toRet.push_back(info);
}
}
return toRet;
}
/**
* \brief a method use to get the username of the user.
* @return a string with the username of the user.
*/
const std::string &User::getUserName() const {
return userName;
}
/**
* \brief method used to change the username.
* @param userName is a string that contains the new username.
*/
void User::setUserName(std::string &userName) {
User::userName = userName;
User::userFilePath = userName+"_user";
}
/**
* \brief method used to get the filepath to the file that stores the information
* of the user.
* @return
*/
const std::string &User::getUserFilePath() const {
return userFilePath;
}
/**
* \brief method used to change the path to the file that stores the information of the user.
* @param userFilePath is a string containing the new filepath.
*/
void User::setUserFilePath(const std::string &userFilePath) {
User::userFilePath = userFilePath;
}
/**
* \brief a method used to refresh the vector strings that
* contains the information stored in the user file.
*
*/
void User::refreshUserInfo() {
std::string userInfo;
userFile.open(userFilePath,std::ios::in);
if(userFile){
userInfoVector.clear();
while(userFile>>userInfo){
userInfoVector.push_back(userInfo);
}
}
userFile.close();
}
/**
* \brief method used to get the password stored in the file of the user.
* @param withoutSignature is a boolean that specifies if the password should have the signature
* "_password" or not.
* @return a string with the password of the user.
*/
std::string User::getPassword(bool withoutSignature) {
refreshUserInfo();
if(withoutSignature){
for(std::string info : userInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_password")){
return info.substr(0,(info.size()-9));
}
}
std::cout<<"\nThe user has no password";
}else{
for(std::string info : userInfoVector){
//std::cout<<info<<"\n";
if(endsWith(info,"_password")){
return info;
}
}
std::cout<<"\nThe user has no password";
}
}
/**
* \brief method used to check if the password given by the
* user matches the records of the user file.
* @param answer is a string with the password given by the user.
* @return a boolean that is true if the password matches with the records of the user.
*/
bool User::validatePassword(std::string answer) {
if((answer+"_password")==getPassword(false)){
return true;
}
return false;
}
/**
* \brief method used to delete a Database owned by the user.
* @param database is s string that contains the name of the file to delete.
*/
void User::deleteDatabase(std::shared_ptr<Database> database){
database->deleteAllTables();
std::string databaseDelete = database->getDatabaseFilePath();
userFile.open(userFilePath, std::ios::in);
std::ofstream temp;
temp.open("temp.txt");
std::string line;
while(userFile>>line){
if(line!=databaseDelete){
temp<<line<<std::endl;
}
}
userFile.close();
temp.close();
remove((userFilePath).c_str());
rename("temp.txt",(userFilePath).c_str());
std::remove((databaseDelete).c_str());
}