forked from Yasna1998/stackoverflow-in-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
147 lines (129 loc) · 3.75 KB
/
User.cpp
File metadata and controls
147 lines (129 loc) · 3.75 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
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
#include <utility>
#include <sstream>
#include "User.h"
#include "Exceptions.h"
#include <iostream>
#include <vector>
#include "Content.h"
#include "Logger.h"
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();
}
void User::log_user(string username){
char llg[100];
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%d-%m-%Y %H:%M:%S",timeinfo);
std::string strr(buffer);
const char *usg = username.c_str();
const char *emg = this->email.c_str();
const char *utg = strr.c_str();
sprintf(llg,"%s %s %s",emg,usg,utg);
_Log(llg);
}
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);
}
void User::add_question(string body){
Content* cp = new Content(body, QUESTION);
contents.push_back(cp);
}
void User::add_answer(string body, Content &q){
Content* cp = new Content(body, ANSWER);
contents.push_back(cp);
cp->add_relation(ANSWER_TO, q);
}
void User::remove_content(Content &target) {
for (auto it = contents.begin(); it != contents.end();it++){
if (*it == &target){
contents.erase(it);
break;
}
}
delete ⌖
}
void User::make_content_dup(Content &source, Content &target) {
if (type == ADMIN){
ContentRelation* instance = new ContentRelation(&target, &source, DUPLICTE_OF);
source.relations.push_back(instance);
target.relations.push_back(instance);
}
else{
NotAdminForDuplicateEception ex;
throw ex;
}
}
void User::get_my_content(int ind) {
cout<<contents[ind]->body<<endl;
cout<<"visits: "<<contents[ind]->visits<<endl;
if (contents[ind]->type == QUESTION){
cout<<"------------------------answers--------------------------"<<endl;
contents[ind]->print_answers();
}
}
fstream& User::to_string(fstream &out){
out << this->email << " " << this->username << endl;
return out;
}
fstream& operator << (fstream &out,User a){
return a.to_string(out);
}