Exemplo n.º 1
0
        /// <summary>
        /// 更新主题锁定状态
        /// </summary>
        /// <param name="ids">主题ID列表</param>
        /// <param name="isLock">是否锁定</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateForumTopicIsLocked(String ids, Boolean isLocked)
        {
            if (!AdminManager.HasPermission(PermissionType.ForumManage))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsNumericIDs(ids))
            {
                return(MethodResult.InvalidRequest(RequestType.ForumTopic));
            }

            Boolean success = ForumTopicRepository.Instance.UpdateEntityIsLocked(ids, isLocked) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No forum topic was {0}!", isLocked ? "locked" : "unlocked"));
            }

            ids.ForEachInIDs(',', id =>
            {
                ForumTopicCache.RemoveForumTopicCache(id);//删除缓存
            });

            return(MethodResult.SuccessAndLog("Admin {1} forum topic, id = {0}", ids, isLocked ? "lock" : "unlock"));
        }
Exemplo n.º 2
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));
        }
Exemplo n.º 3
0
        /// <summary>
        /// 更新一条题目类型种类
        /// </summary>
        /// <param name="entity">题目类型种类实体</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateProblemCategory(ProblemCategoryEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (entity.TypeID <= 0)
            {
                return(MethodResult.InvalidRequest(RequestType.ProblemCategory));
            }

            if (String.IsNullOrEmpty(entity.Title))
            {
                return(MethodResult.FailedAndLog("Problem category title cannot be NULL!"));
            }

            Boolean success = ProblemCategoryRepository.Instance.UpdateEntity(entity) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No problem category was updated!"));
            }

            ProblemCategoryCache.RemoveProblemCategoryListCache();//删除缓存

            return(MethodResult.SuccessAndLog("Admin update problem category, id = {0}", entity.TypeID.ToString()));
        }
Exemplo n.º 4
0
        /// <summary>
        /// 更新题目隐藏状态
        /// </summary>
        /// <param name="ids">题目ID列表</param>
        /// <param name="isHide">隐藏状态</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateProblemIsHide(String ids, Boolean isHide)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsNumericIDs(ids))
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            Boolean success = ProblemRepository.Instance.UpdateEntityIsHide(ids, isHide) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No problem was {0}!", isHide ? "hided" : "unhided"));
            }

            ids.ForEachInIDs(',', id =>
            {
                ProblemCache.RemoveProblemCache(id);                         //删除缓存
                ProblemCache.RemoveProblemSetCache(GetProblemPageIndex(id)); //删除缓存
            });

            return(MethodResult.SuccessAndLog("Admin {1} problem, id = {0}", ids, isHide ? "hide" : "unhide"));
        }
Exemplo n.º 5
0
        /// <summary>
        /// 删除指定ID的题目类型种类
        /// </summary>
        /// <param name="id">题目类型种类ID</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteProblemCategory(Int32 id)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (id <= 0)
            {
                return(MethodResult.InvalidRequest(RequestType.ProblemCategory));
            }

            if (ProblemCategoryItemRepository.Instance.CountEntities(id) > 0)
            {
                return(MethodResult.FailedAndLog("This category still has some problems, please remove these problem from this category first!"));
            }

            Boolean success = ProblemCategoryRepository.Instance.DeleteEntity(id) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No problem category was deleted!"));
            }

            ProblemCategoryCache.RemoveProblemCategoryListCache();//删除缓存

            return(MethodResult.SuccessAndLog("Admin delete problem category, id = {0}", id.ToString()));
        }
Exemplo n.º 6
0
        /// <summary>
        /// 更新竞赛隐藏状态
        /// </summary>
        /// <param name="ids">竞赛ID列表</param>
        /// <param name="isHide">隐藏状态</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateContestIsHide(String ids, Boolean isHide)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsNumericIDs(ids))
            {
                return(MethodResult.InvalidRequest(RequestType.Contest));
            }

            Boolean success = ContestRepository.Instance.UpdateEntityIsHide(ids, isHide) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No contest was {0}!", isHide ? "hided" : "unhided"));
            }

            ids.ForEachInIDs(',', id =>
            {
                ContestCache.RemoveContestCache(id);//删除缓存
            });

            ContestCache.RemoveContestListCountCache();//删除缓存

            return(MethodResult.SuccessAndLog("Admin {1} contest, id = {0}", ids, isHide ? "hide" : "unhide"));
        }
