Skip to content

Commit e744285

Browse files
committed
feat: add tanant API
1 parent 95b55c4 commit e744285

File tree

10 files changed

+65
-36
lines changed

10 files changed

+65
-36
lines changed

app/src/main/resources/sql/h2/create_permission_table_ddl_2025_1029.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ create table `r_auth_users_units_roles`
2020
`id` int not null auto_increment comment '主键id',
2121
`user_id` int not null comment '用户',
2222
`unit_id` int not null comment '业务单元',
23-
`unit_type` int not null comment '业务单元类型',
23+
`unit_type` varchar(60) not null comment '业务单元类型',
2424
`tenant_id` int not null comment '组织id',
2525
`role_id` int not null comment '角色id',
2626
`expired_time` timestamp comment '过期时间',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface LoginUserContext {
4141

4242
/**
4343
* 设置当前组织信息
44-
* @praam tenants
44+
* @param tenants
4545
*/
4646
void setTenants(List<Tenant> tenants);
4747
}

base/src/main/java/com/tinyengine/it/common/handler/MyMetaObjectHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,13 @@ public void insertFill(MetaObject metaObject) {
4141
this.setFieldValByName("createdBy", loginUserContext.getLoginUserId(), metaObject);
4242
this.setFieldValByName("lastUpdatedBy", loginUserContext.getLoginUserId(), metaObject);
4343
this.setFieldValByName("platformId", loginUserContext.getPlatformId(), metaObject);
44-
this.setFieldValByName("tenantId", loginUserContext.getTenants().get(0).getId(), metaObject);
44+
45+
if (loginUserContext.getTenants() != null && loginUserContext.getTenants().isEmpty()) {
46+
Integer tenant = Integer.valueOf(loginUserContext.getTenants().get(0).getId());
47+
if (tenant != null) {
48+
this.setFieldValByName("tenantId", tenant, metaObject);
49+
}
50+
}
4551
}
4652

4753
@Override

base/src/main/java/com/tinyengine/it/controller/TenantController.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import jakarta.validation.Valid;
1515
import org.springframework.beans.factory.annotation.Autowired;
1616
import org.springframework.validation.annotation.Validated;
17+
import org.springframework.web.bind.annotation.DeleteMapping;
1718
import org.springframework.web.bind.annotation.GetMapping;
1819
import org.springframework.web.bind.annotation.PostMapping;
1920
import org.springframework.web.bind.annotation.RequestBody;
@@ -111,20 +112,21 @@ public Result<Tenant> updateTenant(@RequestBody Tenant tenant) {
111112
*/
112113
@Operation(summary = "删除单个组织", description = "删除单个组织",
113114
parameters = {
114-
@Parameter(name = "id", description = "TenantId")
115+
@Parameter(name = "id", description = "id")
115116
}, responses = {
116117
@ApiResponse(responseCode = "200", description = "返回信息",
117118
content = @Content(mediaType = "application/json",
118119
schema = @Schema(implementation = Tenant.class))),
119120
@ApiResponse(responseCode = "400", description = "请求失败")
120121
})
121122
@SystemControllerLog(description = "删除单个组织")
122-
@GetMapping("/tenant/delete")
123+
@DeleteMapping("/tenant/delete")
123124
public Result<Tenant> deleteTenant(@RequestParam Integer id) {
125+
Tenant tenant = tenantService.findTenantById(id);
124126
int result = tenantService.deleteTenantById(id);
125127
if (result != 1) {
126128
return Result.failed(ExceptionEnum.CM009);
127129
}
128-
return Result.success(tenantService.findTenantById(id));
130+
return Result.success(tenant);
129131
}
130132
}

base/src/main/java/com/tinyengine/it/controller/UserController.java

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

