Search in sources :

Example 1 with ContestRegister

use of top.hcode.hoj.pojo.entity.contest.ContestRegister in project HOJ by HimitZH.

the class ContestServiceImpl method checkJudgeAuth.

@Override
public CommonResult checkJudgeAuth(Contest contest, String uid) {
    if (contest.getAuth().intValue() == Constants.Contest.AUTH_PRIVATE.getCode() || contest.getAuth().intValue() == Constants.Contest.AUTH_PROTECT.getCode()) {
        QueryWrapper<ContestRegister> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid", contest.getId()).eq("uid", uid);
        ContestRegister register = contestRegisterService.getOne(queryWrapper, false);
        // 如果还没注册
        if (register == null) {
            return CommonResult.errorResponse("对不起,请你先注册该比赛,提交代码失败!", CommonResult.STATUS_FAIL);
        }
    }
    return null;
}
Also used : ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)

Example 2 with ContestRegister

use of top.hcode.hoj.pojo.entity.contest.ContestRegister in project HOJ by HimitZH.

the class ContestServiceImpl method checkContestAuth.

/**
 * @param contest
 * @param userRolesVo
 * @param isRoot
 * @return CommonResult
 * @MethodName checkContestAuth
 * @Description 需要对该比赛做判断,是否处于开始或结束状态才可以获取,同时若是私有赛需要判断是否已注册(比赛管理员包括超级管理员可以直接获取)
 * @Since 2021/1/17
 */
@Override
public CommonResult checkContestAuth(Contest contest, UserRolesVo userRolesVo, Boolean isRoot) {
    if (contest == null || !contest.getVisible()) {
        return CommonResult.errorResponse("对不起,该比赛不存在!");
    }
    if (!isRoot && !contest.getUid().equals(userRolesVo.getUid())) {
        // 判断一下比赛的状态,还未开始不能查看题目。
        if (contest.getStatus().intValue() != Constants.Contest.STATUS_RUNNING.getCode() && contest.getStatus().intValue() != Constants.Contest.STATUS_ENDED.getCode()) {
            return CommonResult.errorResponse("比赛还未开始,您无权访问该比赛!", CommonResult.STATUS_FORBIDDEN);
        } else {
            // 如果是处于比赛正在进行阶段,需要判断该场比赛是否为私有赛,私有赛需要判断该用户是否已注册
            if (contest.getAuth().intValue() == Constants.Contest.AUTH_PRIVATE.getCode()) {
                QueryWrapper<ContestRegister> registerQueryWrapper = new QueryWrapper<>();
                registerQueryWrapper.eq("cid", contest.getId()).eq("uid", userRolesVo.getUid());
                ContestRegister register = contestRegisterService.getOne(registerQueryWrapper);
                if (register == null) {
                    // 如果数据为空,表示未注册私有赛,不可访问
                    return CommonResult.errorResponse("对不起,请先到比赛首页输入比赛密码进行注册!", CommonResult.STATUS_FORBIDDEN);
                }
                if (contest.getOpenAccountLimit() && !checkAccountRule(contest.getAccountLimitRule(), userRolesVo.getUsername())) {
                    return CommonResult.errorResponse("对不起!本次比赛只允许特定账号规则的用户参赛!", CommonResult.STATUS_ACCESS_DENIED);
                }
            }
        }
    }
    return null;
}
Also used : ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)

Example 3 with ContestRegister

use of top.hcode.hoj.pojo.entity.contest.ContestRegister in project HOJ by HimitZH.

the class ContestValidator method validateJudgeAuth.

public void validateJudgeAuth(Contest contest, String uid) throws StatusForbiddenException {
    if (contest.getAuth().intValue() == Constants.Contest.AUTH_PRIVATE.getCode() || contest.getAuth().intValue() == Constants.Contest.AUTH_PROTECT.getCode()) {
        QueryWrapper<ContestRegister> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("cid", contest.getId()).eq("uid", uid);
        ContestRegister register = contestRegisterEntityService.getOne(queryWrapper, false);
        // 如果还没注册
        if (register == null) {
            throw new StatusForbiddenException("对不起,请你先注册该比赛,提交代码失败!");
        }
    }
}
Also used : ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)

