Search in sources :

Example 21 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class CommentController method getAllReply.

@GetMapping("/reply")
public CommonResult getAllReply(@RequestParam("commentId") Integer commentId, @RequestParam(value = "cid", required = false) Long cid, HttpServletRequest request) {
    // 如果有登录,则获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    List<Reply> replyList = commentService.getAllReplyByCommentId(cid, userRolesVo != null ? userRolesVo.getUid() : null, isRoot, commentId);
    return CommonResult.successResponse(replyList, "获取全部回复列表成功");
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Reply(top.hcode.hoj.pojo.entity.discussion.Reply)

Example 22 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class CommentController method deleteReply.

@DeleteMapping("/reply")
@RequiresAuthentication
public CommonResult deleteReply(@RequestBody ReplyDto replyDto, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    Reply reply = replyDto.getReply();
    // 如果不是评论本人 或者不是管理员 无权限删除该评论
    if (reply.getFromUid().equals(userRolesVo.getUid()) || SecurityUtils.getSubject().hasRole("root") || SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        // 删除该数据
        boolean isOk = replyService.removeById(reply.getId());
        if (isOk) {
            // 如果是讨论区的回复,删除成功需要减少统计该讨论的回复数
            if (replyDto.getDid() != null) {
                UpdateWrapper<Discussion> discussionUpdateWrapper = new UpdateWrapper<>();
                discussionUpdateWrapper.eq("id", replyDto.getDid()).setSql("comment_num=comment_num-1");
                discussionService.update(discussionUpdateWrapper);
            }
            return CommonResult.successResponse(null, "删除成功");
        } else {
            return CommonResult.errorResponse("删除失败,请重新尝试");
        }
    } else {
        return CommonResult.errorResponse("无权删除该回复", CommonResult.STATUS_FORBIDDEN);
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Reply(top.hcode.hoj.pojo.entity.discussion.Reply) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 23 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class CommentController method addComment.

@PostMapping("/comment")
@RequiresPermissions("comment_add")
@RequiresAuthentication
@Transactional
public CommonResult addComment(@RequestBody Comment comment, HttpServletRequest request) {
    if (StringUtils.isEmpty(comment.getContent().trim())) {
        return CommonResult.errorResponse("评论内容不能为空!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 比赛外的评论 除管理员外 只有AC 10道以上才可评论
    if (comment.getCid() == null) {
        if (!SecurityUtils.getSubject().hasRole("root") && !SecurityUtils.getSubject().hasRole("admin") && !SecurityUtils.getSubject().hasRole("problem_admin")) {
            QueryWrapper<UserAcproblem> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uid", userRolesVo.getUid()).select("distinct pid");
            int userAcProblemCount = userAcproblemService.count(queryWrapper);
            if (userAcProblemCount < 10) {
                return CommonResult.errorResponse("对不起,您暂时不能评论!请先去提交题目通过10道以上~", CommonResult.STATUS_FORBIDDEN);
            }
        }
    }
    comment.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
    if (SecurityUtils.getSubject().hasRole("root")) {
        comment.setFromRole("root");
    } else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        comment.setFromRole("admin");
    } else {
        comment.setFromRole("user");
    }
    // 带有表情的字符串转换为编码
    comment.setContent(EmojiUtil.toHtml(comment.getContent()));
    boolean isOk = commentService.saveOrUpdate(comment);
    if (isOk) {
        CommentsVo commentsVo = new CommentsVo();
        commentsVo.setContent(comment.getContent());
        commentsVo.setId(comment.getId());
        commentsVo.setFromAvatar(comment.getFromAvatar());
        commentsVo.setFromName(comment.getFromName());
        commentsVo.setFromUid(comment.getFromUid());
        commentsVo.setLikeNum(0);
        commentsVo.setGmtCreate(comment.getGmtCreate());
        commentsVo.setReplyList(new LinkedList<>());
        // 如果是讨论区的回复,发布成功需要添加统计该讨论的回复数
        if (comment.getDid() != null) {
            Discussion discussion = discussionService.getById(comment.getDid());
            if (discussion != null) {
                discussion.setCommentNum(discussion.getCommentNum() + 1);
                discussionService.updateById(discussion);
                // 更新消息
                commentService.updateCommentMsg(discussion.getUid(), userRolesVo.getUid(), comment.getContent(), comment.getDid());
            }
        }
        return CommonResult.successResponse(commentsVo, "评论成功");
    } else {
        return CommonResult.errorResponse("评论失败,请重新尝试!");
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) CommentsVo(top.hcode.hoj.pojo.vo.CommentsVo) UserAcproblem(top.hcode.hoj.pojo.entity.user.UserAcproblem) Discussion(top.hcode.hoj.pojo.entity.discussion.Discussion) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class ContestAdminController method getContestACInfo.

/**
 * @MethodName getContestACInfo
 * @Params * @param null
 * @Description 获取各个用户的ac情况,仅限于比赛管理者可查看
 * @Return
 * @Since 2021/1/17
 */
@GetMapping("/get-contest-ac-info")
@RequiresAuthentication
public CommonResult getContestACInfo(@RequestParam("cid") Long cid, @RequestParam(value = "currentPage", required = false) Integer currentPage, @RequestParam(value = "limit", required = false) Integer limit, HttpServletRequest request) {
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 获取本场比赛的状态
    Contest contest = contestService.getById(cid);
    // 超级管理员或者该比赛的创建者,则为比赛管理者
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (!isRoot && !contest.getUid().equals(userRolesVo.getUid())) {
        return CommonResult.errorResponse("对不起,你无权查看!", CommonResult.STATUS_FORBIDDEN);
    }
    if (currentPage == null || currentPage < 1)
        currentPage = 1;
    if (limit == null || limit < 1)
        limit = 30;
    // 获取当前比赛的,状态为ac,未被校验的排在签名
    IPage<ContestRecord> contestRecords = contestRecordService.getACInfo(currentPage, limit, Constants.Contest.RECORD_AC.getCode(), cid, contest.getUid());
    return CommonResult.successResponse(contestRecords, "查询成功");
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) ContestRecord(top.hcode.hoj.pojo.entity.contest.ContestRecord) Contest(top.hcode.hoj.pojo.entity.contest.Contest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 25 with UserRolesVo

use of top.hcode.hoj.pojo.vo.UserRolesVo in project HOJ by HimitZH.

the class ProblemController method getUserProblemStatus.

/**
 * @MethodName getUserProblemStatus
 * @Params * @param UidAndPidListDto
 * @Description 获取用户对应该题目列表中各个题目的做题情况
 * @Return CommonResult
 * @Since 2020/12/29
 */
@RequiresAuthentication
@PostMapping("/get-user-problem-status")
public CommonResult getUserProblemStatus(@Validated @RequestBody PidListDto pidListDto, HttpServletRequest request) {
    // 需要获取一下该token对应用户的数据
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    HashMap<Long, Object> result = new HashMap<>();
    // 先查询判断该用户对于这些题是否已经通过,若已通过,则无论后续再提交结果如何,该题都标记为通过
    QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
    queryWrapper.select("distinct pid,status,submit_time,score").in("pid", pidListDto.getPidList()).eq("uid", userRolesVo.getUid()).orderByDesc("submit_time");
    if (pidListDto.getIsContestProblemList()) {
        // 如果是比赛的提交记录需要判断cid
        queryWrapper.eq("cid", pidListDto.getCid());
    } else {
        queryWrapper.eq("cid", 0);
    }
    List<Judge> judges = judgeService.list(queryWrapper);
    boolean isACMContest = true;
    Contest contest = null;
    if (pidListDto.getIsContestProblemList()) {
        contest = contestService.getById(pidListDto.getCid());
        if (contest == null) {
            return CommonResult.errorResponse("比赛参数错误!");
        }
        isACMContest = contest.getType().intValue() == Constants.Contest.TYPE_ACM.getCode();
    }
    for (Judge judge : judges) {
        // 如果是比赛的题目列表状态
        HashMap<String, Object> temp = new HashMap<>();
        if (pidListDto.getIsContestProblemList()) {
            if (!isACMContest) {
                if (!result.containsKey(judge.getPid())) {
                    // 只有比赛结束可以看到,比赛管理员与超级管理员的提交除外
                    if (contestService.isSealRank(userRolesVo.getUid(), contest, true, SecurityUtils.getSubject().hasRole("root"))) {
                        temp.put("status", Constants.Judge.STATUS_SUBMITTED_UNKNOWN_RESULT.getStatus());
                        temp.put("score", null);
                    } else {
                        temp.put("status", judge.getStatus());
                        temp.put("score", judge.getScore());
                    }
                    result.put(judge.getPid(), temp);
                }
            } else {
                // 如果该题目已通过,且同时是为不封榜前提交的,则强制写为通过(0)
                if (judge.getStatus().intValue() == Constants.Judge.STATUS_ACCEPTED.getStatus()) {
                    temp.put("status", Constants.Judge.STATUS_ACCEPTED.getStatus());
                    temp.put("score", null);
                    result.put(judge.getPid(), temp);
                } else if (!result.containsKey(judge.getPid())) {
                    // 还未写入,则使用最新一次提交的结果
                    temp.put("status", judge.getStatus());
                    temp.put("score", null);
                    result.put(judge.getPid(), temp);
                }
            }
        } else {
            // 不是比赛题目
            if (judge.getStatus().intValue() == Constants.Judge.STATUS_ACCEPTED.getStatus()) {
                // 如果该题目已通过,则强制写为通过(0)
                temp.put("status", Constants.Judge.STATUS_ACCEPTED.getStatus());
                result.put(judge.getPid(), temp);
            } else if (!result.containsKey(judge.getPid())) {
                // 还未写入,则使用最新一次提交的结果
                temp.put("status", judge.getStatus());
                result.put(judge.getPid(), temp);
            }
        }
    }
    // 再次检查,应该可能从未提交过该题,则状态写为-10
    for (Long pid : pidListDto.getPidList()) {
        // 如果是比赛的题目列表状态
        if (pidListDto.getIsContestProblemList()) {
            if (!result.containsKey(pid)) {
                HashMap<String, Object> temp = new HashMap<>();
                temp.put("score", null);
                temp.put("status", Constants.Judge.STATUS_NOT_SUBMITTED.getStatus());
                result.put(pid, temp);
            }
        } else {
            if (!result.containsKey(pid)) {
                HashMap<String, Object> temp = new HashMap<>();
                temp.put("status", Constants.Judge.STATUS_NOT_SUBMITTED.getStatus());
                result.put(pid, temp);
            }
        }
    }
    return CommonResult.successResponse(result, "查询成功");
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Judge(top.hcode.hoj.pojo.entity.judge.Judge) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Aggregations

UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)184 Session (org.apache.shiro.session.Session)114 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)97 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)78 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)73 HttpSession (javax.servlet.http.HttpSession)65 Group (top.hcode.hoj.pojo.entity.group.Group)64 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)63 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)53 Contest (top.hcode.hoj.pojo.entity.contest.Contest)38 Transactional (org.springframework.transaction.annotation.Transactional)37 Problem (top.hcode.hoj.pojo.entity.problem.Problem)36 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)35 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)21 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)16 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)15 MultipartFile (org.springframework.web.multipart.MultipartFile)13 Judge (top.hcode.hoj.pojo.entity.judge.Judge)13 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 JSONObject (cn.hutool.json.JSONObject)11