Exemplo n.º 7
0
        /// <summary>
        /// 保存题目数据文件到磁盘
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="file">上传文件</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveProblemData(Int32 problemID, HttpPostedFileBase file)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            if (file == null)
            {
                return(MethodResult.FailedAndLog("No file was uploaded!"));
            }

            if (String.IsNullOrEmpty(file.FileName))
            {
                return(MethodResult.FailedAndLog("Filename can not be NULL!"));
            }

            FileInfo fi = new FileInfo(file.FileName);

            if (!".zip".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
            {
                return(MethodResult.FailedAndLog("Filename is INVALID!"));
            }

            if (!String.Equals(problemID.ToString(), Path.GetFileNameWithoutExtension(fi.Name)))
            {
                return(MethodResult.FailedAndLog("The problem data should have the same name as the problem ID!"));
            }

            if (file.ContentLength <= 0)
            {
                return(MethodResult.FailedAndLog("You can not upload empty file!"));
            }

            String fileNewName = problemID.ToString() + ".zip";
            String savePath    = Path.Combine(ConfigurationManager.ProblemDataPath, fileNewName);

            file.SaveAs(savePath);

            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.SuccessAndLog("Admin upload problem data, id = {0}", problemID.ToString()));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 更新一条主题页面
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <param name="oldname">旧的主题页面名</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateTopicPage(TopicPageEntity entity, String oldname)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsPageName(entity.PageName))
            {
                return(MethodResult.InvalidRequest(RequestType.TopicPage));
            }

            if (String.IsNullOrEmpty(entity.Title))
            {
                return(MethodResult.FailedAndLog("Page title can not be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Description))
            {
                return(MethodResult.FailedAndLog("Page description can not be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Content))
            {
                return(MethodResult.FailedAndLog("Page content can not be NULL!"));
            }

            entity.LastDate = DateTime.Now;

            Boolean success = TopicPageRepository.Instance.UpdateEntity(entity, oldname) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No page was updated!"));
            }

            TopicPageCache.RemoveTopicPageCache(oldname);//更新缓存

            return(MethodResult.SuccessAndLog("Admin update page, name = {0}{1}", entity.PageName,
                                              (!String.Equals(oldname, entity.PageName, StringComparison.OrdinalIgnoreCase) ? ", previous = " + oldname : "")));
        }
Exemplo n.º 9
0
        /// <summary>
        /// 根据ID得到一个竞赛实体
        /// </summary>
        /// <param name="id">竞赛ID</param>
        /// <returns>竞赛实体</returns>
        public static IMethodResult AdminGetContest(Int32 id)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            if (id < ContestRepository.NONECONTEST)
            {
                return(MethodResult.InvalidRequest(RequestType.Contest));
            }

            ContestEntity entity = ContestRepository.Instance.GetEntity(id);

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

            return(MethodResult.Success(entity));
        }
Exemplo n.º 10
0
        /// <summary>
        /// 获取题目数据物理路径
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <returns>题目数据物理路径</returns>
        public static IMethodResult AdminGetProblemDataDownloadPath(Int32 problemID)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            String dataPath = ProblemDataManager.GetProblemDataRealPath(problemID);

            if (String.IsNullOrEmpty(dataPath))
            {
                return(MethodResult.FailedAndLog("This problem doesn't have data!"));
            }

            return(MethodResult.SuccessAndLog <String>(dataPath, "Admin download problem data, id = {0}", problemID.ToString()));
        }
Exemplo n.º 11
0
        /// <summary>
        /// 保存题目数据文件到磁盘
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="problemdata">题目数据文件</param>
        /// <returns>是否保存成功</returns>
        internal static IMethodResult InternalAdminSaveProblemData(Int32 problemID, Byte[] problemdata)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            String fileNewName = problemID.ToString() + ".zip";
            String savePath    = Path.Combine(ConfigurationManager.ProblemDataPath, fileNewName);

            File.WriteAllBytes(savePath, problemdata);

            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.Success());
        }
