Browse Source

update: modify and delete user

master
Dnomd343 3 years ago
parent
commit
5ea834e711
  1. 84
      Web.py

84
Web.py

@ -234,7 +234,7 @@ def isUserToken(token: str) -> bool:
except: except:
return False return False
def addUser(priority: str, remain: int): def addUser(priority: str, remain):
''' '''
添加账号 添加账号
@ -248,6 +248,7 @@ def addUser(priority: str, remain: int):
userId = genRandomId(length = 24) userId = genRandomId(length = 24)
if not priority in ['a','b','c','d','e']: # 优先级无效 if not priority in ['a','b','c','d','e']: # 优先级无效
return False, 'invalid priority' return False, 'invalid priority'
remain = int(remain)
if remain < 0: if remain < 0:
remain = -1 # 不限次数 remain = -1 # 不限次数
userInfo = { userInfo = {
@ -266,6 +267,26 @@ def addUser(priority: str, remain: int):
except: except:
return False, 'server error' return False, 'server error'
def delUser(userId: str):
'''
删除账号
删除成功:
return True, userId
删除失败:
return False, {reason}
'''
try:
if isUserToken(userId) == False:
return False, 'invalid user id'
# TODO: check remain task before delete it
redisObject.srem(redisPrefix + 'users', userId)
redisObject.delete(redisPrefix + 'user-' + userId)
return True, userId
except:
return False, 'server error'
def getUserInfo(userId: str, minus: bool = False): def getUserInfo(userId: str, minus: bool = False):
''' '''
获取账号信息 (minus = True: 剩余次数 - 1) 获取账号信息 (minus = True: 剩余次数 - 1)
@ -327,6 +348,38 @@ def getUserList():
except: except:
return None return None
def modifyUserInfo(userId: str, priority: str = None, remain = None):
'''
修改账号信息
修改成功:
return True
修改失败:
return False
'''
try:
userInfo = getUserInfo(userId)
if userInfo == None: # 账号不存在
return False
if priority != None: # 优先级变动
if not priority in ['a','b','c','d','e']: # 优先级无效
return False
userInfo['priority'] = priority
if remain != None: # 剩余次数变动
remain = int(remain)
if remain < 0:
remain = -1 # 不限次数
userInfo['remain'] = remain
redisObject.set(
redisPrefix + 'user-' + userId, # 记录账号信息
json.dumps(userInfo)
)
return True
except:
return False
@api.route(apiPath + '/user', methods = ['GET','POST']) @api.route(apiPath + '/user', methods = ['GET','POST'])
def apiUser(): def apiUser():
if request.method == 'GET': # 获取账号列表 if request.method == 'GET': # 获取账号列表
@ -346,7 +399,7 @@ def apiUser():
priority = 'c' # 默认优先级 priority = 'c' # 默认优先级
remain = httpPostArg('remain') remain = httpPostArg('remain')
if remain == None: if remain == None:
remain = -1 # 默认剩余次数 remain = '-1' # 默认剩余次数
status, userId = addUser(priority, remain) # 创建新账号 status, userId = addUser(priority, remain) # 创建新账号
if status == False: if status == False:
return genError(userId) # 创建错误 return genError(userId) # 创建错误
@ -361,13 +414,28 @@ def apiUserId(userId):
if userInfo == None: if userInfo == None:
return genError('invalid user id') return genError('invalid user id')
return genSuccess(userInfo) return genSuccess(userInfo)
elif request.method == 'PUT': # 更新账号信息 elif request.method == 'PUT' or request.method == 'PATCH': # 更新账号信息
pass if isAdminToken(httpPostArg('token')) == False: # 非管理员token
elif request.method == 'PATCH': # 修改账号信息 return genError('invalid admin token')
pass priority = httpPostArg('priority')
remain = httpPostArg('remain')
if request.method == 'PUT':
if priority == None or remain == None: # 参数不全
return genError('missing parameter')
if modifyUserInfo(userId, priority = priority, remain = remain) == False: # 更新账号信息
return genError('server error')
return genSuccess(
getUserInfo(userId) # 更新成功
)
elif request.method == 'DELETE': # 销毁账号 elif request.method == 'DELETE': # 销毁账号
pass if isAdminToken(httpPostArg('token')) == False: # 非管理员token
return genError('wait for develop') return genError('invalid admin token')
status, reason = delUser(userId)
if status == False:
return genError(reason)
return genSuccess({
'userId': userId # 删除成功
})
@api.route(apiPath + '/check', methods = ['GET','POST']) @api.route(apiPath + '/check', methods = ['GET','POST'])
def apiCheck(): def apiCheck():

Loading…
Cancel
Save