Skip to content

Commit fed1011

Browse files
committed
fix:update user tenant api
1 parent dfaf671 commit fed1011

File tree

7 files changed

+48
-7
lines changed

7 files changed

+48
-7
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import com.tinyengine.it.login.config.context.DefaultLoginUserContext;
1919
import com.tinyengine.it.login.model.UserInfo;
2020
import com.tinyengine.it.model.entity.Tenant;
21+
import com.tinyengine.it.model.entity.User;
22+
import com.tinyengine.it.service.app.UserService;
2123
import jakarta.servlet.http.HttpServletRequest;
2224
import jakarta.servlet.http.HttpServletResponse;
2325
import lombok.extern.slf4j.Slf4j;
@@ -36,6 +38,8 @@ public class SSOInterceptor implements HandlerInterceptor {
3638

3739
@Autowired
3840
private JwtUtil jwtUtil;
41+
@Autowired
42+
private UserService userService;
3943

4044
@Override
4145
public boolean preHandle(HttpServletRequest request,
@@ -72,8 +76,16 @@ public boolean preHandle(HttpServletRequest request,
7276
log.warn("User information is incomplete - username: {}, userId: {}", username, userId);
7377
throw new ServiceException(ExceptionEnum.CM339.getResultCode(), ExceptionEnum.CM339.getResultMsg());
7478
}
75-
76-
// 存储用户信息到LoginUserContext
79+
User user = userService.queryUserById(userId);// 确认用户存在
80+
if (user == null) {
81+
log.warn("User not found for userId: {}", userId);
82+
throw new ServiceException(ExceptionEnum.CM338.getResultCode(), ExceptionEnum.CM339.getResultMsg());
83+
}
84+
Integer useTenantId = user.getUseTenantId();
85+
for (Tenant tenant : tenants) {
86+
tenant.setIsInUse(tenant.getId().equals(useTenantId.toString()));
87+
}
88+
// 存储用户信息到LoginUserContext
7789
UserInfo userInfo = new UserInfo(userId, username, tenants);
7890

7991
userInfo.setPlatformId(platformId != null ? platformId : 0);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class DefaultLoginUserContext implements LoginUserContext {
1818
private static final int DEFAULT_PLATFORM = 1;
1919
private static final String DEFAULT_TENANT = "1";
2020

21+
2122
/**
2223
* 返回当前用户所在的业务租户id
2324
*
@@ -31,11 +32,10 @@ public String getTenantId() {
3132
return DEFAULT_TENANT;
3233
}
3334
for (Tenant tenant : tenantList) {
34-
if (tenant.getIsInUse()) {
35+
if (tenant.getIsInUse()!=null&&tenant.getIsInUse()) {
3536
return tenant.getId();
3637
}
3738
}
38-
3939
return DEFAULT_TENANT;
4040
}
4141

base/src/main/java/com/tinyengine/it/login/controller/LoginController.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,13 @@ public Result<SSOTicket> setTenant(@RequestParam Integer tenantId) {
264264
if (!found) {
265265
return Result.failed(ExceptionEnum.CM341);
266266
}
267+
//存储当前组织到LoginUserContext
268+
loginUserContext.setTenants(tenantList);
269+
// 更新用户当前使用组织
270+
User user = new User();
271+
user.setId(loginUserContext.getLoginUserId());
272+
user.setUseTenantId(tenantId);
273+
userService.updateUserById(user);
267274

268275
// 通过 RequestContextHolder 获取请求
269276
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())

base/src/main/java/com/tinyengine/it/login/service/impl/LoginServiceImpl.java

Lines changed: 5 additions & 0 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.exception.ExceptionEnum;
1819
import com.tinyengine.it.common.exception.ServiceException;
1920
import com.tinyengine.it.login.service.LoginService;
@@ -42,6 +43,9 @@ public class LoginServiceImpl extends ServiceImpl<UserMapper, User> implements L
4243
@Autowired
4344
AuthUsersUnitsRolesMapper authUsersUnitsRolesMapper;
4445

46+
@Autowired
47+
LoginUserContext loginUserContext;
48+
4549
/**
4650
* 新增表t_user数据
4751
*
@@ -66,6 +70,7 @@ public User createUser(User user) throws Exception {
6670
user.setSalt(cipherText);
6771
user.setPublicKey(publicKeyBase64);
6872
user.setPrivateKey(privateKeyBase64);
73+
user.setUseTenantId(Integer.valueOf(loginUserContext.getTenantId()));
6974
baseMapper.createUser(user);
7075
User result = baseMapper.queryUserById(user.getId());
7176
result.setPrivateKey(null);

base/src/main/java/com/tinyengine/it/model/entity/User.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public class User {
6363
@Schema(name = "private_key", description = "私钥")
6464
private String privateKey;
6565

66+
@Schema(name = "use_tenant_id", description = "当前使用的租户ID")
67+
private Integer useTenantId;
68+
6669
@TableField("enable")
6770
@Schema(name = "enable", description = "账号是否可用")
6871
private Boolean isEnable;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ public AppDto queryAllAppByPage(Integer currentPage, Integer pageSize, String or
108108
pageSize = 1000; // 限制最大页大小
109109
}
110110
int offset = (currentPage - 1) * pageSize;
111+
String tenantId = loginUserContext.getTenantId();
111112
List<App> apps = this.baseMapper.queryAllAppByPage(pageSize, offset, app.getName(),
112113
app.getIndustryId(), app.getSceneId(), app.getFramework(), orderBy, app.getCreatedBy(),
113-
loginUserContext.getTenantId());
114-
Integer total = this.baseMapper.queryAppTotal(loginUserContext.getTenantId());
114+
tenantId);
115+
Integer total = this.baseMapper.queryAppTotal(tenantId);
115116
AppDto appDto = new AppDto();
116117
appDto.setApps(apps);
117118
appDto.setTotal(total);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
<!-- 通用查询列 -->
88
<sql id="Base_Column_List">
99
id
10-
, username, password, email, salt, public_key, private_key, enable, created_time, last_updated_time,
10+
, username, password, email, salt, public_key, private_key, use_tenant_id,enable, created_time, last_updated_time,
1111
is_admin, is_public
1212
</sql>
1313

1414
<!-- 通用条件列 -->
1515
<sql id="UserByCondition">
16+
<if test="id!=null and id!=''">
17+
AND id = #{id}
18+
</if>
1619
<if test="username!=null and username!=''">
1720
AND username = #{username}
1821
</if>
@@ -46,6 +49,9 @@
4649
<if test="isPublic!=null">
4750
AND is_public = #{isPublic}
4851
</if>
52+
<if test="useTenantId!=null">
53+
AND use_tenant_id = #{useTenantId}
54+
</if>
4955
</sql>
5056

5157
<!-- 通用设置列 -->
@@ -77,6 +83,9 @@
7783
<if test="isPublic!=null">
7884
is_public = #{isPublic},
7985
</if>
86+
<if test="useTenantId!=null">
87+
use_tenant_id = #{useTenantId},
88+
</if>
8089
</sql>
8190

8291

@@ -93,6 +102,8 @@
93102
<result column="last_updated_time" property="lastUpdatedTime"/>
94103
<result column="is_admin" property="isAdmin"/>
95104
<result column="is_public" property="isPublic"/>
105+
<result column="use_tenant_id" property="useTenantId"/>
106+
96107
</resultMap>
97108

98109
<!-- 查询表t_user所有数据 -->
@@ -155,6 +166,7 @@
155166
, salt
156167
, public_key
157168
, private_key
169+
, use_tenant_id
158170
, enable
159171
, created_time
160172
, last_updated_time
@@ -167,6 +179,7 @@
167179
, #{salt}
168180
, #{publicKey}
169181
, #{privateKey}
182+
, #{useTenantId}
170183
, #{isEnable}
171184
, #{createdTime}
172185
, #{lastUpdatedTime}

0 commit comments

Comments
 (0)