| | |
| | | package com.sandu.ximon.admin.service; |
| | | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.admin.param.AdminParam; |
| | | import com.sandu.ximon.admin.param.PwdParam; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.domain.Admin; |
| | | import com.sandu.ximon.dao.domain.AdminRoleRelation; |
| | | import com.sandu.ximon.dao.domain.Role; |
| | | import com.sandu.ximon.dao.mapper.AdminMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.security.crypto.password.PasswordEncoder; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @author chenjiantian |
| | | * @date 2021/11/24 11:02 |
| | | */ |
| | | @Service |
| | | @AllArgsConstructor |
| | | public class AdminService extends BaseServiceImpl<AdminMapper, Admin> { |
| | | |
| | | private final PasswordEncoder passwordEncoder; |
| | | private final AdminRoleRelationService adminRoleRelationService; |
| | | private final RoleService roleService; |
| | | |
| | | public Admin findByUserName(String username) { |
| | | return getOne(Wrappers.lambdaQuery(Admin.class).eq(Admin::getUsername, username).last("limit 1")); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean register(AdminParam param) { |
| | | Admin admin = findByUserName(param.getUsername()); |
| | | if (admin != null) { |
| | | throw new BusinessException("当前账号" + param.getUsername() + "已经存在"); |
| | | } |
| | | Admin save = new Admin(); |
| | | save.setUsername(param.getUsername()); |
| | | save.setPassword(passwordEncoder.encode(param.getPassword())); |
| | | save.setNickName(param.getNickName()); |
| | | save.setMobile(param.getMobile()); |
| | | save.setNote(param.getNote()); |
| | | if (!save(save)) { |
| | | throw new BusinessException("注册管理员失败"); |
| | | } |
| | | |
| | | Role role = roleService.getById(param.getRoleId()); |
| | | if (role == null) { |
| | | throw new BusinessException("角色不存在"); |
| | | } |
| | | if (RoleLevelStatus.SUPER.getCode().equals(role.getLevel())) { |
| | | throw new BusinessException("无法添加超级管理员"); |
| | | } |
| | | |
| | | AdminRoleRelation adminRoleRelation = new AdminRoleRelation(); |
| | | adminRoleRelation.setAdminId(save.getId()); |
| | | adminRoleRelation.setRoleId(param.getRoleId()); |
| | | if (!adminRoleRelationService.save(adminRoleRelation)) { |
| | | throw new BusinessException("添加管理员角色失败"); |
| | | } |
| | | return true; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean updateAdmin(Long adminId, AdminParam param) { |
| | | 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); |
| | | 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()); |
| | | 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())) { |
| | | throw new BusinessException("两次密码不一致"); |
| | | } |
| | | Long userId = SecurityUtils.getUserId(); |
| | | Admin admin = getById(userId); |
| | | if (admin == null) { |
| | | throw new BusinessException("用户不存在"); |
| | | } |
| | | if (!passwordEncoder.matches(param.getOldPass(), admin.getPassword())) { |
| | | throw new BusinessException("旧密码不正确"); |
| | | } |
| | | Admin update = new Admin(); |
| | | update.setId(userId); |
| | | 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); |
| | | } |
| | | } |