Exemplo n.º 12
0
        /// <summary>
        /// 根据ID得到一个主题页面实体
        /// </summary>
        /// <param name="id">主题页面ID</param>
        /// <returns>主题页面实体</returns>
        public static IMethodResult AdminGetTopicPage(String name)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsPageName(name))
            {
                return(MethodResult.InvalidRequest(RequestType.TopicPage));
            }

            TopicPageEntity entity = TopicPageRepository.Instance.GetEntity(name);

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

            return(MethodResult.Success(entity));
        }
Exemplo n.º 13
0
        /// <summary>
        /// 根据ID得到一个对象实体
        /// </summary>
        /// <param name="resourceID">实体ID</param>
        /// <returns>对象实体</returns>
        public static IMethodResult AdminGetResource(Int32 id)
        {
            if (!AdminManager.HasPermission(PermissionType.ResourceManage))
            {
                throw new NoPermissionException();
            }

            if (id <= 0)
            {
                return(MethodResult.InvalidRequest(RequestType.Resource));
            }

            ResourceEntity entity = ResourceRepository.Instance.GetEntity(id);

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

            return(MethodResult.Success(entity));
        }
Exemplo n.º 14
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));
        }
Exemplo n.º 15
0
        /// <summary>
        /// 根据ID得到一个题目实体
        /// </summary>
        /// <param name="id">题目ID</param>
        /// <returns>题目实体</returns>
        public static IMethodResult AdminGetProblem(Int32 id)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (id < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            ProblemEntity entity = ProblemRepository.Instance.GetEntity(id);

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

            return(MethodResult.Success(entity));
        }
Exemplo n.º 16
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));
        }
Exemplo n.º 17
0
        /// <summary>
        /// 更新指定ID的用户是否锁定
        /// </summary>
        /// <param name="userNames">用户名</param>
        /// <param name="isLocked">是否锁定</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateUserIsLocked(String userNames, Boolean isLocked)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (String.IsNullOrEmpty(userNames))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            Boolean success = UserRepository.Instance.UpdateEntityIsLocked(userNames, isLocked) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No user was {0}!", isLocked ? "locked" : "unlocked"));
            }

            return(MethodResult.SuccessAndLog("Admin {1} user, name = {0}", userNames, isLocked ? "lock" : "unlock"));
        }
Exemplo n.º 18
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());
        }
Exemplo n.º 19
0
        /// <summary>
        /// 增加一条主题页面
        /// </summary>
        /// <param name="model">对象实体</param>
        /// <returns>是否成功增加</returns>
        public static IMethodResult AdminInsertTopicPage(TopicPageEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsPageName(entity.PageName))
            {
                return(MethodResult.InvalidRequest(RequestType.TopicPage));
            }

            if (String.IsNullOrEmpty(entity.Title))
            {
                return(MethodResult.FailedAndLog("Page title can not be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Description))
            {
                return(MethodResult.FailedAndLog("Page description can not be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Content))
            {
                return(MethodResult.FailedAndLog("Page content can not be NULL!"));
            }

            entity.CreateUser = UserManager.CurrentUserName;
            entity.IsHide     = true;
            entity.LastDate   = DateTime.Now;

            Boolean success = TopicPageRepository.Instance.InsertEntity(entity) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No page was added!"));
            }

            return(MethodResult.SuccessAndLog("Admin add page, name = {0}", entity.PageName));
        }
Exemplo n.º 20
0
        /// <summary>
        /// 删除指定ID的公告
        /// </summary>
        /// <param name="ids">逗号分隔的公告ID</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteNews(String ids)
        {
            if (!AdminManager.HasPermission(PermissionType.NewsManage))
            {
                throw new NoPermissionException();
            }

            if (!RegexVerify.IsNumericIDs(ids))
            {
                return(MethodResult.InvalidRequest(RequestType.News));
            }

            String[] arrids    = ids.Split(',');
            String   defaultID = NewsRepository.DEFAULTID.ToString();

            for (Int32 i = 0; i < arrids.Length; i++)
            {
                if (String.Equals(arrids[i], defaultID))
                {
                    return(MethodResult.FailedAndLog("Can not delete the default news!"));
                }
            }

            Boolean success = NewsRepository.Instance.DeleteEntities(ids) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No news was deleted!"));
            }

            ids.ForEachInIDs(',', id =>
            {
                NewsCache.RemoveNewsCache(id);      //删除缓存
            });
            NewsCache.RemoveLastestNewsListCache(); //删除缓存
            NewsCache.RemoveNewsCountCache();       //删除缓存

            return(MethodResult.SuccessAndLog("Admin delete news, id = {0}", ids));
        }
Exemplo n.º 21
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()));
        }
