Search in sources :

Example 1 with StatusForbiddenException

use of top.hcode.hoj.common.exception.StatusForbiddenException in project HOJ by HimitZH.

the class BeforeDispatchInitManager method initTrainingSubmission.

@Transactional(rollbackFor = Exception.class)
public void initTrainingSubmission(Long tid, String displayId, UserRolesVo userRolesVo, Judge judge) throws StatusForbiddenException, StatusFailException, StatusAccessDeniedException {
    Training training = trainingEntityService.getById(tid);
    if (training == null || !training.getStatus()) {
        throw new StatusFailException("该训练不存在或不允许显示!");
    }
    trainingValidator.validateTrainingAuth(training, userRolesVo);
    // 查询获取对应的pid和cpid
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    trainingProblemQueryWrapper.eq("tid", tid).eq("display_id", displayId);
    TrainingProblem trainingProblem = trainingProblemEntityService.getOne(trainingProblemQueryWrapper);
    judge.setPid(trainingProblem.getPid());
    Problem problem = problemEntityService.getById(trainingProblem.getPid());
    if (problem.getAuth() == 2) {
        throw new StatusForbiddenException("错误!当前题目不可提交!");
    }
    judge.setDisplayPid(problem.getProblemId()).setGid(training.getGid());
    // 将新提交数据插入数据库
    judgeEntityService.save(judge);
    // 非私有训练不记录
    if (!training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        return;
    }
    TrainingRecord trainingRecord = new TrainingRecord();
    trainingRecord.setPid(problem.getId()).setTid(tid).setTpid(trainingProblem.getId()).setSubmitId(judge.getSubmitId()).setUid(userRolesVo.getUid());
    trainingRecordEntityService.save(trainingRecord);
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRecord(top.hcode.hoj.pojo.entity.training.TrainingRecord) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ContestProblem(top.hcode.hoj.pojo.entity.contest.ContestProblem) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with StatusForbiddenException

use of top.hcode.hoj.common.exception.StatusForbiddenException in project HOJ by HimitZH.

the class CommentManager method deleteComment.

@Transactional(rollbackFor = Exception.class)
public void deleteComment(Comment comment) throws StatusForbiddenException, StatusFailException, AccessException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
    // 如果不是评论本人 或者不是管理员 无权限删除该评论
    Long cid = comment.getCid();
    if (cid == null) {
        QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
        discussionQueryWrapper.select("id", "gid").eq("id", comment.getDid());
        Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
        Long gid = discussion.getGid();
        if (gid == null) {
            accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
            if (!comment.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !isProblemAdmin && !isAdmin) {
                throw new StatusForbiddenException("无权删除该评论");
            }
        } else {
            accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
            if (!groupValidator.isGroupAdmin(userRolesVo.getUid(), gid) && !comment.getFromUid().equals(userRolesVo.getUid()) && !isRoot) {
                throw new StatusForbiddenException("无权删除该评论");
            }
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
        Contest contest = contestEntityService.getById(cid);
        Long gid = contest.getGid();
        if (!comment.getFromUid().equals(userRolesVo.getUid()) && !isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), gid))) {
            throw new StatusForbiddenException("无权删除该评论");
        }
    }
    // 获取需要删除该评论的回复数
    int replyNum = replyEntityService.count(new QueryWrapper<Reply>().eq("comment_id", comment.getId()));
    // 删除该数据 包括关联外键的reply表数据
    boolean isDeleteComment = commentEntityService.removeById(comment.getId());
    // 同时需要删除该评论的回复表数据
    replyEntityService.remove(new UpdateWrapper<Reply>().eq("comment_id", comment.getId()));
    if (isDeleteComment) {
        // 如果是讨论区的回复,删除成功需要减少统计该讨论的回复数
        if (comment.getDid() != null) {
            UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
            discussionUpdateWrapper.eq("id", comment.getDid()).setSql("comment_num=comment_num-" + (replyNum + 1));
            discussionEntityService.update(discussionUpdateWrapper);
        }
    } else {
        throw new StatusFailException("删除失败,请重新尝试");
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with StatusForbiddenException

use of top.hcode.hoj.common.exception.StatusForbiddenException in project HOJ by HimitZH.

the class CommentManager method getAllReply.

public List<ReplyVo> getAllReply(Integer commentId, Long cid) throws StatusForbiddenException, StatusFailException, AccessException {
    // 如果有登录,则获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (cid == null) {
        Comment comment = commentEntityService.getById(commentId);
        QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
        discussionQueryWrapper.select("id", "gid").eq("id", comment.getDid());
        Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
        Long gid = discussion.getGid();
        if (gid != null) {
            accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
            if (!isRoot && !groupValidator.isGroupMember(userRolesVo.getUid(), gid)) {
                throw new StatusForbiddenException("对不起,您无权限操作!");
            }
        } else {
            accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
        Contest contest = contestEntityService.getById(cid);
        contestValidator.validateContestAuth(contest, userRolesVo, isRoot);
    }
    return replyEntityService.getAllReplyByCommentId(cid, userRolesVo != null ? userRolesVo.getUid() : null, isRoot, commentId);
}
Also used : Comment(top.hcode.hoj.pojo.entity.discussion.Comment) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Example 4 with StatusForbiddenException

use of top.hcode.hoj.common.exception.StatusForbiddenException in project HOJ by HimitZH.

the class CommentManager method getComments.

public CommentListVo getComments(Long cid, Integer did, Integer limit, Integer currentPage) throws StatusForbiddenException, AccessException {
    // 如果有登录,则获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (cid == null && did != null) {
        QueryWrapper<Discussion> discussionQueryWrapper = new QueryWrapper<>();
        discussionQueryWrapper.select("id", "gid").eq("id", did);
        Discussion discussion = discussionEntityService.getOne(discussionQueryWrapper);
        if (discussion != null && discussion.getGid() != null) {
            accessValidator.validateAccess(HOJAccessEnum.GROUP_DISCUSSION);
            if (!isRoot && !groupValidator.isGroupMember(userRolesVo.getUid(), discussion.getGid())) {
                throw new StatusForbiddenException("对不起,您无权限操作!");
            }
        } else {
            accessValidator.validateAccess(HOJAccessEnum.PUBLIC_DISCUSSION);
        }
    } else {
        accessValidator.validateAccess(HOJAccessEnum.CONTEST_COMMENT);
    }
    IPage<CommentVo> commentList = commentEntityService.getCommentList(limit, currentPage, cid, did, isRoot, userRolesVo != null ? userRolesVo.getUid() : null);
    HashMap<Integer, Boolean> commentLikeMap = new HashMap<>();
    if (userRolesVo != null) {
        // 如果是有登录 需要检查是否对评论有点赞
        List<Integer> commentIdList = new LinkedList<>();
        for (CommentVo commentVo : commentList.getRecords()) {
            commentIdList.add(commentVo.getId());
        }
        if (commentIdList.size() > 0) {
            QueryWrapper<CommentLike> commentLikeQueryWrapper = new QueryWrapper<>();
            commentLikeQueryWrapper.in("cid", commentIdList);
            List<CommentLike> commentLikeList = commentLikeEntityService.list(commentLikeQueryWrapper);
            // 如果存在记录需要修正Map为true
            for (CommentLike tmp : commentLikeList) {
                commentLikeMap.put(tmp.getCid(), true);
            }
        }
    }
    CommentListVo commentListVo = new CommentListVo();
    commentListVo.setCommentList(commentList);
    commentListVo.setCommentLikeMap(commentLikeMap);
    return commentListVo;
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) CommentLike(top.hcode.hoj.pojo.entity.discussion.CommentLike) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) Session(org.apache.shiro.session.Session)

