From 2598c033a95d4b61d5f5ab3da5f1414f25c510d6 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 20 Feb 2022 23:59:23 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=96=B0=E5=A2=9E=E3=80=91=E3=80=90?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E3=80=91=E6=96=B0=E5=BB=BA=E7=A7=9F=E6=88=B7?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E8=87=AA=E5=8A=A8=E5=88=9B=E5=BB=BA=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E7=AE=A1=E7=90=86=E5=91=98=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E3=80=81=E8=A7=92=E8=89=B2=E7=AD=89=E5=9F=BA=E7=A1=80=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/collection/CollectionUtils.java | 3 + .../YudaoTenantSecurityAutoConfiguration.java | 9 ++- .../core/context/TenantContextHolder.java | 14 ---- .../security/TenantSecurityWebFilter.java | 24 ++++++- .../core/service/TenantFrameworkService.java | 7 ++ .../tenant/core/util/TenantUtils.java | 30 ++++++++ .../system/enums/ErrorCodeConstants.java | 3 + .../admin/permission/RoleController.java | 4 +- .../admin/permission/vo/role/RoleBaseVO.java | 3 - .../admin/tenant/TenantController.http | 18 +++++ .../admin/tenant/TenantPackageController.java | 14 ++-- .../packages/TenantPackageSimpleRespVO.java | 21 ++++++ .../admin/tenant/vo/tenant/TenantBaseVO.java | 23 ++++++- .../tenant/vo/tenant/TenantCreateReqVO.java | 17 +++++ .../admin/user/vo/user/UserBaseVO.java | 4 +- .../convert/permission/RoleConvert.java | 3 + .../system/convert/tenant/TenantConvert.java | 11 ++- .../convert/tenant/TenantPackageConvert.java | 4 ++ .../dal/dataobject/permission/RoleDO.java | 3 +- .../dal/mysql/tenant/TenantPackageMapper.java | 5 ++ .../permission/PermissionServiceImpl.java | 2 - .../service/permission/RoleService.java | 6 +- .../service/permission/RoleServiceImpl.java | 5 +- .../permission/bo/RoleCreateReqBO.java | 49 +++++++++++++ .../service/tenant/TenantPackageService.java | 16 +++++ .../tenant/TenantPackageServiceImpl.java | 23 ++++++- .../service/tenant/TenantServiceImpl.java | 68 +++++++++++++++++- .../system/service/user/AdminUserService.java | 9 +-- .../service/user/AdminUserServiceImpl.java | 3 +- .../service/permission/RoleServiceTest.java | 4 +- .../src/main/resources/application.yaml | 2 +- .../src/api/system/tenantPackage.js | 10 ++- .../src/views/system/tenant/index.vue | 69 +++++++++++++++++-- 更新日志.md | 3 +- 34 files changed, 425 insertions(+), 64 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/util/TenantUtils.java create mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantController.http create mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/packages/TenantPackageSimpleRespVO.java create mode 100644 yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/bo/RoleCreateReqBO.java diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java index cbbfb5233..1dccd9cb2 100644 --- a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/collection/CollectionUtils.java @@ -147,4 +147,7 @@ public class CollectionUtils { coll.add(item); } + public static Collection singleton(T deptId) { + return deptId == null ? Collections.emptyList() : Collections.singleton(deptId); + } } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantSecurityAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantSecurityAutoConfiguration.java index dc4bae5d0..86ba8ffc5 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantSecurityAutoConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantSecurityAutoConfiguration.java @@ -2,7 +2,9 @@ package cn.iocoder.yudao.framework.tenant.config; import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter; +import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; import cn.iocoder.yudao.framework.web.config.WebProperties; +import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; @@ -22,9 +24,12 @@ public class YudaoTenantSecurityAutoConfiguration { @Bean public FilterRegistrationBean tenantSecurityWebFilter(TenantProperties tenantProperties, - WebProperties webProperties) { + WebProperties webProperties, + GlobalExceptionHandler globalExceptionHandler, + TenantFrameworkService tenantFrameworkService) { FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); - registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties)); + registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties, + globalExceptionHandler, tenantFrameworkService)); registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER); return registrationBean; } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java index d986b44fb..8e4999ac9 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/context/TenantContextHolder.java @@ -11,11 +11,6 @@ public class TenantContextHolder { private static final ThreadLocal TENANT_ID = new TransmittableThreadLocal<>(); - /** - * 租户编号 - 空 - */ - private static final Long TENANT_ID_NULL = 0L; - /** * 获得租户编号。 * @@ -38,15 +33,6 @@ public class TenantContextHolder { return tenantId; } - /** - * 在一些前端场景下,可能无法请求带上租户。例如说, 方式获取图片等 - * 此时,暂时的解决方案,是在该接口的 Controller 方法上,调用该方法 - * TODO 芋艿:思考有没更合适的方案,目标是去掉该方法 - */ - public static void setNullTenantId() { - TENANT_ID.set(TENANT_ID_NULL); - } - public static void setTenantId(Long tenantId) { TENANT_ID.set(tenantId); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java index 87285c412..0e1f63461 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java @@ -8,8 +8,10 @@ import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.framework.tenant.config.TenantProperties; import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; +import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; import cn.iocoder.yudao.framework.web.config.WebProperties; import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter; +import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; import lombok.extern.slf4j.Slf4j; import org.springframework.util.AntPathMatcher; @@ -24,6 +26,7 @@ import java.util.Objects; * 多租户 Security Web 过滤器 * 1. 如果是登陆的用户,校验是否有权限访问该租户,避免越权问题。 * 2. 如果请求未带租户的编号,检查是否是忽略的 URL,否则也不允许访问。 + * 3. 校验租户是合法,例如说被禁用、到期 * * 校验用户访问的租户,是否是其所在的租户, * @@ -33,13 +36,21 @@ import java.util.Objects; public class TenantSecurityWebFilter extends ApiRequestFilter { private final TenantProperties tenantProperties; + private final AntPathMatcher pathMatcher; + private final GlobalExceptionHandler globalExceptionHandler; + private final TenantFrameworkService tenantFrameworkService; + public TenantSecurityWebFilter(TenantProperties tenantProperties, - WebProperties webProperties) { + WebProperties webProperties, + GlobalExceptionHandler globalExceptionHandler, + TenantFrameworkService tenantFrameworkService) { super(webProperties); this.tenantProperties = tenantProperties; this.pathMatcher = new AntPathMatcher(); + this.globalExceptionHandler = globalExceptionHandler; + this.tenantFrameworkService = tenantFrameworkService; } @Override @@ -72,6 +83,17 @@ public class TenantSecurityWebFilter extends ApiRequestFilter { return; } + // 3. 校验租户是合法,例如说被禁用、到期 + if (tenantId != null) { + try { + tenantFrameworkService.validTenant(tenantId); + } catch (Throwable ex) { + CommonResult result = globalExceptionHandler.allExceptionHandler(request, ex); + ServletUtils.writeJSON(response, result); + return; + } + } + // 继续过滤 chain.doFilter(request, response); } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkService.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkService.java index a36e612b6..2ca474d0f 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkService.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/service/TenantFrameworkService.java @@ -16,4 +16,11 @@ public interface TenantFrameworkService { */ List getTenantIds(); + /** + * 校验租户是否合法 + * + * @param id 租户编号 + */ + void validTenant(Long id); + } diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/util/TenantUtils.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/util/TenantUtils.java new file mode 100644 index 000000000..b6caee3f3 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/util/TenantUtils.java @@ -0,0 +1,30 @@ +package cn.iocoder.yudao.framework.tenant.core.util; + +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; + +/** + * 多租户 Util + * + * @author 芋道源码 + */ +public class TenantUtils { + + /** + * 使用指定租户,执行对应的逻辑 + * + * @param tenantId 租户编号 + * @param runnable 逻辑 + */ + public static void execute(Long tenantId, Runnable runnable) { + Long oldTenantId = TenantContextHolder.getTenantId(); + try { + TenantContextHolder.setTenantId(tenantId); + // 执行逻辑 + runnable.run(); + } finally { + TenantContextHolder.setTenantId(oldTenantId); + } + } + + +} diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java index f9d31ff18..9ba7ed8c8 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java @@ -104,10 +104,13 @@ public interface ErrorCodeConstants { // ========== 租户信息 1002014000 ========== ErrorCode TENANT_NOT_EXISTS = new ErrorCode(1002014000, "租户不存在"); + ErrorCode TENANT_DISABLE = new ErrorCode(1002014001, "名字为【{}】的租户已被禁用"); + ErrorCode TENANT_EXPIRE = new ErrorCode(1002014002, "名字为【{}】的租户已过期"); // ========== 租户套餐 1002015000 ========== ErrorCode TENANT_PACKAGE_NOT_EXISTS = new ErrorCode(1002015000, "租户套餐不存在"); ErrorCode TENANT_PACKAGE_USED = new ErrorCode(1002015001, "租户正在使用该套餐,请给租户重新设置套餐后再尝试删除"); + ErrorCode TENANT_PACKAGE_DISABLE = new ErrorCode(1002015002, "名字为【{}】的租户套餐已被禁用"); // ========== 错误码模块 1002016000 ========== ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002016000, "错误码不存在"); diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java index b200aba86..3716c8e7c 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/RoleController.java @@ -40,7 +40,7 @@ public class RoleController { @ApiOperation("创建角色") @PreAuthorize("@ss.hasPermission('system:role:create')") public CommonResult createRole(@Valid @RequestBody RoleCreateReqVO reqVO) { - return success(roleService.createRole(reqVO)); + return success(roleService.createRole(reqVO, null)); } @PutMapping("/update") @@ -88,7 +88,7 @@ public class RoleController { public CommonResult> getSimpleRoles() { // 获得角色列表,只要开启状态的 List list = roleService.getRoles(Collections.singleton(CommonStatusEnum.ENABLE.getStatus())); - // 排序后,返回个诶前端 + // 排序后,返回给前端 list.sort(Comparator.comparing(RoleDO::getSort)); return success(RoleConvert.INSTANCE.convertList02(list)); } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleBaseVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleBaseVO.java index 66b4d2053..60d678aa9 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleBaseVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/permission/vo/role/RoleBaseVO.java @@ -28,9 +28,6 @@ public class RoleBaseVO { @NotNull(message = "显示顺序不能为空") private Integer sort; - @ApiModelProperty(value = "角色类型", required = true, example = "1", notes = "见 RoleTypeEnum 枚举") - private Integer type; - @ApiModelProperty(value = "备注", example = "我是一个角色") private String remark; diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantController.http b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantController.http new file mode 100644 index 000000000..ea3eb3457 --- /dev/null +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantController.http @@ -0,0 +1,18 @@ +### 创建租户 /admin-api/system/tenant/create +POST {{baseUrl}}/system/tenant/create +Content-Type: application/json +Authorization: Bearer {{token}} +tenant-id: {{adminTenentId}} + +{ + "name": "芋道", + "contactName": "芋艿", + "contactMobile": "15601691300", + "status": 0, + "domain": "https://www.iocoder.cn", + "packageId": 110, + "expireTime": 1699545600000, + "accountCount": 20, + "username": "admin", + "password": "123321" +} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantPackageController.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantPackageController.java index 1aabaa531..7959e887f 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantPackageController.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/TenantPackageController.java @@ -1,11 +1,9 @@ package cn.iocoder.yudao.module.system.controller.admin.tenant; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageResult; -import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; -import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; -import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageRespVO; -import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageUpdateReqVO; +import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.*; import cn.iocoder.yudao.module.system.convert.tenant.TenantPackageConvert; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; import cn.iocoder.yudao.module.system.service.tenant.TenantPackageService; @@ -82,4 +80,12 @@ public class TenantPackageController { return success(TenantPackageConvert.INSTANCE.convertPage(pageResult)); } + @GetMapping("/get-simple-list") + @ApiOperation(value = "获取租户套餐精简信息列表", notes = "只包含被开启的租户套餐,主要用于前端的下拉选项") + public CommonResult> getTenantPackageList() { + // 获得角色列表,只要开启状态的 + List list = tenantPackageService.getTenantPackageListByStatus(CommonStatusEnum.ENABLE.getStatus()); + return success(TenantPackageConvert.INSTANCE.convertList02(list)); + } + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/packages/TenantPackageSimpleRespVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/packages/TenantPackageSimpleRespVO.java new file mode 100644 index 000000000..4ec1c97f7 --- /dev/null +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/packages/TenantPackageSimpleRespVO.java @@ -0,0 +1,21 @@ +package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import javax.validation.constraints.NotNull; + +@ApiModel("管理后台 - 租户套餐精简 Response VO") +@Data +public class TenantPackageSimpleRespVO { + + @ApiModelProperty(value = "套餐编号", required = true, example = "1024") + @NotNull(message = "套餐编号不能为空") + private Long id; + + @ApiModelProperty(value = "套餐名", required = true, example = "VIP") + @NotNull(message = "套餐名不能为空") + private String name; + +} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantBaseVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantBaseVO.java index d47915db4..4a703e911 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantBaseVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantBaseVO.java @@ -2,7 +2,10 @@ package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant; import lombok.*; import io.swagger.annotations.*; +import org.hibernate.validator.constraints.URL; + import javax.validation.constraints.*; +import java.util.Date; /** * 租户 Base VO,提供给添加、修改、详细的子 VO 使用 @@ -22,8 +25,24 @@ public class TenantBaseVO { @ApiModelProperty(value = "联系手机", example = "15601691300") private String contactMobile; - @ApiModelProperty(value = "租户状态(0正常 1停用)", required = true, example = "1") - @NotNull(message = "租户状态(0正常 1停用)不能为空") + @ApiModelProperty(value = "租户状态", required = true, example = "1") + @NotNull(message = "租户状态") private Integer status; + @ApiModelProperty(value = "绑定域名", example = "https://www.iocoder.cn") + @URL(message = "绑定域名的地址非 URL 格式") + private String domain; + + @ApiModelProperty(value = "租户套餐编号", required = true, example = "1024") + @NotNull(message = "租户套餐编号不能为空") + private Long packageId; + + @ApiModelProperty(value = "过期时间", required = true) + @NotNull(message = "过期时间不能为空") + private Date expireTime; + + @ApiModelProperty(value = "账号数量", required = true, example = "1024") + @NotNull(message = "账号数量不能为空") + private Integer accountCount; + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantCreateReqVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantCreateReqVO.java index 3e0d6068f..ff52811eb 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantCreateReqVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/tenant/vo/tenant/TenantCreateReqVO.java @@ -2,6 +2,12 @@ package cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant; import lombok.*; import io.swagger.annotations.*; +import org.hibernate.validator.constraints.Length; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Pattern; +import javax.validation.constraints.Size; @ApiModel("管理后台 - 租户创建 Request VO") @Data @@ -9,4 +15,15 @@ import io.swagger.annotations.*; @ToString(callSuper = true) public class TenantCreateReqVO extends TenantBaseVO { + @ApiModelProperty(value = "用户账号", required = true, example = "yudao") + @NotBlank(message = "用户账号不能为空") + @Pattern(regexp = "^[a-zA-Z0-9]{4,30}$", message = "用户账号由 数字、字母 组成") + @Size(min = 4, max = 30, message = "用户账号长度为 4-30 个字符") + private String username; + + @ApiModelProperty(value = "密码", required = true, example = "123456") + @NotEmpty(message = "密码不能为空") + @Length(min = 4, max = 16, message = "密码长度为 4-16 位") + private String password; + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java index d136731e1..5268b10c7 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/controller/admin/user/vo/user/UserBaseVO.java @@ -1,8 +1,8 @@ package cn.iocoder.yudao.module.system.controller.admin.user.vo.user; +import cn.iocoder.yudao.framework.common.validation.Mobile; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import org.hibernate.validator.constraints.Length; import javax.validation.constraints.Email; import javax.validation.constraints.NotBlank; @@ -42,7 +42,7 @@ public class UserBaseVO { private String email; @ApiModelProperty(value = "手机号码", example = "15601691300") - @Length(min = 11, max = 11, message = "手机号长度必须 11 位") + @Mobile private String mobile; @ApiModelProperty(value = "用户性别", example = "1", notes = "参见 SexEnum 枚举类") diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/permission/RoleConvert.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/permission/RoleConvert.java index 646d7061d..73475ee4d 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/permission/RoleConvert.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/permission/RoleConvert.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.convert.permission; import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.*; import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; +import cn.iocoder.yudao.module.system.service.permission.bo.RoleCreateReqBO; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -22,4 +23,6 @@ public interface RoleConvert { List convertList03(List list); + RoleDO convert(RoleCreateReqBO bean); + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantConvert.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantConvert.java index 5ed4a6b95..d40e481d8 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantConvert.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantConvert.java @@ -1,11 +1,12 @@ package cn.iocoder.yudao.module.system.convert.tenant; +import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantExcelVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantRespVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantUpdateReqVO; +import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.UserCreateReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import org.mapstruct.Mapper; import org.mapstruct.factory.Mappers; @@ -33,4 +34,12 @@ public interface TenantConvert { List convertList02(List list); + default UserCreateReqVO convert02(TenantCreateReqVO bean) { + UserCreateReqVO reqVO = new UserCreateReqVO(); + reqVO.setUsername(bean.getUsername()); + reqVO.setPassword(bean.getPassword()); + reqVO.setNickname(bean.getContactName()).setMobile(bean.getContactMobile()); + return reqVO; + } + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantPackageConvert.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantPackageConvert.java index ce424c6a3..ff8c7e4ae 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantPackageConvert.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/convert/tenant/TenantPackageConvert.java @@ -1,8 +1,10 @@ package cn.iocoder.yudao.module.system.convert.tenant; import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleSimpleRespVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageRespVO; +import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageSimpleRespVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageUpdateReqVO; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; import org.mapstruct.Mapper; @@ -30,4 +32,6 @@ public interface TenantPackageConvert { PageResult convertPage(PageResult page); + List convertList02(List list); + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/permission/RoleDO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/permission/RoleDO.java index cd3151f45..2a175a866 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/permission/RoleDO.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/dataobject/permission/RoleDO.java @@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.type.JsonLongSetTypeHandler; import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; +import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -50,7 +51,7 @@ public class RoleDO extends BaseDO { /** * 角色类型 * - * 枚举 + * 枚举 {@link RoleTypeEnum} */ private Integer type; /** diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantPackageMapper.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantPackageMapper.java index d9046d89e..7e280fbd6 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantPackageMapper.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/tenant/TenantPackageMapper.java @@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.Tenant import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; import org.apache.ibatis.annotations.Mapper; +import java.util.List; + /** * 租户套餐 Mapper * @@ -24,4 +26,7 @@ public interface TenantPackageMapper extends BaseMapperX { .orderByDesc(TenantPackageDO::getId)); } + default List selectListByStatus(Integer status) { + return selectList(TenantPackageDO::getStatus, status); + } } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java index ba873cb66..f07f61719 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/PermissionServiceImpl.java @@ -224,8 +224,6 @@ public class PermissionServiceImpl implements PermissionService { UserRoleDO::getRoleId); } - - @Override public void assignUserRole(Long userId, Set roleIds) { // 获得角色拥有角色编号 diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java index 003ea62cc..6214ef1c3 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleService.java @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleUp import cn.iocoder.yudao.module.system.dal.dataobject.permission.RoleDO; import org.springframework.lang.Nullable; +import javax.validation.Valid; import java.util.Collection; import java.util.List; import java.util.Set; @@ -28,16 +29,17 @@ public interface RoleService { * 创建角色 * * @param reqVO 创建角色信息 + * @param type 角色类型 * @return 角色编号 */ - Long createRole(RoleCreateReqVO reqVO); + Long createRole(@Valid RoleCreateReqVO reqVO, Integer type); /** * 更新角色 * * @param reqVO 更新角色信息 */ - void updateRole(RoleUpdateReqVO reqVO); + void updateRole(@Valid RoleUpdateReqVO reqVO); /** * 删除角色 diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java index 48182a3f6..ea3cf0f52 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceImpl.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.permission; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.util.ObjectUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; @@ -120,12 +121,12 @@ public class RoleServiceImpl implements RoleService { } @Override - public Long createRole(RoleCreateReqVO reqVO) { + public Long createRole(RoleCreateReqVO reqVO, Integer type) { // 校验角色 checkDuplicateRole(reqVO.getName(), reqVO.getCode(), null); // 插入到数据库 RoleDO role = RoleConvert.INSTANCE.convert(reqVO); - role.setType(RoleTypeEnum.CUSTOM.getType()); + role.setType(ObjectUtil.defaultIfNull(type, RoleTypeEnum.CUSTOM.getType())); role.setStatus(CommonStatusEnum.ENABLE.getStatus()); role.setDataScope(DataScopeEnum.ALL.getScope()); // 默认可查看所有数据。原因是,可能一些项目不需要项目权限 roleMapper.insert(role); diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/bo/RoleCreateReqBO.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/bo/RoleCreateReqBO.java new file mode 100644 index 000000000..d570436d1 --- /dev/null +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/permission/bo/RoleCreateReqBO.java @@ -0,0 +1,49 @@ +package cn.iocoder.yudao.module.system.service.permission.bo; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +/** + * 角色创建 Request BO + * + * @author 芋道源码 + */ +@Data +public class RoleCreateReqBO { + + /** + * 租户编号 + */ + @NotNull(message = "租户编号不能为空") + private Long tenantId; + + /** + * 角色名称 + */ + @NotBlank(message = "角色名称不能为空") + @Size(max = 30, message = "角色名称长度不能超过30个字符") + private String name; + + /** + * 角色标志 + */ + @NotBlank(message = "角色标志不能为空") + @Size(max = 100, message = "角色标志长度不能超过100个字符") + private String code; + + /** + * 显示顺序 + */ + @NotNull(message = "显示顺序不能为空") + private Integer sort; + + /** + * 角色类型 + */ + @NotNull(message = "角色类型不能为空") + private Integer type; + +} diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageService.java index 213bd9690..9c5b0e576 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageService.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageService.java @@ -63,4 +63,20 @@ public interface TenantPackageService { */ PageResult getTenantPackagePage(TenantPackagePageReqVO pageReqVO); + /** + * 校验租户套餐 + * + * @param id 编号 + * @return 租户套餐 + */ + TenantPackageDO validTenantPackage(Long id); + + /** + * 获得指定状态的租户套餐列表 + * + * @param status 状态 + * @return 租户套餐 + */ + List getTenantPackageListByStatus(Integer status); + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java index 7eb68258d..54b1a1eaa 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantPackageServiceImpl.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.module.system.service.tenant; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackageCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; @@ -7,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.packages.Tenant import cn.iocoder.yudao.module.system.convert.tenant.TenantPackageConvert; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantPackageDO; import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantPackageMapper; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.validation.annotation.Validated; @@ -15,8 +17,7 @@ import java.util.Collection; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_PACKAGE_NOT_EXISTS; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_PACKAGE_USED; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; /** * 租户套餐 Service 实现类 @@ -31,6 +32,7 @@ public class TenantPackageServiceImpl implements TenantPackageService { private TenantPackageMapper tenantPackageMapper; @Resource + @Lazy // 避免循环依赖的报错 private TenantService tenantService; @Override @@ -88,4 +90,21 @@ public class TenantPackageServiceImpl implements TenantPackageService { return tenantPackageMapper.selectPage(pageReqVO); } + @Override + public TenantPackageDO validTenantPackage(Long id) { + TenantPackageDO tenantPackage = tenantPackageMapper.selectById(id); + if (tenantPackage == null) { + throw exception(TENANT_PACKAGE_NOT_EXISTS); + } + if (tenantPackage.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) { + throw exception(TENANT_PACKAGE_DISABLE, tenantPackage.getName()); + } + return tenantPackage; + } + + @Override + public List getTenantPackageListByStatus(Integer status) { + return tenantPackageMapper.selectListByStatus(status); + } + } diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java index 07055044c..b43c2a654 100755 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/tenant/TenantServiceImpl.java @@ -1,7 +1,11 @@ package cn.iocoder.yudao.module.system.service.tenant; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; +import cn.iocoder.yudao.framework.common.util.date.DateUtils; +import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; +import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantCreateReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantExportReqVO; import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO; @@ -9,15 +13,22 @@ import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantUp import cn.iocoder.yudao.module.system.convert.tenant.TenantConvert; import cn.iocoder.yudao.module.system.dal.dataobject.tenant.TenantDO; import cn.iocoder.yudao.module.system.dal.mysql.tenant.TenantMapper; +import cn.iocoder.yudao.module.system.enums.permission.RoleCodeEnum; +import cn.iocoder.yudao.module.system.enums.permission.RoleTypeEnum; +import cn.iocoder.yudao.module.system.service.permission.PermissionService; +import cn.iocoder.yudao.module.system.service.permission.RoleService; +import cn.iocoder.yudao.module.system.service.user.AdminUserService; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.util.Collection; +import java.util.Collections; import java.util.List; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.TENANT_NOT_EXISTS; +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; /** * 租户 Service 实现类 @@ -31,6 +42,15 @@ public class TenantServiceImpl implements TenantService { @Resource private TenantMapper tenantMapper; + @Resource + private TenantPackageService tenantPackageService; + @Resource + private AdminUserService userService; + @Resource + private RoleService roleService; + @Resource + private PermissionService permissionService; + @Override public List getTenantIds() { List tenants = tenantMapper.selectList(); @@ -38,18 +58,62 @@ public class TenantServiceImpl implements TenantService { } @Override + public void validTenant(Long id) { + TenantDO tenant = tenantMapper.selectById(id); + if (tenant == null) { + throw exception(TENANT_NOT_EXISTS); + } + if (tenant.getStatus().equals(CommonStatusEnum.DISABLE.getStatus())) { + throw exception(TENANT_DISABLE, tenant.getName()); + } + if (DateUtils.isExpired(tenant.getExpireTime())) { + throw exception(TENANT_EXPIRE, tenant.getName()); + } + } + + @Override + @Transactional(rollbackFor = Exception.class) public Long createTenant(TenantCreateReqVO createReqVO) { - // 插入 + // 校验套餐被禁用 + tenantPackageService.validTenantPackage(createReqVO.getPackageId()); + + // 创建租户 TenantDO tenant = TenantConvert.INSTANCE.convert(createReqVO); tenantMapper.insert(tenant); + + TenantUtils.execute(tenant.getId(), () -> { + // 创建角色 + Long roleId = createRole(); + // 创建用户,并分配角色 + Long userId = createUser(roleId, createReqVO); + // 修改租户的管理员 + tenantMapper.updateById(new TenantDO().setId(tenant.getId()).setContactUserId(userId)); + }); // 返回 return tenant.getId(); } + private Long createUser(Long roleId, TenantCreateReqVO createReqVO) { + // 创建用户 + Long userId = userService.createUser(TenantConvert.INSTANCE.convert02(createReqVO)); + // 分配角色 + permissionService.assignUserRole(userId, Collections.singleton(roleId)); + return userId; + } + + private Long createRole() { + RoleCreateReqVO reqVO = new RoleCreateReqVO(); + reqVO.setName(RoleCodeEnum.ADMIN.name()).setCode(RoleCodeEnum.ADMIN.getKey()).setSort(0); + return roleService.createRole(reqVO, RoleTypeEnum.SYSTEM.getType()); + } + @Override public void updateTenant(TenantUpdateReqVO updateReqVO) { // 校验存在 this.validateTenantExists(updateReqVO.getId()); + // 校验套餐被禁用 + tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); + // 更新 TenantDO updateObj = TenantConvert.INSTANCE.convert(updateReqVO); tenantMapper.updateById(updateObj); diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java index 68dd3bb57..e9a6fdc96 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserService.java @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.system.controller.admin.user.vo.user.*; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; +import javax.validation.Valid; import java.io.InputStream; import java.util.*; @@ -24,14 +25,14 @@ public interface AdminUserService { * @param reqVO 用户信息 * @return 用户编号 */ - Long createUser(UserCreateReqVO reqVO); + Long createUser(@Valid UserCreateReqVO reqVO); /** * 修改用户 * * @param reqVO 用户信息 */ - void updateUser(UserUpdateReqVO reqVO); + void updateUser(@Valid UserUpdateReqVO reqVO); /** * 更新用户的最后登陆信息 @@ -47,7 +48,7 @@ public interface AdminUserService { * @param id 用户编号 * @param reqVO 用户个人信息 */ - void updateUserProfile(Long id, UserProfileUpdateReqVO reqVO); + void updateUserProfile(Long id, @Valid UserProfileUpdateReqVO reqVO); /** * 修改用户个人密码 @@ -55,7 +56,7 @@ public interface AdminUserService { * @param id 用户编号 * @param reqVO 更新用户个人密码 */ - void updateUserPassword(Long id, UserProfileUpdatePasswordReqVO reqVO); + void updateUserPassword(Long id, @Valid UserProfileUpdatePasswordReqVO reqVO); /** * 更新用户头像 diff --git a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java index 8f308a0c7..a0682485a 100644 --- a/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java +++ b/yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/user/AdminUserServiceImpl.java @@ -2,7 +2,6 @@ package cn.iocoder.yudao.module.system.service.user; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.io.IoUtil; -import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.common.exception.ServiceException; @@ -258,7 +257,7 @@ public class AdminUserServiceImpl implements AdminUserService { // 校验邮箱唯一 this.checkEmailUnique(id, email); // 校验部门处于开启状态 - deptService.validDepts(Collections.singleton(deptId)); + deptService.validDepts(CollectionUtils.singleton(deptId)); // 校验岗位处于开启状态 postService.validPosts(postIds); } diff --git a/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java b/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java index e607cd725..a2ce98f7a 100644 --- a/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java +++ b/yudao-module-system/yudao-module-system-impl/src/test/java/cn/iocoder/yudao/module/system/service/permission/RoleServiceTest.java @@ -72,10 +72,9 @@ public class RoleServiceTest extends BaseDbUnitTest { o.setCode("role_code"); o.setName("role_name"); o.setRemark("remark"); - o.setType(RoleTypeEnum.CUSTOM.getType()); o.setSort(1); }); - Long roleId = sysRoleService.createRole(reqVO); + Long roleId = sysRoleService.createRole(reqVO, null); //断言 assertNotNull(roleId); @@ -96,7 +95,6 @@ public class RoleServiceTest extends BaseDbUnitTest { o.setId(roleId); o.setCode("role_code"); o.setName("update_name"); - o.setType(RoleTypeEnum.SYSTEM.getType()); o.setSort(999); }); sysRoleService.updateRole(reqVO); diff --git a/yudao-server/src/main/resources/application.yaml b/yudao-server/src/main/resources/application.yaml index c66539269..5bb1736e4 100644 --- a/yudao-server/src/main/resources/application.yaml +++ b/yudao-server/src/main/resources/application.yaml @@ -80,7 +80,7 @@ yudao: tenant: # 多租户相关配置项 enable: true ignore-urls: /admin-api/system/captcha/get-image, /admin-api/infra/file/get/* - ignore-tables: infra_config, infra_file, infra_job, infra_job_log, infra_job_log, system_tenant, system_tenant_package, system_dict_data, system_dict_type, system_error_code, system_menu, system_role, system_role_menu, system_sms_channel, tool_codegen_column, tool_codegen_table, tool_test_demo, tables, columns + ignore-tables: infra_config, infra_file, infra_job, infra_job_log, infra_job_log, system_tenant, system_tenant_package, system_dict_data, system_dict_type, system_error_code, system_menu, system_sms_channel, tool_codegen_column, tool_codegen_table, tool_test_demo, tables, columns sms-code: # 短信验证码相关的配置项 expire-times: 10m send-frequency: 1m diff --git a/yudao-ui-admin/src/api/system/tenantPackage.js b/yudao-ui-admin/src/api/system/tenantPackage.js index 7c6de8b2a..d3d9c9c48 100755 --- a/yudao-ui-admin/src/api/system/tenantPackage.js +++ b/yudao-ui-admin/src/api/system/tenantPackage.js @@ -43,12 +43,10 @@ export function getTenantPackagePage(query) { }) } -// 导出租户套餐 Excel -export function exportTenantPackageExcel(query) { +// 获取租户套餐精简信息列表 +export function getTenantPackageList() { return request({ - url: '/system/tenant-package/export-excel', - method: 'get', - params: query, - responseType: 'blob' + url: '/system/tenant-package/get-simple-list', + method: 'get' }) } diff --git a/yudao-ui-admin/src/views/system/tenant/index.vue b/yudao-ui-admin/src/views/system/tenant/index.vue index 3c4e176af..748976516 100755 --- a/yudao-ui-admin/src/views/system/tenant/index.vue +++ b/yudao-ui-admin/src/views/system/tenant/index.vue @@ -18,10 +18,6 @@ :key="dict.value" :label="dict.label" :value="dict.value"/> - - - 搜索 重置 @@ -45,8 +41,24 @@ + + + + + + + + + +