Exemplo n.º 22
0
        /// <summary>
        /// 更新题目通过总数
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateProblemSolvedCount(Int32 problemID)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            Boolean success = ProblemRepository.Instance.UpdateEntitySolvedCount(problemID) > 0;

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

            ProblemCache.RemoveProblemCache(problemID);                         //删除缓存
            ProblemCache.RemoveProblemSetCache(GetProblemPageIndex(problemID)); //删除缓存

            return(MethodResult.SuccessAndLog("Admin update problem's solved count, id = {0}", problemID.ToString()));
        }
Exemplo n.º 23
0
        /// <summary>
        /// 删除题目数据
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <returns>是否删除成功</returns>
        public static IMethodResult AdminDeleteProblemDataRealPath(Int32 problemID)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            String dataPath = ProblemDataManager.GetProblemDataRealPath(problemID);

            if (String.IsNullOrEmpty(dataPath))
            {
                return(MethodResult.FailedAndLog("This problem does not have data!"));
            }

            File.Delete(dataPath);
            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.SuccessAndLog("Admin delete problem data, id = {0}", problemID.ToString()));
        }
Exemplo n.º 24
0
        /// <summary>
        /// 设置指定ID的题目类型对
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="sourceIDs">旧逗号分隔的题目类型ID</param>
        /// <param name="targetIDs">新逗号分隔的题目类型ID</param>
        /// <returns>是否成功设置</returns>
        public static IMethodResult AdminUpdateProblemCategoryItems(Int32 problemID, String sourceIDs, String targetIDs)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            if ((!String.IsNullOrEmpty(sourceIDs) && !RegexVerify.IsNumericIDs(sourceIDs)) || (!String.IsNullOrEmpty(targetIDs) && !RegexVerify.IsNumericIDs(targetIDs)))
            {
                return(MethodResult.InvalidRequest(RequestType.ProblemCategory));
            }

            StringBuilder deleteIDs = new StringBuilder();
            StringBuilder insertIDs = new StringBuilder();
            List <String> sourceid  = (String.IsNullOrEmpty(sourceIDs) ? new List <String>() : new List <String>(sourceIDs.Split(',')));
            List <String> targetid  = (String.IsNullOrEmpty(targetIDs) ? new List <String>() : new List <String>(targetIDs.Split(',')));

            for (Int32 i = 0; i < targetid.Count; i++)//删除sourceIDs和targetIDs中相同的数据
            {
                for (Int32 j = 0; j < sourceid.Count; j++)
                {
                    if (String.Equals(targetid[i], sourceid[j], StringComparison.OrdinalIgnoreCase))
                    {
                        targetid.RemoveAt(i);
                        sourceid.RemoveAt(j);
                        i--;
                        j--;
                        break;
                    }
                }
            }

            for (Int32 i = 0; i < sourceid.Count; i++)//设置deleteIDs
            {
                if (i > 0)
                {
                    deleteIDs.Append(',');
                }
                deleteIDs.Append(sourceid[i]);
            }

            for (Int32 i = 0; i < targetid.Count; i++)//设置insertIDs
            {
                if (i > 0)
                {
                    insertIDs.Append(',');
                }
                insertIDs.Append(targetid[i]);
            }

            Boolean ret = true;

            if (deleteIDs.Length > 0)
            {
                ret &= (ProblemCategoryItemRepository.Instance.DeleteEntities(problemID, deleteIDs.ToString()) > 0);
            }
            if (insertIDs.Length > 0)
            {
                ret &= (ProblemCategoryItemRepository.Instance.InsertEntity(problemID, insertIDs.ToString()) > 0);
            }

            if (!ret)
            {
                return(MethodResult.FailedAndLog("No problem's category was updated!"));
            }

            return(MethodResult.SuccessAndLog("Admin update problem's category, id = {0}, new category = {1}", problemID.ToString(), targetIDs));
        }
