Exemple #1
0
        /// <summary>
        /// 根据用户名和提交结果获取通过提交列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="contestID">竞赛ID</param>
        /// <returns>题目ID列表</returns>
        public static List <Int32> GetSolvedProblemIDListByUser(String userName, Int32 contestID)
        {
            if (String.IsNullOrEmpty(userName))
            {
                return(new List <Int32>());
            }

            if (!RegexVerify.IsUserName(userName))
            {
                throw new InvalidRequstException(RequestType.User);
            }

            List <Int32> lstSolved = (contestID == -1 ? SolutionCache.GetProblemIDListCache(userName, false) : null);//获取缓存

            if (lstSolved == null)
            {
                lstSolved = SolutionRepository.Instance.GetEntityIDsByUserAndResultType(userName, contestID, ResultType.Accepted);

                if (contestID == -1)
                {
                    SolutionCache.SetProblemIDListCache(userName, false, lstSolved);                 //获取缓存
                }
            }

            return(lstSolved != null ? lstSolved : new List <Int32>());
        }
Exemple #2
0
        /// <summary>
        /// 根据用户名获取未通过提交列表
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="solvedList">该用户AC题目ID的列表</param>
        /// <param name="contestID">竞赛ID</param>
        /// <returns>题目ID列表</returns>
        public static List <Int32> GetUnSolvedProblemIDListByUser(String userName, List <Int32> lstSolved, Int32 contestID)
        {
            if (String.IsNullOrEmpty(userName))
            {
                return(new List <Int32>());
            }

            if (!RegexVerify.IsUserName(userName))
            {
                throw new InvalidRequstException(RequestType.User);
            }

            List <Int32> lstUnsolved = (contestID == -1 ? SolutionCache.GetProblemIDListCache(userName, true) : null);//获取缓存

            if (lstUnsolved == null)
            {
                lstUnsolved = SolutionRepository.Instance.GetEntityIDsByUserAndLessResultType(userName, contestID, ResultType.Accepted);

                if (lstUnsolved != null && lstUnsolved.Count > 0 && lstSolved != null && lstSolved.Count > 0)
                {
                    foreach (Int32 pid in lstSolved)
                    {
                        lstUnsolved.Remove(pid);
                    }
                }

                if (contestID == -1)
                {
                    SolutionCache.SetProblemIDListCache(userName, true, lstUnsolved);//设置缓存
                }
            }

            return(lstUnsolved != null ? lstUnsolved : new List <Int32>());
        }
Exemple #3
0
        /// <summary>
        /// 重置用户密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="passWord">新密码</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminResetUserPassword(String userName, String passWord)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            if (String.IsNullOrEmpty(passWord))
            {
                return(MethodResult.FailedAndLog("New password can not be NULL!"));
            }
            else
            {
                passWord = PassWordEncrypt.Encrypt(userName, passWord);
            }

            Boolean success = UserRepository.Instance.UpdateEntityPassword(userName, passWord) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No user's password was reset!"));
            }

            return(MethodResult.SuccessAndLog("User reset password, name = {0}", userName));
        }
Exemple #4
0
        /// <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);
            }
        }
Exemple #5
0
        /// <summary>
        /// 根据ID得到一个用户实体
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户实体</returns>
        public static UserEntity GetUserInfo(String userName)
        {
            if (!RegexVerify.IsUserName(userName))
            {
                throw new InvalidRequstException(RequestType.User);
            }

            UserEntity entity = UserRepository.Instance.GetEntityWithBasicInfo(userName);

            if (entity == null)
            {
                throw new NullResponseException(RequestType.User);
            }

            return(entity);
        }
Exemple #6
0
        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>用户实体</returns>
        public static IMethodResult AdminGetUserInfo(String userName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            UserEntity entity = UserRepository.Instance.GetEntityWithAllInfo(userName);

            if (entity == null)
            {
                return(MethodResult.NotExist(RequestType.User));
            }

            return(MethodResult.Success(entity));
        }
Exemple #7
0
        /// <summary>
        /// 更新指定ID的用户通过题目总数
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateSolvedCount(String userName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            Boolean success = UserRepository.Instance.UpdateEntitySolvedCount(userName) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No user's solved count was recalculated!"));
            }

            return(MethodResult.SuccessAndLog("Admin update user's solved count, name = {0}", userName));
        }
Exemple #8
0
        /// <summary>
        /// 更新指定ID的用户的用户权限
        /// </summary>
        /// <param name="userNames">用户名</param>
        /// <param name="permission">权限类型</param>
        /// <returns>是否成功更新</returns>
        internal static IMethodResult InternalAdminUpdatePermission(String userName, PermissionType permission)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            Boolean success = UserRepository.Instance.UpdateEntityPermision(userName, permission) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("Failed to update user's permission!"));
            }

            return(MethodResult.Success());
        }
