-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodifyUsers.py
381 lines (337 loc) · 12.7 KB
/
modifyUsers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import json
from datetime import datetime
import hashlib
with open("userData.json", "r", encoding="utf-8") as file:
userData = json.load(file)
# userData =
# {
# "UID": "12900001",
# "userName": "MuYYY",
# "userAccount": "18012341234",
# "userPassword": "abcdefg",
# "creatingDate": "20241217",
# "creatingTime": "120821",
# "totalDeposit": 100000,
# "totalUsed": 100,
# "currentBalance": 99900
# }
# data = {
# "todo": "addUser",
# "userName": "ABC",
# "userAccount": "18012341234",
# "userPassword": "abcdefg"
# }
# data = {
# "todo": "xxxxx",
# "UID": "12900001",
# interface begin at here
def modifyUserFunc(dataFromWeb):
todo = dataFromWeb.get("todo")
# "todo" means the operation that the user wants to do
if todo == None:
return -1
# because addUser do not need UID,
# so we do not need to check UID here
elif todo == "addUser":
return addUserFunc(dataFromWeb)
elif todo == "updateUser":
updateUserFunc(dataFromWeb)
return 0
elif todo == "findUserField":
return findUserFunc(dataFromWeb)
elif todo == "rechargeAccount":
# -1: negative balance; other number: balance
return rechargeAccount(dataFromWeb)
elif todo == "addUsage":
# -1: negative balance; other number: balance
# this would not get from web, but from the server
# but we will use the same data structure
return addUsage(dataFromWeb)
elif todo == "updateCurrentBalance":
return updateCurrentBalance(dataFromWeb)
elif todo == "isVerified":
return isVerified(dataFromWeb)
elif todo == "generateUserTempToken":
return generateUserTempToken(dataFromWeb)
# if UID is none but userAccount is not none,
# we can get UID by userAccount
UID = dataFromWeb.get("UID")
if UID == None:
userAccount = dataFromWeb.get("userAccount")
if userAccount == None or userAccount == "":
return -1
else:
UID = str(getUserInfoByAccount(userAccount))
if UID == None:
return -1
else:
dataFromWeb["UID"] = UID
# interface
elif todo == "updateUser":
updateUserFunc(dataFromWeb)
return 0
elif todo == "findUser":
return findUserFunc(dataFromWeb)
elif todo == "rechargeAccount":
# -1: negative balance; other number: balance
return rechargeAccount(dataFromWeb)
elif todo == "addUsage":
# -1: negative balance; other number: balance
# this would not get from web, but from the server
# but we will use the same data structure
return addUsage(dataFromWeb)
elif todo == "updateCurrentBalance":
updateCurrentBalance(dataFromWeb)
return 0
elif todo == "isVerified":
isVerified(dataFromWeb)
return 0
else:
return -1
###
# second level interfaces
def addUserFunc(dataFromWeb):
print("Now is in addUserFunc")
return createUser(dataFromWeb)
def updateUserFunc(dataFromWeb):
print("Now is in updateUserFunc")
UID = dataFromWeb.get("UID")
field = dataFromWeb.get("field")
newValue = dataFromWeb.get("newValue")
try:
setUserInfo(UID, field, newValue)
except UserNotFoundException as e:
print(e) # Print user not found error message
return "eroor: no such user"
except FieldNotFoundException as e:
print(e) # Print field not found error message
return "error: no such field"
def findUserFunc(dataFromWeb):
print("Now is in findUserFunc")
UID = dataFromWeb.get("UID")
if UID == None:
userAccount = dataFromWeb.get("userAccount")
if userAccount == None or userAccount == "":
return None
else:
UID = str(getUserInfoByAccount(userAccount))
# if UID is None, means the user is not found
field = dataFromWeb.get("field")
print(f"UID: {UID}, field: {field}")
userInfo = getUserInfo(UID, field)
print(f"userInfo: {userInfo}")
return userInfo
# end of the interfaces
###########
def createUser(dataFromWeb, deposit=0):
# deposit is optional, default is 0
# if we want to give an initial amount,
# we can set deposit to a positive number
if deposit < 0:
return -1
print("Now is in addUserFunction")
userName = dataFromWeb.get("userName")
userAccount = dataFromWeb.get("userAccount")
userPassword = dataFromWeb.get("userPassword")
current_time = datetime.now()
creating_date = current_time.strftime("%Y%m%d") # "20241217"
creating_time = current_time.strftime("%H%M%S") # "120821"
userHashedPassword = hashPassword(userPassword, creating_date, creating_time)
# find the largest UID
max_uid = 0
with open("userData.json", "r", encoding="utf-8") as file:
userDataNow = json.load(file)
for user in userDataNow["users"]:
try:
max_uid = max(max_uid, int(user["UID"]))
except ValueError:
continue
new_uid = str(max_uid + 1)
new_user = {
"UID": new_uid,
"userName": userName,
"userAccount": userAccount,
"userPassword": userHashedPassword,
"creatingDate": creating_date,
"creatingTime": creating_time,
"totalDeposit": int(deposit),
"totalUsed": 0,
"currentBalance": int(deposit),
}
userDataNow["users"].append(new_user)
with open("userData.json", "w", encoding="utf-8") as file:
json.dump(userDataNow, file, ensure_ascii=False, indent=4)
# Note: Printing passwords is a security risk!!!
# Avoid doing this in production.
# print the new user information
print(
f"New user {userName} created successfully, UID is {new_uid}. Password is {userPassword}"
)
return new_user
def rechargeAccount(data):
UID = str(data.get("UID"))
if UID == None or UID == "":
UID = str(getUserInfoByAccount(data.get("userAccount")))
addNum = data.get("addNum")
print(f"UID: {UID}, addNum: {addNum}")
if str(addNum) == "type1" or str(addNum) == "type2":
# some function to change the addNum to a number
pass
else:
addNum = int(addNum)
print(str(getUserInfo(UID, "totalDeposit")))
totalDeposit = int(getUserInfo(UID, "totalDeposit")) + addNum
print(f"totalDeposit: {totalDeposit}")
setUserInfo(UID, "totalDeposit", int(totalDeposit))
print("the first time to call setUserInfo")
currentBalance = updateCurrentBalance(UID)
if currentBalance <= 0:
return -1
return currentBalance
def addUsage(data):
UID = data.get("UID")
if UID == None or UID == "":
UID = str(getUserInfoByAccount(data.get("userAccount")))
addNum = data.get("addNum")
totalUsed = getUserInfo(UID, "totalUsed")
print("totalUsed: " + str(totalUsed))
if UID == None or UID == "":
UID = str(getUserInfoByAccount(data.get("userAccount")))
if isVerified(data) == False:
return -1
totalUsedNew = totalUsed + addNum
temp1 = setUserInfo(UID, "totalUsed", totalUsedNew)
remaining = updateCurrentBalance(UID)
if remaining <= 0:
return -1
else:
return remaining
def updateCurrentBalance(UID):
UID = str(UID)
totalUsed = getUserInfo(UID, "totalUsed")
totalDeposit = getUserInfo(UID, "totalDeposit")
currentBalance = totalDeposit - totalUsed
setUserInfo(UID, "currentBalance", currentBalance)
return currentBalance
def getUserInfoByAccount(userAccount):
with open("userData.json", "r", encoding="utf-8") as file:
userDataNow = json.load(file)
for user in userDataNow["users"]:
if str(user["userAccount"]) == str(userAccount):
return user["UID"]
return None
def isVerified(dataFromWeb):
print("now is in “isVerified”, phone: " + str(dataFromWeb.get("userAccount")))
print("uerPassword: " + str(dataFromWeb.get("userPassword")))
UID = getUserInfoByAccount(str(dataFromWeb.get("userAccount")))
print(f"UID: {UID}")
if UID == None or UID == "":
UID = dataFromWeb.get("UID")
if UID == None or UID == "":
raise Exception("no such user!")
userPassword = dataFromWeb.get("userPassword")
if userPassword == None:
raise Exception("user's Password is required")
with open("userData.json", "r", encoding="utf-8") as file:
userDataNow = json.load(file)
for user in userDataNow["users"]:
if user["UID"] == UID:
creatingDate = user.get("creatingDate")
creatingTime = user.get("creatingTime")
if creatingDate == None or creatingTime == None:
raise Exception("creatingDate and creatingTime are required")
if user["userPassword"] == hashPassword(
userPassword, creatingDate, creatingTime
):
return True
else:
return False
# 返回根据 UID 查找的指定字段值
def getUserInfo(UID, field):
with open("userData.json", "r", encoding="utf-8") as file:
userDataNow = json.load(file)
if field is None or field == "":
return None
# 遍历 users 数组
for user in userDataNow["users"]:
if user["UID"] == UID:
return user.get(field, None)
return None
# 自定义异常类
class UserNotFoundException(Exception):
def __init__(self, message="User not found"):
self.message = message
super().__init__(self.message)
class FieldNotFoundException(Exception):
def __init__(self, message="Field not found"):
self.message = message
super().__init__(self.message)
def hashPassword(password, creating_date, creating_time):
"""
Use the creation date and time as salt.
Parameters:
password (str): The user's plaintext password.
creating_date (str): Creation date, formatted as "20241217".
creating_time (str): Creation time, formatted as "120821".
"""
salt = creating_date + creating_time
password_with_salt = password + salt
# sha256
hashed_password = hashlib.sha256(password_with_salt.encode("utf-8")).hexdigest()
return hashed_password
# 根据 UID 更新指定字段
def setUserInfo(uid, field, newValue):
with open("userData.json", "r", encoding="utf-8") as file:
userDataNow = json.load(file)
for user in userDataNow["users"]:
if user["UID"] == uid:
if field == "userPassword":
date = user["creatingDate"]
time = user["creatingTime"]
newValue = hashPassword(newValue, date, time)
if field in user:
user[field] = newValue # update the field
print(f"Field {field} updated successfully!" f" New value: {newValue}")
with open("userData.json", "w") as f:
print("write to file")
json.dump(userDataNow, f, ensure_ascii=False, indent=4)
print("write to file done")
return user # return the updated user information
else:
raise FieldNotFoundException(
f"Field {field} does not exist!"
) # Raise an exception if the field does not exist
raise UserNotFoundException(
f"Cannot find the information of UID: {uid}"
) # Raise an exception if the user is not found
def generateUserTempToken(dataFromWeb):
userAccount = dataFromWeb.get("userAccount")
experitedTime = dataFromWeb.get("experitedTime") # hours
if experitedTime == None or experitedTime == "":
experitedTime = 14 * 24 # 14 days
if userAccount == None or userAccount == "":
UID = dataFromWeb.get("UID")
if UID == None:
return -1
userAccount = getUserInfo(UID, "userAccount")
createTime = datetime.now().strftime("%Y%m%d%H%M%S")
return hashPassword(userAccount, createTime, experitedTime)
######################################################
# test:
# 测试:'12900001' userName 和 totalUsed
# uid_to_update = "12900001"
# field_to_update_1 = "userName" # 更新 userName
# new_value_1 = "MuYYY_Updated" # 新的 userName 值
# field_to_update_2 = "totalUsed" # 更新 totalUsed
# new_value_2 = 150 # 新的 totalUsed 值
# try:
# 更新 userName
# updated_user_1 = setUserInfo(uid_to_update, field_to_update_1, new_value_1)
# print(f"更新后的用户信息 (userName): {updated_user_1}")
# 更新 totalUsed
# updated_user_2 = setUserInfo(uid_to_update, field_to_update_2, new_value_2)
# print(f"更新后的用户信息 (totalUsed): {updated_user_2}")
# except UserNotFoundException as e:
# print(e) # 打印用户未找到的错误信息
# except FieldNotFoundException as e:
# print(e) # 打印字段未找到的错误信息