2021与蓝度共同重构项目,服务端
chenjiantian
2022-01-18 29febf388aa86d87198fe82e4af7eb88567d0d65
ximon-admin/src/main/java/com/sandu/ximon/admin/service/AdminService.java
@@ -71,29 +71,43 @@
        return true;
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAdmin(Long adminId, AdminParam param) {
        Admin admin = getById(adminId);
        if(admin == null){
        if (admin == null) {
            throw new BusinessException("找不到管理员");
        }
        List<Role> roles = roleService.listByAdminId(admin.getId());
        if(CollectionUtil.isEmpty(roles)){
        if (CollectionUtil.isEmpty(roles)) {
            throw new BusinessException("当前用户没有角色");
        }
        List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
        int min = Collections.min(levels);
        roleService.assertLevels(min);
        Admin update = new Admin();
        update.setId(adminId);
        update.setPassword(passwordEncoder.encode(param.getPassword()));
        update.setNickName(param.getNickName());
        update.setMobile(param.getMobile());
        update.setNote(param.getNote());
        return updateById(update);
        if (!updateById(update)) {
            throw new BusinessException("编辑管理员失败");
        }
        adminRoleRelationService.remove(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId,admin));
        AdminRoleRelation adminRoleRelation = new AdminRoleRelation();
        adminRoleRelation.setAdminId(adminId);
        adminRoleRelation.setRoleId(param.getRoleId());
        if (!adminRoleRelationService.save(adminRoleRelation)) {
            throw new BusinessException("添加管理员角色失败");
        }
        return true;
    }
    public boolean updateMyPassword(PwdParam param) {
        if(!StrUtil.equals(param.getNewPass(),param.getConfirmPass())){
        if (!StrUtil.equals(param.getNewPass(), param.getConfirmPass())) {
            throw new BusinessException("两次密码不一致");
        }
        Long userId = SecurityUtils.getUserId();
@@ -101,7 +115,7 @@
        if (admin == null) {
            throw new BusinessException("用户不存在");
        }
        if(!passwordEncoder.matches(param.getOldPass(),admin.getPassword())){
        if (!passwordEncoder.matches(param.getOldPass(), admin.getPassword())) {
            throw new BusinessException("旧密码不正确");
        }
        Admin update = new Admin();
@@ -109,4 +123,23 @@
        update.setPassword(passwordEncoder.encode(param.getNewPass()));
        return updateById(update);
    }
    public boolean deleteAdmin(Long adminId) {
        Admin admin = getById(adminId);
        if (admin == null) {
            throw new BusinessException("找不到管理员");
        }
        List<Role> roles = roleService.listByAdminId(admin.getId());
        if (CollectionUtil.isEmpty(roles)) {
            throw new BusinessException("当前用户没有角色");
        }
        List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList());
        int min = Collections.min(levels);
        int maxLevel = roleService.assertLevels(min);
        if(!RoleLevelStatus.SUPER.getCode().equals(maxLevel)){
            throw new BusinessException("只有超级管理员才能删除用户");
        }
        return removeById(adminId);
    }
}