Search in sources :

Example 21 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class AccountController method changeEmail.

/**
 * @MethodName changeEmail
 * @Params * @param null
 * @Description 修改邮箱的操作,连续半小时内密码错误5次,则需要半个小时后才可以再次尝试修改
 * @Return
 * @Since 2021/1/9
 */
@PostMapping("/change-email")
@RequiresAuthentication
public CommonResult changeEmail(@RequestBody Map params, HttpServletRequest request) {
    String password = (String) params.get("password");
    String newEmail = (String) params.get("newEmail");
    // 数据可用性判断
    if (StringUtils.isEmpty(password) || StringUtils.isEmpty(newEmail)) {
        return CommonResult.errorResponse("密码或新邮箱不能为空!");
    }
    if (!Validator.isEmail(newEmail)) {
        return CommonResult.errorResponse("邮箱格式错误!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    // 如果已经被锁定半小时不能修改
    String lockKey = Constants.Account.CODE_CHANGE_EMAIL_LOCK + userRolesVo.getUid();
    // 统计失败的key
    String countKey = Constants.Account.CODE_CHANGE_EMAIL_FAIL + userRolesVo.getUid();
    HashMap<String, Object> resp = new HashMap<>();
    if (redisUtils.hasKey(lockKey)) {
        long expire = redisUtils.getExpire(lockKey);
        Date now = new Date();
        long minute = expire / 60;
        long second = expire % 60;
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        resp.put("code", 403);
        Date afterDate = new Date(now.getTime() + expire * 1000);
        String msg = "由于您多次修改邮箱失败,修改邮箱功能已锁定,请在" + minute + "分" + second + "秒后(" + formatter.format(afterDate) + ")再进行尝试!";
        resp.put("msg", msg);
        return CommonResult.successResponse(resp, "修改邮箱失败!");
    }
    // 与当前登录用户的密码进行比较判断
    if (userRolesVo.getPassword().equals(SecureUtil.md5(password))) {
        // 如果相同,则进行修改操作
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("email", newEmail).eq("uuid", userRolesVo.getUid());
        boolean result = userInfoDao.update(updateWrapper);
        if (result) {
            resp.put("code", 200);
            resp.put("msg", "修改邮箱成功!");
            resp.put("userInfo", MapUtil.builder().put("uid", userRolesVo.getUid()).put("username", userRolesVo.getUsername()).put("nickname", userRolesVo.getNickname()).put("avatar", userRolesVo.getAvatar()).put("email", newEmail).put("number", userRolesVo.getNumber()).put("gender", userRolesVo.getGender()).put("school", userRolesVo.getSchool()).put("course", userRolesVo.getCourse()).put("signature", userRolesVo.getSignature()).put("realname", userRolesVo.getRealname()).put("github", userRolesVo.getGithub()).put("blog", userRolesVo.getBlog()).put("cfUsername", userRolesVo.getCfUsername()).put("roleList", userRolesVo.getRoles().stream().map(Role::getRole)).map());
            // 清空记录
            redisUtils.del(countKey);
            // 更新session
            userRolesVo.setEmail(newEmail);
            session.setAttribute("userInfo", userRolesVo);
            return CommonResult.successResponse(resp, "修改邮箱成功!");
        } else {
            return CommonResult.errorResponse("系统错误:修改邮箱失败!", CommonResult.STATUS_ERROR);
        }
    } else {
        // 如果不同,则进行记录,当失败次数达到5次,半个小时后才可重试
        Integer count = (Integer) redisUtils.get(countKey);
        if (count == null) {
            // 三十分钟不尝试,该限制会自动清空消失
            redisUtils.set(countKey, 1, 60 * 30);
            count = 0;
        } else if (count < 5) {
            redisUtils.incr(countKey, 1);
        }
        count++;
        if (count == 5) {
            // 清空统计
            redisUtils.del(countKey);
            // 设置锁定更改
            redisUtils.set(lockKey, "lock", 60 * 30);
        }
        resp.put("code", 400);
        resp.put("msg", "密码错误!您已累计修改邮箱失败" + count + "次...");
        return CommonResult.successResponse(resp, "修改邮箱失败!");
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) SimpleDateFormat(java.text.SimpleDateFormat) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 22 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class CommentController method addReply.

@PostMapping("/reply")
@RequiresPermissions("reply_add")
@RequiresAuthentication
public CommonResult addReply(@RequestBody ReplyDto replyDto, HttpServletRequest request) {
    if (StringUtils.isEmpty(replyDto.getReply().getContent().trim())) {
        return CommonResult.errorResponse("回复内容不能为空!");
    }
    // 获取当前登录的用户
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    Reply reply = replyDto.getReply();
    reply.setFromAvatar(userRolesVo.getAvatar()).setFromName(userRolesVo.getUsername()).setFromUid(userRolesVo.getUid());
    if (SecurityUtils.getSubject().hasRole("root")) {
        reply.setFromRole("root");
    } else if (SecurityUtils.getSubject().hasRole("admin") || SecurityUtils.getSubject().hasRole("problem_admin")) {
        reply.setFromRole("admin");
    } else {
        reply.setFromRole("user");
    }
    // 带有表情的字符串转换为编码
    reply.setContent(EmojiUtil.toHtml(reply.getContent()));
    boolean isOk = replyService.saveOrUpdate(reply);
    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);
            // 更新消息
            replyService.updateReplyMsg(replyDto.getDid(), "Discussion", reply.getContent(), replyDto.getQuoteId(), replyDto.getQuoteType(), reply.getToUid(), reply.getFromUid());
        }
        return CommonResult.successResponse(reply, "回复成功");
    } else {
        return CommonResult.errorResponse("回复失败,请重新尝试!");
    }
}
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) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication)