Example 4 with ContestRegister

use of top.hcode.hoj.pojo.entity.contest.ContestRegister in project HOJ by HimitZH.

the class AdminContestController method updateContest.

@PutMapping("")
@RequiresAuthentication
@RequiresRoles(value = { "root", "admin", "problem_admin" }, logical = Logical.OR)
@Transactional(rollbackFor = Exception.class)
public CommonResult updateContest(@RequestBody AdminContestVo adminContestVo, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和比赛拥有者才能操作
    if (!isRoot && !userRolesVo.getUid().equals(adminContestVo.getUid())) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    Contest contest = BeanUtil.copyProperties(adminContestVo, Contest.class, "starAccount");
    JSONObject accountJson = new JSONObject();
    accountJson.set("star_account", adminContestVo.getStarAccount());
    contest.setStarAccount(accountJson.toString());
    Contest oldContest = contestService.getById(contest.getId());
    boolean result = contestService.saveOrUpdate(contest);
    if (result) {
        if (!contest.getAuth().equals(Constants.Contest.AUTH_PUBLIC.getCode())) {
            if (!Objects.equals(oldContest.getPwd(), contest.getPwd())) {
                // 改了比赛密码则需要删掉已有的注册比赛用户
                UpdateWrapper<ContestRegister> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("cid", contest.getId());
                contestRegisterService.remove(updateWrapper);
            }
        }
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) JSONObject(cn.hutool.json.JSONObject) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Contest(top.hcode.hoj.pojo.entity.contest.Contest) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ContestRegister

use of top.hcode.hoj.pojo.entity.contest.ContestRegister in project HOJ by HimitZH.

the class AdminContestManager method updateContest.

public void updateContest(AdminContestVo adminContestVo) throws StatusForbiddenException, StatusFailException {
    // 获取当前登录的用户
    Session session = SecurityUtils.getSubject().getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和比赛拥有者才能操作
    if (!isRoot && !userRolesVo.getUid().equals(adminContestVo.getUid())) {
        throw new StatusForbiddenException("对不起,你无权限操作!");
    }
    Contest contest = BeanUtil.copyProperties(adminContestVo, Contest.class, "starAccount");
    JSONObject accountJson = new JSONObject();
    accountJson.set("star_account", adminContestVo.getStarAccount());
    contest.setStarAccount(accountJson.toString());
    Contest oldContest = contestEntityService.getById(contest.getId());
    boolean isOk = contestEntityService.saveOrUpdate(contest);
    if (isOk) {
        if (!contest.getAuth().equals(Constants.Contest.AUTH_PUBLIC.getCode())) {
            if (!Objects.equals(oldContest.getPwd(), contest.getPwd())) {
                // 改了比赛密码则需要删掉已有的注册比赛用户
                UpdateWrapper<ContestRegister> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("cid", contest.getId());
                contestRegisterEntityService.remove(updateWrapper);
            }
        }
    } else {
        throw new StatusFailException("修改失败");
    }
}
Also used : StatusForbiddenException(top.hcode.hoj.common.exception.StatusForbiddenException) ContestRegister(top.hcode.hoj.pojo.entity.contest.ContestRegister) JSONObject(cn.hutool.json.JSONObject) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) StatusFailException(top.hcode.hoj.common.exception.StatusFailException) Contest(top.hcode.hoj.pojo.entity.contest.Contest) Session(org.apache.shiro.session.Session)

Aggregations

ContestRegister (top.hcode.hoj.pojo.entity.contest.ContestRegister)7 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)4 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)4 JSONObject (cn.hutool.json.JSONObject)3 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)3 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)3 Contest (top.hcode.hoj.pojo.entity.contest.Contest)3 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)3 Session (org.apache.shiro.session.Session)2 HttpSession (javax.servlet.http.HttpSession)1 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)1 RequiresRoles (org.apache.shiro.authz.annotation.RequiresRoles)1 Transactional (org.springframework.transaction.annotation.Transactional)1 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)1 Group (top.hcode.hoj.pojo.entity.group.Group)1