|
@@ -10,9 +10,11 @@ import cn.iocoder.byzs.framework.common.util.collection.CollectionUtils;
|
|
|
import cn.iocoder.byzs.framework.common.util.date.DateUtils;
|
|
import cn.iocoder.byzs.framework.common.util.date.DateUtils;
|
|
|
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
|
|
import cn.iocoder.byzs.framework.common.util.object.BeanUtils;
|
|
|
import cn.iocoder.byzs.framework.datapermission.core.annotation.DataPermission;
|
|
import cn.iocoder.byzs.framework.datapermission.core.annotation.DataPermission;
|
|
|
|
|
+import cn.iocoder.byzs.framework.security.core.util.SecurityFrameworkUtils;
|
|
|
import cn.iocoder.byzs.framework.tenant.config.TenantProperties;
|
|
import cn.iocoder.byzs.framework.tenant.config.TenantProperties;
|
|
|
import cn.iocoder.byzs.framework.tenant.core.context.TenantContextHolder;
|
|
import cn.iocoder.byzs.framework.tenant.core.context.TenantContextHolder;
|
|
|
import cn.iocoder.byzs.framework.tenant.core.util.TenantUtils;
|
|
import cn.iocoder.byzs.framework.tenant.core.util.TenantUtils;
|
|
|
|
|
+import cn.iocoder.byzs.module.system.api.permission.PermissionApi;
|
|
|
import cn.iocoder.byzs.module.system.controller.admin.permission.vo.role.RoleSaveReqVO;
|
|
import cn.iocoder.byzs.module.system.controller.admin.permission.vo.role.RoleSaveReqVO;
|
|
|
import cn.iocoder.byzs.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO;
|
|
import cn.iocoder.byzs.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO;
|
|
|
import cn.iocoder.byzs.module.system.controller.admin.tenant.vo.tenant.TenantSaveReqVO;
|
|
import cn.iocoder.byzs.module.system.controller.admin.tenant.vo.tenant.TenantSaveReqVO;
|
|
@@ -74,6 +76,8 @@ public class TenantServiceImpl implements TenantService {
|
|
|
private MenuService menuService;
|
|
private MenuService menuService;
|
|
|
@Resource
|
|
@Resource
|
|
|
private PermissionService permissionService;
|
|
private PermissionService permissionService;
|
|
|
|
|
+ @Resource
|
|
|
|
|
+ private PermissionApi permissionApi;
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public List<Long> getTenantIdList() {
|
|
public List<Long> getTenantIdList() {
|
|
@@ -195,25 +199,45 @@ public class TenantServiceImpl implements TenantService {
|
|
|
@Override
|
|
@Override
|
|
|
@DSTransactional
|
|
@DSTransactional
|
|
|
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
|
|
public void updateTenantRoleMenu(Long tenantId, Set<Long> menuIds) {
|
|
|
- TenantUtils.execute(tenantId, () -> {
|
|
|
|
|
- // 获得所有角色
|
|
|
|
|
- List<RoleDO> roles = roleService.getRoleList();
|
|
|
|
|
- roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配",
|
|
|
|
|
- role.getId(), role.getTenantId(), tenantId)); // 兜底校验
|
|
|
|
|
- // 重新分配每个角色的权限
|
|
|
|
|
- roles.forEach(role -> {
|
|
|
|
|
- // 如果是租户管理员,重新分配其权限为租户套餐的权限
|
|
|
|
|
- if (Objects.equals(role.getCode(), RoleCodeEnum.TENANT_ADMIN.getCode())) {
|
|
|
|
|
- permissionService.assignRoleMenu(role.getId(), menuIds);
|
|
|
|
|
- log.info("[updateTenantRoleMenu][租户管理员({}/{}) 的权限修改为({})]", role.getId(), role.getTenantId(), menuIds);
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- // 如果是其他角色,则去掉超过套餐的权限
|
|
|
|
|
- Set<Long> roleMenuIds = permissionService.getRoleMenuListByRoleId(role.getId());
|
|
|
|
|
- roleMenuIds = CollUtil.intersectionDistinct(roleMenuIds, menuIds);
|
|
|
|
|
- permissionService.assignRoleMenu(role.getId(), roleMenuIds);
|
|
|
|
|
- log.info("[updateTenantRoleMenu][角色({}/{}) 的权限修改为({})]", role.getId(), role.getTenantId(), roleMenuIds);
|
|
|
|
|
|
|
+ Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
|
|
|
|
|
+ boolean isSuperAdmin = permissionApi.hasAnyRoles(loginUserId, RoleCodeEnum.SUPER_ADMIN.getCode());
|
|
|
|
|
+
|
|
|
|
|
+ if (isSuperAdmin) {
|
|
|
|
|
+ // 超级管理员:忽略租户隔离,直接操作
|
|
|
|
|
+ TenantUtils.executeIgnore(() -> {
|
|
|
|
|
+ List<RoleDO> roles = roleService.getRoleListByTenantId(tenantId);
|
|
|
|
|
+ updateRoleMenus(roles, menuIds, tenantId);
|
|
|
|
|
+ });
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 普通用户:使用租户切换
|
|
|
|
|
+ TenantUtils.execute(tenantId, () -> {
|
|
|
|
|
+ List<RoleDO> roles = roleService.getRoleList();
|
|
|
|
|
+ updateRoleMenus(roles, menuIds, tenantId);
|
|
|
});
|
|
});
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 更新角色菜单权限的通用方法
|
|
|
|
|
+ */
|
|
|
|
|
+ private void updateRoleMenus(List<RoleDO> roles, Set<Long> menuIds, Long tenantId) {
|
|
|
|
|
+ roles.forEach(role -> Assert.isTrue(tenantId.equals(role.getTenantId()), "角色({}/{}) 租户不匹配",
|
|
|
|
|
+ role.getId(), role.getTenantId(), tenantId)); // 兜底校验
|
|
|
|
|
+
|
|
|
|
|
+ // 重新分配每个角色的权限
|
|
|
|
|
+ roles.forEach(role -> {
|
|
|
|
|
+ // 如果是租户管理员,重新分配其权限为租户套餐的权限
|
|
|
|
|
+ if (Objects.equals(role.getCode(), RoleCodeEnum.TENANT_ADMIN.getCode())) {
|
|
|
|
|
+ permissionService.assignRoleMenu(role.getId(), menuIds);
|
|
|
|
|
+ log.info("[updateTenantRoleMenu][租户管理员({}/{}) 的权限修改为({})]", role.getId(), role.getTenantId(), menuIds);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果是其他角色,则去掉超过套餐的权限
|
|
|
|
|
+ Set<Long> roleMenuIds = permissionService.getRoleMenuListByRoleId(role.getId());
|
|
|
|
|
+ roleMenuIds = CollUtil.intersectionDistinct(roleMenuIds, menuIds);
|
|
|
|
|
+ permissionService.assignRoleMenu(role.getId(), roleMenuIds);
|
|
|
|
|
+ log.info("[updateTenantRoleMenu][角色({}/{}) 的权限修改为({})]", role.getId(), role.getTenantId(), roleMenuIds);
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|