Example #1
0
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns>是否成功增加</returns>
        public static IMethodResult AdminInsertResource(ResourceEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.ResourceManage))
            {
                throw new NoPermissionException();
            }

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

            if (String.IsNullOrEmpty(entity.Url))
            {
                return(MethodResult.FailedAndLog("Resource url can not be NULL!"));
            }

            if (String.IsNullOrEmpty(entity.Type))
            {
                return(MethodResult.FailedAndLog("Resource type can not be NULL!"));
            }

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

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

            ResourceCache.RemoveResourceListCache();//删除缓存

            return(MethodResult.SuccessAndLog("Admin add resource, title = {0}", entity.Title));
        }
Example #2
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"));
        }
Example #3
0
        /// <summary>
        /// 增加一条公告
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns>是否成功增加</returns>
        public static IMethodResult AdminInsertNews(NewsEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.NewsManage))
            {
                throw new NoPermissionException();
            }

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

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

            entity.PublishDate = DateTime.Now;

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

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

            NewsCache.RemoveLastestNewsListCache(); //删除缓存
            NewsCache.RemoveNewsCountCache();       //删除缓存

            return(MethodResult.SuccessAndLog("Admin add news, title = {0}", entity.Title));
        }
Example #4
0
        /// <summary>
        /// 更新一条公告
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateNews(NewsEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.NewsManage))
            {
                throw new NoPermissionException();
            }

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

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

            entity.PublishDate = DateTime.Now;

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

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

            NewsCache.SetNewsCache(entity);//更新缓存

            if (entity.AnnounceID != NewsRepository.DEFAULTID)
            {
                NewsCache.RemoveLastestNewsListCache();//删除缓存
            }

            return(MethodResult.SuccessAndLog("Admin update news, id = {0}", entity.AnnounceID.ToString()));
        }
Example #5
0
        /// <summary>
        /// 更新竞赛信息
        /// </summary>
        /// <param name="entity">对象实体</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateContest(ContestEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

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

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

            if (entity.StartTime >= entity.EndTime)
            {
                return(MethodResult.FailedAndLog("Start time must be less than end time!"));
            }

            if (entity.ContestType == ContestType.RegisterPrivate || entity.ContestType == ContestType.RegisterPublic)
            {
                if (!entity.RegisterStartTime.HasValue || !entity.RegisterEndTime.HasValue)
                {
                    return(MethodResult.FailedAndLog("Register time cannot be NULL!"));
                }

                if (entity.RegisterStartTime >= entity.RegisterEndTime)
                {
                    return(MethodResult.FailedAndLog("Register start time must be less than register end time!"));
                }

                if (entity.RegisterEndTime >= entity.StartTime)
                {
                    return(MethodResult.FailedAndLog("Register end time must be less than contest start time!"));
                }
            }

            entity.LastDate = DateTime.Now;

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

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

            ContestCache.RemoveContestCache(entity.ContestID); //删除缓存
            ContestCache.RemoveContestListCountCache();        //删除缓存

            return(MethodResult.SuccessAndLog("Admin update contest, id = {0}", entity.ContestID.ToString()));
        }
        /// <summary>
        /// 保存配置到配置文件
        /// </summary>
        /// <param name="col">配置信息</param>
        public static IMethodResult AdminSaveToConfig(NameValueCollection col)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!ConfigurationManager.SaveToConfig(col))
            {
                return(MethodResult.FailedAndLog("The configuration has not been modified!"));
            }

            return(MethodResult.SuccessAndLog("Admin update web.config"));
        }
Example #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()));
        }
Example #8
0
        /// <summary>
        /// 删除评测机账号
        /// </summary>
        /// <param name="userName">评测机ID</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteJudgeAccount(String userName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            IMethodResult ret = UserManager.InternalAdminUpdatePermission(userName, PermissionType.None);

            if (!ret.IsSuccess)
            {
                return(MethodResult.FailedAndLog("No judger was deleted!"));
            }

            return(MethodResult.SuccessAndLog("Admin delete judger, name = {0}", userName));
        }
