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, "修改邮箱失败!");
}
}
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("回复失败,请重新尝试!");
}
}
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);
}
}
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;
}
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);
}
}
Aggregations