Exemple #9
0
        /// <summary>
        /// 更新指定ID的用户的用户权限
        /// </summary>
        /// <param name="userNames">用户名</param>
        /// <param name="permissions">权限类型</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdatePermision(String userName, String permissions)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            PermissionType permission = AdminManager.GetPermission(permissions);
            Boolean        success    = UserRepository.Instance.UpdateEntityPermision(userName, permission) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No user's permission was updated!"));
            }

            return(MethodResult.SuccessAndLog("Admin update permission, name = {0}, permission = {1}", userName, ((Int32)permission).ToString()));
        }
Exemple #10
0
        /// <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));
        }
Exemple #11
0
        private static Boolean AdminGetSolutionParams(String sids, String cid, String pid, String name, String lang, String type, String startDate, String endDate,
                                                      out String solutionIDs, out Int32 problemID, out Int32 contestID, out String userName, out LanguageType languageType, out ResultType?resultType, out DateTime?dtStart, out DateTime?dtEnd)
        {
            Boolean noCondition = true;
            String  dateFormat  = "yyyy-MM-dd HH:mm:ss";

            solutionIDs  = String.Empty;
            contestID    = -1;
            problemID    = -1;
            userName     = String.Empty;
            languageType = LanguageType.Null;
            resultType   = new Nullable <ResultType>();
            dtStart      = null;
            dtEnd        = null;

            if (!String.IsNullOrEmpty(sids))
            {
                solutionIDs = sids.SearchOptimized();

                if (!RegexVerify.IsNumericIDs(solutionIDs))
                {
                    throw new InvalidRequstException(RequestType.Solution);
                }

                noCondition = false;
            }

            if (!String.IsNullOrEmpty(cid))
            {
                if (!Int32.TryParse(cid, out contestID))
                {
                    throw new InvalidRequstException(RequestType.Contest);
                }

                if (contestID < ContestRepository.NONECONTEST)
                {
                    throw new InvalidRequstException(RequestType.Contest);
                }

                noCondition = false;
            }

            if (!String.IsNullOrEmpty(pid))
            {
                if (!Int32.TryParse(pid, out problemID))
                {
                    throw new InvalidRequstException(RequestType.Problem);
                }

                if (problemID < ConfigurationManager.ProblemSetStartID)
                {
                    throw new InvalidRequstException(RequestType.Problem);
                }

                noCondition = false;
            }

            if (!String.IsNullOrEmpty(name))
            {
                if (!RegexVerify.IsUserName(name))
                {
                    throw new InvalidRequstException(RequestType.User);
                }

                userName    = name;
                noCondition = false;
            }

            if (!String.IsNullOrEmpty(lang))
            {
                Byte langType = Byte.MaxValue;

                if (!Byte.TryParse(lang, out langType))
                {
                    throw new InvalidInputException("Language Type is INVALID!");
                }

                languageType = LanguageType.FromLanguageID(langType);
                noCondition  = false;
            }

            if (!String.IsNullOrEmpty(type))
            {
                Byte resType = Byte.MaxValue;

                if (!Byte.TryParse(type, out resType))
                {
                    throw new InvalidInputException("Result Type is INVALID!");
                }

                resultType  = (ResultType)resType;
                noCondition = false;
            }

            if (!String.IsNullOrEmpty(startDate))
            {
                DateTime temp;
                if (!DateTime.TryParseExact(startDate, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp))
                {
                    throw new InvalidInputException("Datetime is INVALID!");
                }

                dtStart     = temp;
                noCondition = false;
            }

            if (!String.IsNullOrEmpty(endDate))
            {
                DateTime temp;
                if (!DateTime.TryParseExact(endDate, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out temp))
                {
                    throw new InvalidInputException("Datetime is INVALID!");
                }

                dtEnd = temp;

                if (dtStart.HasValue && dtStart.Value >= dtEnd.Value)
                {
                    throw new InvalidInputException("Start date CANNOT be later than end date!");
                }

                noCondition = false;
            }

            return(noCondition);
        }
