-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathUser.cpp
More file actions
83 lines (70 loc) · 1.97 KB
/
User.cpp
File metadata and controls
83 lines (70 loc) · 1.97 KB
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
#include <utility>
//
// Created by spsina on 11/8/18.
//
#include <sstream>
#include "User.h"
#include "Exceptions.h"
#include <iostream>
vector<User> User::users;
string User::salt;
User::User(string username, string password, string email, UserType type){
lower(username);
this->username = username;
set_password(std::move(password));
this->email = email;
this->type = type;
}
void User::set_password(string password){
size_t ps = pass_hash(password + salt);
stringstream out;
out << ps;
this->password = out.str();
}
bool User::check_password(string password){
size_t check = pass_hash(password + salt);
stringstream out;
out << check;
return (this->password == out.str());
}
bool User::authenticate(string username, string password){
lower(username);
return this->username == username and check_password(password);
}
void User::deleteAccount(){
if (this->type == UserType::ADMIN) {
throw DeleteAdminException();
}
for (auto user = users.begin(); user != users.end();user++){
if ( user->username == this->username ) {
users.erase(user);
break;
}
}
}
User& User::login(string username, string password){
for (auto &user : users) {
if(user.authenticate(username, password)) {
return user;
}
}
throw WrongUsernameOrPasswordException();
}
User& User::signup(string username, string password, string email){
for (auto &user : users) {
if (user.username == username) {
throw UsernameAlreadyExistsException();
}
else if (user.email == email) {
throw EmailAlreadyExistsException();
}
}
//Create user
users.emplace_back(username, password, email, UserType::MEMBER);
return users[users.size() - 1];
}
void User::init(const string &salt) {
User::salt = salt;
users.reserve(20);
users.emplace_back("admin", "admin", "admin@stackoverflow.com", UserType::ADMIN);
}