feat:提交角色授权

This commit is contained in:
曾文豪
2024-08-07 22:15:40 +08:00
parent 0f5cdb49c0
commit ba80cebc45
9 changed files with 243 additions and 96 deletions

View File

@@ -10,6 +10,7 @@ import com.tiesheng.util.config.TsTokenConfig;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.pojos.TokenBean;
import com.tiesheng.util.service.TsCacheService;
import com.tiesheng.util.service.role.TsAuthorityHandler;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@@ -23,14 +24,14 @@ import java.util.stream.Collectors;
@Aspect
@Component
public class HasAuthorityAspect {
public class RoleAuthorityAspect {
public static final String CACHE_HAS_AUTHORITY = "CACHE:HAS_AUTHORITY:{}";
@Resource
CoreRoleUserMapper coreRoleUserMapper;
@Resource
TsTokenConfig tsTokenConfig;
@Resource
TsAuthorityHandler tsAuthorityHandler;
/**
@@ -57,9 +58,7 @@ public class HasAuthorityAspect {
.stream().filter(StrUtil::isNotEmpty).collect(Collectors.toList());
if (CollUtil.isEmpty(authorityList)) {
List<CoreRoleAuthority> list = coreRoleUserMapper.getOwnerAuthorityLeafList(tokenBean.getId(),
tokenBean.getRoleId());
authorityList = list.stream().map(CoreRoleAuthority::getNo).collect(Collectors.toList());
authorityList = tsAuthorityHandler.getAuthorities(tokenBean);
if (CollUtil.isNotEmpty(authorityList)) {
TsCacheService.of().put(cacheKey, StrUtil.join(";", authorityList));
}

View File

@@ -117,7 +117,7 @@ public class RoleController {
/**
* 色的权限编辑
* 色的权限编辑
*
* @return
*/
@@ -158,11 +158,7 @@ public class RoleController {
@PostMapping("/user/update")
@RoleAuthority(value = "userUpdate")
public ApiResp<String> userUpdate(@RequestBody CoreRoleUser roleUser) {
if (StrUtil.isNotEmpty(roleUser.getId())) {
coreRoleService.getUserMapper().updateById(roleUser);
} else {
coreRoleService.getUserMapper().insert(roleUser);
}
coreRoleService.roleUserUpdate(roleUser);
return ApiResp.respOK("");
}
@@ -175,10 +171,7 @@ public class RoleController {
@PostMapping("/user/deleted")
@RoleAuthority(value = "userDeleted")
public ApiResp<String> userDeleted(@RequestBody @Valid IdDTO dto) {
CoreRoleUser coreRoleUser = new CoreRoleUser();
coreRoleUser.setId(dto.getId());
coreRoleUser.setIsDeleted(1);
coreRoleService.getUserMapper().updateById(coreRoleUser);
coreRoleService.roleUserDeleted(dto.getId());
return ApiResp.respOK("");
}

View File

@@ -3,18 +3,23 @@ package com.tiesheng.role.service;
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiesheng.annotation.role.RoleAuthority;
import com.tiesheng.role.config.RoleAuthorityAspect;
import com.tiesheng.role.mapper.*;
import com.tiesheng.role.pojos.dao.CoreRoleAuthority;
import com.tiesheng.role.pojos.dao.CoreRoleGroup;
import com.tiesheng.role.pojos.dao.CoreRoleGroupRx;
import com.tiesheng.role.pojos.dao.CoreRoleUser;
import com.tiesheng.role.pojos.dto.GroupRxUpdateDTO;
import com.tiesheng.role.pojos.dto.OwnerMenuDTO;
import com.tiesheng.role.pojos.vo.ServiceMenuVO;
import com.tiesheng.util.config.GlobalConfig;
import com.tiesheng.util.exception.ApiException;
import com.tiesheng.util.service.TsCacheService;
import com.tiesheng.util.service.TsServiceBase;
import com.tiesheng.util.service.role.TsAuthorityHandler;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
@@ -24,10 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.*;
import java.util.stream.Collectors;
@Service
@@ -43,7 +45,7 @@ public class CoreRoleService extends TsServiceBase<CoreRoleGroupMapper, CoreRole
@Resource
CoreRoleServerMapper coreRoleServerMapper;
@Resource
GlobalConfig globalConfig;
TsAuthorityHandler tsAuthorityHandler;
public CoreRoleServerMapper getServerMapper() {
return coreRoleServerMapper;
@@ -98,16 +100,73 @@ public class CoreRoleService extends TsServiceBase<CoreRoleGroupMapper, CoreRole
coreRoleGroupRxMapper.delete(new QueryWrapper<CoreRoleGroupRx>()
.eq("group_id", dto.getId())
);
CoreRoleGroup roleGroup = getById(dto.getId());
Validator.validateNotEmpty(roleGroup, "角色不存在");
List<CoreRoleAuthority> authorities = coreRoleAuthorityMapper.selectList(new QueryWrapper<CoreRoleAuthority>()
.eq(CoreRoleAuthority.IS_DELETED, 0)
.in("id", dto.getMenuIds())
);
List<CoreRoleGroupRx> list = new ArrayList<>();
for (String menuId : dto.getMenuIds()) {
for (CoreRoleAuthority authority : authorities) {
CoreRoleGroupRx coreRoleGroupRx = new CoreRoleGroupRx();
coreRoleGroupRx.setGroupId(dto.getId());
coreRoleGroupRx.setMenuId(menuId);
coreRoleGroupRx.setGroupId(roleGroup.getId());
coreRoleGroupRx.setMenuId(authority.getId());
list.add(coreRoleGroupRx);
}
if (CollUtil.isNotEmpty(list)) {
coreRoleGroupRxMapper.batchInsert(list);
onRoleChange(dto.getId(), "");
}
}
/**
* 编辑、更新用户的授权
*
* @param roleUser
*/
public void roleUserUpdate(CoreRoleUser roleUser) {
if (StrUtil.isNotEmpty(roleUser.getId())) {
coreRoleUserMapper.updateById(roleUser);
} else {
coreRoleUserMapper.insert(roleUser);
}
onRoleChange(roleUser.getTypeId(), roleUser.getUserId());
}
/**
* 删除用户授权
*
* @param recordId
*/
public void roleUserDeleted(String recordId) {
CoreRoleUser coreRoleUser = coreRoleUserMapper.selectById(recordId);
if (coreRoleUser == null || coreRoleUser.getIsDeleted() == 1) {
throw new ApiException("该授权已被删除");
}
coreRoleUser.setIsDeleted(1);
coreRoleUserMapper.updateById(coreRoleUser);
onRoleChange(coreRoleUser.getTypeId(), coreRoleUser.getUserId());
}
/**
* 当授权发生变化时
*/
public void onRoleChange(String roleId, String userId) {
if (StrUtil.isNotEmpty(userId)) {
TsCacheService.of().remove(StrUtil.format(RoleAuthorityAspect.CACHE_HAS_AUTHORITY, userId));
} else {
TsCacheService.of().keys(StrUtil.replace(RoleAuthorityAspect.CACHE_HAS_AUTHORITY,
"{}", "")).forEach(key -> TsCacheService.of().remove(key));
}
}
@@ -180,78 +239,9 @@ public class CoreRoleService extends TsServiceBase<CoreRoleGroupMapper, CoreRole
points.add(methodAuthority);
}
}
addOrUpdateAuthority(menu, points);
tsAuthorityHandler.addRoleAuthority(menu, points);
}
}
/**
* 添加权限
*
* @param menu
* @param points
*/
private void addOrUpdateAuthority(RoleAuthority menu, List<RoleAuthority> points) {
if (menu.group().length == 0 || CollUtil.isEmpty(points)) {
return;
}
List<CoreRoleAuthority> list = new ArrayList<>();
// 分组
CoreRoleAuthority groupAuthority = new CoreRoleAuthority();
groupAuthority.setLevel(0);
for (String group : menu.group()) {
String parentId = groupAuthority.getParent();
int level = groupAuthority.getLevel() + 1;
String no = group;
if (StrUtil.isNotEmpty(groupAuthority.getNo())) {
no = StrUtil.join("_", groupAuthority.getNo(), group);
}
groupAuthority = new CoreRoleAuthority();
groupAuthority.setNo(no);
groupAuthority.setName(group);
groupAuthority.setService(globalConfig.getService());
groupAuthority.setType("group");
groupAuthority.setLevel(level);
groupAuthority.setPlatform(menu.platform());
groupAuthority.setParent(parentId);
groupAuthority.setId(StrUtil.join("_", groupAuthority.getService(), groupAuthority.getNo()));
list.add(groupAuthority);
}
// 菜单
CoreRoleAuthority menuAuthority = new CoreRoleAuthority();
menuAuthority.setNo(StrUtil.join("_", menu.group(), menu.value()));
menuAuthority.setName(menu.value());
menuAuthority.setService(globalConfig.getService());
menuAuthority.setType("menu");
menuAuthority.setLevel(groupAuthority.getLevel() + 1);
menuAuthority.setParent(groupAuthority.getId());
menuAuthority.setPlatform(menu.platform());
menuAuthority.setId(StrUtil.join("_", menuAuthority.getService(), menuAuthority.getNo()));
list.add(menuAuthority);
// 功能点
for (RoleAuthority authority : points) {
CoreRoleAuthority point = new CoreRoleAuthority();
point.setNo(StrUtil.join("_", menuAuthority.getNo(), authority.value()));
point.setName(authority.value());
point.setService(globalConfig.getService());
point.setType("point");
point.setLevel(menuAuthority.getLevel() + 1);
point.setParent(menuAuthority.getId());
point.setPlatform(StrUtil.emptyToDefault(authority.platform(), menu.platform()));
point.setId(StrUtil.join("_", point.getService(), point.getNo()));
list.add(point);
}
coreRoleAuthorityMapper.batchInsert(list);
}
}

