| | |
| | | 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(); |
| | |
| | | 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(); |
| | |
| | | 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); |
| | | } |
| | | } |