def register(): """判断为新用户,新用户注册,需要结合传递过来的邮箱(邮箱要唯一)以及邮箱验证码,用户名(唯一)和密码 :return: json结果 """ if request.method == "POST": email = request.form.get("email", type=str, default="") username = request.form.get("username", type=str, default="") passwd = request.form.get("passwd", type=str, default="") capture = request.form.get("capture", type=str, default="") # 用户名,密码,验证码, 邮箱为空 if not (username and passwd and email and capture): code = "-1" msg = "用户输入了空参数" # 查询本地是否有同名用户名和邮箱 else: # 本地数据库不存在已注册相关用户名和邮箱信息,核实验证码注册 if not DB_Sync.exist_UserName_email(username, email): captureCode = DB_Sync.query_redis_Capture( email) # 如果email没找到,capturecode为None而不是"" if captureCode == capture: code = "0" msg = "注册成功" # 新用户写入数据库 # 先对用户密码加密,需要盐 salt = create_Salt() passwd = computePW(passwd, salt) # 新用户入库 isOk = DB_Sync.sync_redis_insert(username, passwd, salt, email) if not isOk: code = "-1" msg = "新用户注册入库失败" else: # 刷新同步到redis DB_Sync.refresh_redis(username) session['isLogin'] = True session['username'] = username # 最后创建该用户的文件夹 iscreate, user_home_str = User_Action.create_user_homedir( username) if iscreate: logger.info("为新注册用户%s创建文件夹成功" % username) else: logger.error("为新注册用户%s创建文件夹失败" % username) else: code = "-1" msg = "验证码失效或错误" # 本地已存在相关信息用户信息 else: code = "-1" msg = "用户名或邮箱已存在,请重新输入" else: code = '-1' msg = '{} Method is not allowed !'.format(request.method) resp = jsonify(code=code, msg=msg) resp.set_cookie("datetime", getlocaltime()) return resp
def forgetPWD(): """用户名和邮箱以及邮箱的验证码和新密码,查询本地用户名和邮箱是否对应,是发送验证码,然后检验验证码 :return: json结果 """ email = request.form.get("email", type=str, default="") username = request.form.get("username", type=str, default="") captureCode = request.form.get("capture", type=str, default="") new_passwd = request.form.get("new_passwd", type=str, default="") # 本地数据库无对应数据,修改不了 if not DB_Sync.exist_UserName_email(username, email): code = "-1" msg = "用户名或者邮箱错误" # 本地有数据可以修改,核对验证码 else: # 判断根据邮箱查到的用户名是否一致,避免修改他人的数据 # 验证码一致,而且email和用户名也对应.(默认不考虑用户忘记用户名的情况) isConsist = DB_Sync.is_consistent(email, username) if captureCode == DB_Sync.query_redis_Capture(email) and isConsist: result = DB_Sync.query_redis(username) # 根据用户名查询到了结果(salt) print(result) salt = result["data"]['USER_SALT'] compute_new_pwd = computePW(new_passwd, salt) isOK = DB_Sync.sync_redis_insert(username, compute_new_pwd, salt, email) print("is ok", isOK) if isOK: code = "0" msg = "密码重置成功,请用新的密码登录" logger.info("%s用户密码修改成功" % username) # 修改密码后需要清除session数据 session["isLogin"] = False session.clear() if DB_Sync.refresh_redis(username): print("redis刷新成功...") else: print("redis刷新失败...") else: code = "-1" msg = "密码重置失败,请重试" print("{0}用户重置密码失败".format(username)) logger.error("%s用户重置密码失败" % username) else: code = "-1" msg = ("验证码失效" if isConsist else "用户名密码不一致") logger.error("%s用户重置密码失败,原因:%s" % (username, msg)) resp = jsonify(code=code, msg=msg) resp.set_cookie("datetime", getlocaltime()) return resp
def eraseUSER(): """ 清空redis数据并删除本地数据库用户信息,同步redis :return: json结果 """ username = request.form.get("username", type=str, default="") email = request.form.get("email", type=str, default="") captureCode = request.form.get("capture", type=str, default="") # 判断是否处于登录状态 if session["isLogin"] and session.get("username") == username: # 判断验证码是否正确 isConsist = DB_Sync.is_consistent(email, username) if captureCode == DB_Sync.query_redis_Capture(email) and isConsist: # 删除redis数据,然后删除本地mysql数据 isDelete = DB_Sync.sync_redis_delete(username, email) if isDelete: code = "0" msg = "用户账号注销成功" session["isLogin"] = False # 清除服务器数据 session.clear() logger.info("%s用户账号注销成功" % username) # 用户删除成功时候,删除用户目录 if User_Action.remove_user_homedir(username): logger.warning("注销删除%s用户文件目录数据成功" % username) else: logger.error("注销删除%s用户文件目录数据失败" % username) else: code = "-1" msg = "用户账号注销失败" logger.warning("%s用户账户注销失败" % username) else: code = "-1" msg = "验证码不一致" logger.info("%s用户输入验证码不一致" % username) # 当前不是处于登录状态 else: code = "-1" msg = "您当前不处于登录状态,请先登录" logger.warning("%s用户删除数据失败,原因:当前处于非登录状态" % username) resp = jsonify(code=code, msg=msg) resp.set_cookie("datetime", getlocaltime()) return resp