Example 23 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper 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 24 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper 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)

Example 25 with UpdateWrapper

use of com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper in project HOJ by HimitZH.

the class AdminProblemController method changeProblemAuth.

@PutMapping("/change-problem-auth")
@RequiresAuthentication
@RequiresRoles(value = { "root", "problem_admin", "admin" }, logical = Logical.OR)
public CommonResult changeProblemAuth(@RequestBody Problem problem, HttpServletRequest request) {
    // 普通管理员只能将题目变成隐藏题目和比赛题目
    boolean root = SecurityUtils.getSubject().hasRole("root");
    boolean problemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
    if (!problemAdmin && !root && problem.getAuth() == 1) {
        return CommonResult.errorResponse("修改失败!你无权限公开题目!", CommonResult.STATUS_FORBIDDEN);
    }
    HttpSession session = request.getSession();
    UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
    UpdateWrapper<Problem> problemUpdateWrapper = new UpdateWrapper<>();
    problemUpdateWrapper.eq("id", problem.getId()).set("auth", problem.getAuth()).set("modified_user", userRolesVo.getUsername());
    boolean result = problemService.update(problemUpdateWrapper);
    if (result) {
        // 更新成功
        return CommonResult.successResponse(null, "修改成功!");
    } else {
        return CommonResult.errorResponse("修改失败", CommonResult.STATUS_FAIL);
    }
}
Also used : UpdateWrapper(com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper) HttpSession(javax.servlet.http.HttpSession) UserRolesVo(top.hcode.hoj.pojo.vo.UserRolesVo) Problem(top.hcode.hoj.pojo.entity.problem.Problem) RequiresAuthentication(org.apache.shiro.authz.annotation.RequiresAuthentication) RequiresRoles(org.apache.shiro.authz.annotation.RequiresRoles)

Aggregations

UpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper)97 Transactional (org.springframework.transaction.annotation.Transactional)41 QueryWrapper (com.baomidou.mybatisplus.core.conditions.query.QueryWrapper)40 UserRolesVo (top.hcode.hoj.pojo.vo.UserRolesVo)34 StatusFailException (top.hcode.hoj.common.exception.StatusFailException)28 Session (org.apache.shiro.session.Session)24 StatusForbiddenException (top.hcode.hoj.common.exception.StatusForbiddenException)21 Judge (top.hcode.hoj.pojo.entity.judge.Judge)17 LambdaUpdateWrapper (com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper)16 HttpSession (javax.servlet.http.HttpSession)14 Problem (top.hcode.hoj.pojo.entity.problem.Problem)14 RequiresAuthentication (org.apache.shiro.authz.annotation.RequiresAuthentication)13 StatusNotFoundException (top.hcode.hoj.common.exception.StatusNotFoundException)11 Group (top.hcode.hoj.pojo.entity.group.Group)11 Date (java.util.Date)10 Discussion (top.hcode.hoj.pojo.entity.discussion.Discussion)10 Contest (top.hcode.hoj.pojo.entity.contest.Contest)8 User (com.baomidou.mybatisplus.samples.wrapper.entity.User)5 Result (org.jeecg.common.api.vo.Result)5 LoginUser (org.jeecg.common.system.vo.LoginUser)5