Skip to content

Commit 91ebd34

Browse files
authored
feat: Add Multi-Tenant Support with Context Isolation, Improve Token Handling, and Update Tests (opentiny#278)
1 parent 0afb11f commit 91ebd34

File tree

12 files changed

+101
-47
lines changed

12 files changed

+101
-47
lines changed

base/src/main/java/com/tinyengine/it/common/context/LoginUserContext.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@
2121
*/
2222
public interface LoginUserContext {
2323
/**
24-
* 返回当前用户所诉的业务租户信息
25-
* @return 租户ID
24+
* 返回当前用户所在的业务租户id
25+
* @return 租户Id
26+
*/
27+
String getTenantId();
28+
29+
/**
30+
* 返回当前用户所在的业务租户信息
31+
* @return 租户
2632
*/
2733
List<Tenant> getTenants();
2834

base/src/main/java/com/tinyengine/it/login/config/SSOInterceptor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ public class SSOInterceptor implements HandlerInterceptor {
4141
public boolean preHandle(HttpServletRequest request,
4242
HttpServletResponse response, Object handler) throws Exception {
4343

44-
String token = request.getHeader("Authorization");
45-
String requestURI = request.getRequestURI();
46-
47-
log.info("Intercepting: {}, Token: {}", requestURI, token != null ? "present" : "null");
48-
44+
String authorization = request.getHeader("Authorization");
4945
// 如果没有token,重定向到登录页
50-
if (token == null || token.isEmpty()) {
46+
if (authorization == null || authorization.isEmpty()) {
5147
log.info("No token, redirecting to: {}", SSO_SERVER);
5248
response.sendRedirect(SSO_SERVER);
5349
return false;
5450
}
51+
String token = jwtUtil.getTokenFromRequest(authorization);
52+
String requestURI = request.getRequestURI();
53+
54+
log.info("Intercepting: {}, Token: {}", requestURI, token != null ? "present" : "null");
5555

5656
try {
5757
// 验证token

base/src/main/java/com/tinyengine/it/login/config/context/DefaultLoginUserContext.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,23 @@ public class DefaultLoginUserContext implements LoginUserContext {
1616
private static final ThreadLocal<UserInfo> CURRENT_USER = new ThreadLocal<>();
1717

1818
private static final int DEFAULT_PLATFORM = 1;
19+
private static final String DEFAULT_TENANT = "1";
20+
21+
/**
22+
* 返回当前用户所在的业务租户id
23+
*
24+
* @return 租户Id
25+
*/
26+
@Override
27+
public String getTenantId() {
28+
UserInfo userInfo = CURRENT_USER.get();
29+
List<Tenant> tenantList = userInfo != null ? userInfo.getTenants() : null;
30+
if (tenantList == null || tenantList.isEmpty()) {
31+
return DEFAULT_TENANT;
32+
}
33+
34+
return tenantList.get(0).getId();
35+
}
1936

2037
@Override
2138
public List<Tenant> getTenants() {

base/src/main/java/com/tinyengine/it/mapper/AppMapper.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public interface AppMapper extends BaseMapper<App> {
3131
*
3232
* @return the list
3333
*/
34-
List<App> queryAllApp();
34+
List<App> queryAllApp(String tenantId);
3535

3636
/**
3737
* 分页查询应用所有信息
@@ -45,24 +45,24 @@ public interface AppMapper extends BaseMapper<App> {
4545
* @param createdBy the createdBy
4646
* @return the list
4747
*/
48-
List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name,
49-
Integer industryId, Integer sceneId, String framework, String orderBy, String createdBy);
48+
List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name, Integer industryId,
49+
Integer sceneId, String framework, String orderBy, String createdBy, String tenantId);
5050

5151
/**
5252
* 查询表t_app 应用总数
5353
*
5454
* @return the int
5555
*/
56-
@Select("SELECT COUNT(id) FROM t_app WHERE is_template IS NOT TRUE")
57-
int queryAppTotal();
56+
@Select("SELECT COUNT(id) FROM t_app WHERE tenant_id = #{tenantId} AND is_template IS NOT TRUE")
57+
int queryAppTotal(String tenantId);
5858

5959
/**
6060
* 查询表t_app 模版总数
6161
*
6262
* @return the int
6363
*/
64-
@Select("SELECT COUNT(id) FROM t_app WHERE is_template = TRUE")
65-
int queryAppTemplateTotal();
64+
@Select("SELECT COUNT(id) FROM t_app WHERE tenant_id = #{tenantId} AND is_template = TRUE")
65+
int queryAppTemplateTotal(String tenantId);
6666

6767
/**
6868
* 分页查询应用模版所有信息
@@ -76,24 +76,24 @@ List<App> queryAllAppByPage(Integer pageSize, Integer offset, String name,
7676
* @param createdBy the createdBy
7777
* @return the list
7878
*/
79-
List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name,
80-
Integer industryId, Integer sceneId, String framework, String orderBy, String createdBy);
79+
List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name, Integer industryId,
80+
Integer sceneId, String framework, String orderBy, String createdBy, String tenantId);
8181

8282
/**
8383
* 根据主键id查询应用模版数据
8484
*
8585
* @param id the id
8686
* @return the app
8787
*/
88-
App queryAppTemplateById(Integer id);
88+
App queryAppTemplateById(Integer id, String tenantId);
8989

9090
/**
9191
* 根据主键id查询表t_app数据
9292
*
9393
* @param id the id
9494
* @return the app
9595
*/
96-
App queryAppById(Integer id);
96+
App queryAppById(Integer id, String tenantId);
9797

9898
/**
9999
* 根据条件查询表t_app数据
@@ -109,7 +109,7 @@ List<App> queryAllAppTemplate(Integer pageSize, Integer offset, String name,
109109
* @param id the id
110110
* @return the integer
111111
*/
112-
Integer deleteAppById(@Param("id") Integer id);
112+
Integer deleteAppById(@Param("id") Integer id, String tenantId);
113113

114114
/**
115115
* 根据主键id更新表t_app数据

base/src/main/java/com/tinyengine/it/service/app/impl/AppServiceImpl.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
1616
import com.tinyengine.it.common.base.Result;
17+
import com.tinyengine.it.common.context.LoginUserContext;
1718
import com.tinyengine.it.common.enums.Enums;
1819
import com.tinyengine.it.common.exception.ExceptionEnum;
1920
import com.tinyengine.it.common.log.SystemServiceLog;
@@ -28,6 +29,7 @@
2829
import com.tinyengine.it.model.entity.App;
2930
import com.tinyengine.it.model.entity.I18nEntry;
3031
import com.tinyengine.it.model.entity.Platform;
32+
import com.tinyengine.it.model.entity.Tenant;
3133
import com.tinyengine.it.service.app.AppService;
3234
import com.tinyengine.it.service.app.I18nEntryService;
3335
import com.tinyengine.it.service.app.impl.v1.AppV1ServiceImpl;
@@ -74,14 +76,17 @@ public class AppServiceImpl extends ServiceImpl<AppMapper, App> implements AppSe
7476
@Autowired
7577
private AppV1ServiceImpl appV1ServiceImpl;
7678

79+
@Autowired
80+
private LoginUserContext loginUserContext;
81+
7782
/**
7883
* 查询表t_app所有数据
7984
*
8085
* @return App
8186
*/
8287
@Override
8388
public List<App> queryAllApp() {
84-
return baseMapper.queryAllApp();
89+
return baseMapper.queryAllApp(loginUserContext.getTenantId());
8590
}
8691

8792
/**
@@ -105,8 +110,9 @@ public AppDto queryAllAppByPage(Integer currentPage, Integer pageSize, String or
105110
}
106111
int offset = (currentPage - 1) * pageSize;
107112
List<App> apps = this.baseMapper.queryAllAppByPage(pageSize, offset, app.getName(),
108-
app.getIndustryId(), app.getSceneId(), app.getFramework(), orderBy, app.getCreatedBy());
109-
Integer total = this.baseMapper.queryAppTotal();
113+
app.getIndustryId(), app.getSceneId(), app.getFramework(), orderBy, app.getCreatedBy(),
114+
loginUserContext.getTenantId());
115+
Integer total = this.baseMapper.queryAppTotal(loginUserContext.getTenantId());
110116
AppDto appDto = new AppDto();
111117
appDto.setApps(apps);
112118
appDto.setTotal(total);
@@ -122,7 +128,7 @@ public AppDto queryAllAppByPage(Integer currentPage, Integer pageSize, String or
122128
@Override
123129
@SystemServiceLog(description = "通过id查询应用实现方法")
124130
public Result<App> queryAppById(Integer id) {
125-
App app = baseMapper.queryAppById(id);
131+
App app = baseMapper.queryAppById(id, loginUserContext.getTenantId());
126132
if (app == null) {
127133
return Result.failed(ExceptionEnum.CM009);
128134
}
@@ -149,8 +155,8 @@ public List<App> queryAppByCondition(App app) {
149155
@Override
150156
@SystemServiceLog(description = "应用删除实现方法")
151157
public Result<App> deleteAppById(Integer id) {
152-
App app = baseMapper.queryAppById(id);
153-
int result = baseMapper.deleteAppById(id);
158+
App app = baseMapper.queryAppById(id, loginUserContext.getTenantId());
159+
int result = baseMapper.deleteAppById(id, loginUserContext.getTenantId());
154160
if (result < 1) {
155161
return Result.failed(ExceptionEnum.CM009);
156162
}
@@ -168,7 +174,7 @@ public Result<App> deleteAppById(Integer id) {
168174
public Result<App> updateAppById(App app) {
169175
// 如果更新extend_config字段,从platform获取数据,继承非route部分
170176
if (app.getExtendConfig() != null && !app.getExtendConfig().isEmpty()) {
171-
App appResult = baseMapper.queryAppById(app.getId());
177+
App appResult = baseMapper.queryAppById(app.getId(), loginUserContext.getTenantId());
172178
Platform platform = platformService.queryPlatformById(appResult.getPlatformId());
173179
Map<String, Object> appExtendConfig = platform.getAppExtendConfig();
174180
appExtendConfig.remove("route");
@@ -178,7 +184,7 @@ public Result<App> updateAppById(App app) {
178184
if (result < 1) {
179185
return Result.failed(ExceptionEnum.CM001);
180186
}
181-
App selectedApp = baseMapper.queryAppById(app.getId());
187+
App selectedApp = baseMapper.queryAppById(app.getId(), loginUserContext.getTenantId());
182188
return Result.success(selectedApp);
183189
}
184190

base/src/main/java/com/tinyengine/it/service/app/impl/AppTemplateServiceImpl.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import cn.hutool.core.bean.BeanUtil;
1616
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
1717
import com.tinyengine.it.common.base.Result;
18+
import com.tinyengine.it.common.context.LoginUserContext;
1819
import com.tinyengine.it.common.exception.ExceptionEnum;
1920
import com.tinyengine.it.common.exception.ServiceException;
2021
import com.tinyengine.it.mapper.AppExtensionMapper;
@@ -91,6 +92,9 @@ public class AppTemplateServiceImpl extends ServiceImpl<AppMapper, App> implemen
9192
@Autowired
9293
private ModelMapper modelMapper;
9394

95+
@Autowired
96+
private LoginUserContext loginUserContext;
97+
9498
/**
9599
* 分页查询应用模版所有信息
96100
* @param currentPage the currentPage
@@ -113,8 +117,9 @@ public AppDto queryAllAppTemplate(Integer currentPage, Integer pageSize, String
113117
int offset = (currentPage - 1) * pageSize;
114118

115119
List<App> apps = this.baseMapper.queryAllAppTemplate(pageSize, offset, app.getName(),
116-
app.getIndustryId(), app.getSceneId(), app.getFramework(), orderBy, app.getCreatedBy());
117-
Integer total = this.baseMapper.queryAppTemplateTotal();
120+
app.getIndustryId(), app.getSceneId(), app.getFramework(), orderBy, app.getCreatedBy(),
121+
loginUserContext.getTenantId());
122+
Integer total = this.baseMapper.queryAppTemplateTotal(loginUserContext.getTenantId());
118123
AppDto appDto = new AppDto();
119124
appDto.setApps(apps);
120125
appDto.setTotal(total);
@@ -129,7 +134,7 @@ public AppDto queryAllAppTemplate(Integer currentPage, Integer pageSize, String
129134
*/
130135
@Override
131136
public Result<App> queryAppTemplateById(Integer id) {
132-
App app = baseMapper.queryAppTemplateById(id);
137+
App app = baseMapper.queryAppTemplateById(id, loginUserContext.getTenantId());
133138
if (app == null) {
134139
return Result.failed(ExceptionEnum.CM009);
135140
}
@@ -157,7 +162,7 @@ public App createAppByTemplate(App app) {
157162
throw new ServiceException(ExceptionEnum.CM001.getResultCode(), ExceptionEnum.CM001.getResultMsg());
158163
}
159164
copyData(templateId, app.getId());
160-
return appMapper.queryAppById(app.getId());
165+
return appMapper.queryAppById(app.getId(), loginUserContext.getTenantId());
161166
}
162167

163168
private void copyData(int templateId, int appId) {

base/src/main/java/com/tinyengine/it/service/app/impl/PageServiceImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public List<Page> queryAllPage(Integer aid) {
177177
public Page queryPageById(Integer id) {
178178
Page pageInfo = baseMapper.queryPageById(id);
179179
// 获取schemaMeta进行获取materialHistory中的framework进行判断
180-
String framework = appMapper.queryAppById(pageInfo.getApp()).getFramework();
180+
String framework = appMapper.queryAppById(pageInfo.getApp(), loginUserContext.getTenantId()).getFramework();
181181
if (framework.isEmpty()) {
182182
throw new ServiceException(ExceptionEnum.CM312.getResultCode(), ExceptionEnum.CM312.getResultMsg());
183183
}
@@ -495,7 +495,7 @@ public Page addIsHome(Page pageInfo) {
495495
* @return the app home page id
496496
*/
497497
public int getAppHomePageId(int appId) {
498-
App appInfo = appMapper.queryAppById(appId);
498+
App appInfo = appMapper.queryAppById(appId, loginUserContext.getTenantId());
499499
// appHomePageId 存在为null的情况,即app没有设置首页
500500
Integer homePage = appInfo.getHomePage();
501501

base/src/main/java/com/tinyengine/it/service/app/impl/v1/AppV1ServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import static com.tinyengine.it.common.utils.Utils.findMaxVersion;
1717

1818
import com.fasterxml.jackson.core.type.TypeReference;
19+
import com.tinyengine.it.common.context.LoginUserContext;
1920
import com.tinyengine.it.common.exception.ServiceException;
2021
import com.tinyengine.it.common.log.SystemServiceLog;
2122
import com.tinyengine.it.common.utils.Schema;
@@ -142,6 +143,9 @@ public class AppV1ServiceImpl implements AppV1Service {
142143
@Autowired
143144
private ComponentLibraryMapper componentLibraryMapper;
144145

146+
@Autowired
147+
private LoginUserContext loginUserContext;
148+
145149
/**
146150
* 获取应用schema
147151
*
@@ -275,7 +279,7 @@ private List<PackagesDto> getPackages() {
275279
* @return the meta
276280
*/
277281
public MetaDto getMetaDto(Integer id) {
278-
App app = appMapper.queryAppById(id);
282+
App app = appMapper.queryAppById(id, loginUserContext.getTenantId());
279283

280284
Platform platform = platformService.queryPlatformById(app.getPlatformId());
281285

base/src/main/java/com/tinyengine/it/service/material/impl/BlockServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ public Result<List<Block>> listNew(String appId, String groupId) {
639639
if (appId != null && !appId.isEmpty()) {
640640
appIdTemp = Integer.parseInt(appId);
641641
}
642-
App apps = appMapper.queryAppById(appIdTemp);
642+
App apps = appMapper.queryAppById(appIdTemp, loginUserContext.getTenantId());
643643
if (groupIdTemp != 0) {
644644
if (!apps.getId().equals(appIdTemp)) {
645645
return Result.failed(ExceptionEnum.CM206);

base/src/main/resources/mappers/AppMapper.xml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@
430430
SELECT
431431
<include refid="Common_Column_List"/>
432432
<include refid="Common_Join"/>
433-
WHERE A.is_template IS NOT TRUE
433+
WHERE A.is_template IS NOT TRUE AND A.tenant_id = #{tenantId}
434434
ORDER BY A.created_time DESC
435435
</select>
436436

@@ -455,6 +455,9 @@
455455
<if test="createdBy != null">
456456
AND A.created_by = #{createdBy}
457457
</if>
458+
<if test="tenantId != null">
459+
AND A.tenant_id = #{tenantId}
460+
</if>
458461
</where>
459462
ORDER BY
460463
<choose>
@@ -488,6 +491,9 @@
488491
<if test="createdBy != null">
489492
AND A.created_by = #{createdBy}
490493
</if>
494+
<if test="tenantId != null">
495+
AND A.tenant_id = #{tenantId}
496+
</if>
491497
</where>
492498
ORDER BY
493499
<choose>
@@ -505,7 +511,7 @@
505511
<include refid="Common_Column_List"/>
506512
<include refid="Common_Join"/>
507513
<where>
508-
A.id = #{id}
514+
A.id = #{id} AND A.tenant_id = #{tenantId}
509515
</where>
510516
</select>
511517

@@ -515,7 +521,8 @@
515521
<include refid="Common_Column_List"/>
516522
<include refid="Common_Join"/>
517523
<where>
518-
A.id = #{id} AND A.is_template = true
524+
A.tenant_id = #{tenantId} AND
525+
A.id = #{id} AND A.is_template = true
519526
</where>
520527
</select>
521528

@@ -532,7 +539,7 @@
532539
<delete id="deleteAppById">
533540
DELETE
534541
FROM t_app
535-
WHERE id = #{id}
542+
WHERE id = #{id} AND tenant_id = #{tenantId}
536543
</delete>
537544

538545
<!-- 根据主键id更新表t_app数据 -->
@@ -542,7 +549,7 @@
542549
<include refid="AppSetColumns"/>
543550
</set>
544551
WHERE
545-
id=#{id}
552+
id = #{id} AND tenant_id = #{tenantId}
546553
</update>
547554

548555
<!-- 新增表t_app数据 -->

0 commit comments

Comments
 (0)