登录接口调整
parent
c54540b11e
commit
e919d88554
|
|
@ -66,6 +66,7 @@ public class AuthController {
|
|||
@PostMapping("/login")
|
||||
@PermitAll
|
||||
@Operation(summary = "使用账号密码登录")
|
||||
@TenantIgnore
|
||||
public CommonResult<AuthLoginRespVO> login(@RequestBody @Valid AuthLoginReqVO reqVO) {
|
||||
return success(authService.login(reqVO));
|
||||
}
|
||||
|
|
@ -123,6 +124,20 @@ public class AuthController {
|
|||
return success(authService.register(registerReqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/switch-tenant")
|
||||
@Operation(summary = "切换租户并重新登录")
|
||||
public CommonResult<AuthLoginRespVO> switchTenant(@Valid @RequestBody AuthSwitchTenantReqVO reqVO) {
|
||||
return success(authService.switchTenant(reqVO));
|
||||
}
|
||||
|
||||
@GetMapping("/get-user-tenant-list")
|
||||
@Operation(summary = "获取当前登录用户的租户列表")
|
||||
public CommonResult<List<TenantInfoVO>> getUserTenantList() {
|
||||
Long userId = getLoginUserId();
|
||||
List<TenantInfoVO> tenants = authService.getUserTenantList(userId);
|
||||
return success(tenants);
|
||||
}
|
||||
|
||||
// ========== 短信登录相关 ==========
|
||||
|
||||
@PostMapping("/sms-login")
|
||||
|
|
@ -149,14 +164,6 @@ public class AuthController {
|
|||
return success(true);
|
||||
}
|
||||
|
||||
@PostMapping("/get-user-tenant-list")
|
||||
@PermitAll
|
||||
@TenantIgnore
|
||||
@Operation(summary = "获取用户的租户列表(未登录状态)")
|
||||
public CommonResult<AuthUserTenantListRespVO> getUserTenantList(@RequestBody @Valid AuthUserTenantListReqVO reqVO) {
|
||||
return success(authService.getUserTenantList(reqVO));
|
||||
}
|
||||
|
||||
// ========== 社交登录相关 ==========
|
||||
|
||||
@GetMapping("/social-auth-redirect")
|
||||
|
|
|
|||
|
|
@ -1,59 +0,0 @@
|
|||
package com.fjrcloud.community.module.system.controller.admin.auth;
|
||||
|
||||
import com.fjrcloud.community.framework.common.enums.UserTypeEnum;
|
||||
import com.fjrcloud.community.framework.common.pojo.CommonResult;
|
||||
import com.fjrcloud.community.framework.tenant.core.util.TenantUtils;
|
||||
import com.fjrcloud.community.module.system.controller.admin.auth.vo.SwitchTenantReqVO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.user.UserTenantRelDO;
|
||||
import com.fjrcloud.community.module.system.enums.ErrorCodeConstants;
|
||||
import com.fjrcloud.community.module.system.enums.oauth2.OAuth2ClientConstants;
|
||||
import com.fjrcloud.community.module.system.service.oauth2.OAuth2TokenService;
|
||||
import com.fjrcloud.community.module.system.service.usertenant.UserTenantRelService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.validation.Valid;
|
||||
|
||||
import static com.fjrcloud.community.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static com.fjrcloud.community.framework.common.pojo.CommonResult.success;
|
||||
import static com.fjrcloud.community.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
|
||||
@Tag(name = "管理后台 - 认证 - 租户切换")
|
||||
@RestController
|
||||
@RequestMapping("/system/auth")
|
||||
@Validated
|
||||
public class AuthTenantSwitchController {
|
||||
|
||||
@Resource
|
||||
private UserTenantRelService userTenantRelService;
|
||||
|
||||
@Resource
|
||||
private OAuth2TokenService oauth2TokenService;
|
||||
|
||||
@PostMapping("/switch-tenant")
|
||||
@Operation(summary = "切换租户")
|
||||
public CommonResult<String> switchTenant(@Valid @RequestBody SwitchTenantReqVO reqVO) {
|
||||
Long userId = getLoginUserId();
|
||||
|
||||
UserTenantRelDO rel = userTenantRelService.getUserTenantRel(userId, reqVO.getTenantId());
|
||||
if (rel == null) {
|
||||
throw exception(ErrorCodeConstants.USER_TENANT_REL_NOT_EXISTS);
|
||||
}
|
||||
|
||||
String newToken = TenantUtils.execute(reqVO.getTenantId(), () -> {
|
||||
OAuth2AccessTokenDO newAccessToken = oauth2TokenService.createAccessToken(
|
||||
userId, UserTypeEnum.ADMIN.getValue(),
|
||||
OAuth2ClientConstants.CLIENT_ID_DEFAULT, null);
|
||||
return newAccessToken.getAccessToken();
|
||||
});
|
||||
|
||||
return success(newToken);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
package com.fjrcloud.community.module.system.controller.admin.auth.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotNull;
|
||||
|
||||
@Schema(description = "管理后台 - 切换租户 Request VO")
|
||||
@Data
|
||||
public class AuthSwitchTenantReqVO {
|
||||
|
||||
@Schema(description = "租户ID", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
@NotNull(message = "租户ID不能为空")
|
||||
private Long tenantId;
|
||||
|
||||
}
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
package com.fjrcloud.community.module.system.controller.admin.user.vo.user;
|
||||
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fjrcloud.community.framework.common.validation.Mobile;
|
||||
import com.fjrcloud.community.module.system.framework.operatelog.core.DeptParseFunction;
|
||||
import com.fjrcloud.community.module.system.framework.operatelog.core.PostParseFunction;
|
||||
import com.fjrcloud.community.module.system.framework.operatelog.core.SexParseFunction;
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.mzt.logapi.starter.annotation.DiffLogField;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
|
@ -51,7 +51,8 @@ public class UserSaveReqVO {
|
|||
@DiffLogField(name = "用户邮箱")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "手机号码", example = "15601691300")
|
||||
@Schema(description = "手机号码", requiredMode = Schema.RequiredMode.REQUIRED, example = "15601691300")
|
||||
@NotBlank(message = "手机号码不能为空")
|
||||
@Mobile
|
||||
@DiffLogField(name = "手机号码")
|
||||
private String mobile;
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.KeySequence;
|
|||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import com.fjrcloud.community.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.fjrcloud.community.framework.tenant.core.aop.TenantIgnore;
|
||||
import lombok.*;
|
||||
|
||||
@TableName("system_user_tenant_rel")
|
||||
|
|
@ -14,6 +15,7 @@ import lombok.*;
|
|||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@TenantIgnore
|
||||
public class UserTenantRelDO extends BaseDO {
|
||||
|
||||
@TableId
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.fjrcloud.community.module.system.dal.mysql.user;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fjrcloud.community.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import com.fjrcloud.community.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.user.UserTenantRelDO;
|
||||
|
|
@ -30,4 +31,10 @@ public interface UserTenantRelMapper extends BaseMapperX<UserTenantRelDO> {
|
|||
return delete(new LambdaQueryWrapperX<UserTenantRelDO>()
|
||||
.eq(UserTenantRelDO::getUserId, userId));
|
||||
}
|
||||
|
||||
default int clearDefaultByUserId(Long userId) {
|
||||
return update(new UserTenantRelDO().setDefaultTenant(false),
|
||||
new LambdaQueryWrapper<UserTenantRelDO>().eq(UserTenantRelDO::getUserId, userId));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ public interface ErrorCodeConstants {
|
|||
ErrorCode AUTH_THIRD_LOGIN_NOT_BIND = new ErrorCode(1_002_000_005, "未绑定账号,需要进行绑定");
|
||||
ErrorCode AUTH_MOBILE_NOT_EXISTS = new ErrorCode(1_002_000_007, "手机号不存在");
|
||||
ErrorCode AUTH_REGISTER_CAPTCHA_CODE_ERROR = new ErrorCode(1_002_000_008, "验证码不正确,原因:{}");
|
||||
ErrorCode AUTH_NO_TENANT_PERMISSION = new ErrorCode(1_002_000_009, "当前用户无权限访问系统,请联系管理员");
|
||||
|
||||
// ========== 菜单模块 1-002-001-000 ==========
|
||||
ErrorCode MENU_NAME_DUPLICATE = new ErrorCode(1_002_001_000, "已经存在该名字的菜单");
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.fjrcloud.community.module.system.controller.admin.auth.vo.*;
|
|||
import com.fjrcloud.community.module.system.dal.dataobject.user.AdminUserDO;
|
||||
|
||||
import javax.validation.Valid;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 管理后台的认证 Service 接口
|
||||
|
|
@ -80,6 +81,20 @@ public interface AdminAuthService {
|
|||
|
||||
void resetPassword(@Valid AuthResetPasswordReqVO reqVO);
|
||||
|
||||
AuthUserTenantListRespVO getUserTenantList(@Valid AuthUserTenantListReqVO reqVO);
|
||||
/**
|
||||
* 获取用户的租户列表
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @return 租户列表
|
||||
*/
|
||||
List<TenantInfoVO> getUserTenantList(Long userId);
|
||||
|
||||
/**
|
||||
* 切换租户并重新登录
|
||||
*
|
||||
* @param reqVO 切换请求
|
||||
* @return 登录响应
|
||||
*/
|
||||
AuthLoginRespVO switchTenant(AuthSwitchTenantReqVO reqVO);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package com.fjrcloud.community.module.system.service.auth;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.anji.captcha.model.common.ResponseModel;
|
||||
import com.anji.captcha.model.vo.CaptchaVO;
|
||||
import com.anji.captcha.service.CaptchaService;
|
||||
|
|
@ -11,6 +13,7 @@ import com.fjrcloud.community.framework.common.util.object.BeanUtils;
|
|||
import com.fjrcloud.community.framework.common.util.servlet.ServletUtils;
|
||||
import com.fjrcloud.community.framework.common.util.validation.ValidationUtils;
|
||||
import com.fjrcloud.community.framework.datapermission.core.annotation.DataPermission;
|
||||
import com.fjrcloud.community.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import com.fjrcloud.community.framework.tenant.core.context.TenantContextHolder;
|
||||
import com.fjrcloud.community.framework.tenant.core.util.TenantUtils;
|
||||
import com.fjrcloud.community.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
||||
|
|
@ -23,6 +26,7 @@ import com.fjrcloud.community.module.system.convert.auth.AuthConvert;
|
|||
import com.fjrcloud.community.module.system.dal.dataobject.oauth2.OAuth2AccessTokenDO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.user.UserTenantRelDO;
|
||||
import com.fjrcloud.community.module.system.enums.ErrorCodeConstants;
|
||||
import com.fjrcloud.community.module.system.enums.logger.LoginLogTypeEnum;
|
||||
import com.fjrcloud.community.module.system.enums.logger.LoginResultEnum;
|
||||
import com.fjrcloud.community.module.system.enums.oauth2.OAuth2ClientConstants;
|
||||
|
|
@ -79,24 +83,49 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||
@Override
|
||||
public AdminUserDO authenticate(String username, String password) {
|
||||
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_USERNAME;
|
||||
// 校验账号是否存在
|
||||
AdminUserDO user = userService.getUserByUsername(username);
|
||||
// 校验账号是否存在(支持用户名或手机号登录)
|
||||
AdminUserDO user;
|
||||
String loginAccount = username;
|
||||
|
||||
// 判断是手机号还是用户名
|
||||
if (isMobile(username)) {
|
||||
// 手机号登录
|
||||
user = userService.getUserByMobile(username);
|
||||
} else {
|
||||
// 用户名登录
|
||||
user = userService.getUserByUsername(username);
|
||||
}
|
||||
|
||||
if (user == null) {
|
||||
createLoginLog(null, username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
createLoginLog(null, loginAccount, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
if (!userService.isPasswordMatch(password, user.getPassword())) {
|
||||
createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
createLoginLog(user.getId(), loginAccount, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
// 校验是否禁用
|
||||
if (CommonStatusEnum.isDisable(user.getStatus())) {
|
||||
createLoginLog(user.getId(), username, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
||||
createLoginLog(user.getId(), loginAccount, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
||||
throw exception(AUTH_LOGIN_USER_DISABLED);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为手机号
|
||||
*
|
||||
* @param account 账号
|
||||
* @return 是否为手机号
|
||||
*/
|
||||
private boolean isMobile(String account) {
|
||||
if (StrUtil.isBlank(account)) {
|
||||
return false;
|
||||
}
|
||||
// 手机号格式:11位数字,以1开头
|
||||
return account.matches("^1[3-9]\\d{9}$");
|
||||
}
|
||||
|
||||
@Override
|
||||
@DataPermission(enable = false)
|
||||
public AuthLoginRespVO login(AuthLoginReqVO reqVO) {
|
||||
|
|
@ -106,20 +135,38 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||
// 使用账号密码,进行登录
|
||||
AdminUserDO user = authenticate(reqVO.getUsername(), reqVO.getPassword());
|
||||
|
||||
// 获取用户租户关联关系
|
||||
List<UserTenantRelDO> rels = userTenantRelService.getUserTenantRels(user.getId());
|
||||
if (CollUtil.isEmpty(rels)) {
|
||||
createLoginLog(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.TENANT_PERMISSION_DENIED);
|
||||
throw exception(ErrorCodeConstants.AUTH_NO_TENANT_PERMISSION);
|
||||
}
|
||||
|
||||
// 查找默认租户
|
||||
UserTenantRelDO defaultRel = rels.stream()
|
||||
.filter(rel -> Boolean.TRUE.equals(rel.getDefaultTenant()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
Long tenantId;
|
||||
if (defaultRel != null) {
|
||||
tenantId = defaultRel.getTenantId();
|
||||
} else {
|
||||
// 没有默认租户,取第一条
|
||||
tenantId = rels.get(0).getTenantId();
|
||||
// 设置为默认租户
|
||||
userTenantRelService.setDefaultTenant(user.getId(), tenantId);
|
||||
}
|
||||
|
||||
// 设置当前请求的租户上下文
|
||||
TenantContextHolder.setTenantId(tenantId);
|
||||
|
||||
// 如果 socialType 非空,说明需要绑定社交用户
|
||||
if (reqVO.getSocialType() != null) {
|
||||
socialUserService.bindSocialUser(new SocialUserBindReqDTO(user.getId(), getUserType().getValue(),
|
||||
reqVO.getSocialType(), reqVO.getSocialCode(), reqVO.getSocialState()));
|
||||
}
|
||||
|
||||
Long tenantId = TenantContextHolder.getTenantId();
|
||||
if (tenantId == null) {
|
||||
createLoginLog(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME, LoginResultEnum.BAD_CREDENTIALS);
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
|
||||
validateUserTenantPermission(user.getId(), tenantId);
|
||||
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
}
|
||||
|
||||
|
|
@ -337,27 +384,32 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||
|
||||
@Override
|
||||
@DataPermission(enable = false)
|
||||
public AuthUserTenantListRespVO getUserTenantList(AuthUserTenantListReqVO reqVO) {
|
||||
AdminUserDO user = userService.getUserByUsername(reqVO.getUsername());
|
||||
if (user == null) {
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
public List<TenantInfoVO> getUserTenantList(Long userId) {
|
||||
return userTenantRelService.getUserTenants(userId);
|
||||
}
|
||||
|
||||
if (!userService.isPasswordMatch(reqVO.getPassword(), user.getPassword())) {
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
@Override
|
||||
@DataPermission(enable = false)
|
||||
public AuthLoginRespVO switchTenant(AuthSwitchTenantReqVO reqVO) {
|
||||
Long userId = SecurityFrameworkUtils.getLoginUserId();
|
||||
|
||||
// 获得用户信息
|
||||
AdminUserDO user = userService.getUser(userId);
|
||||
|
||||
// 校验新租户是否在用户授权列表中
|
||||
List<UserTenantRelDO> rels = userTenantRelService.getUserTenantRels(userId);
|
||||
boolean exists = rels.stream().anyMatch(r -> r.getTenantId().equals(reqVO.getTenantId()));
|
||||
if (!exists) {
|
||||
throw exception(USER_TENANT_REL_NOT_EXISTS);
|
||||
}
|
||||
|
||||
if (CommonStatusEnum.isDisable(user.getStatus())) {
|
||||
throw exception(AUTH_LOGIN_USER_DISABLED);
|
||||
}
|
||||
// 设置新租户为默认
|
||||
userTenantRelService.setDefaultTenant(userId, reqVO.getTenantId());
|
||||
|
||||
List<TenantInfoVO> tenants = userTenantRelService.getUserTenants(user.getId());
|
||||
// 切换租户上下文
|
||||
TenantContextHolder.setTenantId(reqVO.getTenantId());
|
||||
|
||||
return AuthUserTenantListRespVO.builder()
|
||||
.userId(user.getId())
|
||||
.username(user.getUsername())
|
||||
.nickname(user.getNickname())
|
||||
.tenants(tenants)
|
||||
.build();
|
||||
// 创建新租户下的 Token(自动失效旧 Token)
|
||||
return createTokenAfterLoginSuccess(userId, user.getUsername(), LoginLogTypeEnum.LOGIN_USERNAME);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -358,6 +358,11 @@ public class AdminUserServiceImpl implements AdminUserService {
|
|||
private AdminUserDO validateUserForCreateOrUpdate(Long id, String username, String mobile, String email,
|
||||
Long deptId, Set<Long> postIds) {
|
||||
return DataPermissionUtils.executeIgnore(() -> {
|
||||
// 校验手机号不能为空
|
||||
if (StrUtil.isBlank(mobile)) {
|
||||
throw exception(USER_MOBILE_NOT_EXISTS);
|
||||
}
|
||||
|
||||
AdminUserDO user = validateUserExists(id);
|
||||
validateUsernameUnique(id, username);
|
||||
validateMobileUnique(id, mobile);
|
||||
|
|
@ -416,9 +421,6 @@ public class AdminUserServiceImpl implements AdminUserService {
|
|||
|
||||
@VisibleForTesting
|
||||
void validateMobileUnique(Long id, String mobile) {
|
||||
if (StrUtil.isBlank(mobile)) {
|
||||
return;
|
||||
}
|
||||
AdminUserDO user = userMapper.selectByMobile(mobile);
|
||||
if (user == null) {
|
||||
return;
|
||||
|
|
|
|||
|
|
@ -20,4 +20,13 @@ public interface UserTenantRelService {
|
|||
void removeUserFromTenant(Long userId, Long tenantId);
|
||||
|
||||
void batchAssignUserToTenants(Long userId, List<Long> tenantIds);
|
||||
|
||||
/**
|
||||
* 设置用户默认租户
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param tenantId 租户ID
|
||||
*/
|
||||
void setDefaultTenant(Long userId, Long tenantId);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.fjrcloud.community.module.system.service.usertenant;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.fjrcloud.community.module.system.controller.admin.auth.vo.TenantInfoVO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.tenant.TenantDO;
|
||||
import com.fjrcloud.community.module.system.dal.dataobject.user.UserTenantRelDO;
|
||||
|
|
@ -115,6 +116,22 @@ public class UserTenantRelServiceImpl implements UserTenantRelService {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void setDefaultTenant(Long userId, Long tenantId) {
|
||||
// 1. 清除该用户所有关联租户的默认标记
|
||||
userTenantRelMapper.clearDefaultByUserId(userId);
|
||||
// 2. 设置指定租户为默认
|
||||
UserTenantRelDO rel = userTenantRelMapper.selectOne(new LambdaQueryWrapper<UserTenantRelDO>()
|
||||
.eq(UserTenantRelDO::getUserId, userId)
|
||||
.eq(UserTenantRelDO::getTenantId, tenantId));
|
||||
if (rel != null) {
|
||||
rel.setDefaultTenant(true);
|
||||
userTenantRelMapper.updateById(rel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void clearDefaultTenant(Long userId) {
|
||||
UserTenantRelDO defaultRel = userTenantRelMapper.selectDefaultByUserId(userId);
|
||||
if (defaultRel != null) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue