Search in sources :

Example 6 with UserRolesVo

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

the class JudgeController method getALLCaseResult.

/**
 * @MethodName getJudgeCase
 * @Params * @param null
 * @Description 获得指定提交id的测试样例结果,暂不支持查看测试数据,只可看测试点结果,时间,空间,或者IO得分
 * @Return
 * @Since 2020/10/29
 */
@GetMapping("/get-all-case-result")
public CommonResult getALLCaseResult(@RequestParam(value = "submitId", required = true) Long submitId, HttpServletRequest request) {
    Judge judge = judgeService.getById(submitId);
    if (judge == null) {
        return CommonResult.errorResponse("此提交数据不存在!");
    }
    Problem problem = problemService.getById(judge.getPid());
    // 如果该题不支持开放测试点结果查看
    if (!problem.getOpenCaseResult()) {
        return CommonResult.successResponse(null, "对不起,该题测试样例详情不支持开放!");
    }
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    if (judge.getCid() != 0 && userRolesVo != null && !isRoot) {
        Contest contest = contestService.getById(judge.getCid());
        // 如果不是比赛管理员 比赛封榜不能看
        if (!contest.getUid().equals(userRolesVo.getUid())) {
            // 当前是比赛期间 同时处于封榜时间
            if (contest.getSealRank() && contest.getStatus().intValue() == Constants.Contest.STATUS_RUNNING.getCode() && contest.getSealRankTime().before(new Date())) {
                return CommonResult.successResponse(null, "对不起,该题测试样例详情不能查看!");
            }
            // 若是比赛题目,只支持OI查看测试点情况,ACM强制禁止查看,比赛管理员除外
            if (problem.getType().intValue() == Constants.Contest.TYPE_ACM.getCode()) {
                return CommonResult.successResponse(null, "对不起,该题测试样例详情不能查看!");
            }
        }
    }
    QueryWrapper<JudgeCase> wrapper = new QueryWrapper<>();
    if (userRolesVo == null || (!isRoot && !SecurityUtils.getSubject().hasRole("admin") && !SecurityUtils.getSubject().hasRole("problem_admin"))) {
        wrapper.select("time", "memory", "score", "status", "user_output");
    }
    wrapper.eq("submit_id", submitId).last("order by length(input_data) asc,input_data asc");
    // 当前所有测试点只支持 空间 时间 状态码 IO得分 输出文件名 输入文件名和错误信息提示查看而已
    List<JudgeCase> judgeCaseList = judgeCaseService.list(wrapper);
    if (judgeCaseList.isEmpty()) {
        // 未查询到一条数据
        return CommonResult.successResponse(null, "暂无数据");
    } else {
        return CommonResult.successResponse(judgeCaseList, "获取成功");
    }
}
Also used : JudgeCase(top.hcode.hoj.pojo.entity.judge.JudgeCase) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Judge(top.hcode.hoj.pojo.entity.judge.Judge)

Example 7 with UserRolesVo

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

the class AdminTrainingController method importTrainingRemoteOJProblem.