1515
import com.tinyengine.it.common.base.Result;
1616
import com.tinyengine.it.common.context.LoginUserContext;
17+
import com.tinyengine.it.common.exception.ExceptionEnum;
1718
import com.tinyengine.it.common.log.SystemControllerLog;
1819
import com.tinyengine.it.mapper.AuthUsersUnitsRolesMapper;
1920
import com.tinyengine.it.model.entity.Tenant;
@@ -75,14 +76,20 @@ public class UserController {
7576
@GetMapping("/user/me")
7677
public Result<User> me() {
7778
String loginUserId = loginUserContext.getLoginUserId();
79+
if (loginUserId == null) {
80+
return Result.failed(ExceptionEnum.CM009);
81+
}
82+
Integer userId = Integer.valueOf(loginUserId);
83+
List<Tenant> tenants = authUsersUnitsRolesMapper.queryAllTenantByUserId(userId);
7884
User user = userService.queryUserById(loginUserId);
79-
List<Tenant> tenants = authUsersUnitsRolesMapper.queryAllTenantByUserId(Integer.valueOf(loginUserId));
80-
user.setTenant(tenants);
8185
if (user == null) {
8286
user = new User();
8387
user.setId(loginUserContext.getLoginUserId());
8488
user.setUsername(loginUserContext.getLoginUserId());
8589
}
90+
user.setTenant(tenants);
91+
8692
return Result.success(user);
8793
}
94+
8895
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,7 @@ public boolean preHandle(HttpServletRequest request,
4848

4949
// 如果没有token,重定向到登录页
5050
if (token == null || token.isEmpty()) {
51-
String redirectUrl = SSO_SERVER;
52-
log.info("No token, redirecting to: {}", redirectUrl);
53-
response.sendRedirect(redirectUrl);
51+
log.info("No token, redirecting to: {}", SSO_SERVER);
5452
response.sendRedirect(SSO_SERVER);
5553
return false;
5654
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,18 @@ public Result<ValidationResult> validateToken(@RequestParam String token) {
239239
@GetMapping("/user/tenant")
240240
public Result<SSOTicket> setTenant(@RequestParam Integer tenantId) {
241241
List<Tenant> tenants = loginUserContext.getTenants();
242+
if (tenants == null || tenants.isEmpty()) {
243+
return Result.failed(ExceptionEnum.CM009);
244+
}
242245
List<Tenant> currentTenant = new ArrayList<>();
243246
for (Tenant tenant : tenants) {
244247
if (tenant.getId().equals(tenantId.toString())) {
245248
currentTenant.add(tenant);
246249
}
247250
}
251+
if (currentTenant.isEmpty()) {
252+
return Result.failed(ExceptionEnum.CM009);
253+
}
248254
// 通过 RequestContextHolder 获取请求
249255
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
250256
.getRequest();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public User createUser(User user) throws Exception {
5555
List<User> users = baseMapper.queryUserByCondition(userParam);
5656
if (!users.isEmpty()) {
5757
throw new ServiceException(ExceptionEnum.CM003.getResultCode(),
58-
ExceptionEnum.CM003.getResultMsg());
58+
ExceptionEnum.CM003.getResultMsg());
5959
}
6060
KeyPair keyPair = generateSM2KeyPair();
6161
PublicKey publicKey = keyPair.getPublic();
@@ -92,7 +92,7 @@ public Result forgotPassword(User user) throws Exception {
9292
userParam.setUsername(user.getUsername());
9393
List<User> users = baseMapper.queryUserByCondition(userParam);
9494
if (users.isEmpty()) {
95-
Result.failed(ExceptionEnum.CM002);
95+
return Result.failed(ExceptionEnum.CM002);
9696
}
9797
User userResult = users.get(0);
9898
PublicKey publicKey = getPublicKeyFromBase64(user.getPublicKey());
@@ -107,7 +107,7 @@ public Result forgotPassword(User user) throws Exception {
107107
baseMapper.updateUserById(user);
108108
User result = baseMapper.queryUserById(user.getId());
109109
result.setPrivateKey(null);
110-
if (result.getSalt().isEmpty()) {
110+
if (result.getSalt() == null || result.getSalt().isEmpty()) {
111111
return Result.failed(ExceptionEnum.CM335);
112112
}
113113
return Result.success(ExceptionEnum.CM334.getResultCode(), ExceptionEnum.CM334.getResultMsg());

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@
3131
AND expired_time = #{expiredTime}
3232
</if>
3333
<if test="createdBy!=null and createdBy!=''">
34-
AND A.created_by = #{createdBy}
34+
AND created_by = #{createdBy}
3535
</if>
3636
<if test="lastUpdatedBy!=null and lastUpdatedBy!=''">
37-
AND A.last_updated_by = #{lastUpdatedBy}
37+
AND last_updated_by = #{lastUpdatedBy}
3838
</if>
3939
<if test="createdTime!=null">
40-
AND A.created_time = #{createdTime}
40+
AND created_time = #{createdTime}
4141
</if>
4242
<if test="lastUpdatedTime!=null">
43-
AND A.last_updated_time = #{lastUpdatedTime}
43+
AND last_updated_time = #{lastUpdatedTime}
4444
</if>
4545
</sql>
4646

@@ -65,16 +65,16 @@
6565
expired_time = #{expiredTime},
6666
</if>
6767
<if test="createdBy!=null and createdBy!=''">
68-
A.created_by = #{createdBy},
68+
created_by = #{createdBy},
6969
</if>
7070
<if test="lastUpdatedBy!=null and lastUpdatedBy!=''">
71-
A.last_updated_by = #{lastUpdatedBy},
71+
last_updated_by = #{lastUpdatedBy},
7272
</if>
7373
<if test="createdTime!=null">
74-
A.created_time = #{createdTime},
74+
created_time = #{createdTime},
7575
</if>
7676
<if test="lastUpdatedTime!=null">
77-
A.last_updated_time = #{lastUpdatedTime},
77+
last_updated_time = #{lastUpdatedTime},
7878
</if>
7979
</sql>
8080

base/src/test/java/com/tinyengine/it/common/handler/MockUserContext.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,40 +12,50 @@
1212
package com.tinyengine.it.common.handler;
1313

1414
import com.tinyengine.it.common.context.LoginUserContext;
15+
import com.tinyengine.it.model.entity.Tenant;
16+
17+
import java.util.ArrayList;
18+
import java.util.List;
1519

1620
/**
1721
* Mock user context
1822
*
1923
* @since 2025-04-14
2024
*/
2125
public class MockUserContext implements LoginUserContext {
26+
/**
27+
* 返回当前用户所诉的业务租户信息
28+
*
29+
* @return 租户ID
30+
*/
2231
@Override
23-
public String getTenantId() {
24-
return "1";
25-
}
26-
27-
@Override
28-
public Integer getLoginUserId() {
29-
return "1";
32+
public List<Tenant> getTenants() {
33+
Tenant tenant = new Tenant();
34+
tenant.setId("1");
35+
List<Tenant> tenantList = new ArrayList<>();
36+
tenantList.add(tenant);
37+
return tenantList;
3038
}
3139

3240
@Override
33-
public String getRenterId() {
41+
public String getLoginUserId() {
3442
return "1";
3543
}
3644

37-
@Override
38-
public int getAppId() {
39-
return 1;
40-
}
4145

4246
@Override
4347
public int getPlatformId() {
4448
return 1;
4549
}
4650

51+
/**
52+
* 设置当前组织信息
53+
*
54+
* @param tenants
55+
*/
4756
@Override
48-
public String getSiteId() {
49-
return "1";
57+
public void setTenants(List<Tenant> tenants) {
58+
5059
}
60+
5161
}

0 commit comments

Comments
 (0)