use of me.zhengjie.exception.BadRequestException in project eladmin by elunez.
the class DeptServiceImpl method update.
@Override
@Transactional(rollbackFor = Exception.class)
public void update(Dept resources) {
// 旧的部门
Long oldPid = findById(resources.getId()).getPid();
Long newPid = resources.getPid();
if (resources.getPid() != null && resources.getId().equals(resources.getPid())) {
throw new BadRequestException("上级不能为自己");
}
Dept dept = deptRepository.findById(resources.getId()).orElseGet(Dept::new);
ValidationUtil.isNull(dept.getId(), "Dept", "id", resources.getId());
resources.setId(dept.getId());
deptRepository.save(resources);
// 更新父节点中子节点数目
updateSubCnt(oldPid);
updateSubCnt(newPid);
// 清理缓存
delCaches(resources.getId());
}
use of me.zhengjie.exception.BadRequestException in project eladmin by elunez.
the class UserServiceImpl method updateAvatar.
@Override
@Transactional(rollbackFor = Exception.class)
public Map<String, String> updateAvatar(MultipartFile multipartFile) {
// 文件大小验证
FileUtil.checkSize(properties.getAvatarMaxSize(), multipartFile.getSize());
// 验证文件上传的格式
String image = "gif jpg png jpeg";
String fileType = FileUtil.getExtensionName(multipartFile.getOriginalFilename());
if (fileType != null && !image.contains(fileType)) {
throw new BadRequestException("文件格式错误!, 仅支持 " + image + " 格式");
}
User user = userRepository.findByUsername(SecurityUtils.getCurrentUsername());
String oldPath = user.getAvatarPath();
File file = FileUtil.upload(multipartFile, properties.getPath().getAvatar());
user.setAvatarPath(Objects.requireNonNull(file).getPath());
user.setAvatarName(file.getName());
userRepository.save(user);
if (StringUtils.isNotBlank(oldPath)) {
FileUtil.del(oldPath);
}
@NotBlank String username = user.getUsername();
flushCache(username);
return new HashMap<String, String>(1) {
{
put("avatar", file.getName());
}
};
}
use of me.zhengjie.exception.BadRequestException in project eladmin by elunez.
the class QuartzManage method pauseJob.
/**
* 暂停一个job
* @param quartzJob /
*/
public void pauseJob(QuartzJob quartzJob) {
try {
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.pauseJob(jobKey);
} catch (Exception e) {
log.error("定时任务暂停失败", e);
throw new BadRequestException("定时任务暂停失败");
}
}
use of me.zhengjie.exception.BadRequestException in project eladmin by elunez.
the class QuartzManage method resumeJob.
/**
* 恢复一个job
* @param quartzJob /
*/
public void resumeJob(QuartzJob quartzJob) {
try {
TriggerKey triggerKey = TriggerKey.triggerKey(JOB_NAME + quartzJob.getId());
CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
// 如果不存在则创建一个定时任务
if (trigger == null) {
addJob(quartzJob);
}
JobKey jobKey = JobKey.jobKey(JOB_NAME + quartzJob.getId());
scheduler.resumeJob(jobKey);
} catch (Exception e) {
log.error("恢复定时任务失败", e);
throw new BadRequestException("恢复定时任务失败");
}
}
use of me.zhengjie.exception.BadRequestException in project eladmin by elunez.
the class UserDetailsServiceImpl method loadUserByUsername.
@Override
public JwtUserDto loadUserByUsername(String username) {
JwtUserDto jwtUserDto = null;
Future<JwtUserDto> future = USER_DTO_CACHE.get(username);
if (!loginProperties.isCacheEnable()) {
UserDto user;
try {
user = userService.findByName(username);
} catch (EntityNotFoundException e) {
// SpringSecurity会自动转换UsernameNotFoundException为BadCredentialsException
throw new UsernameNotFoundException(username, e);
}
if (user == null) {
throw new UsernameNotFoundException("");
} else {
if (!user.getEnabled()) {
throw new BadRequestException("账号未激活!");
}
jwtUserDto = new JwtUserDto(user, dataService.getDeptIds(user), roleService.mapToGrantedAuthorities(user));
}
return jwtUserDto;
}
if (future == null) {
Callable<JwtUserDto> call = () -> getJwtBySearchDb(username);
FutureTask<JwtUserDto> ft = new FutureTask<>(call);
future = USER_DTO_CACHE.putIfAbsent(username, ft);
if (future == null) {
future = ft;
executor.submit(ft);
}
try {
return future.get();
} catch (CancellationException e) {
USER_DTO_CACHE.remove(username);
System.out.println("error" + Thread.currentThread().getName());
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getMessage());
}
} else {
try {
jwtUserDto = future.get();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e.getMessage());
}
// 检查dataScope是否修改
List<Long> dataScopes = jwtUserDto.getDataScopes();
dataScopes.clear();
dataScopes.addAll(dataService.getDeptIds(jwtUserDto.getUser()));
}
return jwtUserDto;
}
Aggregations