/// <summary> /// 获取用户忘记密码请求 /// </summary> /// <param name="hashKey">哈希KEY</param> /// <returns>是否可以重置密码</returns> public static UserForgetPasswordEntity GetUserForgetRequest(String hashKey) { if (!SQLValidator.IsNonNullANDSafe(hashKey)) { throw new InvalidRequstException(RequestType.UserForgetPassword); } UserForgetPasswordEntity entity = UserForgetPasswordRepository.Instance.GetEntity(hashKey); if (entity == null) { throw new NullResponseException(RequestType.UserForgetPassword); } if (!String.IsNullOrEmpty(entity.AccessIP)) { throw new InvalidRequstException(RequestType.UserForgetPassword); } TimeSpan ts = DateTime.Now - entity.SubmitDate; if (ts.TotalHours >= 24) { throw new InvalidRequstException(RequestType.UserForgetPassword); } return(entity); }
/// <summary> /// 尝试将使用用户名密码登陆系统 /// </summary> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <param name="user">若成功返回用户实体</param> /// <returns>失败则返回出错信息,成功则不返回任何信息</returns> public static String TryGetUserByUsernameAndPassword(String userName, String passWord, out UserEntity user) { user = null; try { if (String.IsNullOrEmpty(userName)) { return("Username can not be NULL!"); } if (String.IsNullOrEmpty(passWord)) { return("Password can not be NULL!"); } if (!RegexVerify.IsUserName(userName) || !SQLValidator.IsNonNullANDSafe(userName)) { return("Username is INVALID!"); } passWord = PassWordEncrypt.Encrypt(userName, passWord); user = UserRepository.Instance.GetEntityByNameAndPassword(userName, passWord); if (user == null) { return("No such user or wrong password!"); } if (!String.Equals(user.PassWord, passWord, StringComparison.OrdinalIgnoreCase)) { return("Password is wrong!"); } if (user.IsLocked) { return("The user is locked, please contact the administrator!"); } if ("NULL".Equals(user.PassWord, StringComparison.OrdinalIgnoreCase)) { return("The user's password is INVALID, please visit \"Forget Password\" and reset your password!"); } return(String.Empty); } catch (System.Exception ex) { return(ex.Message); } }
/// <summary> /// 根据题目搜索结果获取题目列表 /// </summary> /// <param name="type">搜索类别</param> /// <param name="content">搜索内容</param> /// <returns>题目列表</returns> public static List <ProblemEntity> GetProblemBySearch(String type, String content) { List <ProblemEntity> list = null; if (String.Equals(type, "category", StringComparison.OrdinalIgnoreCase)) { Int32 typeID = -1; if (!Int32.TryParse(content, out typeID) || typeID <= 0) { throw new InvalidRequstException(RequestType.ProblemCategory); } list = ProblemManager.GetProblemListByType(typeID); } else if (String.Equals(type, "pid", StringComparison.OrdinalIgnoreCase)) { content = content.SearchOptimized(); if (!RegexVerify.IsNumericIDs(content)) { throw new InvalidRequstException(RequestType.Problem); } list = ProblemManager.GetProblemListByID(content); } else if (String.Equals(type, "title", StringComparison.OrdinalIgnoreCase)) { if (String.IsNullOrEmpty(content) || !SQLValidator.IsNonNullANDSafe(content)) { throw new InvalidInputException("Problem Title is INVALID!"); } list = ProblemManager.GetProblemListByTitle(content); } else if (String.Equals(type, "source", StringComparison.OrdinalIgnoreCase)) { if (String.IsNullOrEmpty(content) || !SQLValidator.IsNonNullANDSafe(content)) { throw new InvalidInputException("Problem Source is INVALID!"); } list = ProblemManager.GetProblemListBySource(content); } list = GetUserCanViewProblemList(list); return(list); }
/// <summary> /// 获取提交列表 /// </summary> /// <param name="pageIndex">页面索引</param> /// <param name="cid">竞赛ID</param> /// <param name="pid">题目ID</param> /// <param name="userName">用户名</param> /// <param name="languageType">提交语言</param> /// <param name="resultType">提交结果</param> /// <param name="order">排序顺序</param> /// <returns>提交列表</returns> public static PagedList <SolutionEntity> GetSolutionList(Int32 pageIndex, Int32 cid, Int32 pid, String userName, String languageType, String resultType, String order) { Int32 pageSize = SolutionManager.STATUS_PAGE_SIZE; Int32 recordCount = 0; List <SolutionEntity> list = null; if (pid <= 0 && String.IsNullOrEmpty(userName) && String.IsNullOrEmpty(languageType) && String.IsNullOrEmpty(resultType)) { recordCount = SolutionManager.CountSolutions(cid, -1, null, null, null); list = SolutionRepository.Instance.GetEntities(cid, -1, null, LanguageType.Null, new Nullable <ResultType>(), -1, pageIndex, pageSize, recordCount); } else { if (!String.IsNullOrEmpty(userName) && (!RegexVerify.IsUserName(userName) || !SQLValidator.IsSafe(userName))) { throw new InvalidInputException("Username is INVALID!"); } if (!String.IsNullOrEmpty(languageType) && !RegexVerify.IsNumeric(languageType)) { throw new InvalidInputException("Language Type is INVALID!"); } if (!String.IsNullOrEmpty(resultType) && !RegexVerify.IsNumeric(resultType)) { throw new InvalidInputException("Result Type is INVALID!"); } recordCount = SolutionManager.CountSolutions(cid, pid, userName, languageType, resultType); list = SolutionRepository.Instance.GetEntities( cid, pid, userName, (String.IsNullOrEmpty(languageType) ? LanguageType.Null : LanguageType.FromLanguageID(Convert.ToByte(languageType))), (String.IsNullOrEmpty(resultType) ? new Nullable <ResultType>() : (ResultType)Convert.ToByte(resultType)), (String.IsNullOrEmpty(order) ? -1 : Convert.ToInt32(order)), pageIndex, pageSize, recordCount); } return(list.ToPagedList(pageSize, recordCount)); }
/// <summary> /// 尝试注册用户 /// </summary> /// <param name="entity">用户实体</param> /// <param name="password">密码</param> /// <param name="password2">重复密码</param> /// <param name="checkCode">验证码</param> /// <param name="userip">用户IP</param> /// <returns>执行结果</returns> public static IMethodResult SignUp(UserEntity entity, String password, String password2, String checkCode, String userip) { if (!CheckCodeStatus.VerifyCheckCode(checkCode)) { return(MethodResult.Failed("The verification code you input didn't match the picture, Please try again!")); } if (String.IsNullOrEmpty(entity.UserName)) { return(MethodResult.Failed("Username can not be NULL!")); } if (!RegexVerify.IsUserName(entity.UserName) || !SQLValidator.IsNonNullANDSafe(entity.UserName)) { return(MethodResult.Failed("Username can not contain illegal characters!")); } if (!KeywordsFilterManager.IsUserNameLegal(entity.UserName)) { return(MethodResult.Failed("Username can not contain illegal keywords!")); } if (entity.UserName.Length > UserRepository.USERNAME_MAXLEN) { return(MethodResult.Failed("Username is too long!")); } if (String.IsNullOrEmpty(password)) { return(MethodResult.Failed("Password can not be NULL!")); } if (!String.Equals(password, password2)) { return(MethodResult.Failed("Two passwords are not match!")); } if (String.IsNullOrEmpty(entity.Email)) { return(MethodResult.Failed("Email address can not be NULL!")); } if (!RegexVerify.IsEmail(entity.Email)) { return(MethodResult.Failed("Email address is INVALID!")); } if (entity.Email.Length > UserRepository.EMAIL_MAXLEN) { return(MethodResult.Failed("Email address is too long!")); } if (!String.IsNullOrEmpty(entity.NickName) && entity.NickName.Length > UserRepository.NICKNAME_MAXLEN) { return(MethodResult.Failed("Nick Name is too long!")); } if (!KeywordsFilterManager.IsUserNameLegal(entity.NickName)) { return(MethodResult.Failed("Nick Name can not contain illegal keywords!")); } if (!String.IsNullOrEmpty(entity.School) && entity.School.Length > UserRepository.SCHOOL_MAXLEN) { return(MethodResult.Failed("School Name is too long!")); } if (UserRepository.Instance.ExistsEntity(entity.UserName)) { return(MethodResult.Failed("The username \"{0}\" has already existed!", entity.UserName)); } if (!UserIPStatus.CheckLastRegisterTime(userip)) { return(MethodResult.Failed("You can only register one user from single ip in {0} seconds!", ConfigurationManager.RegisterInterval.ToString())); } entity.PassWord = PassWordEncrypt.Encrypt(entity.UserName, password); entity.NickName = HtmlEncoder.HtmlEncode(entity.NickName); entity.Permission = PermissionType.None; entity.CreateIP = userip; entity.CreateDate = DateTime.Now; try { if (UserRepository.Instance.InsertEntity(entity) == 0) { return(MethodResult.Failed("User Registration Failed!")); } } catch (System.Exception ex) { return(MethodResult.Failed(ex.Message)); } UserCache.RemoveRanklistUserCountCache();//删除缓存 return(MethodResult.SuccessAndLog("User sign up")); }