Example 5 with StatusForbiddenException

use of top.hcode.hoj.common.exception.StatusForbiddenException in project HOJ by HimitZH.

the class ContestAdminManager method getContestACInfo.

public IPage<ContestRecord> getContestACInfo(Long cid, Integer currentPage, Integer limit) throws StatusForbiddenException {
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 获取本场比赛的状态
    Contest contest = contestEntityService.getById(cid);
    // 超级管理员或者该比赛的创建者,则为比赛管理者
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (!isRoot && !contest.getUid().equals(userRolesVo.getUid()) && !(contest.getIsGroup() && groupValidator.isGroupRoot(userRolesVo.getUid(), contest.getGid()))) {
        throw new StatusForbiddenException("对不起,您无权限操作!");
    }
    if (currentPage == null || currentPage < 1)
        currentPage = 1;
    if (limit == null || limit < 1)
        limit = 30;
    // 获取当前比赛的,状态为ac,未被校验的排在前面
    return contestRecordEntityService.getACInfo(currentPage, limit, Constants.Contest.RECORD_AC.getCode(), cid, contest.getUid());
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Session(org.apache.shiro.session.Session)

Aggregations

StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)113 Session (org.apache.shiro.session.Session)105 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)93 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)78 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)67 Group (top.hcode.hoj.pojo.entity.group.Group)64 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)52 Contest (top.hcode.hoj.pojo.entity.contest.Contest)32 Problem (top.hcode.hoj.pojo.entity.problem.Problem)21 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)20 Transactional (org.springframework.transaction.annotation.Transactional)15 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)13 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)12 GroupMember (top.hcode.hoj.pojo.entity.group.GroupMember)6 JSONObject (cn.hutool.json.JSONObject)5 FileReader (cn.hutool.core.io.file.FileReader)4 FileWriter (cn.hutool.core.io.file.FileWriter)4 LinkedList (java.util.LinkedList)4 Judge (top.hcode.hoj.pojo.entity.judge.Judge)4 Tag (top.hcode.hoj.pojo.entity.problem.Tag)4