def delete(): """ 删除数据库 :return: """ client_ip = request.remote_addr user_token = session.get("user_token") if not user_token: # 写运行日志、操作日志 LogService.write_log(client_ip=client_ip, action_cn="删除数据库", action_en="drop db", result_cn="失败", result_en="FAIL", reason="数据库删除失败,用户token失效") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="数据库删除失败,用户token失效", msg_en="operation fail.").to_dict() return jsonify(result) user = UserService().get_user_by_token(user_token) if not user: # 写运行日志、操作日志 LogService.write_log(client_ip=client_ip, action_cn="删除数据库", action_en="drop db", result_cn="失败", result_en="FAIL", reason="数据库删除失败,用户不存在") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="数据库删除失败,用户不存在", msg_en="operation fail.").to_dict() return jsonify(result) try: if DBService.clear_db(): # 写运行日志 result = Message(status_code=STATUS_CODE["SUCCESS"], msg_cn="操作成功", msg_en="operation success.").to_dict() return jsonify(result) except Exception as ex: print("数据库删除失败,{}".format(ex)) # 写运行日志、操作日志 LogService.write_log(client_ip=client_ip, action_cn="删除数据库", action_en="drop db", result_cn="失败", result_en="FAIL", reason="数据库删除失败,{}".format(ex), user_id=user.id) result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="操作失败", msg_en="operation fail.").to_dict() return jsonify(result)
def patch(uid=None): if not check_utils.check_param_format( request.path, [r"^/v1/bms/user/([1-9][0-9]*)/password/$"]): return status_code.URL_ERROR try: user_service = UserService() new_password = get_reset_password() user_service.update_user_password(user_id=uid, new_password=new_password) result = status_code.SUCCESS result["new_password"] = new_password return result except Exception as ex: print(ex) logger.error("重置用户密码失败,{}".format(ex)) return status_code.FAIL
def wrapper(*args, **kwargs): if not (session and session.get("user_id") and session.get("token")): return status_code.USER_NO_LOGIN try: user_service = UserService() user = user_service.get_user_by_id(session.get("user_id")) if not user: return status_code.USER_NOT_EXIST if not user.get("is_active"): return status_code.USER_IS_NOT_ACTIVE if user.get("token") != session.get("token"): return status_code.PLEASE_LOGIN except Exception as ex: print(ex) logger.error("认证失败,{}".format(ex)) return status_code.FAIL return func(*args, **kwargs)
def post(): data = request.json session.clear() if not (data and isinstance(data, dict)): return status_code.JSON_PARAMS_ERROR account = data.get("account") password = data.get("password") if not account: return { "code": status_code.REQUIRED_PARAM_CODE, "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("帐号"), "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("account") } if not password: return { "code": status_code.REQUIRED_PARAM_CODE, "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("密码"), "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("password") } try: user_service = UserService() user = user_service.get_user_by_account(account=account) if not user: return status_code.USER_NOT_EXIST if not user.get("is_active"): return status_code.USER_IS_NOT_ACTIVE if not user_service.check_user_password(user_id=user.get("id"), password=password): return status_code.PASSWORD_ERROR token = user_service.change_user_token(user_id=user.get("id")) session["user_id"] = user.get("id") session["token"] = token result = status_code.SUCCESS result["token"] = token return result except Exception as ex: print(ex) logger.error("用户登录失败,{}".format(ex)) return status_code.FAIL
def delete(uid=None): if not check_utils.check_param_format(request.path, [r"^/v1/bms/user/$"]): return status_code.URL_ERROR data = request.json if not (data and isinstance(data, dict)) or not isinstance( data.get("ids"), list): return status_code.JSON_PARAMS_ERROR if not data.get("ids"): return { "code": status_code.REQUIRED_PARAM_CODE, "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("用户id"), "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("user ids") } try: user_service = UserService() user_service.delete_user(user_id_list=data.get("ids")) return status_code.SUCCESS except Exception as ex: print(ex) logger.error("删除用户失败,{}".format(ex)) return status_code.FAIL
def get(): """ 列表 :return: """ client_ip = request.remote_addr user = UserService().get_user_by_id(session.get("user_id")) try: # 获取日记列表 diary_list = DiaryService().get_list() # 写运行日志、操作日志 LogService.write_log(client_ip=client_ip, action_cn="获取日记列表", action_en="get diary list", result_cn="成功", result_en="SUCCESS", reason="获取日记列表成功", user_id=user.id) result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="获取日记列表成功", msg_en="success to get diary list.").to_dict() result["data"] = diary_list return jsonify(result) except Exception as ex: print("获取日记列表失败,{}".format(ex)) # 写运行日志、操作日志 LogService.write_log(client_ip=client_ip, action_cn="获取日记列表", action_en="get diary list", result_cn="失败", result_en="FAIL", reason="获取日记列表失败,{}".format(ex), user_id=user.id) result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="获取日记列表失败,{}".format(ex), msg_en="failed to get diary list.").to_dict() return jsonify(result)
def get(uid=None): if not check_utils.check_param_format( request.path, [r"^/v1/bms/user/([1-9][0-9]*)/$", r"^/v1/bms/user/$"]): return status_code.URL_ERROR try: user_service = UserService() if uid: user = user_service.get_user_by_id(user_id=uid) if not user: return status_code.USER_NOT_EXIST result = status_code.SUCCESS result["data"] = user return result else: users = user_service.get_users() result = status_code.SUCCESS result["data"] = users return result except Exception as ex: print(ex) logger.error("获取用户失败,{}".format(ex)) return status_code.FAIL
def patch(): if not check_utils.check_param_format(request.path, [r"^/v1/bms/user/password/$"]): return status_code.URL_ERROR user_id = session.get("user_id") data = request.json if not (data and isinstance(data, dict)): return status_code.JSON_PARAMS_ERROR if not data.get("old_password"): return { "code": status_code.REQUIRED_PARAM_CODE, "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("原密码"), "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("old password") } if not data.get("new_password"): return { "code": status_code.REQUIRED_PARAM_CODE, "msg_cn": status_code.REQUIRED_PARAM_MSG_CN.format("新密码"), "msg_en": status_code.REQUIRED_PARAM_MSG_EN.format("new password") } try: user_service = UserService() user = user_service.get_user_by_id(user_id=user_id) if not user: return status_code.USER_NOT_EXIST if not user_service.check_user_password( user_id=user_id, password=data.get("old_password")): return status_code.PASSWORD_ERROR user_service.update_user_password( user_id=user_id, new_password=data.get("new_password")) return status_code.SUCCESS except Exception as ex: print(ex) logger.error("修改用户密码失败,{}".format(ex)) return status_code.FAIL
def wrapper(*args, **kwargs): client_ip = request.remote_addr # 判断session中用户id if not session.get("user_id"): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="判断session中用户id", action_en="judged user id in session", result_cn="失败", result_en="FAIL", reason="session中用户id不存在") result = Message( status_code=STATUS_CODE["ERROR"], msg_cn="session中用户id不存在", msg_en="user id in session is not existed.").to_dict() return jsonify(result) # 判断session中用户token if not session.get("user_token"): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="判断session中用户token", action_en="judged user token in session", result_cn="失败", result_en="FAIL", reason="session中用户token不存在") result = Message( status_code=STATUS_CODE["ERROR"], msg_cn="session中用户token不存在", msg_en="user token in session is not existed.").to_dict() return jsonify(result) user = UserService().get_user_by_id(session.get("user_id")) if not user: # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="获取用户", action_en="get user by id", result_cn="失败", result_en="FAIL", reason="用户信息不存在") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="用户信息不存在", msg_en="user is not existed.").to_dict() return jsonify(result) if user.token != session.get("user_token"): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="判断用户token", action_en="judged user token", result_cn="失败", result_en="FAIL", reason="session中的用户token与数据库不一致") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="session中的用户token与数据库不一致", msg_en="user is not existed.").to_dict() return jsonify(result) return func(*args, **kwargs)
def post(): """ 登录 :return: """ client_ip = request.remote_addr data = request.json # 判断是否有数据 if data is None: # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="请求参数错误") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="请求参数错误", msg_en="request parameter error.").to_dict() return jsonify(result) # 判断账户、密码的数据格式 if not data.get("account").strip(): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="登录账号为空") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录账号为空", msg_en="login account is empty.").to_dict() return jsonify(result) if not data.get("password").strip(): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="登录密码为空") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录密码为空", msg_en="login password is empty.").to_dict() return jsonify(result) # UserService对象 user_service = UserService() # 获取用户 user = user_service.get_user_by_account(data.get("account").strip()) # 判断是否存在 if not user: # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="账户不存在") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="账户不存在", msg_en="user account is not existed.").to_dict() return jsonify(result) # 判断密码是否正确 if not user.check_password(data.get("password").strip()): # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="登录密码错误") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="登录密码错误", msg_en="login password error.").to_dict() return jsonify(result) # 判断是否激活 if not user.is_active: # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="失败", result_en="FAIL", reason="用户未激活") result = Message(status_code=STATUS_CODE["ERROR"], msg_cn="用户未激活, 请联系管理员", msg_en="user is not active, please contact administrator.").to_dict() return jsonify(result) # 生成新的token user_token = uuid.uuid1().hex # 更新token user_service.update_user_token(user, user_token) # 将用户信息保存在session中 session["user_token"] = user_token session["user_id"] = user.id # 写运行日志 # 写操作日志 LogService.write_log(client_ip=client_ip, action_cn="用户登录", action_en="user login", result_cn="成功", result_en="SUCCESS", reason="登录成功", user_id=user.id) result = Message(status_code=STATUS_CODE["SUCCESS"], msg_cn="登录成功", msg_en="user login successful.").to_dict() return jsonify(result)