| | |
| | | import cn.hutool.core.collection.CollectionUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.github.pagehelper.PageHelper; |
| | | import com.sandu.common.enums.RoleLevelStatus; |
| | | import com.sandu.common.execption.BusinessException; |
| | | import com.sandu.common.execption.EntityExistException; |
| | | import com.sandu.common.service.impl.BaseServiceImpl; |
| | | import com.sandu.ximon.dao.domain.AdminRoleRelation; |
| | | import com.sandu.ximon.dao.domain.Menu; |
| | | import com.sandu.ximon.dao.domain.Role; |
| | | import com.sandu.ximon.dao.domain.RoleMenuRelation; |
| | | import com.sandu.common.util.BeanConvertUtil; |
| | | import com.sandu.ximon.admin.param.RoleParam; |
| | | import com.sandu.ximon.admin.security.SecurityUtils; |
| | | import com.sandu.ximon.dao.domain.*; |
| | | import com.sandu.ximon.dao.mapper.RoleMapper; |
| | | import lombok.AllArgsConstructor; |
| | | import org.springframework.security.core.GrantedAuthority; |
| | | import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.util.Collection; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getAdminId, adminId).select(AdminRoleRelation::getRoleId)); |
| | | List<Long> roleIdList = list.stream().map(AdminRoleRelation::getRoleId).collect(Collectors.toList()); |
| | | if (CollectionUtil.isEmpty(roleIdList)) { |
| | | throw new BusinessException("当前用户没有角色"); |
| | | return null; |
| | | } |
| | | return listByIds(roleIdList); |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean addRole(RoleParam param) { |
| | | Role role = new Role(); |
| | | role.setName(param.getName()); |
| | | role.setRemark(param.getRemark()); |
| | | role.setLevel(RoleLevelStatus.COMMON.getCode()); |
| | | if(!save(role)){ |
| | | throw new BusinessException("添加角色失败"); |
| | | } |
| | | |
| | | roleMenuRelationService.addRoleMenuList(role.getId(),param.getMenuIdList()); |
| | | |
| | | return true; |
| | | } |
| | | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public boolean updateRole(Long roleId, RoleParam param) { |
| | | Role role = getById(roleId); |
| | | if (role == null) { |
| | | throw new BusinessException("找不到角色信息"); |
| | | } |
| | | assertLevels(role.getLevel()); |
| | | Role update = new Role(); |
| | | update.setId(roleId); |
| | | update.setName(param.getName()); |
| | | update.setRemark(param.getRemark()); |
| | | if(!updateById(update)){ |
| | | throw new BusinessException("编辑角色失败"); |
| | | } |
| | | roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId,roleId)); |
| | | |
| | | roleMenuRelationService.addRoleMenuList(role.getId(),param.getMenuIdList()); |
| | | return true; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 判断当前角色能不能操作目标用户 不行的话直接抛出异常 |
| | | * @param roleLevel 目标用户的最大角色 |
| | | * @return |
| | | */ |
| | | public int assertLevels(Integer roleLevel) { |
| | | Long userId = SecurityUtils.getUserId(); |
| | | List<Role> roles = listByAdminId(userId); |
| | | if(CollectionUtil.isEmpty(roles)){ |
| | | throw new BusinessException("当前用户没有角色"); |
| | | } |
| | | List<Integer> levels = roles.stream().map(Role::getLevel).collect(Collectors.toList()); |
| | | int min = Collections.min(levels); |
| | | if (roleLevel != null) { |
| | | if (roleLevel < min) { |
| | | throw new BusinessException("权限不足,你的角色级别:" + min + ",低于操作的角色级别:" + roleLevel); |
| | | } |
| | | } |
| | | return min; |
| | | } |
| | | |
| | | public boolean delRole(Long roleId) { |
| | | Role role = getById(roleId); |
| | | if (role == null) { |
| | | throw new BusinessException("找不到角色"); |
| | | } |
| | | assertLevels(role.getLevel()); |
| | | List<AdminRoleRelation> list = adminRoleRelationService.list(Wrappers.lambdaQuery(AdminRoleRelation.class).eq(AdminRoleRelation::getRoleId, role)); |
| | | if(CollectionUtil.isNotEmpty(list)){ |
| | | throw new BusinessException("当前角色有管理员使用,无法删除"); |
| | | } |
| | | roleMenuRelationService.remove(Wrappers.lambdaQuery(RoleMenuRelation.class).eq(RoleMenuRelation::getRoleId,roleId)); |
| | | return removeById(roleId); |
| | | } |
| | | |
| | | public List<RoleDetail> listRole(int pageNo, int pageSize) { |
| | | PageHelper.startPage(pageNo, pageSize); |
| | | List<Long> roleIdList = list().stream().map(Role::getId).collect(Collectors.toList()); |
| | | if(CollectionUtil.isEmpty(roleIdList)){ |
| | | return null; |
| | | } |
| | | return baseMapper.listRole(roleIdList); |
| | | } |
| | | } |