-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAccount.py
145 lines (117 loc) · 3.53 KB
/
Account.py
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
"""
Account Management
TODO: change password and add userName
"""
import DB
import bcrypt
def createAcc(email, psd, name, techinterest):
"""
For OU
:param email:
:param psd:
:return: false: user already exists, ask if forget password or change password
true: user added into database
"""
if DB.checkUserExists(email):
return False
else:
"""password encoded"""
hashedPSD = bcrypt.hashpw(psd.encode('utf8'), bcrypt.gensalt())
print(type(hashedPSD))
DB.addUser(email, hashedPSD.hex(), '1', name, techinterest) # 1 for OU
return True
def logIn(email, psd):
"""
For both SU and OU
:param email: login email
:param psd: plain text password to be checked
:return: True: login successes
False: login failed
IF USER NOT EXISTS, return string
-1: user not exists
2: already logged in
"""
if DB.checkUserExists(email):
if not DB.checkLoginStat(email):
"""
if account exists and not logged in, proceed login
"""
if not (psd == "" or DB.currentUserGroup(email) == "2"):
"""
If the password string is not empty and is not a GU, proceed psd check
"""
hashed = bytes.fromhex(DB.loginPassword(email))
if bcrypt.checkpw(psd.encode('utf8'), hashed):
DB.changeLoginStat(email, True)
return 1
else:
return 0
else:
DB.changeLoginStat(email, True)
return 3
else:
return 2
else:
return -1
def logOut(email):
"""
For both SU and OU
:param email:
:return:
"""
if DB.checkLoginStat(email):
DB.changeLoginStat(email, False)
return True
else:
return False
def promoteToSU(email):
"""
If current database doesn't contain a super user, promote the current logged
in user to be a super user.
:param email:
:return:
"""
DB.changeUserType(email, '0')
return True
def createGuest(email, password, name, techInterest):
"""
For GU
:param email:
:param password: IS AN EMPTY STRING
:return:
"""
hashedPSD = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())
if not DB.checkUserExists(email):
if not DB.checkAppExist(email):
DB.addGuestPending(email, hashedPSD.hex() , '1', name, techInterest)
return True
else:
return 2
else:
return -1
def OUapplication(email, name, listoftechinterests):
"""
For GU to apply become an OU, SU has to approve it
ONLY 1 APPLICATION FOR EACH GU
:param email:
:param name:
:param listoftechinterests:
:return: 1 for application sent successfully,
-1 for reject because of multiple applications
"""
if DB.firstTimeApply(email):
DB.application(email, name, listoftechinterests)
return True
else:
return False
def GUpromotion(email, psd):
ref = DB.user.child(DB.removeIllegalChar(email))
ref.child('Application').delete()
hashedPSD = bcrypt.hashpw(psd.encode('utf8'), bcrypt.gensalt())
ref.update({
"User_type": '1',
"Password":hashedPSD.hex()
})
if __name__ == "__main__":
#print(OUapplication('guest1', 'benzhou', ['google', 'baidu']))
GUpromotion('guest','123')