tfse-admin-api-v0.2/user/process.py

101 lines
2.7 KiB
Python
Raw Normal View History

2022-01-10 05:22:25 +08:00
import time
2022-01-11 07:13:28 +08:00
import random
2022-01-10 05:22:25 +08:00
from werkzeug.security import generate_password_hash
2022-01-11 07:13:28 +08:00
from common.db import insert_data, find_data
from user.scripts import check_mail_fmt, check_pwd_fmt
from common.security.rsa import decrypt_data
2022-01-10 05:22:25 +08:00
2022-01-11 07:13:28 +08:00
def create_user_process(email, name, pwd, role, duty):
2022-01-10 05:22:25 +08:00
"""
2022-01-10 18:07:02 +08:00
创建新用户流程
2022-01-10 05:22:25 +08:00
Parameters:
2022-01-11 07:13:28 +08:00
email str 邮箱
name str 姓名
pwd str 密码
role str 角色
duty str 职责
2022-01-10 05:22:25 +08:00
Returns:
2022-01-11 07:13:28 +08:00
执行成功 bool True
执行失败 str 异常信息
2022-01-10 05:22:25 +08:00
"""
2022-01-11 07:13:28 +08:00
def check_params():
"""
参数检查
"""
roles = ['admin', 'member', 'guest']
if role not in roles:
return "用户角色异常"
2022-01-10 05:22:25 +08:00
2022-01-11 11:02:46 +08:00
duties = ['developer', 'analysts', 'manager', None]
2022-01-11 07:13:28 +08:00
if duty not in duties:
return "成员职责异常"
2022-01-10 05:22:25 +08:00
2022-01-11 07:13:28 +08:00
if not check_mail_fmt(email):
return "邮箱格式错误"
2022-01-10 05:22:25 +08:00
2022-01-11 07:13:28 +08:00
if not check_pwd_fmt(pwd):
return "密码格式错误"
2022-01-10 18:07:02 +08:00
2022-01-11 07:13:28 +08:00
if len(find_data("tfse_admin", "用户", "用户信息", {"email": email})) > 0:
return "邮箱已被注册"
return True
def gen_new_uid():
"""
生成新的用户ID如果该ID存在则重新生成
Returns:
生成的用户ID
"""
def make_id(num):
"""
随机生成字符串
"""
choices = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
salt = ''
for i in range(num):
salt += random.choice(choices)
return salt
def gen_id():
"""
生成新ID
检查新ID是否存在如果存在则继续生成新ID
若新ID可使用返回新ID
"""
new_id = make_id(8)
case = find_data("tfse_admin", "用户", "用户信息", {"企业ID": new_id}) is []
while case:
new_id = make_id(8)
return new_id
return gen_id()
def process_main():
"""
执行流程
"""
check_param_result = check_params()
if check_param_result is not True:
return check_param_result
user = dict()
user['UID'] = gen_new_uid()
user['email'] = email
user['name'] = name
user['pwd'] = generate_password_hash(decrypt_data(encrypt_msg=pwd))
user['status'] = 'normal'
user['role'] = role
user['duty'] = duty
user['create_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
insert_data("tfse_admin", "用户", "用户信息", user)
return True
return process_main()