Search in sources :

Example 1 with BadRequestException

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());
}
Also used : Dept(me.zhengjie.modules.system.domain.Dept) BadRequestException(me.zhengjie.exception.BadRequestException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BadRequestException

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());
        }
    };
}
Also used : User(me.zhengjie.modules.system.domain.User) NotBlank(javax.validation.constraints.NotBlank) BadRequestException(me.zhengjie.exception.BadRequestException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with BadRequestException

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("定时任务暂停失败");
    }
}
Also used : BadRequestException(me.zhengjie.exception.BadRequestException) BadRequestException(me.zhengjie.exception.BadRequestException)

Example 4 with 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("恢复定时任务失败");
    }
}
Also used : BadRequestException(me.zhengjie.exception.BadRequestException) BadRequestException(me.zhengjie.exception.BadRequestException)

Example 5 with 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;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDto(me.zhengjie.modules.system.service.dto.UserDto) JwtUserDto(me.zhengjie.modules.security.service.dto.JwtUserDto) JwtUserDto(me.zhengjie.modules.security.service.dto.JwtUserDto) EntityNotFoundException(me.zhengjie.exception.EntityNotFoundException) BadRequestException(me.zhengjie.exception.BadRequestException)

Aggregations

BadRequestException (me.zhengjie.exception.BadRequestException)26 Transactional (org.springframework.transaction.annotation.Transactional)8 ApiOperation (io.swagger.annotations.ApiOperation)4 File (java.io.File)4 ResponseEntity (org.springframework.http.ResponseEntity)4 IOException (java.io.IOException)3 UserDto (me.zhengjie.modules.system.service.dto.UserDto)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 AlipayClient (com.alipay.api.AlipayClient)2 DefaultAlipayClient (com.alipay.api.DefaultAlipayClient)2 Configuration (com.qiniu.storage.Configuration)2 Auth (com.qiniu.util.Auth)2 Date (java.util.Date)2 Log (me.zhengjie.annotation.Log)2 LocalStorage (me.zhengjie.domain.LocalStorage)2 QiniuContent (me.zhengjie.domain.QiniuContent)2 ExecuteShellUtil (me.zhengjie.modules.mnt.util.ExecuteShellUtil)2 JwtUserDto (me.zhengjie.modules.security.service.dto.JwtUserDto)2 CronTriggerImpl (org.quartz.impl.triggers.CronTriggerImpl)2 MailAccount (cn.hutool.extra.mail.MailAccount)1