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

91 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
import time
from werkzeug.security import generate_password_hash
from common.rsa import decrypt_data
from common.scripts import read_json_file, make_id
from user.db import find_user_info
def check_mail_fmt(email):
"""
邮箱地址格式校验
Parameters:
email: 邮箱
Returns:
result: 邮箱校验结果正确返回True不正确返回False
"""
regex = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"
case = (len(email) > 7) and (re.match(regex, email) is not None)
result = True if case else False
return result
def check_pwd_fmt(pwd):
"""
密码强度校验
Parameters:
pwd: 密码(已加密)
Returns:
result: 密码强度校验结果正确返回True不正确返回False
"""
try:
password = decrypt_data(encrypt_msg=pwd)
except Exception:
return False
regex = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)^.{8,}$"
case = (len(password) >= 8) and (re.match(regex, password) is not None)
result = True if case else False
return result
def gen_new_uid():
"""
生成新的用户ID如果该ID存在则重新生成
Parameters:
-
Returns:
new_id: 生成的用户ID
"""
# 生成新ID
new_id = make_id()
# 检查新ID是否存在如果存在则继续生成新ID
case = find_user_info({"企业ID": new_id}) is []
while case:
new_id = make_id()
# 新ID可使用返回新ID
return new_id
def create_user_scripts(email, name, pwd, role, group):
"""
Notes
Parameters:
email: desc
name: desc
pwd: desc
role: desc
group: desc
Returns:
res: desc
"""
if not check_mail_fmt(email):
return False
if not check_pwd_fmt(pwd):
return False
user = read_json_file('/user/static/db_design/user.json')
user['UID'] = gen_new_uid()
user['email'] = email
user['name'] = name
user['pwd'] = generate_password_hash(decrypt_data(encrypt_msg=pwd))
user['role'] = role
user['group'] = group
user['create_time'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())