Example #9
0
        /// <summary>
        /// 压缩指定数据库
        /// </summary>
        /// <param name="fileName">数据库文件名</param>
        /// <returns>是否压缩成功</returns>
        public static IMethodResult AdminCompactDataBase(String fileName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!DatabaseManager.IsAccessDB)
            {
                throw new DatabaseNotSupportException();
            }

            String sourcePath = Path.Combine(DatabaseManager.AccessDataDirectory, fileName);
            String tempPath   = Path.Combine(DatabaseManager.AccessDataDirectory, DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".resx");

            if (!File.Exists(sourcePath))
            {
                return(MethodResult.FailedAndLog("Database does not exist!"));
            }

            if (DatabaseManager.DataBaseConnectionString.IndexOf("Microsoft.Jet.OLEDB.4.0", StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                Object objJRO     = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
                String connSource = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + sourcePath;
                String connTemp   = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tempPath;

                Object[] args = new Object[] { connSource, connTemp };
                objJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, args);
            }
            else if (DatabaseManager.DataBaseConnectionString.IndexOf("Microsoft.ACE.OLEDB.12.0", StringComparison.InvariantCultureIgnoreCase) > -1)
            {
                Object   objDAO = Activator.CreateInstance(Type.GetTypeFromProgID("Dao.DBEngine.120"));
                Object[] args   = new Object[] { sourcePath, tempPath };

                objDAO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objDAO, args);
            }
            else
            {
                return(MethodResult.FailedAndLog("Your database engine doesn't support database compacting!"));
            }

            File.Copy(tempPath, sourcePath, true);
            File.Delete(tempPath);

            return(MethodResult.SuccessAndLog("Admin compact database, name = {0}", fileName));
        }
Example #10
0
        /// <summary>
        /// 获取竞赛排行Excel文件
        /// </summary>
        /// <param name="cid">竞赛ID</param>
        /// <param name="userrealnames">用户姓名对照表</param>
        /// <returns>竞赛排行</returns>
        public static IMethodResult AdminGetExportRanklist(Int32 cid, String userrealnames)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            ContestEntity contest = ContestManager.GetContest(cid);
            Dictionary <String, String>   userdict    = null;
            Dictionary <String, RankItem> rank        = SolutionRepository.Instance.GetContestRanklist(contest.ContestID, contest.StartTime);
            List <ContestProblemEntity>   problemlist = ContestProblemManager.GetContestProblemList(contest.ContestID);
            List <RankItem> list = new List <RankItem>();

            foreach (RankItem userRank in rank.Values)
            {
                list.Add(userRank);
            }

            list.Sort();

            if (!String.IsNullOrEmpty(userrealnames))
            {
                userdict = new Dictionary <String, String>();
                String[] nametable = userrealnames.Lines();

                for (Int32 i = 0; i < nametable.Length; i++)
                {
                    if (String.IsNullOrEmpty(nametable[i]))
                    {
                        continue;
                    }

                    String[] namepair = nametable[i].Replace('\t', ' ').Split(' ');

                    if (namepair.Length == 2 && !String.IsNullOrEmpty(namepair[0]) && !String.IsNullOrEmpty(namepair[1]))
                    {
                        userdict.Add(namepair[0], namepair[1]);
                    }
                }
            }

            Byte[] data = ContestResultExport.ExportResultToExcel(contest, problemlist, list, userdict);

            return(MethodResult.SuccessAndLog <Byte[]>(data, "Admin export contest result, id = {0}", cid.ToString()));
        }
Example #11
0
        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="ids">逗号分隔的实体ID</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteResources(String ids)
        {
            if (!AdminManager.HasPermission(PermissionType.ResourceManage))
            {
                throw new NoPermissionException();
            }

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

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

            ResourceCache.RemoveResourceListCache();//删除缓存

            return(MethodResult.SuccessAndLog("Admin delete resource, id = {0}", ids));
        }
