Search in sources :

Example 1 with Training

use of top.hcode.hoj.pojo.entity.training.Training 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 2 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class TrainingController method getTrainingProblemList.

/**
 * @param tid
 * @param request
 * @MethodName getTrainingProblemList
 * @Description 根据tid获取指定训练的题单题目列表
 * @Return
 * @Since 2021/11/20
 */
@GetMapping("/get-training-problem-list")
@RequiresAuthentication
public CommonResult getTrainingProblemList(@RequestParam(value = "tid") Long tid, HttpServletRequest request) {
    Training training = trainingService.getById(tid);
    if (training == null || !training.getStatus()) {
        return CommonResult.errorResponse("该训练不存在或不允许显示!");
    }
    CommonResult result = trainingRegisterService.checkTrainingAuth(training, request);
    if (result != null) {
        return result;
    }
    List<ProblemVo> trainingProblemList = trainingProblemService.getTrainingProblemList(tid);
    return CommonResult.successResponse(trainingProblemList, "success");
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) ProblemVo(top.hcode.hoj.pojo.vo.ProblemVo) CommonResult(top.hcode.hoj.common.result.CommonResult) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 3 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class TrainingController method getTrainingAccess.

/**
 * @param tid
 * @param request
 * @MethodName getTrainingAccess
 * @Description 私有权限的训练需要获取当前用户是否有进入训练的权限
 * @Return
 * @Since 2021/11/20
 */
@RequiresAuthentication
@GetMapping("/get-training-access")
public CommonResult getTrainingAccess(@RequestParam(value = "tid") Long tid, HttpServletRequest request) {
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    QueryWrapper<TrainingRegister> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid).eq("uid", userRolesVo.getUid());
    TrainingRegister trainingRegister = trainingRegisterService.getOne(queryWrapper, false);
    boolean access = false;
    if (trainingRegister != null) {
        access = true;
        Training training = trainingService.getById(tid);
        if (training == null || !training.getStatus()) {
            return CommonResult.errorResponse("对不起,该训练不存在!");
        }
    }
    HashMap<String, Object> result = new HashMap<>();
    result.put("access", access);
    return CommonResult.successResponse(result);
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRegister(top.hcode.hoj.pojo.entity.training.TrainingRegister) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 4 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class TrainingServiceImpl method getAdminTrainingDto.

@Override
public CommonResult getAdminTrainingDto(Long tid, HttpServletRequest request) {
    // 获取本场训练的信息
    Training training = trainingMapper.selectById(tid);
    if (training == null) {
        // 查询不存在
        return CommonResult.errorResponse("查询失败:该训练不存在,请检查参数tid是否准确!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 是否为超级管理员
    boolean isRoot = SecurityUtils.getSubject().hasRole("root");
    // 只有超级管理员和训练拥有者才能操作
    if (!isRoot && !userRolesVo.getUsername().equals(training.getAuthor())) {
        return CommonResult.errorResponse("对不起,你无权限操作!", CommonResult.STATUS_FORBIDDEN);
    }
    TrainingDto trainingDto = new TrainingDto();
    trainingDto.setTraining(training);
    QueryWrapper<MappingTrainingCategory> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq("tid", tid);
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryMapper.selectOne(queryWrapper);
    TrainingCategory trainingCategory = null;
    if (mappingTrainingCategory != null) {
        trainingCategory = trainingCategoryService.getById(mappingTrainingCategory.getCid());
    }
    trainingDto.setTrainingCategory(trainingCategory);
    return CommonResult.successResponse(trainingDto, "查询成功!");
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingDto(top.hcode.hoj.pojo.dto.TrainingDto) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory)

Example 5 with Training

use of top.hcode.hoj.pojo.entity.training.Training in project HOJ by HimitZH.

the class TrainingServiceImpl method updateTraining.

@Override
@Transactional(rollbackFor = Exception.class)
public boolean updateTraining(TrainingDto trainingDto) {
    Training training = trainingDto.getTraining();
    Training oldTraining = trainingMapper.selectById(training.getId());
    trainingMapper.updateById(training);
    // 私有训练 修改密码 需要清空之前注册训练的记录
    if (training.getAuth().equals(Constants.Training.AUTH_PRIVATE.getValue())) {
        if (!Objects.equals(training.getPrivatePwd(), oldTraining.getPrivatePwd())) {
            UpdateWrapper<TrainingRegister> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId());
            trainingRegisterMapper.delete(updateWrapper);
        }
    }
    TrainingCategory trainingCategory = trainingDto.getTrainingCategory();
    if (trainingCategory.getId() == null) {
        try {
            trainingCategoryService.save(trainingCategory);
        } catch (Exception ignored) {
            QueryWrapper<TrainingCategory> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", trainingCategory.getName());
            trainingCategory = trainingCategoryService.getOne(queryWrapper, false);
        }
    }
    MappingTrainingCategory mappingTrainingCategory = mappingTrainingCategoryMapper.selectOne(new QueryWrapper<MappingTrainingCategory>().eq("tid", training.getId()));
    if (mappingTrainingCategory == null) {
        mappingTrainingCategoryMapper.insert(new MappingTrainingCategory().setTid(training.getId()).setCid(trainingCategory.getId()));
    } else {
        if (!mappingTrainingCategory.getCid().equals(trainingCategory.getId())) {
            UpdateWrapper<MappingTrainingCategory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("tid", training.getId()).set("cid", trainingCategory.getId());
            int update = mappingTrainingCategoryMapper.update(null, updateWrapper);
            return update > 0;
        }
    }
    return true;
}
Also used : Training(top.hcode.hoj.pojo.entity.training.Training) TrainingRegister(top.hcode.hoj.pojo.entity.training.TrainingRegister) UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) QueryWrapper(com.baomidou.mybatisplus.core.conditions.query.QueryWrapper) TrainingCategory(top.hcode.hoj.pojo.entity.training.TrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) MappingTrainingCategory(top.hcode.hoj.pojo.entity.training.MappingTrainingCategory) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Training (top.hcode.hoj.pojo.entity.training.Training)19 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)12 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)9 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)7 TrainingCategory (top.hcode.hoj.pojo.entity.training.TrainingCategory)7 Transactional (org.springframework.transaction.annotation.Transactional)6 MappingTrainingCategory (top.hcode.hoj.pojo.entity.training.MappingTrainingCategory)6 HttpSession (javax.servlet.http.HttpSession)5 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)5 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)5 UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)4 Session (org.apache.shiro.session.Session)4 TrainingProblem (top.hcode.hoj.pojo.entity.training.TrainingProblem)4 TrainingRegister (top.hcode.hoj.pojo.entity.training.TrainingRegister)4 CommonResult (top.hcode.hoj.common.result.CommonResult)3 Problem (top.hcode.hoj.pojo.entity.problem.Problem)3 Async (org.springframework.scheduling.annotation.Async)2 TrainingDto (top.hcode.hoj.pojo.dto.TrainingDto)2 ContestProblem (top.hcode.hoj.pojo.entity.contest.ContestProblem)2 TrainingRecord (top.hcode.hoj.pojo.entity.training.TrainingRecord)2