-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabaseUser.cpp
58 lines (43 loc) · 1.46 KB
/
DatabaseUser.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
#include "DatabaseUser.h"
//protected-------------------------------------------
void DatabaseUser::setUsername(const string& username){
if(isEmpty(username)){
throw std::invalid_argument(USERNAME_EMPTY_ERR);
}
if(username.find(" ") != string::npos){
throw std::invalid_argument(USERNAME_CONTAINS_SPACE_ERR);
}
this->username = username;
}
void DatabaseUser::setPassword(const string& password){
if(isEmpty(password)){
throw std::invalid_argument(PASS_EMPTY_ERR);
}
if(password.find(" ") != string::npos){
throw std::invalid_argument(PASS_CONTAINS_SPACE_ERR);
}
this->password = BCrypt::generateHash(password);
}
DatabaseUser::DatabaseUser(const string& username, const string& password){
setUsername(username);
setPassword(password);
}
//public--------------------------------------------
string DatabaseUser::getUsername() const{
return username;
}
bool DatabaseUser::validatePassword(const string& password) const{
return BCrypt::validatePassword(password, this->password);
}
void DatabaseUser::serialize(std::ofstream& fout) const {
const char* separator = " ";
fout << username << separator << password;
}
void DatabaseUser::deserialize(std::istream& fin){
fin >> username >> password;
}
std::ostream& operator<<(std::ostream& out, const DatabaseUser& user){
out << "Username: " << user.username << std::endl;
out << "Password: " << user.password;
return out;
}