/// <summary> /// 尝试将使用用户名密码登陆系统 /// </summary> /// <param name="userName">用户名</param> /// <param name="passWord">密码</param> /// <param name="userip">用户IP</param> /// <returns>返回是否成功登陆,若失败则返回出错信息</returns> public static IMethodResult SignIn(String userName, String passWord, String userip) { UserEntity user = null; String error = TryGetUserByUsernameAndPassword(userName, passWord, out user); if (!String.IsNullOrEmpty(error)) { return(MethodResult.FailedAndLog(error)); } if (AdminManager.InternalCheckPermission(user.Permission, PermissionType.HttpJudge)) { return(MethodResult.FailedAndLog("You can not login a httpjudge account!")); } try { Int32 unreadMailCount = UserMailManager.InternalCountUserUnReadMails(user.UserName); UserBrowserStatus.SetCurrentUserBrowserStatus(user.UserName, user.Permission, unreadMailCount); UserSubmitStatus.InitLastSubmitTime(user.UserName); UserCurrentStatus.SetCurrentUserStatus(user); UpdateLoginInfomation(userName, userip); } catch (System.Exception ex) { return(MethodResult.Failed(ex.Message)); } return(MethodResult.SuccessAndLog("User sign in")); }
/// <summary> /// 用户登出系统 /// </summary> public static void SignOut() { String userName = UserManager.CurrentUserName; UserCurrentStatus.RemoveCurrentUserStatus(); UserBrowserStatus.RemoveCurrentUserBrowserStatus(); UserSubmitStatus.RemoveLastSubmitTime(userName); UserMailCache.RemoveUserUnReadMailCountCache(userName); }
/// <summary> /// 用户注销 /// </summary> /// <returns>操作后的结果</returns> public ActionResult Logout() { String userName = UserManager.CurrentUserName; UserCurrentStatus.RemoveCurrentUserStatus(); UserBrowserStatus.RemoveCurrentUserBrowserStatus(); UserSubmitStatus.RemoveLastSubmitTime(userName); UserMailCache.RemoveUserUnReadMailCountCache(userName); return(RedirectToRefferer()); }
/// <summary> /// 增加一条提交 /// </summary> /// <param name="entity">对象实体</param> /// <param name="userip">用户IP</param> /// <returns>是否成功增加</returns> public static Boolean InsertSolution(SolutionEntity entity, String userip) { if (!UserManager.IsUserLogined) { throw new UserUnLoginException(); } if (String.IsNullOrEmpty(entity.SourceCode) || entity.SourceCode.Length < SolutionRepository.SOURCECODE_MINLEN) { throw new InvalidInputException("Code is too short!"); } if (entity.SourceCode.Length > SolutionRepository.SOURCECODE_MAXLEN) { throw new InvalidInputException("Code is too long!"); } if (LanguageType.IsNull(entity.LanguageType)) { throw new InvalidInputException("Language Type is INVALID!"); } if (!UserSubmitStatus.CheckLastSubmitSolutionTime(UserManager.CurrentUserName)) { throw new InvalidInputException(String.Format("You can not submit code more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString())); } ProblemEntity problem = ProblemManager.InternalGetProblemModel(entity.ProblemID); if (problem == null)//判断题目是否存在 { throw new NullResponseException(RequestType.Problem); } if (entity.ContestID <= 0 && problem.IsHide && !AdminManager.HasPermission(PermissionType.ProblemManage))//非竞赛下判断是否有权访问题目 { throw new NoPermissionException("You have no privilege to submit the problem!"); } entity.UserName = UserManager.CurrentUserName; entity.SubmitTime = DateTime.Now; entity.SubmitIP = userip; Boolean success = SolutionRepository.Instance.InsertEntity(entity) > 0; if (success) { ProblemCache.UpdateProblemCacheSubmitCount(entity.ProblemID, -1);//更新缓存 SolutionCache.RemoveProblemIDListCache(entity.UserName, true); } return(success); }
/// <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="post">帖子实体</param> /// <param name="topic">主题实体</param> /// <param name="parentPost">回复的帖子实体</param> /// <param name="postip">发布者IP</param> /// <param name="link">当前页面地址</param> /// <returns>是否成功增加</returns> public static Boolean InsertForumPost(ForumPostEntity post, ForumTopicEntity topic, ForumPostEntity parentPost, String postip, String link) { if (!UserManager.IsUserLogined) { throw new UserUnLoginException(); } if (String.IsNullOrEmpty(post.Title)) { throw new InvalidInputException("Reply title can not be NULL!"); } if (post.Title.Length > ForumPostRepository.TITLE_MAXLEN) { throw new InvalidInputException("Reply title is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(post.Title)) { throw new InvalidInputException("Reply title can not contain illegal keywords!"); } if (String.IsNullOrEmpty(post.Content) || post.Content.Length < ForumPostRepository.POST_MINLEN) { throw new InvalidInputException("Reply content is too short!"); } if (post.Content.Length > ForumPostRepository.POST_MAXLEN) { throw new InvalidInputException("Reply content is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(post.Content)) { throw new InvalidInputException("Reply content can not contain illegal keywords!"); } if (parentPost.Deepth + 1 < ForumPostRepository.DEEPTH_MIN) { throw new InvalidInputException("Reply deepth is INVALID!"); } if (parentPost.Deepth + 1 > ForumPostRepository.DEEPTH_MAX) { throw new InvalidInputException("Reply deepth is too deep!"); } if (topic.IsLocked) { throw new NoPermissionException("You have no privilege to reply the post!"); } if (!UserSubmitStatus.CheckLastSubmitForumPostTime(UserManager.CurrentUserName)) { throw new InvalidInputException(String.Format("You can not submit post more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString())); } post.TopicID = parentPost.TopicID; post.Title = HtmlEncoder.HtmlEncode(post.Title); post.Content = HtmlEncoder.HtmlEncode(post.Content); post.UserName = UserManager.CurrentUserName; post.Deepth = parentPost.Deepth + 1; post.ParentPostID = parentPost.PostID; post.PostDate = DateTime.Now; post.PostIP = postip; Boolean success = ForumPostRepository.Instance.InsertEntity(post) > 0; if (success && !String.Equals(parentPost.UserName, post.UserName)) { if (ConfigurationManager.ReplyPostMailNotification) { try { UserMailEntity mail = new UserMailEntity(); String url = ConfigurationManager.DomainUrl + ((link[0] == '/') ? link.Substring(1) : link); mail.FromUserName = ConfigurationManager.SystemAccount; mail.ToUserName = parentPost.UserName; mail.Title = "Your post has new reply!"; mail.Content = String.Format("Your post \"{0}\" has new reply, <br/>", parentPost.Title) + String.Format("Please visit <a href=\"{0}\" target=\"_blank\">{0}</a>", url); UserMailManager.InternalSendUserMail(mail); } catch { } } } return(success); }
/// <summary> /// 发布新主题 /// </summary> /// <param name="topic">主题实体</param> /// <param name="cid">竞赛ID</param> /// <param name="pid">题目ID</param> /// <param name="content">主题帖内容</param> /// <param name="postip">发布者IP</param> /// <returns>是否成功发布</returns> public static Boolean InsertForumTopic(ForumTopicEntity topic, String cid, String pid, String content, String postip) { if (!UserManager.IsUserLogined) { throw new UserUnLoginException(); } if (String.IsNullOrEmpty(topic.Title)) { throw new InvalidInputException("Topic title can not be NULL!"); } if (topic.Title.Length > ForumPostRepository.TITLE_MAXLEN) { throw new InvalidInputException("Topic title is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(topic.Title)) { throw new InvalidInputException("Topic title can not contain illegal keywords!"); } if (String.IsNullOrEmpty(content) || content.Length < ForumPostRepository.POST_MINLEN) { throw new InvalidInputException("Topic content is too short!"); } if (content.Length > ForumPostRepository.POST_MAXLEN) { throw new InvalidInputException("Topic content is too long!"); } if (!KeywordsFilterManager.IsForumPostContentLegal(content)) { throw new InvalidInputException("Topic content can not contain illegal keywords!"); } if (!UserSubmitStatus.CheckLastSubmitForumPostTime(UserManager.CurrentUserName)) { throw new InvalidInputException(String.Format("You can not submit post more than twice in {0} seconds!", ConfigurationManager.SubmitInterval.ToString())); } topic.Type = ForumTopicManager.GetForumTopicType(cid, pid); topic.RelativeID = (topic.Type == ForumTopicType.Default ? 0 : ForumTopicManager.GetRelativeID(cid, pid)); if (topic.Type == ForumTopicType.Problem && !ProblemManager.InternalExistsProblem(topic.RelativeID)) { throw new InvalidRequstException(RequestType.Problem); } else if (topic.Type == ForumTopicType.Contest && !ContestManager.InternalExistsContest(topic.RelativeID)) { throw new InvalidRequstException(RequestType.Contest); } topic.UserName = UserManager.CurrentUserName; topic.LastDate = DateTime.Now; topic.Title = HtmlEncoder.HtmlEncode(topic.Title); content = HtmlEncoder.HtmlEncode(content); Boolean success = ForumTopicRepository.Instance.InsertEntity(topic, content, postip) > 0; if (success) { ForumTopicCache.IncreaseForumTopicCountCache(topic.Type, topic.RelativeID);//更新缓存 if (topic.Type == ForumTopicType.Problem) { ForumTopicCache.IncreaseForumTopicCountCache(ForumTopicType.Default, 0);//更新缓存 } } return(success); }