feat(system): 完善租户创建权限控制

- 在 TenantType 枚举中添加 SUPERADMIN 类型
- 实现 allowCreateByType 方法控制租户创建权限- 调整 SysTenantController 中的权限注解
-优化 SysTenantServiceImpl 中的租户创建逻辑
This commit is contained in:
huk 2025-09-05 18:24:05 +08:00
parent bf728cee9b
commit 2dd6ac0631
7 changed files with 47 additions and 50 deletions

View File

@ -14,9 +14,23 @@ public enum TenantType {
PROXY(1, "代理"), PROXY(1, "代理"),
REFERENCE(2, "达人"); REFERENCE(2, "达人"),
private final int status; SUPERADMIN(3, "平台");
private final int type;
private final String desc; private final String desc;
public static boolean allowCreateByType(Integer currentTenantType, Integer newTenantType) {
boolean allow = false;
if(currentTenantType == SUPERADMIN.type){
// 超级管理租户创建租户没有限制
allow = true;
} else if (currentTenantType == TenantType.PROXY.type ){
// 代理商只能创建商铺或达人
allow = newTenantType == TenantType.SHOP.type || newTenantType == TenantType.REFERENCE.type;
}
return allow;
}
} }

View File

@ -80,7 +80,7 @@ public class SysMenuController extends BaseController {
/** /**
* 获取菜单下拉树列表 * 获取菜单下拉树列表
*/ */
@SaCheckPermission("system:menu:query") @SaCheckPermission(value = "system:menu:query", orRole = TenantConstants.TENANT_ADMIN_ROLE_KEY)
@GetMapping("/treeselect") @GetMapping("/treeselect")
public R<List<Tree<Long>>> treeselect(SysMenuBo menu) { public R<List<Tree<Long>>> treeselect(SysMenuBo menu) {
List<SysMenuVo> menus = menuService.selectMenuList(menu, LoginHelper.getUserId()); List<SysMenuVo> menus = menuService.selectMenuList(menu, LoginHelper.getUserId());
@ -92,7 +92,7 @@ public class SysMenuController extends BaseController {
* *
* @param roleId 角色ID * @param roleId 角色ID
*/ */
@SaCheckPermission("system:menu:query") @SaCheckPermission(value = "system:menu:query", orRole = TenantConstants.TENANT_ADMIN_ROLE_KEY)
@GetMapping(value = "/roleMenuTreeselect/{roleId}") @GetMapping(value = "/roleMenuTreeselect/{roleId}")
public R<MenuTreeSelectVo> roleMenuTreeselect(@PathVariable("roleId") Long roleId) { public R<MenuTreeSelectVo> roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
List<SysMenuVo> menus = menuService.selectMenuList(LoginHelper.getUserId()); List<SysMenuVo> menus = menuService.selectMenuList(LoginHelper.getUserId());

View File

@ -92,7 +92,7 @@ public class SysTenantController extends BaseController {
/** /**
* 新增租户 * 新增租户
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenant:add") @SaCheckPermission("system:tenant:add")
@Log(title = "租户管理", businessType = BusinessType.INSERT) @Log(title = "租户管理", businessType = BusinessType.INSERT)
@Lock4j @Lock4j
@ -108,7 +108,7 @@ public class SysTenantController extends BaseController {
/** /**
* 修改租户 * 修改租户
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenant:edit") @SaCheckPermission("system:tenant:edit")
@Log(title = "租户管理", businessType = BusinessType.UPDATE) @Log(title = "租户管理", businessType = BusinessType.UPDATE)
@RepeatSubmit() @RepeatSubmit()
@ -124,7 +124,7 @@ public class SysTenantController extends BaseController {
/** /**
* 状态修改 * 状态修改
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenant:edit") @SaCheckPermission("system:tenant:edit")
@Log(title = "租户管理", businessType = BusinessType.UPDATE) @Log(title = "租户管理", businessType = BusinessType.UPDATE)
@PutMapping("/changeStatus") @PutMapping("/changeStatus")
@ -179,7 +179,7 @@ public class SysTenantController extends BaseController {
*/ */
@Tag(name = "删除租户表") @Tag(name = "删除租户表")
@Log(title = "租户表", businessType = BusinessType.DELETE) @Log(title = "租户表", businessType = BusinessType.DELETE)
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) @SaCheckRole(value = {TenantConstants.SUPER_ADMIN_ROLE_KEY, TenantConstants.TENANT_ADMIN_ROLE_KEY})
@SaCheckPermission("system:tenant:remove") @SaCheckPermission("system:tenant:remove")
@DeleteMapping("/remove") @DeleteMapping("/remove")
public R batchRemove(@RequestParam("ids") String ids) { public R batchRemove(@RequestParam("ids") String ids) {
@ -196,7 +196,7 @@ public class SysTenantController extends BaseController {
* *
* @param tenantId 租户ID * @param tenantId 租户ID
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@GetMapping("/dynamic/{tenantId}") @GetMapping("/dynamic/{tenantId}")
public R<Void> dynamicTenant(@NotBlank(message = "租户ID不能为空") @PathVariable String tenantId) { public R<Void> dynamicTenant(@NotBlank(message = "租户ID不能为空") @PathVariable String tenantId) {
TenantHelper.setDynamic(tenantId, true); TenantHelper.setDynamic(tenantId, true);
@ -206,7 +206,7 @@ public class SysTenantController extends BaseController {
/** /**
* 清除动态租户 * 清除动态租户
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@GetMapping("/dynamic/clear") @GetMapping("/dynamic/clear")
public R<Void> dynamicClear() { public R<Void> dynamicClear() {
TenantHelper.clearDynamic(); TenantHelper.clearDynamic();
@ -220,7 +220,7 @@ public class SysTenantController extends BaseController {
* @param tenantId 租户id * @param tenantId 租户id
* @param packageId 套餐id * @param packageId 套餐id
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenant:edit") @SaCheckPermission("system:tenant:edit")
@Log(title = "租户管理", businessType = BusinessType.UPDATE) @Log(title = "租户管理", businessType = BusinessType.UPDATE)
@GetMapping("/syncTenantPackage") @GetMapping("/syncTenantPackage")
@ -232,7 +232,7 @@ public class SysTenantController extends BaseController {
/** /**
* 同步租户字典 * 同步租户字典
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@Log(title = "租户管理", businessType = BusinessType.INSERT) @Log(title = "租户管理", businessType = BusinessType.INSERT)
@GetMapping("/syncTenantDict") @GetMapping("/syncTenantDict")
public R<Void> syncTenantDict() { public R<Void> syncTenantDict() {

View File

@ -43,8 +43,8 @@ public class SysTenantPackageController extends BaseController {
/** /**
* 查询租户套餐列表 * 查询租户套餐列表
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenantPackage:list") // @SaCheckPermission("system:tenantPackage:list")
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo<SysTenantPackageVo> list(SysTenantPackageBo bo, PageQuery pageQuery) { public TableDataInfo<SysTenantPackageVo> list(SysTenantPackageBo bo, PageQuery pageQuery) {
return tenantPackageService.queryPageList(bo, pageQuery); return tenantPackageService.queryPageList(bo, pageQuery);
@ -53,8 +53,8 @@ public class SysTenantPackageController extends BaseController {
/** /**
* 查询租户套餐下拉选列表 * 查询租户套餐下拉选列表
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenantPackage:list") // @SaCheckPermission("system:tenantPackage:list")
@GetMapping("/selectList") @GetMapping("/selectList")
public R<List<SysTenantPackageVo>> selectList() { public R<List<SysTenantPackageVo>> selectList() {
return R.ok(tenantPackageService.selectList()); return R.ok(tenantPackageService.selectList());
@ -63,8 +63,8 @@ public class SysTenantPackageController extends BaseController {
/** /**
* 导出租户套餐列表 * 导出租户套餐列表
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenantPackage:export") // @SaCheckPermission("system:tenantPackage:export")
@Log(title = "租户套餐", businessType = BusinessType.EXPORT) @Log(title = "租户套餐", businessType = BusinessType.EXPORT)
@PostMapping("/export") @PostMapping("/export")
public void export(SysTenantPackageBo bo, HttpServletResponse response) { public void export(SysTenantPackageBo bo, HttpServletResponse response) {
@ -77,8 +77,8 @@ public class SysTenantPackageController extends BaseController {
* *
* @param packageId 主键 * @param packageId 主键
*/ */
@SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY) // @SaCheckRole(TenantConstants.SUPER_ADMIN_ROLE_KEY)
@SaCheckPermission("system:tenantPackage:query") // @SaCheckPermission("system:tenantPackage:query")
@GetMapping("/{packageId}") @GetMapping("/{packageId}")
public R<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空") public R<SysTenantPackageVo> getInfo(@NotNull(message = "主键不能为空")
@PathVariable Long packageId) { @PathVariable Long packageId) {

View File

@ -3,6 +3,7 @@ package org.dromara.system.domain.bo;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic; import com.baomidou.mybatisplus.annotation.TableLogic;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import java.math.BigDecimal; import java.math.BigDecimal;
@ -159,6 +160,7 @@ public class SysTenantExtendBo {
private String qualification; private String qualification;
@Schema(description = "类型(0.店铺 1.代理 2.推广人)") @Schema(description = "类型(0.店铺 1.代理 2.推广人)")
@NotNull(message = "租户类型不能为空")
private Integer type; private Integer type;
@Schema(description = "推广人姓名") @Schema(description = "推广人姓名")

View File

@ -3,6 +3,8 @@ package org.dromara.system.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.*; import org.apache.ibatis.annotations.*;
import org.dromara.common.mybatis.annotation.DataColumn;
import org.dromara.common.mybatis.annotation.DataPermission;
import org.dromara.system.domain.SysTenant; import org.dromara.system.domain.SysTenant;
import org.dromara.system.domain.bo.SysTenantBo; import org.dromara.system.domain.bo.SysTenantBo;
import org.dromara.system.domain.bo.SysTenantExtendBo; import org.dromara.system.domain.bo.SysTenantExtendBo;
@ -44,6 +46,10 @@ public interface SysTenantMapper extends BaseMapperPlus<SysTenant, SysTenantVo>
"LIMIT 20") "LIMIT 20")
List<Map<String, Object>> getTop20Stores(); List<Map<String, Object>> getTop20Stores();
@DataPermission({
@DataColumn(key = "deptName", value = "t.create_dept"),
@DataColumn(key = "userName", value = "t.create_by")
})
IPage<TenantDTO> getlist(@Param("page") Page<SysTenant> page,@Param("query") SysTenantExtendBo query); IPage<TenantDTO> getlist(@Param("page") Page<SysTenant> page,@Param("query") SysTenantExtendBo query);
@Select(" SELECT\n" + @Select(" SELECT\n" +

View File

@ -19,6 +19,7 @@ import org.dromara.common.core.constant.Constants;
import org.dromara.common.core.constant.SystemConstants; import org.dromara.common.core.constant.SystemConstants;
import org.dromara.common.core.constant.TenantConstants; import org.dromara.common.core.constant.TenantConstants;
import org.dromara.common.core.enums.TenantSignStatus; import org.dromara.common.core.enums.TenantSignStatus;
import org.dromara.common.core.enums.TenantType;
import org.dromara.common.core.exception.ServiceException; import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.service.WorkflowService; import org.dromara.common.core.service.WorkflowService;
import org.dromara.common.core.utils.SpringUtils; import org.dromara.common.core.utils.SpringUtils;
@ -131,39 +132,13 @@ public class SysTenantServiceImpl implements ISysTenantService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public Boolean insertByBo(SysTenantExtendBo bo) { public Boolean insertByBo(SysTenantExtendBo bo) {
if (bo.getType() == null){
throw new ServiceException("租户类型不能为空");
}
// 处理sys_tenant表数据 // 处理sys_tenant表数据
SysTenant tenant = new SysTenant(); SysTenant tenant = new SysTenant();
String currentTenantId = LoginHelper.getTenantId(); String currentTenantId = LoginHelper.getTenantId();
if (bo.getExpiryDate() != null) { SysTenant sysTenant = baseMapper.selectByTenantId(currentTenantId);
SysTenant sysTenant = baseMapper.selectByTenantId(currentTenantId); Assert.isTrue(TenantType.allowCreateByType(sysTenant.getType(), bo.getType()), () -> new ServiceException("您无权限创建此类型的租户"));
if (sysTenant != null) {
Integer type = sysTenant.getType();
if (type != 1 && type != 3) {
throw new RuntimeException("只有代理能添加推广人");
}
} else {
throw new RuntimeException("未查询到对应的租户信息");
}
}
if (bo.getStoreName() != null) {
SysTenant sysTenant = baseMapper.selectByTenantId(currentTenantId);
if (sysTenant != null) {
Integer type = sysTenant.getType();
if (type != 2 && type != 3) {
throw new RuntimeException("只有推广人能添加店铺");
}
} else {
throw new RuntimeException("未查询到对应的租户信息");
}
}
// 生成租户ID // 生成租户ID
List<String> tenantIds = baseMapper.selectObjs( List<String> tenantIds = baseMapper.selectObjs(
@ -199,10 +174,10 @@ public class SysTenantServiceImpl implements ISysTenantService {
tenant.setBirthday(bo.getBirthday()); tenant.setBirthday(bo.getBirthday());
tenant.setQualification(bo.getQualification()); tenant.setQualification(bo.getQualification());
tenant.setType(bo.getType()); tenant.setType(bo.getType());
if (bo.getType() == 2){ if (bo.getType() == TenantType.REFERENCE.getType()){
tenant.setAgencyTenantId(currentTenantId); tenant.setAgencyTenantId(currentTenantId);
} }
if (bo.getType() == 0){ if (bo.getType() == TenantType.SHOP.getType()){
tenant.setPromoterTenantId(currentTenantId); tenant.setPromoterTenantId(currentTenantId);
} }