diff --git a/README.md b/README.md index c5a210733..7643e877b 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ | | 菜单管理 | 配置系统菜单,操作权限,按钮权限标识等 | | | 部门管理 | 配置系统组织机构(公司、部门、小组),树结构展现支持数据权限 | | | 岗位管理 | 配置系统用户所属担任职务 | +| | 租户管理 | 配置系统租户,支持 SaaS 场景下的多租户功能 | | | 字典管理 | 对系统中经常使用的一些较为固定的数据进行维护 | | 🚀 | 短信管理 | 短信渠道、短息模板、短信日志,对接阿里云、云片等主流短信平台 | | 🚀 | 操作日志 | 系统正常操作日志记录和查询,集成 Swagger 生成日志内容 | diff --git a/img.png b/img.png new file mode 100644 index 000000000..0e0c73ca1 Binary files /dev/null and b/img.png differ diff --git a/yudao-admin-server/pom.xml b/yudao-admin-server/pom.xml index 5cdf85712..4bd789b5c 100644 --- a/yudao-admin-server/pom.xml +++ b/yudao-admin-server/pom.xml @@ -117,7 +117,15 @@ yudao-spring-boot-starter-excel + + cn.iocoder.boot + yudao-spring-boot-starter-tenant + + + cn.iocoder.boot + yudao-spring-boot-starter-data-permission + org.apache.velocity diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java deleted file mode 100644 index 720fc5a74..000000000 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/config/AsyncConfiguration.java +++ /dev/null @@ -1,9 +0,0 @@ -package cn.iocoder.yudao.adminserver.framework.async.config; - -import org.springframework.context.annotation.Configuration; -import org.springframework.scheduling.annotation.EnableAsync; - -@Configuration -@EnableAsync -public class AsyncConfiguration { -} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java deleted file mode 100644 index aac87cd3a..000000000 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/async/package-info.java +++ /dev/null @@ -1,4 +0,0 @@ -/** - * 异步执行,基于 Spring @Async 实现 - */ -package cn.iocoder.yudao.adminserver.framework.async; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/security/SecurityConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/security/SecurityConfiguration.java index 8bb4bc9a6..e99c106b4 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/security/SecurityConfiguration.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/framework/security/SecurityConfiguration.java @@ -22,9 +22,11 @@ public class SecurityConfiguration { @Bean public Customizer.ExpressionInterceptUrlRegistry> authorizeRequestsCustomizer() { return registry -> { - // 通用的接口,可匿名访问 TODO 芋艿:需要抽象出去 + // 验证码的接口 registry.antMatchers(api("/system/captcha/**")).anonymous(); - // Spring Boot Admin Server 的安全配置 TODO 芋艿:需要抽象出去 + // 获得租户编号的接口 + registry.antMatchers(api("/system/tenant/get-id-by-name")).anonymous(); + // Spring Boot Admin Server 的安全配置 registry.antMatchers(adminSeverContextPath).anonymous() .antMatchers(adminSeverContextPath + "/**").anonymous(); // 短信回调 API diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/message/config/InfConfigRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/message/config/InfConfigRefreshMessage.java index 433048143..4846d9b90 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/message/config/InfConfigRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/message/config/InfConfigRefreshMessage.java @@ -1,13 +1,13 @@ package cn.iocoder.yudao.adminserver.modules.infra.mq.message.config; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; /** * 配置数据刷新 Message */ @Data -public class InfConfigRefreshMessage implements ChannelMessage { +public class InfConfigRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/producer/config/InfConfigProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/producer/config/InfConfigProducer.java index 39a8e76b3..2a0dc66fd 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/producer/config/InfConfigProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/infra/mq/producer/config/InfConfigProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.infra.mq.producer.config; import cn.iocoder.yudao.adminserver.modules.infra.mq.message.config.InfConfigRefreshMessage; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -14,14 +13,14 @@ import javax.annotation.Resource; public class InfConfigProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link InfConfigRefreshMessage} 消息 */ public void sendConfigRefreshMessage() { InfConfigRefreshMessage message = new InfConfigRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.http b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.http index 125a36846..f8e32a5b8 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.http +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/auth/SysAuthController.http @@ -1,21 +1,25 @@ ### 请求 /login 接口 => 成功 POST {{baseUrl}}/login Content-Type: application/json +tenant-id: 1 { "username": "admin", "password": "admin123", - "uuid": "9b2ffbc1-7425-4155-9894-9d5c08541d62", + "uuid": "3acd87a09a4f48fb9118333780e94883", "code": "1024" } ### 请求 /get-permission-info 接口 => 成功 GET {{baseUrl}}/get-permission-info Authorization: Bearer {{token}} +tenant-id: 1 ### 请求 /list-menus 接口 => 成功 GET {{baseUrl}}/list-menus Authorization: Bearer {{token}} +#Authorization: Bearer a6aa7714a2e44c95aaa8a2c5adc2a67a +tenant-id: 1 ### 请求 /druid/xxx 接口 => 失败 TODO 临时测试 GET http://127.0.0.1:8080/druid/123 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/SysTenantController.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/SysTenantController.java new file mode 100644 index 000000000..85c51b134 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/SysTenantController.java @@ -0,0 +1,106 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant; + +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.*; +import cn.iocoder.yudao.adminserver.modules.system.convert.tenant.SysTenantConvert; +import cn.iocoder.yudao.adminserver.modules.system.service.tenant.SysTenantService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.pojo.CommonResult; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; +import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiImplicitParam; +import io.swagger.annotations.ApiOperation; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import javax.validation.Valid; +import java.io.IOException; +import java.util.Collection; +import java.util.List; + +import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; +import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT; + +@Api(tags = "租户") +@RestController +@RequestMapping("/system/tenant") +public class SysTenantController { + + @Resource + private SysTenantService tenantService; + + @GetMapping("/get-id-by-name") + @ApiOperation(value = "使用租户名,获得租户编号", notes = "登录界面,根据用户的租户名,获得租户编号") + @ApiImplicitParam(name = "name", value = "租户名", required = true, example = "芋道源码", dataTypeClass = Long.class) + public CommonResult getTenantIdByName(@RequestParam("name") String name) { + SysTenantDO tenantDO = tenantService.getTenantByName(name); + return success(tenantDO != null ? tenantDO.getId() : null); + } + + @PostMapping("/create") + @ApiOperation("创建租户") + @PreAuthorize("@ss.hasPermission('system:tenant:create')") + public CommonResult createTenant(@Valid @RequestBody SysTenantCreateReqVO createReqVO) { + return success(tenantService.createTenant(createReqVO)); + } + + @PutMapping("/update") + @ApiOperation("更新租户") + @PreAuthorize("@ss.hasPermission('system:tenant:update')") + public CommonResult updateTenant(@Valid @RequestBody SysTenantUpdateReqVO updateReqVO) { + tenantService.updateTenant(updateReqVO); + return success(true); + } + + @DeleteMapping("/delete") + @ApiOperation("删除租户") + @ApiImplicitParam(name = "id", value = "编号", required = true) + @PreAuthorize("@ss.hasPermission('system:tenant:delete')") + public CommonResult deleteTenant(@RequestParam("id") Long id) { + tenantService.deleteTenant(id); + return success(true); + } + + @GetMapping("/get") + @ApiOperation("获得租户") + @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class) + @PreAuthorize("@ss.hasPermission('system:tenant:query')") + public CommonResult getTenant(@RequestParam("id") Long id) { + SysTenantDO tenant = tenantService.getTenant(id); + return success(SysTenantConvert.INSTANCE.convert(tenant)); + } + + @GetMapping("/list") + @ApiOperation("获得租户列表") + @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class) + @PreAuthorize("@ss.hasPermission('system:tenant:query')") + public CommonResult> getTenantList(@RequestParam("ids") Collection ids) { + List list = tenantService.getTenantList(ids); + return success(SysTenantConvert.INSTANCE.convertList(list)); + } + + @GetMapping("/page") + @ApiOperation("获得租户分页") + @PreAuthorize("@ss.hasPermission('system:tenant:query')") + public CommonResult> getTenantPage(@Valid SysTenantPageReqVO pageVO) { + PageResult pageResult = tenantService.getTenantPage(pageVO); + return success(SysTenantConvert.INSTANCE.convertPage(pageResult)); + } + + @GetMapping("/export-excel") + @ApiOperation("导出租户 Excel") + @PreAuthorize("@ss.hasPermission('system:tenant:export')") + @OperateLog(type = EXPORT) + public void exportTenantExcel(@Valid SysTenantExportReqVO exportReqVO, + HttpServletResponse response) throws IOException { + List list = tenantService.getTenantList(exportReqVO); + // 导出 Excel + List datas = SysTenantConvert.INSTANCE.convertList02(list); + ExcelUtils.write(response, "租户.xls", "数据", SysTenantExcelVO.class, datas); + } + + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantBaseVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantBaseVO.java new file mode 100755 index 000000000..12511123f --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantBaseVO.java @@ -0,0 +1,30 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +/** +* 租户 Base VO,提供给添加、修改、详细的子 VO 使用 +* 如果子 VO 存在差异的字段,请不要添加到这里,影响 Swagger 文档生成 +*/ +@Data +public class SysTenantBaseVO { + + @ApiModelProperty(value = "租户名", required = true, example = "芋道") + @NotNull(message = "租户名不能为空") + private String name; + + @ApiModelProperty(value = "联系人", required = true, example = "芋艿") + @NotNull(message = "联系人不能为空") + private String contactName; + + @ApiModelProperty(value = "联系手机", example = "15601691300") + private String contactMobile; + + @ApiModelProperty(value = "租户状态(0正常 1停用)", required = true, example = "1") + @NotNull(message = "租户状态(0正常 1停用)不能为空") + private Integer status; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantCreateReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantCreateReqVO.java new file mode 100755 index 000000000..4b90bc5c3 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantCreateReqVO.java @@ -0,0 +1,14 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("租户创建 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTenantCreateReqVO extends SysTenantBaseVO { + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExcelVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExcelVO.java new file mode 100755 index 000000000..e98c8e66e --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExcelVO.java @@ -0,0 +1,39 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +import com.alibaba.excel.annotation.ExcelProperty; +import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; +import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; + + +/** + * 租户 Excel VO + * + * @author 芋道源码 + */ +@Data +public class SysTenantExcelVO { + + @ExcelProperty("租户编号") + private Long id; + + @ExcelProperty("租户名") + private String name; + + @ExcelProperty("联系人") + private String contactName; + + @ExcelProperty("联系手机") + private String contactMobile; + + @ExcelProperty(value = "租户状态(0正常 1停用)", converter = DictConvert.class) + @DictFormat("sys_common_status") // TODO 代码优化:建议设置到对应的 XXXDictTypeConstants 枚举类中 + private Integer status; + + @ExcelProperty("创建时间") + private Date createTime; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExportReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExportReqVO.java new file mode 100755 index 000000000..e71e21db4 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantExportReqVO.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel(value = "租户 Excel 导出 Request VO", description = "参数和 SysTenantPageReqVO 是一致的") +@Data +public class SysTenantExportReqVO { + + @ApiModelProperty(value = "租户名", example = "芋道") + private String name; + + @ApiModelProperty(value = "联系人", example = "芋艿") + private String contactName; + + @ApiModelProperty(value = "联系手机", example = "15601691300") + private String contactMobile; + + @ApiModelProperty(value = "租户状态(0正常 1停用)", example = "1") + private Integer status; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始创建时间") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束创建时间") + private Date endCreateTime; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantPageReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantPageReqVO.java new file mode 100755 index 000000000..b2f5227a6 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantPageReqVO.java @@ -0,0 +1,41 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import cn.iocoder.yudao.framework.common.pojo.PageParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.ToString; +import org.springframework.format.annotation.DateTimeFormat; + +import java.util.Date; + +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND; + +@ApiModel("租户分页 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTenantPageReqVO extends PageParam { + + @ApiModelProperty(value = "租户名", example = "芋道") + private String name; + + @ApiModelProperty(value = "联系人", example = "芋艿") + private String contactName; + + @ApiModelProperty(value = "联系手机", example = "15601691300") + private String contactMobile; + + @ApiModelProperty(value = "租户状态(0正常 1停用)", example = "1") + private Integer status; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "开始创建时间") + private Date beginCreateTime; + + @DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND) + @ApiModelProperty(value = "结束创建时间") + private Date endCreateTime; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantRespVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantRespVO.java new file mode 100755 index 000000000..ab12ae53c --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantRespVO.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; + +@ApiModel("租户 Response VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTenantRespVO extends SysTenantBaseVO { + + @ApiModelProperty(value = "租户编号", required = true, example = "1024") + private Long id; + + @ApiModelProperty(value = "创建时间", required = true) + private Date createTime; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantUpdateReqVO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantUpdateReqVO.java new file mode 100755 index 000000000..0ae05af38 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/tenant/vo/SysTenantUpdateReqVO.java @@ -0,0 +1,18 @@ +package cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo; + +import lombok.*; +import java.util.*; +import io.swagger.annotations.*; +import javax.validation.constraints.*; + +@ApiModel("租户更新 Request VO") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +public class SysTenantUpdateReqVO extends SysTenantBaseVO { + + @ApiModelProperty(value = "租户编号", required = true, example = "1024") + @NotNull(message = "租户编号不能为空") + private Long id; + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http index 87283759d..ce138afcd 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/controller/user/SysUserController.http @@ -1,3 +1,4 @@ ### 请求 /system/user/page 接口 => 没有权限 GET {{baseUrl}}/system/user/page?pageNo=1&pageSize=10 -Authorization: Bearer test104 # 使用测试账号 +Authorization: Bearer test1 # 使用测试账号 +tenant-id: 1 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/tenant/SysTenantConvert.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/tenant/SysTenantConvert.java new file mode 100755 index 000000000..3df63f30b --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/convert/tenant/SysTenantConvert.java @@ -0,0 +1,36 @@ +package cn.iocoder.yudao.adminserver.modules.system.convert.tenant; + +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantCreateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantExcelVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantRespVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantUpdateReqVO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import org.mapstruct.Mapper; +import org.mapstruct.factory.Mappers; + +import java.util.List; + +/** + * 租户 Convert + * + * @author 芋道源码 + */ +@Mapper +public interface SysTenantConvert { + + SysTenantConvert INSTANCE = Mappers.getMapper(SysTenantConvert.class); + + SysTenantDO convert(SysTenantCreateReqVO bean); + + SysTenantDO convert(SysTenantUpdateReqVO bean); + + SysTenantRespVO convert(SysTenantDO bean); + + List convertList(List list); + + PageResult convertPage(PageResult page); + + List convertList02(List list); + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysDeptDO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysDeptDO.java index 46d7b9818..b10c13b7e 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysDeptDO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysDeptDO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -15,7 +15,7 @@ import lombok.EqualsAndHashCode; @TableName("sys_dept") @Data @EqualsAndHashCode(callSuper = true) -public class SysDeptDO extends BaseDO { +public class SysDeptDO extends TenantBaseDO { /** * 部门ID diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysPostDO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysPostDO.java index 52b19c7ea..29652a0d8 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysPostDO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/dept/SysPostDO.java @@ -1,7 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept; import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -15,14 +15,13 @@ import lombok.EqualsAndHashCode; @TableName("sys_post") @Data @EqualsAndHashCode(callSuper = true) -public class SysPostDO extends BaseDO { +public class SysPostDO extends TenantBaseDO { /** * 岗位序号 */ @TableId private Long id; - /** * 岗位名称 */ diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/logger/SysOperateLogDO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/logger/SysOperateLogDO.java index e937327ee..417c6f45f 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/logger/SysOperateLogDO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/logger/SysOperateLogDO.java @@ -3,7 +3,7 @@ package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.logger; import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum; -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableName; @@ -22,7 +22,7 @@ import java.util.Map; @TableName(value = "sys_operate_log", autoResultMap = true) @Data @EqualsAndHashCode(callSuper = true) -public class SysOperateLogDO extends BaseDO { +public class SysOperateLogDO extends TenantBaseDO { /** * {@link #javaMethodArgs} 的最大长度 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/notice/SysNoticeDO.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/notice/SysNoticeDO.java index 1ddff6537..c61398fe6 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/notice/SysNoticeDO.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/dataobject/notice/SysNoticeDO.java @@ -1,8 +1,8 @@ package cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.notice; -import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; -import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.adminserver.modules.system.enums.notice.SysNoticeTypeEnum; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.tenant.core.db.TenantBaseDO; import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableName; import lombok.Data; @@ -16,7 +16,7 @@ import lombok.EqualsAndHashCode; @TableName("sys_notice") @Data @EqualsAndHashCode(callSuper = true) -public class SysNoticeDO extends BaseDO { +public class SysNoticeDO extends TenantBaseDO { /** * 公告ID diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/dept/SysDeptMapper.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/dept/SysDeptMapper.java index 9b3cb176f..d4e0098cd 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/dept/SysDeptMapper.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/dept/SysDeptMapper.java @@ -24,7 +24,7 @@ public interface SysDeptMapper extends BaseMapperX { } default Integer selectCountByParentId(Long parentId) { - return selectCount(new QueryWrapper().eq("parent_id", parentId)); + return selectCount("parent_id", parentId); } default boolean selectExistsByUpdateTimeAfter(Date maxUpdateTime) { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/permission/SysMenuMapper.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/permission/SysMenuMapper.java index 58913f420..7502bc0e6 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/permission/SysMenuMapper.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/permission/SysMenuMapper.java @@ -19,7 +19,7 @@ public interface SysMenuMapper extends BaseMapperX { } default Integer selectCountByParentId(Long parentId) { - return selectCount(new QueryWrapper().eq("parent_id", parentId)); + return selectCount("parent_id", parentId); } default List selectList(SysMenuListReqVO reqVO) { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/tenant/SysTenantMapper.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/tenant/SysTenantMapper.java new file mode 100755 index 000000000..e0e87d158 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/dal/mysql/tenant/SysTenantMapper.java @@ -0,0 +1,44 @@ +package cn.iocoder.yudao.adminserver.modules.system.dal.mysql.tenant; + +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantExportReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantPageReqVO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +/** + * 租户 Mapper + * + * @author 芋道源码 + */ +@Mapper +public interface SysTenantMapper extends BaseMapperX { + + default PageResult selectPage(SysTenantPageReqVO reqVO) { + return selectPage(reqVO, new QueryWrapperX() + .likeIfPresent("name", reqVO.getName()) + .likeIfPresent("contact_name", reqVO.getContactName()) + .likeIfPresent("contact_mobile", reqVO.getContactMobile()) + .eqIfPresent("status", reqVO.getStatus()) + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) + .orderByDesc("id")); + } + + default List selectList(SysTenantExportReqVO reqVO) { + return selectList(new QueryWrapperX() + .likeIfPresent("name", reqVO.getName()) + .likeIfPresent("contact_name", reqVO.getContactName()) + .likeIfPresent("contact_mobile", reqVO.getContactMobile()) + .eqIfPresent("status", reqVO.getStatus()) + .betweenIfPresent("create_time", reqVO.getBeginCreateTime(), reqVO.getEndCreateTime()) + .orderByDesc("id")); + } + + default SysTenantDO selectByName(String name) { + return selectOne("name", name); + } +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java index 7da1f4239..fed445b9c 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/enums/SysErrorCodeConstants.java @@ -85,10 +85,12 @@ public interface SysErrorCodeConstants { ErrorCode SMS_TEMPLATE_NOT_EXISTS = new ErrorCode(1002011000, "短信模板不存在"); ErrorCode SMS_TEMPLATE_CODE_DUPLICATE = new ErrorCode(1002011001, "已经存在编码为【{}】的短信模板"); + // ========== 租户模块 1002012000 ========== + ErrorCode TENANT_NOT_EXISTS = new ErrorCode(1002012000, "租户不存在"); + // ========== 错误码模块 1002013000 ========== ErrorCode ERROR_CODE_NOT_EXISTS = new ErrorCode(1002013000, "错误码不存在"); ErrorCode ERROR_CODE_DUPLICATE = new ErrorCode(1002013001, "已经存在编码为【{}】的错误码"); - } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/captcha/config/CaptchaProperties.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/captcha/config/CaptchaProperties.java index 6eadd4d12..73939df75 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/captcha/config/CaptchaProperties.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/captcha/config/CaptchaProperties.java @@ -12,6 +12,13 @@ import java.time.Duration; @Data public class CaptchaProperties { + private static final Boolean ENABLE_DEFAULT = true; + + /** + * 是否开启 + * 注意,这里仅仅是后端 Server 是否校验,暂时不控制前端的逻辑 + */ + private Boolean enable = ENABLE_DEFAULT; /** * 验证码的过期时间 */ diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/config/SysDataPermissionConfiguration.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/config/SysDataPermissionConfiguration.java new file mode 100644 index 000000000..20f2e1602 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/config/SysDataPermissionConfiguration.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.adminserver.modules.system.framework.datapermission.config; + +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.user.SysUserDO; +import cn.iocoder.yudao.framework.datapermission.core.dept.rule.DeptDataPermissionRuleCustomizer; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * system 模块的数据权限 Configuration + * + * @author 芋道源码 + */ +@Configuration +public class SysDataPermissionConfiguration { + + @Bean + public DeptDataPermissionRuleCustomizer sysDeptDataPermissionRuleCustomizer() { + return rule -> { + rule.addDeptColumn(SysUserDO.class); + rule.addDeptColumn(SysDeptDO.class, "id"); + }; + } + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/package-info.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/package-info.java new file mode 100644 index 000000000..cdd6173e2 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/framework/datapermission/package-info.java @@ -0,0 +1,4 @@ +/** + * system 模块的数据权限配置 + */ +package cn.iocoder.yudao.adminserver.modules.system.framework.datapermission; diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/job/auth/SysUserSessionTimeoutJob.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/job/auth/SysUserSessionTimeoutJob.java index 4614ac619..57588fa80 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/job/auth/SysUserSessionTimeoutJob.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/job/auth/SysUserSessionTimeoutJob.java @@ -2,6 +2,7 @@ package cn.iocoder.yudao.adminserver.modules.system.job.auth; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.adminserver.modules.system.service.auth.SysUserSessionService; +import cn.iocoder.yudao.framework.tenant.core.job.TenantJob; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; @@ -13,6 +14,7 @@ import javax.annotation.Resource; * @author 願 */ @Component +@TenantJob @Slf4j public class SysUserSessionTimeoutJob implements JobHandler { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dept/SysDeptRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dept/SysDeptRefreshMessage.java index 724547d68..96362a8fe 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dept/SysDeptRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dept/SysDeptRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.dept; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 部门数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysDeptRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysDeptRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dict/SysDictDataRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dict/SysDictDataRefreshMessage.java index 7b735deb9..4415d60bc 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dict/SysDictDataRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/dict/SysDictDataRefreshMessage.java @@ -1,13 +1,15 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.dict; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 字典数据数据刷新 Message */ @Data -public class SysDictDataRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysDictDataRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/mail/SysMailSendMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/mail/SysMailSendMessage.java index bb9f62170..a0e258796 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/mail/SysMailSendMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/mail/SysMailSendMessage.java @@ -1,7 +1,8 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.mail; -import cn.iocoder.yudao.framework.mq.core.stream.StreamMessage; +import cn.iocoder.yudao.framework.mq.core.stream.AbstractStreamMessage; import lombok.Data; +import lombok.EqualsAndHashCode; import javax.validation.constraints.NotNull; import java.util.Map; @@ -12,7 +13,8 @@ import java.util.Map; * @author 芋道源码 */ @Data -public class SysMailSendMessage implements StreamMessage { +@EqualsAndHashCode(callSuper = true) +public class SysMailSendMessage extends AbstractStreamMessage { /** * 邮箱地址 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysMenuRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysMenuRefreshMessage.java index 1fa2a3879..3f0ef3da9 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysMenuRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysMenuRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 菜单数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysMenuRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysMenuRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleMenuRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleMenuRefreshMessage.java index 8b9f50c91..8ddcb17d4 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleMenuRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleMenuRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 角色与菜单数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysRoleMenuRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysRoleMenuRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleRefreshMessage.java index 8d8d1e01a..9e89fa186 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/permission/SysRoleRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.permission; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 角色数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysRoleRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysRoleRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsChannelRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsChannelRefreshMessage.java index a37295615..22c9341d5 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsChannelRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsChannelRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 短信渠道的数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysSmsChannelRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysSmsChannelRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsTemplateRefreshMessage.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsTemplateRefreshMessage.java index c8bb00af2..8401829cb 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsTemplateRefreshMessage.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/message/sms/SysSmsTemplateRefreshMessage.java @@ -1,13 +1,17 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.message.sms; -import cn.iocoder.yudao.framework.mq.core.pubsub.ChannelMessage; +import cn.iocoder.yudao.framework.mq.core.pubsub.AbstractChannelMessage; import lombok.Data; +import lombok.EqualsAndHashCode; /** * 短信模板的数据刷新 Message + * + * @author 芋道源码 */ @Data -public class SysSmsTemplateRefreshMessage implements ChannelMessage { +@EqualsAndHashCode(callSuper = true) +public class SysSmsTemplateRefreshMessage extends AbstractChannelMessage { @Override public String getChannel() { diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dept/SysDeptProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dept/SysDeptProducer.java index 948796e22..c4bbb7419 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dept/SysDeptProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dept/SysDeptProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dept; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.dept.SysDeptRefreshMessage; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -14,14 +13,14 @@ import javax.annotation.Resource; public class SysDeptProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysDeptRefreshMessage} 消息 */ public void sendDeptRefreshMessage() { SysDeptRefreshMessage message = new SysDeptRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dict/SysDictDataProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dict/SysDictDataProducer.java index ea0183722..4bce9db1c 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dict/SysDictDataProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/dict/SysDictDataProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.dict; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.dict.SysDictDataRefreshMessage; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -14,14 +13,14 @@ import javax.annotation.Resource; public class SysDictDataProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysDictDataRefreshMessage} 消息 */ public void sendDictDataRefreshMessage() { SysDictDataRefreshMessage message = new SysDictDataRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysMenuProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysMenuProducer.java index 6b3493469..1f4296530 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysMenuProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysMenuProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysMenuRefreshMessage; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -14,14 +13,14 @@ import javax.annotation.Resource; public class SysMenuProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysMenuRefreshMessage} 消息 */ public void sendMenuRefreshMessage() { SysMenuRefreshMessage message = new SysMenuRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysPermissionProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysPermissionProducer.java index d9a1bfcc2..def201962 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysPermissionProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysPermissionProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleMenuRefreshMessage; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -14,14 +13,14 @@ import javax.annotation.Resource; public class SysPermissionProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysRoleMenuRefreshMessage} 消息 */ public void sendRoleMenuRefreshMessage() { SysRoleMenuRefreshMessage message = new SysRoleMenuRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysRoleProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysRoleProducer.java index 6888f27bc..c3ab839a1 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysRoleProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/permission/SysRoleProducer.java @@ -1,8 +1,7 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.permission.SysRoleRefreshMessage; -import org.springframework.data.redis.core.StringRedisTemplate; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; @@ -16,14 +15,14 @@ import javax.annotation.Resource; public class SysRoleProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysRoleRefreshMessage} 消息 */ public void sendRoleRefreshMessage() { SysRoleRefreshMessage message = new SysRoleRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/sms/SysSmsProducer.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/sms/SysSmsProducer.java index 739603301..acc4fee7a 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/sms/SysSmsProducer.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/mq/producer/sms/SysSmsProducer.java @@ -1,15 +1,12 @@ package cn.iocoder.yudao.adminserver.modules.system.mq.producer.sms; -import cn.iocoder.yudao.framework.common.core.KeyValue; -import cn.iocoder.yudao.framework.mq.core.util.RedisMessageUtils; import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsChannelRefreshMessage; import cn.iocoder.yudao.adminserver.modules.system.mq.message.sms.SysSmsTemplateRefreshMessage; +import cn.iocoder.yudao.framework.mq.core.RedisMQTemplate; import lombok.extern.slf4j.Slf4j; -import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import javax.annotation.Resource; -import java.util.List; /** * Sms 短信相关消息的 Producer @@ -22,14 +19,14 @@ import java.util.List; public class SysSmsProducer { @Resource - private StringRedisTemplate stringRedisTemplate; + private RedisMQTemplate redisMQTemplate; /** * 发送 {@link SysSmsChannelRefreshMessage} 消息 */ public void sendSmsChannelRefreshMessage() { SysSmsChannelRefreshMessage message = new SysSmsChannelRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } /** @@ -37,7 +34,7 @@ public class SysSmsProducer { */ public void sendSmsTemplateRefreshMessage() { SysSmsTemplateRefreshMessage message = new SysSmsTemplateRefreshMessage(); - RedisMessageUtils.sendChannelMessage(stringRedisTemplate, message); + redisMQTemplate.send(message); } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java index 762649d9d..6060f7e23 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/impl/SysAuthServiceImpl.java @@ -92,9 +92,7 @@ public class SysAuthServiceImpl implements SysAuthService { throw new UsernameNotFoundException(username); } // 创建 LoginUser 对象 - LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); - loginUser.setPostIds(user.getPostIds()); - return loginUser; + return this.buildLoginUser(user); } @Override @@ -107,9 +105,7 @@ public class SysAuthServiceImpl implements SysAuthService { this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_MOCK, SysLoginResultEnum.SUCCESS); // 创建 LoginUser 对象 - LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 - return loginUser; + return this.buildLoginUser(user); } @Override @@ -117,10 +113,9 @@ public class SysAuthServiceImpl implements SysAuthService { // 判断验证码是否正确 this.verifyCaptcha(reqVO.getUsername(), reqVO.getUuid(), reqVO.getCode()); - // 使用账号密码,进行登录。 + // 使用账号密码,进行登录 LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 - loginUser.setGroups(this.getUserPosts(loginUser.getPostIds())); + // 缓存登陆用户到 Redis 中,返回 sessionId 编号 return userSessionCoreService.createUserSession(loginUser, userIp, userAgent); } @@ -133,9 +128,13 @@ public class SysAuthServiceImpl implements SysAuthService { } private void verifyCaptcha(String username, String captchaUUID, String captchaCode) { + // 如果验证码关闭,则不进行校验 + if (!captchaService.isCaptchaEnable()) { + return; + } + // 验证码不存在 final SysLoginLogTypeEnum logTypeEnum = SysLoginLogTypeEnum.LOGIN_USERNAME; String code = captchaService.getCaptchaCode(captchaUUID); - // 验证码不存在 if (code == null) { // 创建登录失败日志(验证码不存在) this.createLoginLog(username, logTypeEnum, SysLoginResultEnum.CAPTCHA_NOT_FOUND); @@ -230,8 +229,7 @@ public class SysAuthServiceImpl implements SysAuthService { this.createLoginLog(user.getUsername(), SysLoginLogTypeEnum.LOGIN_SOCIAL, SysLoginResultEnum.SUCCESS); // 创建 LoginUser 对象 - LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 + LoginUser loginUser = this.buildLoginUser(user); // 绑定社交用户(更新) socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); @@ -248,7 +246,6 @@ public class SysAuthServiceImpl implements SysAuthService { // 使用账号密码,进行登录。 LoginUser loginUser = this.login0(reqVO.getUsername(), reqVO.getPassword()); - loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); // 获取用户角色列表 // 绑定社交用户(新增) socialService.bindSocialUser(loginUser.getId(), reqVO.getType(), authUser, userTypeEnum); @@ -301,15 +298,14 @@ public class SysAuthServiceImpl implements SysAuthService { return null; } // 刷新 LoginUser 缓存 - this.refreshLoginUserCache(token, loginUser); - return loginUser; + return this.refreshLoginUserCache(token, loginUser); } - private void refreshLoginUserCache(String token, LoginUser loginUser) { + private LoginUser refreshLoginUserCache(String token, LoginUser loginUser) { // 每 1/3 的 Session 超时时间,刷新 LoginUser 缓存 if (System.currentTimeMillis() - loginUser.getUpdateTime().getTime() < userSessionCoreService.getSessionTimeoutMillis() / 3) { - return; + return loginUser; } // 重新加载 SysUserDO 信息 @@ -319,9 +315,18 @@ public class SysAuthServiceImpl implements SysAuthService { } // 刷新 LoginUser 缓存 + LoginUser newLoginUser= this.buildLoginUser(user); + userSessionCoreService.refreshUserSession(token, newLoginUser); + return newLoginUser; + } + + private LoginUser buildLoginUser(SysUserDO user) { + LoginUser loginUser = SysAuthConvert.INSTANCE.convert(user); + // 补全字段 loginUser.setDeptId(user.getDeptId()); loginUser.setRoleIds(this.getUserRoleIds(loginUser.getId())); - userSessionCoreService.refreshUserSession(token, loginUser); + loginUser.setGroups(this.getUserPosts(user.getPostIds())); + return loginUser; } } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/SysCaptchaService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/SysCaptchaService.java index 286a5ef1f..32aff5b06 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/SysCaptchaService.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/SysCaptchaService.java @@ -14,6 +14,13 @@ public interface SysCaptchaService { */ SysCaptchaImageRespVO getCaptchaImage(); + /** + * 是否开启图片验证码 + * + * @return 是否 + */ + Boolean isCaptchaEnable(); + /** * 获得 uuid 对应的验证码 * diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/impl/SysCaptchaServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/impl/SysCaptchaServiceImpl.java index 44d291ffd..daefc007f 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/impl/SysCaptchaServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/common/impl/SysCaptchaServiceImpl.java @@ -35,6 +35,11 @@ public class SysCaptchaServiceImpl implements SysCaptchaService { return SysCaptchaConvert.INSTANCE.convert(uuid, captcha); } + @Override + public Boolean isCaptchaEnable() { + return captchaProperties.getEnable(); + } + @Override public String getCaptchaCode(String uuid) { return captchaRedisDAO.get(uuid); diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/impl/SysDeptServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/impl/SysDeptServiceImpl.java index cac9bf565..2eccdf721 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/impl/SysDeptServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/impl/SysDeptServiceImpl.java @@ -169,9 +169,12 @@ public class SysDeptServiceImpl implements SysDeptService { @Override public List getDeptsByParentIdFromCache(Long parentId, boolean recursive) { - List result = new ArrayList<>(); + if (parentId == null) { + return Collections.emptyList(); + } + List result = new ArrayList<>(); // TODO 芋艿:待优化,新增缓存,避免每次遍历的计算 // 递归,简单粗暴 - this.listDeptsByParentIdFromCache(result, parentId, + this.getDeptsByParentIdFromCache(result, parentId, recursive ? Integer.MAX_VALUE : 1, // 如果递归获取,则无限;否则,只递归 1 次 parentDeptCache); return result; @@ -185,8 +188,8 @@ public class SysDeptServiceImpl implements SysDeptService { * @param recursiveCount 递归次数 * @param parentDeptMap 父部门 Map,使用缓存,避免变化 */ - private void listDeptsByParentIdFromCache(List result, Long parentId, int recursiveCount, - Multimap parentDeptMap) { + private void getDeptsByParentIdFromCache(List result, Long parentId, int recursiveCount, + Multimap parentDeptMap) { // 递归次数为 0,结束! if (recursiveCount == 0) { return; @@ -198,7 +201,7 @@ public class SysDeptServiceImpl implements SysDeptService { } result.addAll(depts); // 继续递归 - depts.forEach(dept -> listDeptsByParentIdFromCache(result, dept.getId(), + depts.forEach(dept -> getDeptsByParentIdFromCache(result, dept.getId(), recursiveCount - 1, parentDeptMap)); } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionService.java index e159f95be..883a11f4f 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionService.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionService.java @@ -1,5 +1,6 @@ package cn.iocoder.yudao.adminserver.modules.system.service.permission; +import cn.iocoder.yudao.framework.datapermission.core.dept.service.DeptDataPermissionFrameworkService; import cn.iocoder.yudao.framework.security.core.service.SecurityPermissionFrameworkService; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysMenuDO; import org.springframework.lang.Nullable; @@ -15,7 +16,7 @@ import java.util.Set; * * @author 芋道源码 */ -public interface SysPermissionService extends SecurityPermissionFrameworkService { +public interface SysPermissionService extends SecurityPermissionFrameworkService, DeptDataPermissionFrameworkService { /** * 初始化权限的本地缓存 diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysPermissionServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysPermissionServiceImpl.java index b4eb2cb34..90e320057 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysPermissionServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysPermissionServiceImpl.java @@ -3,19 +3,25 @@ package cn.iocoder.yudao.adminserver.modules.system.service.permission.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.util.ArrayUtil; -import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysRoleMenuMapper; -import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysUserRoleMapper; +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysMenuDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleMenuDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysUserRoleDO; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysRoleMenuMapper; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysUserRoleMapper; import cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission.SysPermissionProducer; +import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysMenuService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.MapUtils; +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO; +import cn.iocoder.yudao.framework.security.core.LoginUser; +import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; +import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import com.google.common.collect.ImmutableMultimap; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; @@ -39,6 +45,11 @@ import java.util.*; @Slf4j public class SysPermissionServiceImpl implements SysPermissionService { + /** + * LoginUser 的 Context 缓存 Key + */ + public static final String CONTEXT_KEY = SysPermissionServiceImpl.class.getSimpleName(); + /** * 定时执行 {@link #schedulePeriodicRefresh()} 的周期 * 因为已经通过 Redis Pub/Sub 机制,所以频率不需要高 @@ -75,6 +86,8 @@ public class SysPermissionServiceImpl implements SysPermissionService { private SysRoleService roleService; @Resource private SysMenuService menuService; + @Resource + private SysDeptService deptService; @Resource private SysPermissionProducer permissionProducer; @@ -329,4 +342,58 @@ public class SysPermissionServiceImpl implements SysPermissionService { return CollUtil.containsAny(userRoles, Sets.newHashSet(roles)); } + @Override + public DeptDataPermissionRespDTO getDeptDataPermission(LoginUser loginUser) { + // 判断是否 context 已经缓存 + DeptDataPermissionRespDTO result = loginUser.getContext(CONTEXT_KEY, DeptDataPermissionRespDTO.class); + if (result != null) { + return result; + } + + // 创建 DeptDataPermissionRespDTO 对象 + result = new DeptDataPermissionRespDTO(); + List roles = roleService.getRolesFromCache(loginUser.getRoleIds()); + for (SysRoleDO role : roles) { + // 为空时,跳过 + if (role.getDataScope() == null) { + continue; + } + // 情况一,ALL + if (Objects.equals(role.getDataScope(), DataScopeEnum.ALL.getScope())) { + result.setAll(true); + continue; + } + // 情况二,DEPT_CUSTOM + if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_CUSTOM.getScope())) { + CollUtil.addAll(result.getDeptIds(), role.getDataScopeDeptIds()); + // 自定义可见部门时,保证可以看到自己所在的部门。否则,一些场景下可能会有问题。 + // 例如说,登录时,基于 t_user 的 username 查询会可能被 dept_id 过滤掉 + CollUtil.addAll(result.getDeptIds(), loginUser.getDeptId()); + continue; + } + // 情况三,DEPT_ONLY + if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_ONLY.getScope())) { + CollectionUtils.addIfNotNull(result.getDeptIds(), loginUser.getDeptId()); + continue; + } + // 情况四,DEPT_DEPT_AND_CHILD + if (Objects.equals(role.getDataScope(), DataScopeEnum.DEPT_AND_CHILD.getScope())) { + List depts = deptService.getDeptsByParentIdFromCache(loginUser.getDeptId(), true); + CollUtil.addAll(result.getDeptIds(), CollectionUtils.convertList(depts, SysDeptDO::getId)); + continue; + } + // 情况五,SELF + if (Objects.equals(role.getDataScope(), DataScopeEnum.SELF.getScope())) { + result.setSelf(true); + continue; + } + // 未知情况,error log 即可 + log.error("[getDeptDataPermission][LoginUser({}) role({}) 无法处理]", loginUser.getId(), JsonUtils.toJsonString(result)); + } + + // 添加到缓存,并返回 + loginUser.setContext(CONTEXT_KEY, result); + return result; + } + } diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysRoleServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysRoleServiceImpl.java index c233f4f85..0e82c5051 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysRoleServiceImpl.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/impl/SysRoleServiceImpl.java @@ -18,6 +18,7 @@ import cn.iocoder.yudao.adminserver.modules.system.enums.permission.SysRoleTypeE import cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission.SysRoleProducer; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysPermissionService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.SysRoleService; +import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableMap; import lombok.extern.slf4j.Slf4j; @@ -127,6 +128,7 @@ public class SysRoleServiceImpl implements SysRoleService { SysRoleDO role = SysRoleConvert.INSTANCE.convert(reqVO); role.setType(SysRoleTypeEnum.CUSTOM.getType()); role.setStatus(CommonStatusEnum.ENABLE.getStatus()); + role.setDataScope(DataScopeEnum.ALL.getScope()); // 默认可查看所有数据。原因是,可能一些项目不需要项目权限 roleMapper.insert(role); // 发送刷新消息 roleProducer.sendRoleRefreshMessage(); diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantService.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantService.java new file mode 100755 index 000000000..b32e81f12 --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantService.java @@ -0,0 +1,83 @@ +package cn.iocoder.yudao.adminserver.modules.system.service.tenant; + +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantCreateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantExportReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantPageReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantUpdateReqVO; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; + +import javax.validation.Valid; +import java.util.Collection; +import java.util.List; + +/** + * 租户 Service 接口 + * + * @author 芋道源码 + */ +public interface SysTenantService { + + /** + * 创建租户 + * + * @param createReqVO 创建信息 + * @return 编号 + */ + Long createTenant(@Valid SysTenantCreateReqVO createReqVO); + + /** + * 更新租户 + * + * @param updateReqVO 更新信息 + */ + void updateTenant(@Valid SysTenantUpdateReqVO updateReqVO); + + /** + * 删除租户 + * + * @param id 编号 + */ + void deleteTenant(Long id); + + /** + * 获得租户 + * + * @param id 编号 + * @return 租户 + */ + SysTenantDO getTenant(Long id); + + /** + * 获得租户列表 + * + * @param ids 编号 + * @return 租户列表 + */ + List getTenantList(Collection ids); + + /** + * 获得租户分页 + * + * @param pageReqVO 分页查询 + * @return 租户分页 + */ + PageResult getTenantPage(SysTenantPageReqVO pageReqVO); + + /** + * 获得租户列表, 用于 Excel 导出 + * + * @param exportReqVO 查询条件 + * @return 租户列表 + */ + List getTenantList(SysTenantExportReqVO exportReqVO); + + /** + * 获得名字对应的租户 + * + * @param name 组户名 + * @return 租户 + */ + SysTenantDO getTenantByName(String name); + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/impl/SysTenantServiceImpl.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/impl/SysTenantServiceImpl.java new file mode 100755 index 000000000..80701affb --- /dev/null +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/impl/SysTenantServiceImpl.java @@ -0,0 +1,91 @@ +package cn.iocoder.yudao.adminserver.modules.system.service.tenant.impl; + +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantCreateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantExportReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantPageReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantUpdateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.convert.tenant.SysTenantConvert; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.tenant.SysTenantMapper; +import cn.iocoder.yudao.adminserver.modules.system.service.tenant.SysTenantService; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import org.springframework.stereotype.Service; +import org.springframework.validation.annotation.Validated; + +import javax.annotation.Resource; +import java.util.Collection; +import java.util.List; + +import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.TENANT_NOT_EXISTS; +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; + +/** + * 租户 Service 实现类 + * + * @author 芋道源码 + */ +@Service +@Validated +public class SysTenantServiceImpl implements SysTenantService { + + @Resource + private SysTenantMapper tenantMapper; + + @Override + public Long createTenant(SysTenantCreateReqVO createReqVO) { + // 插入 + SysTenantDO tenant = SysTenantConvert.INSTANCE.convert(createReqVO); + tenantMapper.insert(tenant); + // 返回 + return tenant.getId(); + } + + @Override + public void updateTenant(SysTenantUpdateReqVO updateReqVO) { + // 校验存在 + this.validateTenantExists(updateReqVO.getId()); + // 更新 + SysTenantDO updateObj = SysTenantConvert.INSTANCE.convert(updateReqVO); + tenantMapper.updateById(updateObj); + } + + @Override + public void deleteTenant(Long id) { + // 校验存在 + this.validateTenantExists(id); + // 删除 + tenantMapper.deleteById(id); + } + + private void validateTenantExists(Long id) { + if (tenantMapper.selectById(id) == null) { + throw exception(TENANT_NOT_EXISTS); + } + } + + @Override + public SysTenantDO getTenant(Long id) { + return tenantMapper.selectById(id); + } + + @Override + public List getTenantList(Collection ids) { + return tenantMapper.selectBatchIds(ids); + } + + @Override + public PageResult getTenantPage(SysTenantPageReqVO pageReqVO) { + return tenantMapper.selectPage(pageReqVO); + } + + @Override + public List getTenantList(SysTenantExportReqVO exportReqVO) { + return tenantMapper.selectList(exportReqVO); + } + + @Override + public SysTenantDO getTenantByName(String name) { + return tenantMapper.selectByName(name); + } + +} diff --git a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/tool/service/codegen/impl/ToolCodegenEngine.java b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/tool/service/codegen/impl/ToolCodegenEngine.java index 44c95603f..f7a1e4c9b 100644 --- a/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/tool/service/codegen/impl/ToolCodegenEngine.java +++ b/yudao-admin-server/src/main/java/cn/iocoder/yudao/adminserver/modules/tool/service/codegen/impl/ToolCodegenEngine.java @@ -10,6 +10,7 @@ import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.adminserver.modules.system.framework.codegen.config.CodegenProperties; +import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; import cn.iocoder.yudao.framework.excel.core.annotations.DictFormat; import cn.iocoder.yudao.framework.excel.core.convert.DictConvert; import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils; @@ -119,6 +120,8 @@ public class ToolCodegenEngine { private void initGlobalBindingMap() { // 全局配置 globalBindingMap.put("basePackage", codegenProperties.getBasePackage()); + globalBindingMap.put("baseFrameworkPackage", StrUtil.subBefore(codegenProperties.getBasePackage(), + '.', true) + '.' + "framework"); // 全局 Java Bean globalBindingMap.put("CommonResultClassName", CommonResult.class.getName()); globalBindingMap.put("PageResultClassName", PageResult.class.getName()); @@ -134,6 +137,7 @@ public class ToolCodegenEngine { globalBindingMap.put("ServiceExceptionUtilClassName", ServiceExceptionUtil.class.getName()); globalBindingMap.put("DateUtilsClassName", DateUtils.class.getName()); globalBindingMap.put("ExcelUtilsClassName", ExcelUtils.class.getName()); + globalBindingMap.put("ObjectUtilsClassName", ObjectUtils.class.getName()); globalBindingMap.put("DictConvertClassName", DictConvert.class.getName()); globalBindingMap.put("OperateLogClassName", OperateLog.class.getName()); globalBindingMap.put("OperateTypeEnumClassName", OperateTypeEnum.class.getName()); diff --git a/yudao-admin-server/src/main/resources/application-dev.yaml b/yudao-admin-server/src/main/resources/application-dev.yaml index 94d9cf9db..d04afc83b 100644 --- a/yudao-admin-server/src/main/resources/application-dev.yaml +++ b/yudao-admin-server/src/main/resources/application-dev.yaml @@ -66,6 +66,7 @@ spring: # Quartz 配置项,对应 QuartzProperties 配置类 spring: quartz: + auto-startup: true # 测试环境,需要开启 Job scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。 wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true diff --git a/yudao-admin-server/src/main/resources/application-local.yaml b/yudao-admin-server/src/main/resources/application-local.yaml index d7880547f..54d5447dc 100644 --- a/yudao-admin-server/src/main/resources/application-local.yaml +++ b/yudao-admin-server/src/main/resources/application-local.yaml @@ -80,6 +80,7 @@ spring: # Quartz 配置项,对应 QuartzProperties 配置类 spring: quartz: + auto-startup: false # 本地开发环境,尽量不要开启 Job scheduler-name: schedulerName # Scheduler 名字。默认为 schedulerName job-store-type: jdbc # Job 存储器类型。默认为 memory 表示内存,可选 jdbc 使用数据库。 wait-for-jobs-to-complete-on-shutdown: true # 应用关闭时,是否等待定时任务执行完成。默认为 false ,建议设置为 true @@ -166,6 +167,8 @@ logging: # 芋道配置项,设置当前项目所有自定义的配置 yudao: + captcha: + enable: false # 本地环境,暂时关闭图片验证码,方便登录等接口的测试 security: token-header: Authorization token-secret: abcdefghijklmnopqrstuvwxyz diff --git a/yudao-admin-server/src/main/resources/application.yaml b/yudao-admin-server/src/main/resources/application.yaml index 8674452cf..fc399a3a8 100644 --- a/yudao-admin-server/src/main/resources/application.yaml +++ b/yudao-admin-server/src/main/resources/application.yaml @@ -22,7 +22,7 @@ spring: # MyBatis Plus 的配置项 mybatis-plus: -# 在 mybatis-config/mybatis-config.xml 中设置 +# 在 mybatis-config/mybatis-config.xml 中设置 TODO jason:看看有没其它解决方案 # configuration: # map-underscore-to-camel-case: true # 虽然默认为 true ,但是还是显示去指定下。 # log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 打印日志 @@ -73,5 +73,7 @@ yudao: constants-class-list: - cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants - cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants + tenant: # 多租户相关配置项 + tables: # 配置需要开启多租户的表;如果实体已经继承 TenantBaseDO 类,则无需重复配置 debug: false diff --git a/yudao-admin-server/src/main/resources/codegen/java/test/serviceTest.vm b/yudao-admin-server/src/main/resources/codegen/java/test/serviceTest.vm index f4590b74c..511d65148 100644 --- a/yudao-admin-server/src/main/resources/codegen/java/test/serviceTest.vm +++ b/yudao-admin-server/src/main/resources/codegen/java/test/serviceTest.vm @@ -10,7 +10,6 @@ import ${basePackage}.modules.${table.moduleName}.service.${table.businessName}. import ${basePackage}.modules.${table.moduleName}.controller.${table.businessName}.vo.*; import ${basePackage}.modules.${table.moduleName}.dal.dataobject.${table.businessName}.${table.className}DO; import ${basePackage}.modules.${table.moduleName}.dal.mysql.${table.businessName}.${table.className}Mapper; -import ${basePackage}.util.object.ObjectUtils; import ${PageResultClassName}; import javax.annotation.Resource; @@ -19,9 +18,10 @@ import java.util.*; import static cn.hutool.core.util.RandomUtil.*; import static ${basePackage}.modules.${table.moduleName}.enums.${simpleModuleName_upperFirst}ErrorCodeConstants.*; -import static ${basePackage}.util.AssertUtils.*; -import static ${basePackage}.util.RandomUtils.*; -import static ${basePackage}.util.date.DateUtils.*; +import static ${baseFrameworkPackage}.test.core.util.AssertUtils.*; +import static ${baseFrameworkPackage}.test.core.util.RandomUtils.*; +import static ${ObjectUtilsClassName}.*; +import static ${DateUtilsClassName}.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; @@ -41,7 +41,7 @@ import static org.mockito.Mockito.*; #if (${column.listOperation}) #set ($JavaField = $column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})##首字母大写 // 测试 ${column.javaField} 不匹配 - ${classNameVar}Mapper.insert(ObjectUtils.clone(db${simpleClassName}, o -> o.set$JavaField(null))); + ${classNameVar}Mapper.insert(cloneIgnoreId(db${simpleClassName}, o -> o.set$JavaField(null))); #end #end // 准备参数 diff --git a/yudao-admin-server/src/main/resources/mybatis-config/mybatis-config.xml b/yudao-admin-server/src/main/resources/mybatis-config/mybatis-config.xml index 4f290bc98..d459d9670 100644 --- a/yudao-admin-server/src/main/resources/mybatis-config/mybatis-config.xml +++ b/yudao-admin-server/src/main/resources/mybatis-config/mybatis-config.xml @@ -6,6 +6,7 @@ + diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/config/InfConfigServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/config/InfConfigServiceTest.java index 311792215..ac37cf20c 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/config/InfConfigServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/config/InfConfigServiceTest.java @@ -172,13 +172,13 @@ public class InfConfigServiceTest extends BaseDbUnitTest { }); configMapper.insert(dbConfig); // 测试 name 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆"))); // 测试 key 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setKey("tudou"))); // 测试 type 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); // 测试 createTime 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); // 准备参数 InfConfigPageReqVO reqVO = new InfConfigPageReqVO(); reqVO.setName("艿"); @@ -206,13 +206,13 @@ public class InfConfigServiceTest extends BaseDbUnitTest { }); configMapper.insert(dbConfig); // 测试 name 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setName("土豆"))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setName("土豆"))); // 测试 key 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setKey("tudou"))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setKey("tudou"))); // 测试 type 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setType(InfConfigTypeEnum.CUSTOM.getType()))); // 测试 createTime 不匹配 - configMapper.insert(ObjectUtils.clone(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + configMapper.insert(ObjectUtils.cloneIgnoreId(dbConfig, o -> o.setCreateTime(buildTime(2021, 1, 1)))); // 准备参数 InfConfigExportReqVO reqVO = new InfConfigExportReqVO(); reqVO.setName("艿"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java index 98ec86c81..a5d4d8e95 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/file/InfFileServiceTest.java @@ -41,14 +41,14 @@ public class InfFileServiceTest extends BaseDbUnitTest { }); fileMapper.insert(dbFile); // 测试 id 不匹配 - fileMapper.insert(ObjectUtils.clone(dbFile, o -> o.setId("tudou"))); + fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> o.setId("tudou"))); // 测试 type 不匹配 - fileMapper.insert(ObjectUtils.clone(dbFile, o -> { + fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> { o.setId("yudao02"); o.setType("png"); })); // 测试 createTime 不匹配 - fileMapper.insert(ObjectUtils.clone(dbFile, o -> { + fileMapper.insert(ObjectUtils.cloneIgnoreId(dbFile, o -> { o.setId("yudao03"); o.setCreateTime(buildTime(2020, 1, 15)); })); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobLogServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobLogServiceTest.java index 4e4f17287..3946953fa 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobLogServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobLogServiceTest.java @@ -84,7 +84,7 @@ public class InfJobLogServiceTest extends BaseDbUnitTest { o.setExecuteIndex(1); o.setStatus(randomEle(InfJobLogStatusEnum.values()).getStatus()); // 保证 status 的范围 }); - InfJobLogDO cloneJobLog = ObjectUtils.clone(dbJobLog, o -> o.setHandlerName(randomString())); + InfJobLogDO cloneJobLog = ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString())); jobLogMapper.insert(dbJobLog); // 测试 handlerName 不匹配 jobLogMapper.insert(cloneJobLog); @@ -111,15 +111,15 @@ public class InfJobLogServiceTest extends BaseDbUnitTest { }); jobLogMapper.insert(dbJobLog); // 测试 jobId 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setJobId(randomLongId()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId()))); // 测试 handlerName 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setHandlerName(randomString()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()))); // 测试 beginTime 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7)))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7)))); // 测试 endTime 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9)))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9)))); // 测试 status 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setStatus(InfJobLogStatusEnum.FAILURE.getStatus()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(InfJobLogStatusEnum.FAILURE.getStatus()))); // 准备参数 InfJobLogPageReqVO reqVo = new InfJobLogPageReqVO(); reqVo.setJobId(dbJobLog.getJobId()); @@ -147,15 +147,15 @@ public class InfJobLogServiceTest extends BaseDbUnitTest { }); jobLogMapper.insert(dbJobLog); // 测试 jobId 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setJobId(randomLongId()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setJobId(randomLongId()))); // 测试 handlerName 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setHandlerName(randomString()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setHandlerName(randomString()))); // 测试 beginTime 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7)))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setBeginTime(buildTime(2021, 1, 7)))); // 测试 endTime 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9)))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setEndTime(buildTime(2021, 1, 9)))); // 测试 status 不匹配 - jobLogMapper.insert(ObjectUtils.clone(dbJobLog, o -> o.setStatus(InfJobLogStatusEnum.FAILURE.getStatus()))); + jobLogMapper.insert(ObjectUtils.cloneIgnoreId(dbJobLog, o -> o.setStatus(InfJobLogStatusEnum.FAILURE.getStatus()))); // 准备参数 InfJobLogExportReqVO reqVo = new InfJobLogExportReqVO(); reqVo.setJobId(dbJobLog.getJobId()); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobServiceTest.java index a767519b6..538d4340c 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/job/InfJobServiceTest.java @@ -230,7 +230,7 @@ public class InfJobServiceTest extends BaseDbUnitTest { InfJobDO dbJob = randomPojo(InfJobDO.class, o -> { o.setStatus(randomEle(InfJobStatusEnum.values()).getStatus()); // 保证 status 的范围 }); - InfJobDO cloneJob = ObjectUtils.clone(dbJob, o -> o.setHandlerName(randomString())); + InfJobDO cloneJob = ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString())); jobMapper.insert(dbJob); // 测试 handlerName 不匹配 jobMapper.insert(cloneJob); @@ -255,11 +255,11 @@ public class InfJobServiceTest extends BaseDbUnitTest { }); jobMapper.insert(dbJob); // 测试 name 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setName("土豆"))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆"))); // 测试 status 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setStatus(InfJobStatusEnum.NORMAL.getStatus()))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(InfJobStatusEnum.NORMAL.getStatus()))); // 测试 handlerName 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setHandlerName(randomString()))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()))); // 准备参数 InfJobPageReqVO reqVo = new InfJobPageReqVO(); reqVo.setName("定时"); @@ -283,11 +283,11 @@ public class InfJobServiceTest extends BaseDbUnitTest { }); jobMapper.insert(dbJob); // 测试 name 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setName("土豆"))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setName("土豆"))); // 测试 status 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setStatus(InfJobStatusEnum.NORMAL.getStatus()))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setStatus(InfJobStatusEnum.NORMAL.getStatus()))); // 测试 handlerName 不匹配 - jobMapper.insert(ObjectUtils.clone(dbJob, o -> o.setHandlerName(randomString()))); + jobMapper.insert(ObjectUtils.cloneIgnoreId(dbJob, o -> o.setHandlerName(randomString()))); // 准备参数 InfJobExportReqVO reqVo = new InfJobExportReqVO(); reqVo.setName("定时"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java index 73e918da4..0c401dd2f 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiAccessLogServiceImplTest.java @@ -59,19 +59,19 @@ public class InfApiAccessLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // userId 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); // userType - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); // applicationName 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); // requestUrl 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); // 构造一个早期时间 2021-02-06 00:00:00 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); // duration 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setDuration(100))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100))); // resultCode 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); // 构造调用参数 InfApiAccessLogPageReqVO reqVO = new InfApiAccessLogPageReqVO(); @@ -117,19 +117,19 @@ public class InfApiAccessLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // userId 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserId(3344L))); // userType - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); // applicationName 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setApplicationName("test"))); // requestUrl 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setRequestUrl("bar"))); // 构造一个早期时间 2021-02-06 00:00:00 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setBeginTime(buildTime(2021, 2, 6)))); // duration 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setDuration(100))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setDuration(100))); // resultCode 不同的 - infApiAccessLogMapper.insert(ObjectUtils.clone(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); + infApiAccessLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiAccessLogDO, logDO -> logDO.setResultCode(2))); // 构造调用参数 InfApiAccessLogExportReqVO reqVO = new InfApiAccessLogExportReqVO(); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java index 307f0172a..f7c9eb336 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/infra/service/logger/InfApiErrorLogServiceImplTest.java @@ -1,16 +1,16 @@ package cn.iocoder.yudao.adminserver.modules.infra.service.logger; import cn.iocoder.yudao.adminserver.BaseDbUnitTest; -import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; -import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; -import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.adminserver.modules.infra.controller.logger.vo.apierrorlog.InfApiErrorLogExportReqVO; import cn.iocoder.yudao.adminserver.modules.infra.controller.logger.vo.apierrorlog.InfApiErrorLogPageReqVO; import cn.iocoder.yudao.adminserver.modules.infra.dal.mysql.logger.InfApiErrorLogMapper; import cn.iocoder.yudao.adminserver.modules.infra.enums.logger.InfApiErrorLogProcessStatusEnum; import cn.iocoder.yudao.adminserver.modules.infra.service.logger.impl.InfApiErrorLogServiceImpl; -import cn.iocoder.yudao.framework.test.core.util.RandomUtils; +import cn.iocoder.yudao.coreservice.modules.infra.dal.dataobject.logger.InfApiErrorLogDO; +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; +import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; +import cn.iocoder.yudao.framework.test.core.util.RandomUtils; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.Import; @@ -20,11 +20,10 @@ import java.util.List; import static cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; import static cn.iocoder.yudao.adminserver.modules.infra.enums.InfErrorCodeConstants.API_ERROR_LOG_PROCESSED; +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; -import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; /** * {@link InfApiErrorLogServiceImpl} 单元测试 @@ -60,17 +59,17 @@ public class InfApiErrorLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // userId 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); // userType - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); // applicationName 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); // requestUrl 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); // 构造一个早期时间 2021-02-06 00:00:00 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); // progressStatus 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); // 构造调用参数 InfApiErrorLogPageReqVO reqVO = new InfApiErrorLogPageReqVO(); @@ -113,17 +112,17 @@ public class InfApiErrorLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // userId 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserId(3344L))); // userType - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setUserType(UserTypeEnum.MEMBER.getValue()))); // applicationName 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setApplicationName("test"))); // requestUrl 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setRequestUrl("bar"))); // 构造一个早期时间 2021-02-06 00:00:00 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setExceptionTime(buildTime(2021, 2, 6)))); // progressStatus 不同的 - infApiErrorLogMapper.insert(ObjectUtils.clone(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); + infApiErrorLogMapper.insert(ObjectUtils.cloneIgnoreId(infApiErrorLogDO, logDO -> logDO.setProcessStatus(InfApiErrorLogProcessStatusEnum.DONE.getStatus()))); // 构造调用参数 InfApiErrorLogExportReqVO reqVO = new InfApiErrorLogExportReqVO(); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java index 59be97809..11696fe25 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysAuthServiceImplTest.java @@ -17,6 +17,7 @@ import cn.iocoder.yudao.coreservice.modules.system.service.user.SysUserCoreServi import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.test.core.util.AssertUtils; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; @@ -71,6 +72,11 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest { @MockBean private SysPostService postService; + @BeforeEach + public void setUp() { + when(captchaService.isCaptchaEnable()).thenReturn(true); + } + @Test public void testLoadUserByUsername_success() { // 准备参数 @@ -83,7 +89,6 @@ public class SysAuthServiceImplTest extends BaseDbUnitTest { LoginUser loginUser = (LoginUser) authService.loadUserByUsername(username); // 校验 AssertUtils.assertPojoEquals(user, loginUser, "updateTime"); - assertNull(loginUser.getRoleIds()); // 此时不会加载角色,所以是空的 } @Test diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java index ed7eac563..164cd90d7 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/auth/SysUserSessionServiceImplTest.java @@ -73,12 +73,12 @@ public class SysUserSessionServiceImplTest extends BaseDbAndRedisUnitTest { }); userSessionMapper.insert(dbSession); // 测试 username 不匹配 - userSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { + userSessionMapper.insert(ObjectUtils.cloneIgnoreId(dbSession, o -> { o.setId(randomString()); o.setUserId(123456L); })); // 测试 userIp 不匹配 - userSessionMapper.insert(ObjectUtils.clone(dbSession, o -> { + userSessionMapper.insert(ObjectUtils.cloneIgnoreId(dbSession, o -> { o.setId(randomString()); o.setUserIp("testUserIp"); })); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysDeptServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysDeptServiceTest.java index 076a5aaf5..752982aee 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysDeptServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysDeptServiceTest.java @@ -83,9 +83,9 @@ class SysDeptServiceTest extends BaseDbUnitTest { }); deptMapper.insert(dept); // 测试 name 不匹配 - deptMapper.insert(ObjectUtils.clone(dept, o -> o.setName("发"))); + deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setName("发"))); // 测试 status 不匹配 - deptMapper.insert(ObjectUtils.clone(dept, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 准备参数 SysDeptListReqVO reqVO = new SysDeptListReqVO(); reqVO.setName("开"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysPostServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysPostServiceTest.java index c036a7ec5..c2a811f0b 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysPostServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dept/SysPostServiceTest.java @@ -49,9 +49,9 @@ class SysPostServiceTest extends BaseDbUnitTest { }); postMapper.insert(postDO); // 测试 name 不匹配 - postMapper.insert(ObjectUtils.clone(postDO, o -> o.setName("程序员"))); + postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); // 测试 status 不匹配 - postMapper.insert(ObjectUtils.clone(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 准备参数 SysPostPageReqVO reqVO = new SysPostPageReqVO(); @@ -76,9 +76,9 @@ class SysPostServiceTest extends BaseDbUnitTest { }); postMapper.insert(postDO); // 测试 name 不匹配 - postMapper.insert(ObjectUtils.clone(postDO, o -> o.setName("程序员"))); + postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setName("程序员"))); // 测试 status 不匹配 - postMapper.insert(ObjectUtils.clone(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + postMapper.insert(ObjectUtils.cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 准备参数 SysPostExportReqVO reqVO = new SysPostExportReqVO(); reqVO.setName("码"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictDataServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictDataServiceTest.java index e44024477..e69b6297e 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictDataServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictDataServiceTest.java @@ -14,17 +14,14 @@ import cn.iocoder.yudao.adminserver.modules.system.mq.producer.dict.SysDictDataP import cn.iocoder.yudao.adminserver.modules.system.service.dict.impl.SysDictDataServiceImpl; import cn.iocoder.yudao.framework.common.util.collection.ArrayUtils; import cn.iocoder.yudao.framework.common.util.object.ObjectUtils; -import com.google.common.collect.ImmutableTable; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; import javax.annotation.Resource; -import java.util.Date; import java.util.List; import java.util.function.Consumer; -import static cn.hutool.core.bean.BeanUtil.getFieldValue; import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.*; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; @@ -61,11 +58,11 @@ public class SysDictDataServiceTest extends BaseDbUnitTest { }); dictDataMapper.insert(dbDictData); // 测试 label 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setLabel("艿"))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); // 测试 dictType 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setDictType("nai"))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); // 测试 status 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 准备参数 SysDictDataPageReqVO reqVO = new SysDictDataPageReqVO(); reqVO.setLabel("芋"); @@ -90,11 +87,11 @@ public class SysDictDataServiceTest extends BaseDbUnitTest { }); dictDataMapper.insert(dbDictData); // 测试 label 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setLabel("艿"))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setLabel("艿"))); // 测试 dictType 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setDictType("nai"))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setDictType("nai"))); // 测试 status 不匹配 - dictDataMapper.insert(ObjectUtils.clone(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + dictDataMapper.insert(ObjectUtils.cloneIgnoreId(dbDictData, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 准备参数 SysDictDataExportReqVO reqVO = new SysDictDataExportReqVO(); reqVO.setLabel("芋"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictTypeServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictTypeServiceTest.java index 212701d63..8ca3638d2 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictTypeServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/dict/SysDictTypeServiceTest.java @@ -57,13 +57,13 @@ public class SysDictTypeServiceTest extends BaseDbUnitTest { }); dictTypeMapper.insert(dbDictType); // 测试 name 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setName("tudou"))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); // 测试 type 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setType("土豆"))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); // 测试 status 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 createTime 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); // 准备参数 SysDictTypePageReqVO reqVO = new SysDictTypePageReqVO(); reqVO.setName("nai"); @@ -91,13 +91,13 @@ public class SysDictTypeServiceTest extends BaseDbUnitTest { }); dictTypeMapper.insert(dbDictType); // 测试 name 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setName("tudou"))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setName("tudou"))); // 测试 type 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setType("土豆"))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setType("土豆"))); // 测试 status 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 createTime 不匹配 - dictTypeMapper.insert(ObjectUtils.clone(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); + dictTypeMapper.insert(ObjectUtils.cloneIgnoreId(dbDictType, o -> o.setCreateTime(buildTime(2021, 1, 1)))); // 准备参数 SysDictTypeExportReqVO reqVO = new SysDictTypeExportReqVO(); reqVO.setName("nai"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/errorcode/SysErrorCodeServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/errorcode/SysErrorCodeServiceTest.java index f5dbc7108..56c0b3494 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/errorcode/SysErrorCodeServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/errorcode/SysErrorCodeServiceTest.java @@ -131,15 +131,15 @@ public class SysErrorCodeServiceTest extends BaseDbUnitTest { }); errorCodeMapper.insert(dbErrorCode); // 测试 type 不匹配 - errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setType(SysErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); + errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setType(SysErrorCodeTypeEnum.MANUAL_OPERATION.getType()))); // 测试 applicationName 不匹配 - errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setApplicationName("yunai"))); + errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setApplicationName("yunai"))); // 测试 code 不匹配 - errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setCode(2))); + errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCode(2))); // 测试 message 不匹配 - errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setMessage("nai"))); + errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setMessage("nai"))); // 测试 createTime 不匹配 - errorCodeMapper.insert(ObjectUtils.clone(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); + errorCodeMapper.insert(ObjectUtils.cloneIgnoreId(dbErrorCode, o -> o.setCreateTime(buildTime(2020, 12, 12)))); return dbErrorCode; } diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java index e50141f35..820ee5167 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysLoginLogServiceImplTest.java @@ -51,13 +51,13 @@ public class SysLoginLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // 登录失败的 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setResult(SysLoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(SysLoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))); // 不同ip段的 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); // 不同username - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setUsername("yunai"))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); // 构造一个早期时间 2021-02-06 00:00:00 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); // 构造调用参数 @@ -96,13 +96,13 @@ public class SysLoginLogServiceImplTest extends BaseDbUnitTest { // 下面几个都是不匹配的数据 // 登录失败的 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setResult(SysLoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setResult(SysLoginResultEnum.CAPTCHA_CODE_ERROR.getResult()))); // 不同ip段的 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUserIp("192.168.128.18"))); // 不同username - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setUsername("yunai"))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setUsername("yunai"))); // 构造一个早期时间 2021-02-06 00:00:00 - loginLogMapper.insert(ObjectUtils.clone(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); + loginLogMapper.insert(ObjectUtils.cloneIgnoreId(loginLogDO, logDO -> logDO.setCreateTime(buildTime(2021, 2, 6)))); // 构造调用参数 diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysOperateLogServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysOperateLogServiceImplTest.java index 7523c5c5d..f375be35b 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysOperateLogServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/logger/SysOperateLogServiceImplTest.java @@ -87,15 +87,15 @@ public class SysOperateLogServiceImplTest extends BaseDbUnitTest { // 下面几个是不匹配的数据 // 随机 userId - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); // module 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setModule("user"))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); // type 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); // createTime 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); // resultCode 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setResultCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode()))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode()))); // 构造调用参数 SysOperateLogPageReqVO reqVO = new SysOperateLogPageReqVO(); @@ -138,15 +138,15 @@ public class SysOperateLogServiceImplTest extends BaseDbUnitTest { // 下面几个是不匹配的数据 // 随机 userId - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setUserId(userId + 1))); // module 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setModule("user"))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setModule("user"))); // type 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setType(OperateTypeEnum.IMPORT.getType()))); // createTime 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setStartTime(buildTime(2021, 2, 6)))); // resultCode 不同 - operateLogMapper.insert(ObjectUtils.clone(sysOperateLogDO, logDO -> logDO.setResultCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode()))); + operateLogMapper.insert(ObjectUtils.cloneIgnoreId(sysOperateLogDO, logDO -> logDO.setResultCode(GlobalErrorCodeConstants.BAD_REQUEST.getCode()))); // 构造调用参数 SysOperateLogExportReqVO reqVO = new SysOperateLogExportReqVO(); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/notice/SysNoticeServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/notice/SysNoticeServiceImplTest.java index 9b6626261..8d1325544 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/notice/SysNoticeServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/notice/SysNoticeServiceImplTest.java @@ -46,9 +46,9 @@ class SysNoticeServiceImplTest extends BaseDbUnitTest { sysNoticeMapper.insert(dbNotice); // 测试 title 不匹配 - sysNoticeMapper.insert(ObjectUtils.clone(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); + sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); // 测试 status 不匹配 - sysNoticeMapper.insert(ObjectUtils.clone(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + sysNoticeMapper.insert(ObjectUtils.cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 查询 diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionServiceTest.java index f00e16217..3dd74408d 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysPermissionServiceTest.java @@ -1,12 +1,19 @@ package cn.iocoder.yudao.adminserver.modules.system.service.permission; +import cn.hutool.core.collection.CollUtil; import cn.iocoder.yudao.adminserver.BaseDbUnitTest; +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.dept.SysDeptDO; +import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysRoleMenuDO; import cn.iocoder.yudao.adminserver.modules.system.dal.dataobject.permission.SysUserRoleDO; import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysRoleMenuMapper; import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.permission.SysUserRoleMapper; import cn.iocoder.yudao.adminserver.modules.system.mq.producer.permission.SysPermissionProducer; +import cn.iocoder.yudao.adminserver.modules.system.service.dept.SysDeptService; import cn.iocoder.yudao.adminserver.modules.system.service.permission.impl.SysPermissionServiceImpl; +import cn.iocoder.yudao.framework.datapermission.core.dept.service.dto.DeptDataPermissionRespDTO; +import cn.iocoder.yudao.framework.security.core.LoginUser; +import cn.iocoder.yudao.framework.security.core.enums.DataScopeEnum; import org.junit.jupiter.api.Test; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.context.annotation.Import; @@ -17,8 +24,12 @@ import java.util.List; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomPojo; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static java.util.Collections.singletonList; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.same; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @Import(SysPermissionServiceImpl.class) public class SysPermissionServiceTest extends BaseDbUnitTest { @@ -35,6 +46,8 @@ public class SysPermissionServiceTest extends BaseDbUnitTest { private SysRoleService roleService; @MockBean private SysMenuService menuService; + @MockBean + private SysDeptService deptService; @MockBean private SysPermissionProducer permissionProducer; @@ -106,4 +119,111 @@ public class SysPermissionServiceTest extends BaseDbUnitTest { assertPojoEquals(dbUserRoles.get(0), userRoleDO02); } + @Test // 测试从 context 获取的场景 + public void testGetDeptDataPermission_fromContext() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法 + DeptDataPermissionRespDTO respDTO = new DeptDataPermissionRespDTO(); + loginUser.setContext(SysPermissionServiceImpl.CONTEXT_KEY, respDTO); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertSame(respDTO, result); + } + + @Test + public void testGetDeptDataPermission_All() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法 + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> o.setDataScope(DataScopeEnum.ALL.getScope())); + when(roleService.getRolesFromCache(same(loginUser.getRoleIds()))).thenReturn(singletonList(roleDO)); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertTrue(result.getAll()); + assertFalse(result.getSelf()); + assertTrue(CollUtil.isEmpty(result.getDeptIds())); + assertSame(result, loginUser.getContext(SysPermissionServiceImpl.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); + } + + @Test + public void testGetDeptDataPermission_DeptCustom() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法 + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_CUSTOM.getScope())); + when(roleService.getRolesFromCache(same(loginUser.getRoleIds()))).thenReturn(singletonList(roleDO)); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertFalse(result.getAll()); + assertFalse(result.getSelf()); + assertEquals(roleDO.getDataScopeDeptIds().size() + 1, result.getDeptIds().size()); + assertTrue(CollUtil.containsAll(result.getDeptIds(), roleDO.getDataScopeDeptIds())); + assertTrue(CollUtil.contains(result.getDeptIds(), loginUser.getDeptId())); + assertSame(result, loginUser.getContext(SysPermissionServiceImpl.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); + } + + @Test + public void testGetDeptDataPermission_DeptOnly() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法 + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_ONLY.getScope())); + when(roleService.getRolesFromCache(same(loginUser.getRoleIds()))).thenReturn(singletonList(roleDO)); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertFalse(result.getAll()); + assertFalse(result.getSelf()); + assertEquals(1, result.getDeptIds().size()); + assertTrue(CollUtil.contains(result.getDeptIds(), loginUser.getDeptId())); + assertSame(result, loginUser.getContext(SysPermissionServiceImpl.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); + } + + @Test + public void testGetDeptDataPermission_DeptAndChild() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法(角色) + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> o.setDataScope(DataScopeEnum.DEPT_AND_CHILD.getScope())); + when(roleService.getRolesFromCache(same(loginUser.getRoleIds()))).thenReturn(singletonList(roleDO)); + // mock 方法(部门) + SysDeptDO deptDO = randomPojo(SysDeptDO.class); + when(deptService.getDeptsByParentIdFromCache(eq(loginUser.getDeptId()), eq(true))) + .thenReturn(singletonList(deptDO)); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertFalse(result.getAll()); + assertFalse(result.getSelf()); + assertEquals(1, result.getDeptIds().size()); + assertTrue(CollUtil.contains(result.getDeptIds(), deptDO.getId())); + assertSame(result, loginUser.getContext(SysPermissionServiceImpl.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); + } + + @Test + public void testGetDeptDataPermission_Self() { + // 准备参数 + LoginUser loginUser = randomPojo(LoginUser.class); + // mock 方法 + SysRoleDO roleDO = randomPojo(SysRoleDO.class, o -> o.setDataScope(DataScopeEnum.SELF.getScope())); + when(roleService.getRolesFromCache(same(loginUser.getRoleIds()))).thenReturn(singletonList(roleDO)); + + // 调用 + DeptDataPermissionRespDTO result = permissionService.getDeptDataPermission(loginUser); + // 断言 + assertFalse(result.getAll()); + assertTrue(result.getSelf()); + assertTrue(CollUtil.isEmpty(result.getDeptIds())); + assertSame(result, loginUser.getContext(SysPermissionServiceImpl.CONTEXT_KEY, DeptDataPermissionRespDTO.class)); + } + } diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysRoleServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysRoleServiceTest.java index 3b39e976c..08bd6521b 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysRoleServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/permission/SysRoleServiceTest.java @@ -133,11 +133,11 @@ public class SysRoleServiceTest extends BaseDbUnitTest { //调用 Set deptIdSet = Arrays.asList(1L, 2L, 3L, 4L, 5L).stream().collect(Collectors.toSet()); - sysRoleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScore(), deptIdSet); + sysRoleService.updateRoleDataScope(roleId, DataScopeEnum.DEPT_CUSTOM.getScope(), deptIdSet); //断言 SysRoleDO newRoleDO = roleMapper.selectById(roleId); - assertEquals(DataScopeEnum.DEPT_CUSTOM.getScore(), newRoleDO.getDataScope()); + assertEquals(DataScopeEnum.DEPT_CUSTOM.getScope(), newRoleDO.getDataScope()); Set newDeptIdSet = newRoleDO.getDataScopeDeptIds(); assertTrue(deptIdSet.size() == newDeptIdSet.size()); @@ -242,7 +242,7 @@ public class SysRoleServiceTest extends BaseDbUnitTest { o.setCode("code"); o.setType(SysRoleTypeEnum.CUSTOM.getType()); o.setStatus(1); - o.setDataScope(DataScopeEnum.ALL.getScore()); + o.setDataScope(DataScopeEnum.ALL.getScope()); }); roleMapper.insert(roleDO); @@ -293,7 +293,7 @@ public class SysRoleServiceTest extends BaseDbUnitTest { o.setName(name); o.setType(typeEnum.getType()); o.setStatus(status); - o.setDataScope(scopeEnum.getScore()); + o.setDataScope(scopeEnum.getScope()); o.setCode(code); }); return roleDO; diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsChannelServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsChannelServiceTest.java index 38499d2c4..444275120 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsChannelServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsChannelServiceTest.java @@ -169,11 +169,11 @@ public class SysSmsChannelServiceTest extends BaseDbUnitTest { }); smsChannelMapper.insert(dbSmsChannel); // 测试 signature 不匹配 - smsChannelMapper.insert(ObjectUtils.clone(dbSmsChannel, o -> o.setSignature("源码"))); + smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setSignature("源码"))); // 测试 status 不匹配 - smsChannelMapper.insert(ObjectUtils.clone(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 createTime 不匹配 - smsChannelMapper.insert(ObjectUtils.clone(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11)))); + smsChannelMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsChannel, o -> o.setCreateTime(buildTime(2020, 11, 11)))); // 准备参数 SysSmsChannelPageReqVO reqVO = new SysSmsChannelPageReqVO(); reqVO.setSignature("芋道"); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsLogServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsLogServiceTest.java index 23ab395b7..fdeed2aa8 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsLogServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsLogServiceTest.java @@ -57,19 +57,19 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest { }); smsLogMapper.insert(dbSmsLog); // 测试 channelId 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setChannelId(2L))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); // 测试 templateId 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setTemplateId(20L))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); // 测试 mobile 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setMobile("18818260999"))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); // 测试 sendStatus 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setSendStatus(SysSmsSendStatusEnum.IGNORE.getStatus()))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SysSmsSendStatusEnum.IGNORE.getStatus()))); // 测试 sendTime 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); // 测试 receiveStatus 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setReceiveStatus(SysSmsReceiveStatusEnum.SUCCESS.getStatus()))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SysSmsReceiveStatusEnum.SUCCESS.getStatus()))); // 测试 receiveTime 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); // 准备参数 SysSmsLogPageReqVO reqVO = new SysSmsLogPageReqVO(); reqVO.setChannelId(1L); @@ -104,19 +104,19 @@ public class SysSmsLogServiceTest extends BaseDbUnitTest { }); smsLogMapper.insert(dbSmsLog); // 测试 channelId 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setChannelId(2L))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setChannelId(2L))); // 测试 templateId 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setTemplateId(20L))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setTemplateId(20L))); // 测试 mobile 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setMobile("18818260999"))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setMobile("18818260999"))); // 测试 sendStatus 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setSendStatus(SysSmsSendStatusEnum.IGNORE.getStatus()))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendStatus(SysSmsSendStatusEnum.IGNORE.getStatus()))); // 测试 sendTime 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setSendTime(buildTime(2020, 12, 12)))); // 测试 receiveStatus 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setReceiveStatus(SysSmsReceiveStatusEnum.SUCCESS.getStatus()))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveStatus(SysSmsReceiveStatusEnum.SUCCESS.getStatus()))); // 测试 receiveTime 不匹配 - smsLogMapper.insert(ObjectUtils.clone(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); + smsLogMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsLog, o -> o.setReceiveTime(buildTime(2021, 12, 12)))); // 准备参数 SysSmsLogExportReqVO reqVO = new SysSmsLogExportReqVO(); reqVO.setChannelId(1L); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsTemplateServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsTemplateServiceTest.java index b751fe499..a893ee296 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsTemplateServiceTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/sms/SysSmsTemplateServiceTest.java @@ -190,19 +190,19 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest { }); smsTemplateMapper.insert(dbSmsTemplate); // 测试 type 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setType(SysSmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setType(SysSmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); // 测试 status 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 code 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCode("yuanma"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCode("yuanma"))); // 测试 content 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setContent("源码"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setContent("源码"))); // 测试 apiTemplateId 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); // 测试 channelId 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setChannelId(2L))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setChannelId(2L))); // 测试 createTime 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); // 准备参数 SysSmsTemplatePageReqVO reqVO = new SysSmsTemplatePageReqVO(); reqVO.setType(SysSmsTemplateTypeEnum.PROMOTION.getType()); @@ -236,19 +236,19 @@ public class SysSmsTemplateServiceTest extends BaseDbUnitTest { }); smsTemplateMapper.insert(dbSmsTemplate); // 测试 type 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setType(SysSmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setType(SysSmsTemplateTypeEnum.VERIFICATION_CODE.getType()))); // 测试 status 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 code 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCode("yuanma"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCode("yuanma"))); // 测试 content 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setContent("源码"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setContent("源码"))); // 测试 apiTemplateId 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setApiTemplateId("nai"))); // 测试 channelId 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setChannelId(2L))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setChannelId(2L))); // 测试 createTime 不匹配 - smsTemplateMapper.insert(ObjectUtils.clone(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); + smsTemplateMapper.insert(ObjectUtils.cloneIgnoreId(dbSmsTemplate, o -> o.setCreateTime(buildTime(2021, 12, 12)))); // 准备参数 SysSmsTemplateExportReqVO reqVO = new SysSmsTemplateExportReqVO(); reqVO.setType(SysSmsTemplateTypeEnum.PROMOTION.getType()); diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantServiceTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantServiceTest.java new file mode 100644 index 000000000..abb16e80e --- /dev/null +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/tenant/SysTenantServiceTest.java @@ -0,0 +1,181 @@ +package cn.iocoder.yudao.adminserver.modules.system.service.tenant; + +import cn.iocoder.yudao.adminserver.BaseDbUnitTest; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantCreateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantExportReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantPageReqVO; +import cn.iocoder.yudao.adminserver.modules.system.controller.tenant.vo.SysTenantUpdateReqVO; +import cn.iocoder.yudao.adminserver.modules.system.dal.mysql.tenant.SysTenantMapper; +import cn.iocoder.yudao.adminserver.modules.system.service.tenant.impl.SysTenantServiceImpl; +import cn.iocoder.yudao.coreservice.modules.system.dal.dataobject.tenant.SysTenantDO; +import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.Import; + +import javax.annotation.Resource; +import java.util.List; + +import static cn.iocoder.yudao.adminserver.modules.system.enums.SysErrorCodeConstants.TENANT_NOT_EXISTS; +import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildTime; +import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; +import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*; +import static org.junit.jupiter.api.Assertions.*; + +/** + * {@link SysTenantServiceImpl} 的单元测试类 + * + * @author 芋道源码 + */ +@Import(SysTenantServiceImpl.class) +public class SysTenantServiceTest extends BaseDbUnitTest { + + @Resource + private SysTenantServiceImpl tenantService; + + @Resource + private SysTenantMapper tenantMapper; + + @Test + public void testCreateTenant_success() { + // 准备参数 + SysTenantCreateReqVO reqVO = randomPojo(SysTenantCreateReqVO.class, o -> o.setStatus(randomCommonStatus())); + + // 调用 + Long tenantId = tenantService.createTenant(reqVO); + // 断言 + assertNotNull(tenantId); + // 校验记录的属性是否正确 + SysTenantDO tenant = tenantMapper.selectById(tenantId); + assertPojoEquals(reqVO, tenant); + } + + @Test + public void testUpdateTenant_success() { + // mock 数据 + SysTenantDO dbTenant = randomPojo(SysTenantDO.class, o -> o.setStatus(randomCommonStatus())); + tenantMapper.insert(dbTenant);// @Sql: 先插入出一条存在的数据 + // 准备参数 + SysTenantUpdateReqVO reqVO = randomPojo(SysTenantUpdateReqVO.class, o -> { + o.setId(dbTenant.getId()); // 设置更新的 ID + o.setStatus(randomCommonStatus()); + }); + + // 调用 + tenantService.updateTenant(reqVO); + // 校验是否更新正确 + SysTenantDO tenant = tenantMapper.selectById(reqVO.getId()); // 获取最新的 + assertPojoEquals(reqVO, tenant); + } + + @Test + public void testUpdateTenant_notExists() { + // 准备参数 + SysTenantUpdateReqVO reqVO = randomPojo(SysTenantUpdateReqVO.class); + + // 调用, 并断言异常 + assertServiceException(() -> tenantService.updateTenant(reqVO), TENANT_NOT_EXISTS); + } + + @Test + public void testDeleteTenant_success() { + // mock 数据 + SysTenantDO dbTenant = randomPojo(SysTenantDO.class, + o -> o.setStatus(randomCommonStatus())); + tenantMapper.insert(dbTenant);// @Sql: 先插入出一条存在的数据 + // 准备参数 + Long id = dbTenant.getId(); + + // 调用 + tenantService.deleteTenant(id); + // 校验数据不存在了 + assertNull(tenantMapper.selectById(id)); + } + + @Test + public void testDeleteTenant_notExists() { + // 准备参数 + Long id = randomLongId(); + + // 调用, 并断言异常 + assertServiceException(() -> tenantService.deleteTenant(id), TENANT_NOT_EXISTS); + } + + @Test + public void testGetTenantPage() { + // mock 数据 + SysTenantDO dbTenant = randomPojo(SysTenantDO.class, o -> { // 等会查询到 + o.setName("芋道源码"); + o.setContactName("芋艿"); + o.setContactMobile("15601691300"); + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + o.setCreateTime(buildTime(2020, 12, 12)); + }); + tenantMapper.insert(dbTenant); + // 测试 name 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setName(randomString()))); + // 测试 contactName 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setContactName(randomString()))); + // 测试 contactMobile 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setContactMobile(randomString()))); + // 测试 status 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + // 测试 createTime 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildTime(2021, 12, 12)))); + // 准备参数 + SysTenantPageReqVO reqVO = new SysTenantPageReqVO(); + reqVO.setName("芋道"); + reqVO.setContactName("艿"); + reqVO.setContactMobile("1560"); + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + reqVO.setBeginCreateTime(buildTime(2020, 12, 1)); + reqVO.setEndCreateTime(buildTime(2020, 12, 24)); + + // 调用 + PageResult pageResult = tenantService.getTenantPage(reqVO); + // 断言 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbTenant, pageResult.getList().get(0)); + } + + @Test + public void testGetTenantList() { + // mock 数据 + SysTenantDO dbTenant = randomPojo(SysTenantDO.class, o -> { // 等会查询到 + o.setName("芋道源码"); + o.setContactName("芋艿"); + o.setContactMobile("15601691300"); + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + o.setCreateTime(buildTime(2020, 12, 12)); + }); + tenantMapper.insert(dbTenant); + // 测试 name 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setName(randomString()))); + // 测试 contactName 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setContactName(randomString()))); + // 测试 contactMobile 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setContactMobile(randomString()))); + // 测试 status 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + // 测试 createTime 不匹配 + tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildTime(2021, 12, 12)))); + // 准备参数 + SysTenantExportReqVO reqVO = new SysTenantExportReqVO(); + reqVO.setName("芋道"); + reqVO.setContactName("艿"); + reqVO.setContactMobile("1560"); + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + reqVO.setBeginCreateTime(buildTime(2020, 12, 1)); + reqVO.setEndCreateTime(buildTime(2020, 12, 24)); + + // 调用 + List list = tenantService.getTenantList(reqVO); + // 断言 + assertEquals(1, list.size()); + assertPojoEquals(dbTenant, list.get(0)); + } + +} diff --git a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java index 70751eeac..75472ccf1 100644 --- a/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java +++ b/yudao-admin-server/src/test/java/cn/iocoder/yudao/adminserver/modules/system/service/user/SysUserServiceImplTest.java @@ -309,15 +309,15 @@ public class SysUserServiceImplTest extends BaseDbUnitTest { }); userMapper.insert(dbUser); // 测试 username 不匹配 - userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setUsername("yuanma"))); + userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setUsername("yuanma"))); // 测试 mobile 不匹配 - userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setMobile("18818260888"))); + userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setMobile("18818260888"))); // 测试 status 不匹配 - userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); // 测试 createTime 不匹配 - userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setCreateTime(buildTime(2020, 11, 11)))); + userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setCreateTime(buildTime(2020, 11, 11)))); // 测试 dept 不匹配 - userMapper.insert(ObjectUtils.clone(dbUser, o -> o.setDeptId(0L))); + userMapper.insert(ObjectUtils.cloneIgnoreId(dbUser, o -> o.setDeptId(0L))); return dbUser; } diff --git a/yudao-admin-server/src/test/resources/sql/clean.sql b/yudao-admin-server/src/test/resources/sql/clean.sql index 75080b343..d89c5552e 100644 --- a/yudao-admin-server/src/test/resources/sql/clean.sql +++ b/yudao-admin-server/src/test/resources/sql/clean.sql @@ -24,3 +24,4 @@ DELETE FROM "sys_sms_template"; DELETE FROM "sys_sms_log"; DELETE FROM "sys_error_code"; DELETE FROM "sys_social_user"; +DELETE FROM "sys_tenant"; diff --git a/yudao-admin-server/src/test/resources/sql/create_tables.sql b/yudao-admin-server/src/test/resources/sql/create_tables.sql index db67cad23..93bfd5587 100644 --- a/yudao-admin-server/src/test/resources/sql/create_tables.sql +++ b/yudao-admin-server/src/test/resources/sql/create_tables.sql @@ -1,7 +1,7 @@ -- inf 开头的 DB CREATE TABLE IF NOT EXISTS "inf_config" ( - "id" int NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "id" bigint(20) NOT NULL GENERATED BY DEFAULT AS IDENTITY, "group" varchar(50) NOT NULL, "type" tinyint NOT NULL, "name" varchar(100) NOT NULL DEFAULT '', @@ -26,6 +26,7 @@ CREATE TABLE IF NOT EXISTS "inf_file" ( "updater" varchar(64) DEFAULT '', "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, + "tenant_id" bigint not null default '0', PRIMARY KEY ("id") ) COMMENT '文件表'; @@ -82,6 +83,7 @@ CREATE TABLE IF NOT EXISTS "sys_dept" ( "updater" varchar(64) DEFAULT '', "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, + "tenant_id" bigint not null default '0', PRIMARY KEY ("id") ) COMMENT '部门表'; @@ -189,6 +191,7 @@ CREATE TABLE IF NOT EXISTS `sys_user_session` ( `updater` varchar(64) DEFAULT '' , "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, + "tenant_id" bigint not null default '0', PRIMARY KEY (`id`) ) COMMENT '用户在线 Session'; @@ -204,6 +207,7 @@ CREATE TABLE IF NOT EXISTS "sys_post" ( "updater" varchar(64) DEFAULT '', "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, "deleted" bit NOT NULL DEFAULT FALSE, + "tenant_id" bigint not null default '0', PRIMARY KEY ("id") ) COMMENT '岗位信息表'; @@ -218,7 +222,8 @@ CREATE TABLE IF NOT EXISTS "sys_notice" ( "updater" varchar(64) DEFAULT '' COMMENT '更新者', "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', "deleted" bit NOT NULL DEFAULT 0 COMMENT '是否删除', - PRIMARY KEY("id") + "tenant_id" bigint not null default '0', + PRIMARY KEY("id") ) COMMENT '通知公告表'; CREATE TABLE IF NOT EXISTS `sys_login_log` ( @@ -264,6 +269,7 @@ CREATE TABLE IF NOT EXISTS `sys_operate_log` ( `updater` varchar(64) DEFAULT '', `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, `deleted` bit(1) NOT NULL DEFAULT '0', + "tenant_id" bigint not null default '0', PRIMARY KEY (`id`) ) COMMENT ='操作日志记录'; @@ -287,6 +293,7 @@ CREATE TABLE IF NOT EXISTS "sys_user" ( "updater" varchar(64) default '', "update_time" timestamp not null default current_timestamp, "deleted" bit not null default false, + "tenant_id" bigint not null default '0', primary key ("id") ) comment '用户信息表'; @@ -311,11 +318,12 @@ CREATE TABLE IF NOT EXISTS "inf_api_access_log" ( "updater" varchar(64) default '', "update_time" timestamp not null default current_timestamp, "deleted" bit not null default false, + "tenant_id" bigint not null default '0', primary key ("id") ) COMMENT 'API 访问日志表'; CREATE TABLE IF NOT EXISTS "inf_api_error_log" ( - "id" integer not null GENERATED BY DEFAULT AS IDENTITY, + "id" bigint not null GENERATED BY DEFAULT AS IDENTITY, "trace_id" varchar(64) not null, "user_id" bigint not null default '0', "user_type" tinyint not null default '0', @@ -342,6 +350,7 @@ CREATE TABLE IF NOT EXISTS "inf_api_error_log" ( "updater" varchar(64) default '', "update_time" timestamp not null default current_timestamp, "deleted" bit not null default false, + "tenant_id" bigint not null default '0', primary key ("id") ) COMMENT '系统异常日志'; @@ -449,3 +458,17 @@ CREATE TABLE IF NOT EXISTS "sys_social_user" ( "deleted" bit NOT NULL DEFAULT FALSE, PRIMARY KEY ("id") ) COMMENT '社交用户'; + +CREATE TABLE IF NOT EXISTS "sys_tenant" ( + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "name" varchar(63) NOT NULL, + "contact_name" varchar(255) NOT NULL, + "contact_mobile" varchar(255), + "status" tinyint NOT NULL, + "creator" varchar(64) DEFAULT '', + "create_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "updater" varchar(64) DEFAULT '', + "update_time" timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT FALSE, + PRIMARY KEY ("id") +) COMMENT '租户'; diff --git a/yudao-admin-ui/src/api/system/tenant.js b/yudao-admin-ui/src/api/system/tenant.js new file mode 100755 index 000000000..8c45a38da --- /dev/null +++ b/yudao-admin-ui/src/api/system/tenant.js @@ -0,0 +1,54 @@ +import request from '@/utils/request' + +// 创建租户 +export function createTenant(data) { + return request({ + url: '/system/tenant/create', + method: 'post', + data: data + }) +} + +// 更新租户 +export function updateTenant(data) { + return request({ + url: '/system/tenant/update', + method: 'put', + data: data + }) +} + +// 删除租户 +export function deleteTenant(id) { + return request({ + url: '/system/tenant/delete?id=' + id, + method: 'delete' + }) +} + +// 获得租户 +export function getTenant(id) { + return request({ + url: '/system/tenant/get?id=' + id, + method: 'get' + }) +} + +// 获得租户分页 +export function getTenantPage(query) { + return request({ + url: '/system/tenant/page', + method: 'get', + params: query + }) +} + +// 导出租户 Excel +export function exportTenantExcel(query) { + return request({ + url: '/system/tenant/export-excel', + method: 'get', + params: query, + responseType: 'blob' + }) +} diff --git a/yudao-admin-ui/src/utils/request.js b/yudao-admin-ui/src/utils/request.js index c063ec300..4daa6e464 100644 --- a/yudao-admin-ui/src/utils/request.js +++ b/yudao-admin-ui/src/utils/request.js @@ -3,6 +3,7 @@ import { Notification, MessageBox, Message } from 'element-ui' import store from '@/store' import { getToken } from '@/utils/auth' import errorCode from '@/utils/errorCode' +import Cookies from "js-cookie"; axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' // 创建axios实例 @@ -19,6 +20,11 @@ service.interceptors.request.use(config => { if (getToken() && !isToken) { config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 } + // 设置租户 + const tenantId = Cookies.get('tenantId'); + if (tenantId) { + config.headers['tenant-id'] = tenantId; + } // get请求映射params参数 if (config.method === 'get' && config.params) { let url = config.url + '?'; diff --git a/yudao-admin-ui/src/views/login.vue b/yudao-admin-ui/src/views/login.vue index d963e24c3..f8a3f72a0 100644 --- a/yudao-admin-ui/src/views/login.vue +++ b/yudao-admin-ui/src/views/login.vue @@ -2,6 +2,11 @@