Exemple #12
0
        /// <summary>
        /// 尝试发送邮件
        /// </summary>
        /// <param name="model">邮件实体</param>
        /// <param name="error">出错信息</param>
        /// <returns>是否发送成功</returns>
        public static Boolean TrySendUserMail(UserMailEntity entity, out String error)
        {
            if (!UserManager.IsUserLogined)
            {
                error = "Please login first!";
                return(false);
            }

            if (String.IsNullOrEmpty(entity.Title))
            {
                error = "Title can not be NULL!";
                return(false);
            }

            if (entity.Title.Length > UserMailRepository.TITLE_MAXLEN)
            {
                error = "Title is too long!";
                return(false);
            }

            if (String.IsNullOrEmpty(entity.Content) || entity.Content.Length < UserMailRepository.CONTENT_MINLEN)
            {
                error = "Content is too short!";
                return(false);
            }

            if (entity.Content.Length > UserMailRepository.CONTENT_MAXLEN)
            {
                error = "Content is too long!";
                return(false);
            }

            if (String.IsNullOrEmpty(entity.ToUserName))
            {
                error = "Username can not be NULL!";
                return(false);
            }

            if (!RegexVerify.IsUserName(entity.ToUserName))
            {
                error = "Username is INVALID!";
                return(false);
            }

            if (String.Equals(ConfigurationManager.SystemAccount, entity.ToUserName, StringComparison.OrdinalIgnoreCase))
            {
                error = "You can not send mail to system account!";
                return(false);
            }

            if (String.Equals(UserManager.CurrentUserName, entity.ToUserName, StringComparison.OrdinalIgnoreCase))
            {
                error = "You can not send mail to yourself!";
                return(false);
            }

            if (!UserSubmitStatus.CheckLastSubmitUserMailTime(UserManager.CurrentUserName))
            {
                throw new InvalidInputException(String.Format("You can not submit user mail more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString()));
            }

            if (!UserManager.InternalExistsUser(entity.ToUserName))
            {
                error = String.Format("The username \"{0}\" doesn't exist!", entity.ToUserName);
                return(false);
            }

            entity.Title        = HtmlEncoder.HtmlEncode(entity.Title);
            entity.Content      = HtmlEncoder.HtmlEncode(entity.Content);
            entity.FromUserName = UserManager.CurrentUserName;

            if (!UserMailManager.InternalSendUserMail(entity))
            {
                error = "Failed to send your mail";
                return(false);
            }

            error = String.Empty;
            return(true);
        }
        /// <summary>
        /// 申请找回密码
        /// </summary>
        /// <param name="userName">用户名</param>
        /// <param name="email">电子邮箱</param>
        /// <param name="userip">用户IP</param>
        /// <param name="checkCode">验证码</param>
        /// <param name="link">找回密码链接</param>
        /// <returns>是否可以申请</returns>
        public static async Task <IMethodResult> RequestResetUserPassword(String userName, String email, String userip, String checkCode, String link)
        {
            if (!CheckCodeStatus.VerifyCheckCode(checkCode))
            {
                return(MethodResult.Failed("The verification code you input didn't match the picture, Please try again!"));
            }

            if (!RegexVerify.IsUserName(userName))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            if (!RegexVerify.IsEmail(email))
            {
                return(MethodResult.Failed("Email address is INVALID!"));
            }

            UserEntity user = UserManager.InternalGetUserByNameAndEmail(userName, email);

            if (user == null)
            {
                return(MethodResult.Failed("The username \"{0}\" doesn't exist or the email is wrong!", userName));
            }

            if (user.IsLocked)
            {
                return(MethodResult.Failed("The user is locked, please contact the administrator!"));
            }

            if (String.IsNullOrEmpty(user.Email) || "NULL".Equals(user.Email, StringComparison.OrdinalIgnoreCase))
            {
                return(MethodResult.Failed("The user has no email, please contact the administrator!"));
            }

            Random rand = new Random(DateTime.Now.Millisecond);

            UserForgetPasswordEntity ufp = new UserForgetPasswordEntity()
            {
                UserName   = userName,
                SubmitDate = DateTime.Now,
                SubmitIP   = userip,
                HashKey    = MD5Encrypt.EncryptToHexString(String.Format("{0}-{1}-{2}", userName, DateTime.Now.Ticks.ToString(), rand.Next(DateTime.Now.Millisecond)), true)
            };

            Boolean success = UserForgetPasswordRepository.Instance.InsertEntity(ufp) > 0;

            if (!success)
            {
                return(MethodResult.Failed("Failed to process your request!"));
            }

            String url         = ConfigurationManager.DomainUrl + ((link[0] == '/') ? link.Substring(1) : link);
            String mailSubject = ConfigurationManager.OnlineJudgeName + " Password Recovery";
            String mailContent = UserForgetPasswordManager.GetMailContent(userName, url + ufp.HashKey.ToLowerInvariant());

            try
            {
                await MailClient.SendMailAsync(ConfigurationManager.EmailSMTPServer, ConfigurationManager.EmailAddresser, email, mailSubject, mailContent, true, true, ConfigurationManager.EmailUsername, ConfigurationManager.EmailPassword);
            }
            catch
            {
                return(MethodResult.Failed("Failed to send a password reset link to your email address."));
            }

            return(MethodResult.SuccessAndLog("User forget password, name = {0}", userName));
        }
Exemple #14
0
        /// <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"));
        }