def addUser2dbAndRedis(self): """ 同步一个账号到数据库和redis :return: """ from api.service.redis_service import RedisService from model.default_value import Role from model.user import User, UserService user_name = "b5mali4" full_name = "小明" email = "*****@*****.**" dept_name = "信息安全部" role = Role.USER mobile_phone = "131xxxx9871" if UserService.count(where=(User.user_name == user_name)) <= 0: UserService.save(user_name=user_name, full_name=full_name, email=email, dept_name=dept_name, role=role, mobile_phone=mobile_phone) else: UserService.update(fields=({ User.user_name: user_name, User.full_name: full_name, User.email: email, User.dept_name: dept_name, User.role: role, User.mobile_phone: mobile_phone })) user = UserService.get_fields_by_where( where=(User.user_name == user_name))[0] user_info = { "user_name": "b5mali4", "full_name": "小明", "email": "*****@*****.**", "dept_name": "信息安全部", "role": Role.USER, "mobile_phone": "131xxxx9871", "id": user.id } RedisService.update_user(user_name="b5mali4", user_info=user_info)
def modify_user(user_id): """ 更新用户资料,主要包含权限,部门等等 data: {department: "信息安全部", emails: "", mobilephone: "18324742048", role: "管理员"} :return: """ try: put_data = request.get_json(force=True) param_list = ["email", "mobile_phone", "role", "depart_ment"] if has_dict_value_blank(put_data, param_list): return jsonify( status=400, message="更新失败", data={"extra_info": "请保证%s任一参数值不为空" % ','.join(param_list)}) email = put_data.get("email") mobile_phone = put_data.get("mobile_phone") role = put_data.get("role") depart_ment = put_data.get("depart_ment") UserService.update(fields=({ User.email: email, User.mobile_phone: mobile_phone, User.role: role, User.dept_name: depart_ment }), where=(User.id == user_id)) user = UserService.get_fields_by_where(where=(User.id == user_id))[0] RedisService.update_user( user.user_name, { "dept_name": depart_ment, "role": role, "mobile_phone": mobile_phone, "email": email }) return jsonify(status=200, message="更新用户成功", data={}) except Exception as e: logger.exception("update_user error") return jsonify(status=500, message="未知异常", data={"extra_info": "发生未知异常,请联系管理员查看异常日志"})
def auth_login(username, password, sessionid): """ 代理认证模块,LDAP认证和账户认证,根据开关选择账号认证或者LDAP认证,如果两个开关都开启了,两种方式都支持 :param role: :return: """ try: user = RedisService.get_user(user_name=username) if sessionid and hasattr( user, "proxy_sessionid") and user.proxy_sessionid == sessionid: return True, user raise UserNotFoundInRedisException("账号%s proxy_sessionid不存在或者认证错误" % username) except UserNotFoundInRedisException: if auth_login_logic(username, password): # 设置proxy_session_id proxy_sessionid = generate_session() RedisService.update_user(username, {"proxy_sessionid": proxy_sessionid}) user = RedisService.get_user(user_name=username) return True, user return False, None
def modify_user_info_cache_session(user_name, db_user): """ 认证成功之后,修改redis中用户信息并设置session :return: """ # 存入到redis,和数据库中的数据一致,用户id是最关键的 RedisService.update_user( user_name, { "id": db_user.id, "user_name": db_user.user_name, "full_name": db_user.full_name, "dept_name": db_user.dept_name, "role": db_user.role, "mobile_phone": db_user.mobile_phone, "email": db_user.email }) try: current_task = TaskService.get_working_tasks(user_id=db_user.id)[0] RedisService.update_user(user_name, {"current_task_id": current_task.id}) except IndexError: pass # 设置session session["user_name"] = user_name