Exemplo n.º 25
0
        /// <summary>
        /// 保存题目数据文件到磁盘
        /// </summary>
        /// <param name="problemID">题目ID</param>
        /// <param name="forms">Request.Forms</param>
        /// <param name="httpFiles">Request.Files</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveProblemData(Int32 problemID, NameValueCollection forms, HttpFileCollectionBase httpFiles)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (problemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            SortedDictionary <String, String> dictData = new SortedDictionary <String, String>();

            if (httpFiles != null)
            {
                for (Int32 i = 0; i < httpFiles.Count; i++)
                {
                    HttpPostedFileBase file = httpFiles[i];

                    if (String.IsNullOrEmpty(file.FileName))
                    {
                        return(MethodResult.FailedAndLog("Filename can not be NULL!"));
                    }

                    FileInfo fi = new FileInfo(file.FileName);
                    if (httpFiles.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 && !".in".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (httpFiles.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0 && !".out".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase) && !".ans".Equals(fi.Extension, StringComparison.OrdinalIgnoreCase))
                    {
                        return(MethodResult.FailedAndLog("Filename is INVALID!"));
                    }

                    if (file.ContentLength <= 0)
                    {
                        return(MethodResult.FailedAndLog("You can not upload empty file!"));
                    }

                    StreamReader sr = new StreamReader(file.InputStream);
                    dictData.Add(httpFiles.GetKey(i), sr.ReadToEnd());
                }
            }

            if (forms != null)
            {
                for (Int32 i = 0; i < forms.Count; i++)
                {
                    if (forms.GetKey(i).IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0 || forms.GetKey(i).IndexOf("out", StringComparison.OrdinalIgnoreCase) == 0)
                    {
                        dictData.Add(forms.GetKey(i), forms[i]);
                    }
                }
            }

            if (dictData.Count == 0)
            {
                return(MethodResult.FailedAndLog("No data was added!"));
            }

            if (dictData.Count % 2 != 0)
            {
                return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
            }

            ProblemDataWriter writer = new ProblemDataWriter();

            foreach (KeyValuePair <String, String> pair in dictData)
            {
                if (pair.Key.IndexOf("in", StringComparison.OrdinalIgnoreCase) == 0)
                {
                    String input  = pair.Value;
                    String output = String.Empty;

                    if (!dictData.TryGetValue(pair.Key.ToLower().Replace("in", "out"), out output))
                    {
                        return(MethodResult.FailedAndLog("The count of uploaded data is INVALID!"));
                    }

                    writer.WriteData(input, output);
                }
            }

            String fileNewName = problemID.ToString() + ".zip";
            String savePath    = Path.Combine(ConfigurationManager.ProblemDataPath, fileNewName);

            Byte[] data = writer.WriteTo();
            File.WriteAllBytes(savePath, data);

            ProblemDataCache.RemoveProblemDataVersionCache(problemID);

            return(MethodResult.SuccessAndLog("Admin create problem data, id = {0}", problemID.ToString()));
        }