Example #12
0
        /// <summary>
        /// 更新主题页面隐藏状态
        /// </summary>
        /// <param name="id">主题页面名称</param>
        /// <param name="isHide">隐藏状态</param>
        /// <returns>是否成功更新</returns>
        public static IMethodResult AdminUpdateTopicPageIsHide(String name, Boolean isHide)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            Boolean success = TopicPageRepository.Instance.UpdateEntityIsHide(name, isHide) > 0;

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

            TopicPageCache.RemoveTopicPageCache(name);//删除缓存

            return(MethodResult.SuccessAndLog("Admin {1} page, name = {0}", name, isHide ? "hide" : "unhide"));
        }
        /// <summary>
        /// 设置竞赛题目列表
        /// </summary>
        /// <param name="cid">竞赛ID</param>
        /// <param name="problemids">题目ID列表</param>
        /// <returns>是否成功设置</returns>
        public static IMethodResult AdminSetContestProblemList(Int32 cid, String problemids)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            if (String.IsNullOrEmpty(problemids))
            {
                problemids = "";
            }

            String[]     ids        = problemids.Lines();
            List <Int32> problemIDs = new List <Int32>();

            for (Int32 i = 0; i < ids.Length; i++)
            {
                Int32 pid = 0;

                if (!String.IsNullOrEmpty(ids[i].Trim()) && Int32.TryParse(ids[i].Trim(), out pid))
                {
                    problemIDs.Add(pid);
                }
            }

            ContestProblemCache.RemoveContestProblemListCache(cid);//删除缓存

            try
            {
                Boolean success = ContestProblemRepository.Instance.InsertEntities(cid, problemIDs) > 0;

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

                return(MethodResult.SuccessAndLog("Admin add contest problem, cid = {0}, pid = {1}", cid.ToString(), String.Join(",", ids)));
            }
            catch (DbException)
            {
                return(MethodResult.FailedAndLog("Failed to add these problems, please check whether the problem ids are all correct."));
            }
        }
Example #14
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 : "")));
        }
Example #15
0
        /// <summary>
        /// 保存单个文件到磁盘
        /// </summary>
        /// <param name="file">上传的文件</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveUploadFile(HttpPostedFileBase file)
        {
            if (!AdminManager.HasPermission(PermissionType.Administrator))
            {
                throw new NoPermissionException();
            }

            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 (!UploadsManager.CheckFileExtension(fi.Extension, ALLOW_EXTENSTIONS))
            {
                return(MethodResult.FailedAndLog("Filename is INVALID!"));
            }

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

            String fileNewName = MD5Encrypt.EncryptToHexString(fi.Name + DateTime.Now.ToString("yyyyMMddHHmmssffff"), true) + fi.Extension;
            String savePath    = Path.Combine(ConfigurationManager.UploadDirectoryPath, fileNewName);

            if (File.Exists(savePath))
            {
                return(MethodResult.FailedAndLog("Filename exists!"));
            }

            file.SaveAs(savePath);

            return(MethodResult.SuccessAndLog <String>(fileNewName, "Admin upload file, name = {0}, origin = {1}", fileNewName, fi.Name));
        }
Example #16
0
        /// <summary>
        /// 保存数据库到磁盘
        /// </summary>
        /// <param name="file">上传文件</param>
        /// <returns>是否保存成功</returns>
        public static IMethodResult AdminSaveUploadDataBase(HttpPostedFileBase file)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!DatabaseManager.IsAccessDB)
            {
                throw new DatabaseNotSupportException();
            }

            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 (!String.Equals(fi.Extension, ".resx", StringComparison.OrdinalIgnoreCase))
            {
                return(MethodResult.FailedAndLog("Filename is INVALID!"));
            }

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

            String newName  = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss") + ".resx";
            String savePath = Path.Combine(DatabaseManager.AccessDataDirectory, newName);

            file.SaveAs(savePath);

            return(MethodResult.SuccessAndLog("Admin upload database, name = {0}", newName));
        }
Example #17
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()));
        }
