def unlock_account(database, user, log_file=None): # 解锁账户 import time cur_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) Logger(log_file).write_log(user=user, status="unlock", event="用户解锁成功") return UserInfo(**database).change_info(user=user, user_status="2", status_time=cur_time)
def delete_account(database, admin_name, is_admin=False, log_file=None): select_user = str(input("请输入要删除的用户名:")).strip() delete_check = UserInfo(**database).delete_account( select_user) # 如果返回的不是字典,则说明不存在该用户 if not is_last_super_admin(delete_check) or (not is_admin and type(delete_check) == dict): wait_choose = str(input("确认删除[%s]吗 y/n:" % select_user)).strip() if wait_choose.lower() in [ "y", "yes", ]: print("用户[%s]已被删除" % select_user) Logger(log_file).write_log(user=admin_name, status=True, event="用户%s删除成功" % select_user) return delete_check else: print("操作未改变 !!!") return False elif type(delete_check) == dict: print("管理员[%s]是最后一个具有超级管理权限的账号,操作不允许" % select_user) Logger(log_file).write_log(user=admin_name, status=False, event="用户%s删除失败" % select_user) return False
def lock_account(database, user, log_file=None, user_status="0"): # 锁定账户 get_database = UserInfo(**database).change_info(user, user_status=user_status) if get_database: Logger(log_file).write_log(user=user, status="lock", event="用户已被冻结") return get_database else: return False
def register_account(database, is_admin=False, log_file=None, is_shop_user=False): # 传入一个字典的键值,也可以是一个空字典 new_user = str(input("请输入新用户:")).strip() new_password = str(input("请输入新密码:")).strip() repeat_password = str(input("请再次输入密码:")).strip() if new_password == repeat_password and new_password != "": # 当两次输入密码一致,且密码不为空,才会进入数据库读取注册操作 return UserInfo(**database).register(new_user, new_password) # 调用公共的用户信息管理方法处理 else: if new_password == "": print("密码不能为空 !!!") else: print("两次输入密码不一致 !!!") return False
def auth_account(database, is_admin=False, log_file=None): user = str(input("请输入用户名:")).strip() password = str(input("请输入密码:")).strip() import time today = time.strftime("%Y-%m-%d", time.localtime()) start_time = None last_login_time = None if not is_admin: get_database = search_account_info(database, user) if type(get_database) == dict: if get_database.get("user_status"): if get_database["user_status"] == "1": print("该用户已被冻结,请联系工作人员解锁 !!") Logger(log_file).write_log(user=user, status=False, event="用户登陆失败") return False if get_database.get("status_time"): # 尝试获取最后一次解锁时间 start_time = get_database["status_time"] get_match_list = Logger(log_file).get_match_log( user=user, status="login") # 获取登陆成功的日志 if get_match_list: last_login = get_match_list[-1] last_login_time = " ".join( last_login.split()[0:2]) # 获取最后一次登陆成功的时间 if start_time and last_login_time: # 当既有解锁时间,又有成功登陆时间,则比较大小 if start_time < last_login_time: # 如果登陆时间比解锁时间晚,则登陆时间赋值start_time start_time = last_login_time elif last_login_time: # 否则,如果只有登陆时间,则也赋值给start_time start_time = last_login_time if start_time and start_time < today: start_time = today if get_database["user_status"] == "0" and Logger( log_file).get_match_count(user=user, status=False, start_time=start_time) > 2: print("该用户已被锁定,请联系工作人员解锁,或第二天再次尝试!") Logger(log_file).write_log(user=user, status=False, event="用户登陆失败") return False else: return False login_check = UserInfo(**database).login(user, password) if login_check and is_admin: # 如果登陆成功,且是管理员身份登陆,则返回当前管理员用户名 Logger(log_file).write_log(user=user, status=True, event="管理员登陆成功") return user else: if login_check: Logger(log_file).write_log(user=user, status="login", event="用户登陆成功") return user else: print("用户名或密码错误") Logger(log_file).write_log(user=user, status=False, event="用户登陆失败") if not is_admin: get_database = search_account_info(database, user) if type(get_database) == dict: error_count = Logger(log_file).get_match_count( user=user, status=False, start_time=start_time) if error_count > 2: get_database = lock_account(database, user, log_file=log_file) print("该用户已被锁定,请联系工作人员解锁,或第二天再次尝试!") return get_database return False
def change_password(database, user_name, password, new_password): # 修改密码 return UserInfo(**database).change_password(user=user_name, password=password, new_password=new_password)
def search_account_info(database, user): # 返回指定用户的具体信息,如果不存在则不返回 get_info = UserInfo(**database).change_info(user) if get_info: return get_info[user] else: return False
def change_account_info(database, user, common_info): # 变更用户的除用户名密码以外的信息 return UserInfo(**database).change_info(user, **common_info)