-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
75 lines (64 loc) · 1.51 KB
/
user.cpp
File metadata and controls
75 lines (64 loc) · 1.51 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
#include "user.h"
#include "myLibType.h"
#include <map>
#include <vector>
using namespace std;
extern vector<Book> books;
extern map<string, User> users;
string loggingUser;
int Lib::createUser(std::string name, std::string account, std::string pwd, User::UserType type)
{
if(users.count(account))
return 1;
users[account] = User{name,account,BKDRHash(pwd.c_str()),type};
return 0;
}
bool Lib::exist_user(std::string account){
return users.count(account);
}
User::UserType Lib::login(std::string account, std::string pwd)
{
if(users.count(account) && users[account].pwd == BKDRHash(pwd.c_str()))
{
loggingUser = account;
return users[account].type;
}
return User::UserType::Error;
}
void Lib::modifyName(std::string new_name)
{
users[loggingUser].name = new_name;
}
void Lib::modifyPwd(std::string new_pwd)
{
users[loggingUser].pwd = BKDRHash(new_pwd.c_str());
}
void Lib::quit()
{
loggingUser.clear();
}
void Lib::delUser()
{
users.erase(loggingUser);
loggingUser.clear();
}
bool Lib::is_login()
{
return !loggingUser.empty();
}
const User Lib::currentUser()
{
if(is_login())
return users[loggingUser];
return User{};
}
unsigned int Lib::BKDRHash(const char *str)
{
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str)
{
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}