@GetMapping("/import-remote-oj-problem")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult importTrainingRemoteOJProblem(@RequestParam("name") String name, @RequestParam("problemId") String problemId, @RequestParam("tid") Long tid, HttpServletRequest request) {
    QueryWrapper<Problem> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("problem_id", name.toUpperCase() + "-" + problemId);
    Problem problem = problemService.getOne(queryWrapper, false);
    // 如果该题目不存在,需要先导入
    if (problem == null) {
        HttpSession session = request.getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        try {
            ProblemStrategy.RemoteProblemInfo otherOJProblemInfo = problemService.getOtherOJProblemInfo(name.toUpperCase(), problemId, userRolesVo.getUsername());
            if (otherOJProblemInfo != null) {
                problem = problemService.adminAddOtherOJProblem(otherOJProblemInfo, name);
                if (problem == null) {
                    return CommonResult.errorResponse("导入新题目失败!请重新尝试!");
                }
            } else {
                return CommonResult.errorResponse("导入新题目失败!原因:可能是与该OJ链接超时或题号格式错误!");
            }
        } catch (Exception e) {
            return CommonResult.errorResponse(e.getMessage());
        }
    }
    QueryWrapper<TrainingProblem> trainingProblemQueryWrapper = new QueryWrapper<>();
    Problem finalProblem = problem;
    trainingProblemQueryWrapper.eq("tid", tid).and(wrapper -> wrapper.eq("pid", finalProblem.getId()).or().eq("display_id", finalProblem.getProblemId()));
    TrainingProblem trainingProblem = trainingProblemService.getOne(trainingProblemQueryWrapper, false);
    if (trainingProblem != null) {
        return CommonResult.errorResponse("添加失败,该题目已添加或者题目的训练展示ID已存在!", CommonResult.STATUS_FAIL);
    }
    TrainingProblem newTProblem = new TrainingProblem();
    boolean result = trainingProblemService.saveOrUpdate(newTProblem.setTid(tid).setPid(problem.getId()).setDisplayId(problem.getProblemId()));
    if (result) {
        // 添加成功
        trainingRegisterService.syncAlreadyRegisterUserRecord(tid, problem.getId(), newTProblem.getId());
        return CommonResult.successResponse(null, "添加成功!");
    } else {
        return CommonResult.errorResponse("添加失败", CommonResult.STATUS_FAIL);
    }
}
Also used : QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) ProblemStrategy(top.hcode.hoj.crawler.problem.ProblemStrategy) TrainingProblem(top.hcode.hoj.pojo.entity.training.TrainingProblem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with UserRolesVo

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

the class AdminTrainingController method updateTraining.

@PutMapping("")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public CommonResult updateTraining(@RequestBody TrainingDto trainingDto, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(trainingDto.getTraining().getAuthor())) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    boolean result = trainingService.updateTraining(trainingDto);
    if (result) {
        trainingRecordService.checkSyncRecord(trainingDto.getTraining());
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 9 with UserRolesVo

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

the class AdminTrainingController method changeTrainingStatus.

@PutMapping("/change-training-status")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
public CommonResult changeTrainingStatus(@RequestParam(value = "tid", required = true) Long tid, @RequestParam(value = "author", required = true) String author, @RequestParam(value = "status", required = true) Boolean status, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(author)) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    boolean result = trainingService.saveOrUpdate(new Training().setId(tid).setStatus(status));
    if (result) {
        // 添加成功
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Example 10 with UserRolesVo

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

the class AdminUserController method editUser.

@PutMapping("/edit-user")
@RequiresPermissions("user_admin")
@RequiresAuthentication
@Transactional(rollbackFor = Exception.class)
public CommonResult editUser(@RequestBody Map<String, Object> params, HttpServletRequest request) {
    String username = (String) params.get("username");
    String uid = (String) params.get("uid");
    String realname = (String) params.get("realname");
    String email = (String) params.get("email");
    String password = (String) params.get("password");
    int type = (int) params.get("type");
    int status = (int) params.get("status");
    boolean setNewPwd = (boolean) params.get("setNewPwd");
    UpdateWrapper<UserInfo> updateWrapper1 = new UpdateWrapper<>();
    updateWrapper1.eq("uuid", uid).set("username", username).set("realname", realname).set("email", email).set(setNewPwd, "password", SecureUtil.md5(password)).set("status", status);
    boolean result1 = userInfoService.update(updateWrapper1);
    QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
    userRoleQueryWrapper.eq("uid", uid);
    UserRole userRole = userRoleService.getOne(userRoleQueryWrapper, false);
    boolean result2 = false;
    int oldType = userRole.getRoleId().intValue();
    if (userRole.getRoleId().intValue() != type) {
        userRole.setRoleId(Long.valueOf(type));
        result2 = userRoleService.updateById(userRole);
    }
    if (result1) {
        // 需要重新登录
        userRoleService.deleteCache(uid, true);
    } else if (result2) {
        // 需要重新授权
        userRoleService.deleteCache(uid, false);
    }
    if (result2) {
        // 获取当前登录的用户
        HttpSession session = request.getSession();
        UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
        String title = "权限变更通知(Authority Change Notice)";
        String content = userRoleService.getAuthChangeContent(oldType, type);
        adminSysNoticeService.addSingleNoticeToUser(userRolesVo.getUid(), uid, title, content, "Sys");
    }
    return CommonResult.successResponse(null, "修改成功!");
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) UserRole(top.hcode.hoj.pojo.entity.user.UserRole) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) UserInfo(top.hcode.hoj.pojo.entity.user.UserInfo) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) Transactional(org.springframework.transaction.annotation.Transactional)

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