View File

@@ -0,0 +1,100 @@
package com.tiesheng.role.service;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tiesheng.annotation.role.RoleAuthority;
import com.tiesheng.role.mapper.CoreRoleAuthorityMapper;
import com.tiesheng.role.mapper.CoreRoleUserMapper;
import com.tiesheng.role.pojos.dao.CoreRoleAuthority;
import com.tiesheng.util.config.GlobalConfig;
import com.tiesheng.util.pojos.TokenBean;
import com.tiesheng.util.service.role.TsAuthorityHandler;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class RoleAuthorityHandler implements TsAuthorityHandler {
@Resource
CoreRoleAuthorityMapper coreRoleAuthorityMapper;
@Resource
GlobalConfig globalConfig;
@Resource
CoreRoleUserMapper coreRoleUserMapper;
@Override
public void addRoleAuthority(RoleAuthority menu, List<RoleAuthority> points) {
if (menu.group().length == 0 || CollUtil.isEmpty(points)) {
return;
}
List<CoreRoleAuthority> list = new ArrayList<>();
// 分组
CoreRoleAuthority groupAuthority = new CoreRoleAuthority();
groupAuthority.setLevel(0);
for (String group : menu.group()) {
String parentId = groupAuthority.getParent();
int level = groupAuthority.getLevel() + 1;
String no = group;
if (StrUtil.isNotEmpty(groupAuthority.getNo())) {
no = StrUtil.join("_", groupAuthority.getNo(), group);
}
groupAuthority = new CoreRoleAuthority();
groupAuthority.setNo(no);
groupAuthority.setName(group);
groupAuthority.setService(globalConfig.getService());
groupAuthority.setType("group");
groupAuthority.setLevel(level);
groupAuthority.setPlatform(menu.platform());
groupAuthority.setParent(parentId);
groupAuthority.setId(StrUtil.join("_", groupAuthority.getService(), groupAuthority.getNo()));
list.add(groupAuthority);
}
// 菜单
CoreRoleAuthority menuAuthority = new CoreRoleAuthority();
menuAuthority.setNo(StrUtil.join("_", menu.group(), menu.value()));
menuAuthority.setName(menu.value());
menuAuthority.setService(globalConfig.getService());
menuAuthority.setType("menu");
menuAuthority.setLevel(groupAuthority.getLevel() + 1);
menuAuthority.setParent(groupAuthority.getId());
menuAuthority.setPlatform(menu.platform());
menuAuthority.setId(StrUtil.join("_", menuAuthority.getService(), menuAuthority.getNo()));
list.add(menuAuthority);
// 功能点
for (RoleAuthority authority : points) {
CoreRoleAuthority point = new CoreRoleAuthority();
point.setNo(StrUtil.join("_", menuAuthority.getNo(), authority.value()));
point.setName(authority.value());
point.setService(globalConfig.getService());
point.setType("point");
point.setLevel(menuAuthority.getLevel() + 1);
point.setParent(menuAuthority.getId());
point.setPlatform(StrUtil.emptyToDefault(authority.platform(), menu.platform()));
point.setId(StrUtil.join("_", point.getService(), point.getNo()));
list.add(point);
}
coreRoleAuthorityMapper.batchInsert(list);
}
@Override
public List<String> getAuthorities(TokenBean tokenBean) {
List<CoreRoleAuthority> list = coreRoleUserMapper.getOwnerAuthorityLeafList(tokenBean.getId(),
tokenBean.getRoleId());
return list.stream().map(CoreRoleAuthority::getNo).distinct().collect(Collectors.toList());
}
}