Example #18
0
        /// <summary>
        /// 删除指定ID的主题页面
        /// </summary>
        /// <param name="ids">逗号分隔的主题页面ID</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteTopicPages(String names)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            Boolean success = TopicPageRepository.Instance.DeleteEntities(names) > 0;

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

            names.ForEachInIDs(',', name =>
            {
                TopicPageCache.RemoveTopicPageCache(name);//删除缓存
            });

            return(MethodResult.SuccessAndLog("Admin delete page, name = {0}", names));
        }
Example #19
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));
        }
Example #20
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"));
        }
Example #21
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));
        }
Example #22
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()));
        }
Example #23
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));
        }
Example #24
0
        /// <summary>
        /// 删除上传文件
        /// </summary>
        /// <param name="fileName">文件名</param>
        /// <returns>是否删除成功</returns>
        public static IMethodResult AdminDeleteUploadFile(String fileName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

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

            String filePath = Path.Combine(ConfigurationManager.UploadDirectoryPath, fileName);

            if (!File.Exists(filePath))
            {
                return(MethodResult.FailedAndLog("File does not exist!"));
            }

            File.Delete(filePath);

            return(MethodResult.SuccessAndLog("Admin delete upload file, name = {0}", fileName));
        }
Example #25
0
        /// <summary>
        /// 增加一条题目类型种类
        /// </summary>
        /// <param name="entity">题目类型种类实体</param>
        /// <returns>是否成功增加</returns>
        public static IMethodResult AdminInsertProblemCategory(ProblemCategoryEntity entity)
        {
            if (!AdminManager.HasPermission(PermissionType.ProblemManage))
            {
                throw new NoPermissionException();
            }

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

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

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

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

            return(MethodResult.SuccessAndLog("Admin add problem category, title = {0}", entity.Title));
        }
Example #26
0
        /// <summary>
        /// 删除指定ID的竞赛用户
        /// </summary>
        /// <param name="cid">竞赛ID</param>
        /// <param name="usernames">逗号分隔的用户名</param>
        /// <returns>是否成功删除</returns>
        public static IMethodResult AdminDeleteContestUsers(Int32 cid, String usernames)
        {
            if (!AdminManager.HasPermission(PermissionType.ContestManage))
            {
                throw new NoPermissionException();
            }

            if (String.IsNullOrEmpty(usernames))
            {
                return(MethodResult.FailedAndLog("You must select at least one item!"));
            }

            Boolean success = ContestUserRepository.Instance.DeleteEntities(cid, usernames) > 0;

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

            ContestUserCache.RemoveContestUserListCache(cid);

            return(MethodResult.SuccessAndLog("Admin delete contest user, cid = {0}, username = {1}", cid.ToString(), usernames));
        }
Example #27
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()));
        }
Example #28
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()));
        }
Example #29
0
        /// <summary>
        /// 备份指定数据库
        /// </summary>
        /// <param name="fileName">数据库文件名</param>
        /// <returns>是否备份成功</returns>
        public static IMethodResult AdminBackupDataBase(String fileName)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!DatabaseManager.IsAccessDB)
            {
                throw new DatabaseNotSupportException();
            }

            String sourcePath = DatabaseManager.AccessDBFullPath;
            String destPath   = Path.Combine(DatabaseManager.AccessDataDirectory, fileName);

            if (String.Equals(sourcePath, destPath, StringComparison.OrdinalIgnoreCase))
            {
                return(MethodResult.FailedAndLog("You can not use this filename!"));
            }

            File.Copy(sourcePath, destPath, true);

            return(MethodResult.SuccessAndLog("Admin backup database, name = {0}", fileName));
        }