Exemplo n.º 26
0
        /// <summary>
        /// 更新题目信息
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateProblem(ProblemEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

            if (entity.ProblemID < ConfigurationManager.ProblemSetStartID)
            {
                return(MethodResult.InvalidRequest(RequestType.Problem));
            }

            if (String.IsNullOrEmpty(entity.Title))
            {
                return(MethodResult.FailedAndLog("Problem title cannot be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Description))
            {
                return(MethodResult.FailedAndLog("Problem description cannot be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Input))
            {
                return(MethodResult.FailedAndLog("Problem input cannot be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Output))
            {
                return(MethodResult.FailedAndLog("Problem output cannot be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.SampleInput))
            {
                return(MethodResult.FailedAndLog("Problem sample input cannot be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.SampleOutput))
            {
                return(MethodResult.FailedAndLog("Problem sample output cannot be NULL!"));
            }

            if (entity.TimeLimit <= 0)
            {
                return(MethodResult.FailedAndLog("Time limit must not be less or equal than zero!"));
            }

            if (entity.MemoryLimit <= 0)
            {
                return(MethodResult.FailedAndLog("Memory limit must not be less or equal than zero!"));
            }

            entity.LastDate = DateTime.Now;
            Boolean success = ProblemRepository.Instance.UpdateEntity(entity) > 0;

            if (!success)
            {
                return(MethodResult.FailedAndLog("No problem was updated!"));
            }

            ProblemCache.RemoveProblemCache(entity.ProblemID);//删除缓存
            ProblemCache.RemoveProblemSetCache(GetProblemPageIndex(entity.ProblemID));

            return(MethodResult.SuccessAndLog("Admin update problem, id = {0}", entity.ProblemID.ToString()));
        }
Exemplo n.º 27
0
        /// <summary>
        /// 添加指定ID的竞赛用户
        /// </summary>
        /// <param name="cid">竞赛ID</param>
        /// <param name="usernames">竞赛用户</param>
        /// <returns>是否成功添加</returns>
        public static IMethodResult AdminInsertContestUsers(Int32 cid, String usernames)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            if (String.IsNullOrEmpty(usernames))
            {
                return(MethodResult.InvalidRequest(RequestType.User));
            }

            Dictionary <String, ContestUserEntity> dict = new Dictionary <String, ContestUserEntity>();
            StringBuilder names = new StringBuilder();

            String[] namelist = usernames.Split('\n');

            for (Int32 i = 0; i < namelist.Length; i++)
            {
                String[] name = namelist[i].Replace('\t', ' ').Split(' ');

                if (name.Length < 1 || String.IsNullOrEmpty(name[0].Trim()))
                {
                    continue;
                }

                ContestUserEntity entity = new ContestUserEntity()
                {
                    ContestID    = cid,
                    UserName     = name[0].Replace("\r", "").Trim(),
                    RealName     = (name.Length >= 2 ? name[1].Replace("\r", "").Trim() : ""),
                    IsEnable     = true,
                    RegisterTime = DateTime.Now
                };

                if (!dict.ContainsKey(entity.UserName))
                {
                    names.Append(names.Length > 0 ? "," : "").Append(entity.UserName);
                }

                dict[entity.UserName] = entity;
            }

            try
            {
                Int32 count = ContestUserRepository.Instance.InsertEntities(cid, names.ToString(), dict);

                if (count <= 0)
                {
                    return(MethodResult.FailedAndLog("No contest user was added!"));
                }

                ContestUserCache.RemoveContestUserListCache(cid);

                return(MethodResult.SuccessAndLog <Int32>(count, "Admin add contest user, cid = {0}, username = {1}", cid.ToString(), usernames));
            }
            catch (DbException)
            {
                return(MethodResult.FailedAndLog("Failed to add these users, please check whether the names are all correct."));
            }
        }
Exemplo n.º 28
0
        /// <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));
        }