use of top.hcode.hoj.common.exception.StatusSystemErrorException in project HOJ by HimitZH.
the class AccountManager method changePassword.
/**
* @MethodName changePassword
* @Description 修改密码的操作,连续半小时内修改密码错误5次,则需要半个小时后才可以再次尝试修改密码
* @Return
* @Since 2021/1/8
*/
public ChangeAccountVo changePassword(ChangePasswordDto changePasswordDto) throws StatusSystemErrorException, StatusFailException {
String oldPassword = changePasswordDto.getOldPassword();
String newPassword = changePasswordDto.getNewPassword();
// 数据可用性判断
if (StringUtils.isEmpty(oldPassword) || StringUtils.isEmpty(newPassword)) {
throw new StatusFailException("错误:原始密码或新密码不能为空!");
}
if (newPassword.length() < 6 || newPassword.length() > 20) {
throw new StatusFailException("新密码长度应该为6~20位!");
}
// 获取当前登录的用户
org.apache.shiro.session.Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 如果已经被锁定半小时不能修改
String lockKey = Constants.Account.CODE_CHANGE_PASSWORD_LOCK + userRolesVo.getUid();
// 统计失败的key
String countKey = Constants.Account.CODE_CHANGE_PASSWORD_FAIL + userRolesVo.getUid();
ChangeAccountVo resp = new ChangeAccountVo();
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.setCode(403);
Date afterDate = new Date(now.getTime() + expire * 1000);
String msg = "由于您多次修改密码失败,修改密码功能已锁定,请在" + minute + "分" + second + "秒后(" + formatter.format(afterDate) + ")再进行尝试!";
resp.setMsg(msg);
return resp;
}
// 与当前登录用户的密码进行比较判断
if (userRolesVo.getPassword().equals(SecureUtil.md5(oldPassword))) {
// 如果相同,则进行修改密码操作
UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
// 数据库用户密码全部用md5加密
updateWrapper.set("password", SecureUtil.md5(newPassword)).eq("uuid", userRolesVo.getUid());
boolean isOk = userInfoEntityService.update(updateWrapper);
if (isOk) {
resp.setCode(200);
resp.setMsg("修改密码成功!您将于5秒钟后退出进行重新登录操作!");
// 清空记录
redisUtils.del(countKey);
// 更新session
userRolesVo.setPassword(SecureUtil.md5(newPassword));
session.setAttribute("userInfo", userRolesVo);
return resp;
} else {
throw new StatusSystemErrorException("系统错误:修改密码失败!");
}
} 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.setCode(400);
resp.setMsg("原始密码错误!您已累计修改密码失败" + count + "次...");
return resp;
}
}
use of top.hcode.hoj.common.exception.StatusSystemErrorException in project HOJ by HimitZH.
the class AccountManager method changeEmail.
/**
* @MethodName changeEmail
* @Description 修改邮箱的操作,连续半小时内密码错误5次,则需要半个小时后才可以再次尝试修改
* @Return
* @Since 2021/1/9
*/
public ChangeAccountVo changeEmail(ChangeEmailDto changeEmailDto) throws StatusSystemErrorException, StatusFailException {
String password = changeEmailDto.getPassword();
String newEmail = changeEmailDto.getNewEmail();
// 数据可用性判断
if (StringUtils.isEmpty(password) || StringUtils.isEmpty(newEmail)) {
throw new StatusFailException("错误:密码或新邮箱不能为空!");
}
if (!Validator.isEmail(newEmail)) {
throw new StatusFailException("邮箱格式错误!");
}
// 获取当前登录的用户
org.apache.shiro.session.Session session = SecurityUtils.getSubject().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();
ChangeAccountVo resp = new ChangeAccountVo();
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.setCode(403);
Date afterDate = new Date(now.getTime() + expire * 1000);
String msg = "由于您多次修改邮箱失败,修改邮箱功能已锁定,请在" + minute + "分" + second + "秒后(" + formatter.format(afterDate) + ")再进行尝试!";
resp.setMsg(msg);
return resp;
}
// 与当前登录用户的密码进行比较判断
if (userRolesVo.getPassword().equals(SecureUtil.md5(password))) {
// 如果相同,则进行修改操作
UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
updateWrapper.set("email", newEmail).eq("uuid", userRolesVo.getUid());
boolean isOk = userInfoEntityService.update(updateWrapper);
if (isOk) {
UserInfoVo userInfoVo = new UserInfoVo();
BeanUtil.copyProperties(userRolesVo, userInfoVo, "roles");
userInfoVo.setRoleList(userRolesVo.getRoles().stream().map(Role::getRole).collect(Collectors.toList()));
resp.setCode(200);
resp.setMsg("修改邮箱成功!");
resp.setUserInfo(userInfoVo);
// 清空记录
redisUtils.del(countKey);
// 更新session
userRolesVo.setEmail(newEmail);
session.setAttribute("userInfo", userRolesVo);
return resp;
} else {
throw new StatusSystemErrorException("系统错误:修改邮箱失败!");
}
} 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.setCode(400);
resp.setMsg("密码错误!您已累计修改邮箱失败" + count + "次...");
return resp;
}
}
use of top.hcode.hoj.common.exception.StatusSystemErrorException in project HOJ by HimitZH.
the class ImageManager method uploadCarouselImg.
@Transactional(rollbackFor = Exception.class)
public Map<Object, Object> uploadCarouselImg(MultipartFile image) throws StatusFailException, StatusSystemErrorException {
if (image == null) {
throw new StatusFailException("上传的图片文件不能为空!");
}
// 获取文件后缀
String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1);
if (!"jpg,jpeg,gif,png,webp,jfif,svg".toUpperCase().contains(suffix.toUpperCase())) {
throw new StatusFailException("请选择jpg,jpeg,gif,png,webp,jfif,svg格式的头像图片!");
}
// 若不存在该目录,则创建目录
FileUtil.mkdir(Constants.File.HOME_CAROUSEL_FOLDER.getPath());
// 通过UUID生成唯一文件名
String filename = IdUtil.simpleUUID() + "." + suffix;
try {
// 将文件保存指定目录
image.transferTo(FileUtil.file(Constants.File.HOME_CAROUSEL_FOLDER.getPath() + File.separator + filename));
} catch (Exception e) {
log.error("图片文件上传异常-------------->{}", e.getMessage());
throw new StatusSystemErrorException("服务器异常:图片上传失败!");
}
// 获取当前登录用户
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
// 插入file表记录
top.hcode.hoj.pojo.entity.common.File imgFile = new top.hcode.hoj.pojo.entity.common.File();
imgFile.setName(filename).setFolderPath(Constants.File.HOME_CAROUSEL_FOLDER.getPath()).setFilePath(Constants.File.HOME_CAROUSEL_FOLDER.getPath() + File.separator + filename).setSuffix(suffix).setType("carousel").setUid(userRolesVo.getUid());
fileEntityService.saveOrUpdate(imgFile);
return MapUtil.builder().put("id", imgFile.getId()).put("url", Constants.File.IMG_API.getPath() + filename).map();
}
use of top.hcode.hoj.common.exception.StatusSystemErrorException in project HOJ by HimitZH.
the class MarkDownFileManager method uploadMDImg.
public Map<Object, Object> uploadMDImg(MultipartFile image, Long gid) throws StatusFailException, StatusSystemErrorException, StatusForbiddenException {
Session session = SecurityUtils.getSubject().getSession();
UserRolesVo userRolesVo = (UserRolesVo) session.getAttribute("userInfo");
boolean isRoot = SecurityUtils.getSubject().hasRole("root");
boolean isProblemAdmin = SecurityUtils.getSubject().hasRole("problem_admin");
boolean isAdmin = SecurityUtils.getSubject().hasRole("admin");
if (!isRoot && !isProblemAdmin && !isAdmin && !(gid != null && groupValidator.isGroupAdmin(userRolesVo.getUid(), gid))) {
throw new StatusForbiddenException("对不起,您无权限操作!");
}
if (image == null) {
throw new StatusFailException("上传的图片不能为空!");
}
if (image.getSize() > 1024 * 1024 * 4) {
throw new StatusFailException("上传的图片文件大小不能大于4M!");
}
// 获取文件后缀
String suffix = image.getOriginalFilename().substring(image.getOriginalFilename().lastIndexOf(".") + 1);
if (!"jpg,jpeg,gif,png,webp".toUpperCase().contains(suffix.toUpperCase())) {
throw new StatusFailException("请选择jpg,jpeg,gif,png,webp格式的图片!");
}
// 若不存在该目录,则创建目录
FileUtil.mkdir(Constants.File.MARKDOWN_FILE_FOLDER.getPath());
// 通过UUID生成唯一文件名
String filename = IdUtil.simpleUUID() + "." + suffix;
try {
// 将文件保存指定目录
image.transferTo(FileUtil.file(Constants.File.MARKDOWN_FILE_FOLDER.getPath() + File.separator + filename));
} catch (Exception e) {
log.error("图片文件上传异常-------------->", e);
throw new StatusSystemErrorException("服务器异常:图片文件上传失败!");
}
top.hcode.hoj.pojo.entity.common.File file = new top.hcode.hoj.pojo.entity.common.File();
file.setFolderPath(Constants.File.MARKDOWN_FILE_FOLDER.getPath()).setName(filename).setFilePath(Constants.File.MARKDOWN_FILE_FOLDER.getPath() + File.separator + filename).setSuffix(suffix).setType("md").setUid(userRolesVo.getUid());
fileEntityService.save(file);
return MapUtil.builder().put("link", Constants.File.IMG_API.getPath() + filename).put("fileId", file.getId()).map();
}
use of top.hcode.hoj.common.exception.StatusSystemErrorException in project HOJ by HimitZH.
the class JudgeDispatcher method sendTestJudgeTask.
public void sendTestJudgeTask(TestJudgeReq testJudgeReq) throws StatusSystemErrorException {
testJudgeReq.setToken(judgeToken);
try {
boolean isOk = redisUtils.llPush(Constants.Queue.TEST_JUDGE_WAITING.getName(), JSONUtil.toJsonStr(testJudgeReq));
if (!isOk) {
throw new StatusSystemErrorException("系统错误:当前评测任务进入等待队列失败!");
}
// 调用判题任务处理
judgeReceiver.processWaitingTask();
} catch (Exception e) {
log.error("调用redis将判题纳入判题等待队列异常--------------->{}", e.getMessage());
throw new StatusSystemErrorException("系统错误:当前评测任务进入等待队列失败!");
}
}
Aggregations