Example #30
0
        /// <summary>
        /// 导入题目(不存在时返回null)
        /// </summary>
        /// <param name="request">Http请求</param>
        /// <param name="fileType">文件类型</param>
        /// <param name="uploadType">上传方式</param>
        /// <param name="content">文件内容</param>
        /// <param name="file">上传文件</param>
        /// <returns>题目数据是否插入成功集合(全部失败时为null)</returns>
        public static IMethodResult AdminImportProblem(HttpRequestBase request, String fileType, String uploadType, String content, HttpPostedFileBase file)
        {
            if (!AdminManager.HasPermission(PermissionType.SuperAdministrator))
            {
                throw new NoPermissionException();
            }

            if (!String.Equals("1", fileType))
            {
                return(MethodResult.FailedAndLog("File type is INVALID!"));
            }

            if (String.Equals("1", uploadType))//从文件上传
            {
                if (file == null)
                {
                    return(MethodResult.FailedAndLog("No file was uploaded!"));
                }

                StreamReader sr = new StreamReader(file.InputStream);

                content = sr.ReadToEnd();
            }

            //转换题库模型
            List <ProblemEntity> problems = null;
            List <Byte[]>        datas    = null;
            List <Dictionary <String, Byte[]> > images     = null;
            Dictionary <String, Byte[]>         imagefiles = new Dictionary <String, Byte[]>();

            if (!ProblemImport.TryImportFreeProblemSet(content, out problems, out datas, out images))
            {
                return(MethodResult.FailedAndLog("File content is INVALID!"));
            }

            if (problems == null || problems.Count == 0)
            {
                return(MethodResult.FailedAndLog("No problem was imported!"));
            }

            //处理题目及图片路径
            for (Int32 i = 0; i < problems.Count; i++)
            {
                problems[i].IsHide   = true;
                problems[i].LastDate = DateTime.Now;

                if (images[i] == null)
                {
                    continue;
                }

                String uploadRoot = ConfigurationManager.UploadDirectoryUrl;

                foreach (KeyValuePair <String, Byte[]> pair in images[i])
                {
                    if (pair.Value == null || !pair.Key.Contains("."))
                    {
                        continue;
                    }

                    String oldUrl      = pair.Key;
                    String fileNewName = MD5Encrypt.EncryptToHexString(oldUrl + DateTime.Now.ToString("yyyyMMddHHmmssffff"), true) + pair.Key.Substring(pair.Key.LastIndexOf('.'));
                    String newUrl      = uploadRoot + fileNewName;

                    problems[i].Description = problems[i].Description.Replace(oldUrl, newUrl);
                    problems[i].Input       = problems[i].Input.Replace(oldUrl, newUrl);
                    problems[i].Output      = problems[i].Output.Replace(oldUrl, newUrl);
                    problems[i].Hint        = problems[i].Hint.Replace(oldUrl, newUrl);

                    imagefiles[fileNewName] = pair.Value;
                }
            }

            //将题目插入到数据库
            List <Int32> pids = ProblemRepository.Instance.InsertEntities(problems);

            if (pids == null || pids.Count == 0)
            {
                return(MethodResult.FailedAndLog("Failed to import problem!"));
            }

            //保存题目数据
            Dictionary <Int32, Boolean> dataadded = new Dictionary <Int32, Boolean>();

            for (Int32 i = 0; i < pids.Count; i++)
            {
                if (pids[i] < 0)
                {
                    continue;
                }

                try
                {
                    if (datas[i] != null)
                    {
                        IMethodResult ret = ProblemDataManager.InternalAdminSaveProblemData(pids[i], datas[i]);

                        if (!ret.IsSuccess)
                        {
                            return(ret);
                        }

                        dataadded[pids[i]] = true;
                    }
                }
                catch
                {
                    dataadded[pids[i]] = false;
                }

                ProblemCache.IncreaseProblemSetCountCache();                      //更新缓存
                ProblemCache.IncreaseProblemIDMaxCache();                         //更新缓存
                ProblemCache.RemoveProblemSetCache(GetProblemPageIndex(pids[i])); //删除缓存
            }

            //保存题目图片
            foreach (KeyValuePair <String, Byte[]> pair in imagefiles)
            {
                try
                {
                    UploadsManager.InternalAdminSaveUploadFile(pair.Value, pair.Key);
                }
                catch { }
            }

            return(MethodResult.SuccessAndLog <Dictionary <Int32, Boolean> >(dataadded, "Admin import problem, id = {0}", String.Join(",", pids)));
        }