|
@@ -25,9 +25,7 @@ import cn.iocoder.yudao.module.system.api.logger.dto.LoginLogCreateReqDTO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.auth.vo.AuthLoginRespVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.menu.MenuListReqVO;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.permission.vo.role.RoleSaveReqVO;
|
|
|
-import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantJoinReqVO;
|
|
|
-import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantPageReqVO;
|
|
|
-import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.TenantSaveReqVO;
|
|
|
+import cn.iocoder.yudao.module.system.controller.admin.tenant.vo.tenant.*;
|
|
|
import cn.iocoder.yudao.module.system.controller.admin.user.vo.tenant.UserTenantRelateSaveReqVO;
|
|
|
import cn.iocoder.yudao.module.system.convert.auth.AuthConvert;
|
|
|
import cn.iocoder.yudao.module.system.convert.tenant.TenantConvert;
|
|
@@ -61,6 +59,7 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.time.LocalDateTime;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
@@ -132,7 +131,7 @@ public class TenantServiceImpl implements TenantService {
|
|
|
|
|
|
@Override
|
|
|
@DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
- public Long createTenant(TenantSaveReqVO createReqVO) {
|
|
|
+ public TenantSimpleRespVO createTenant(TenantSaveReqVO createReqVO) {
|
|
|
// 校验租户名称是否重复
|
|
|
validTenantNameDuplicate(createReqVO.getName(), null);
|
|
|
// 校验租户域名是否重复
|
|
@@ -142,21 +141,23 @@ public class TenantServiceImpl implements TenantService {
|
|
|
|
|
|
// 创建租户
|
|
|
TenantDO tenant = BeanUtils.toBean(createReqVO, TenantDO.class);
|
|
|
- tenant.setCorpId(IdUtil.fastSimpleUUID());
|
|
|
+ String corpId = IdUtil.fastSimpleUUID();
|
|
|
+ tenant.setCorpId(corpId);
|
|
|
+ tenant.setExpireTime(LocalDateTime.now().plusYears(10));// 默认设置10年有效期
|
|
|
tenantMapper.insert(tenant);
|
|
|
// 获得用户信息
|
|
|
LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
|
|
- // 更新用户信息当前租户
|
|
|
- userService.updateUserTenantId(user.getId(), tenant.getId());
|
|
|
- // 将这个用户所有租户关系设为不生效
|
|
|
- userTenantRelateService.deactivateAllForUser(user.getId());
|
|
|
- // 插入用户租户关系
|
|
|
- userTenantRelateService.createUserTenantRelate(new UserTenantRelateSaveReqVO().setUserId(user.getId()).setTenantId(tenant.getId()).setActived(true));
|
|
|
// 自动根据账号创建对应人事信息,只保存姓名、头像、手机号
|
|
|
AdminUserDO adminUserDO = userService.getUser(user.getId());
|
|
|
if (adminUserDO == null) {
|
|
|
throw exception(USER_NOT_EXISTS);
|
|
|
}
|
|
|
+// // 更新用户信息当前租户
|
|
|
+// userService.updateUserTenantId(user.getId(), tenant.getId());
|
|
|
+// // 将这个用户所有租户关系设为不生效
|
|
|
+// userTenantRelateService.deactivateAllForUser(user.getId());
|
|
|
+ // 插入用户租户关系
|
|
|
+ userTenantRelateService.createUserTenantRelate(new UserTenantRelateSaveReqVO().setUserId(user.getId()).setTenantId(tenant.getId()).setActived(false));
|
|
|
// 先判断该租户下员工信息是否存在,如果不存在则创建
|
|
|
EmployeeRespDTO employeeRespDTO = employeeApi.getEmployee(new EmployeeQueryReqDTO().setUserId(user.getId()).setTenantId(tenant.getId()));
|
|
|
if (employeeRespDTO == null) {
|
|
@@ -174,12 +175,16 @@ public class TenantServiceImpl implements TenantService {
|
|
|
// 修改租户的管理员
|
|
|
tenantMapper.updateById(new TenantDO().setId(tenant.getId()).setContactUserId(user.getId()));
|
|
|
});
|
|
|
- return tenant.getId();
|
|
|
+ TenantSimpleRespVO tenantSimpleRespVO = new TenantSimpleRespVO();
|
|
|
+ tenantSimpleRespVO.setId(tenant.getId());
|
|
|
+ tenantSimpleRespVO.setName(tenant.getName());
|
|
|
+ tenantSimpleRespVO.setCorpId(corpId);
|
|
|
+ return tenantSimpleRespVO;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
@DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
- public Long joinTenant(TenantJoinReqVO createReqVO) {
|
|
|
+ public TenantSimpleRespVO joinTenant(TenantJoinReqVO createReqVO) {
|
|
|
// 根据租户邀请码查询租户信息
|
|
|
TenantDO tenant = tenantMapper.selectByCorpId(createReqVO.getCorpId());
|
|
|
if (tenant == null || tenant.getId() == null) {
|
|
@@ -187,12 +192,16 @@ public class TenantServiceImpl implements TenantService {
|
|
|
}
|
|
|
// 获得用户信息
|
|
|
LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
|
|
- // 更新用户信息当前租户
|
|
|
- userService.updateUserTenantId(user.getId(), tenant.getId());
|
|
|
- // 将这个用户所有租户关系设为不生效
|
|
|
- userTenantRelateService.deactivateAllForUser(user.getId());
|
|
|
+ // 校验该用户是否在该租户中
|
|
|
+ if (userTenantRelateService.checkUserHasTenant(user.getId(), tenant.getId())) {
|
|
|
+ throw exception(USER_TENANT_EXISTS, tenant.getName());
|
|
|
+ }
|
|
|
+// // 更新用户信息当前租户
|
|
|
+// userService.updateUserTenantId(user.getId(), tenant.getId());
|
|
|
+// // 将这个用户所有租户关系设为不生效
|
|
|
+// userTenantRelateService.deactivateAllForUser(user.getId());
|
|
|
// 插入用户租户关系
|
|
|
- userTenantRelateService.createUserTenantRelate(new UserTenantRelateSaveReqVO().setUserId(user.getId()).setTenantId(tenant.getId()).setActived(true));
|
|
|
+ userTenantRelateService.createUserTenantRelate(new UserTenantRelateSaveReqVO().setUserId(user.getId()).setTenantId(tenant.getId()).setActived(false));
|
|
|
// 自动根据账号创建对应人事信息,只保存姓名、头像、手机号
|
|
|
AdminUserDO adminUserDO = userService.getUser(user.getId());
|
|
|
if (adminUserDO == null) {
|
|
@@ -206,7 +215,11 @@ public class TenantServiceImpl implements TenantService {
|
|
|
} else {
|
|
|
throw exception(USER_TENANT_EMPLOYEE_DUPLICATE, tenant.getName());
|
|
|
}
|
|
|
- return tenant.getId();
|
|
|
+ TenantSimpleRespVO tenantSimpleRespVO = new TenantSimpleRespVO();
|
|
|
+ tenantSimpleRespVO.setId(tenant.getId());
|
|
|
+ tenantSimpleRespVO.setName(tenant.getName());
|
|
|
+ tenantSimpleRespVO.setCorpId(tenant.getCorpId());
|
|
|
+ return tenantSimpleRespVO;
|
|
|
}
|
|
|
|
|
|
private Long createUser(Long roleId, TenantSaveReqVO createReqVO) {
|
|
@@ -249,6 +262,14 @@ public class TenantServiceImpl implements TenantService {
|
|
|
@Override
|
|
|
@DSTransactional // 多数据源,使用 @DSTransactional 保证本地事务,以及数据源的切换
|
|
|
public void updateTenant(TenantSaveReqVO updateReqVO) {
|
|
|
+ // 1.1 获得用户信息
|
|
|
+ AdminUserDO user = userService.getUser(getLoginUserId());
|
|
|
+ if (user == null || user.getTenantId() == null) {
|
|
|
+ throw exception(USER_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ if (updateReqVO.getId() == null) {
|
|
|
+ updateReqVO.setId(user.getTenantId());
|
|
|
+ }
|
|
|
// 校验存在
|
|
|
TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
|
|
|
// 校验租户名称是否重复
|
|
@@ -292,7 +313,7 @@ public class TenantServiceImpl implements TenantService {
|
|
|
// 将这个用户的这个租户关系设为生效
|
|
|
userTenantRelateService.activateForUser(new UserTenantRelateSaveReqVO().setUserId(user.getId()).setTenantId(tenant.getId()));
|
|
|
authService.logout(createReqVO.getToken(), LoginLogTypeEnum.LOGOUT_CHANGE_TENANT.getType());
|
|
|
- return createTokenAfterLoginSuccess(adminUserDO.getId(), adminUserDO.getUsername(), LoginLogTypeEnum.LOGIN_CHANGE_TENANT);
|
|
|
+ return createTokenAfterLoginSuccess(adminUserDO.getId(), adminUserDO.getUsername(), LoginLogTypeEnum.LOGIN_CHANGE_TENANT, tenant.getId());
|
|
|
}
|
|
|
|
|
|
private void validTenantNameDuplicate(String name, Long id) {
|
|
@@ -376,6 +397,26 @@ public class TenantServiceImpl implements TenantService {
|
|
|
return tenantMapper.selectById(id);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<TenantRespVO> getOwnCreateTenants() {
|
|
|
+ // 获得用户信息
|
|
|
+ LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
|
|
+ if (user == null) {
|
|
|
+ throw exception(USER_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ return tenantMapper.getOwnCreateTenants(user.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<TenantRespVO> getOwnJoinTenants() {
|
|
|
+ // 获得用户信息
|
|
|
+ LoginUser user = SecurityFrameworkUtils.getLoginUser();
|
|
|
+ if (user == null) {
|
|
|
+ throw exception(USER_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ return tenantMapper.getOwnJoinTenants(user.getId());
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public PageResult<TenantDO> getTenantPage(TenantPageReqVO pageReqVO) {
|
|
|
return tenantMapper.selectPage(pageReqVO);
|
|
@@ -444,12 +485,12 @@ public class TenantServiceImpl implements TenantService {
|
|
|
return tenantProperties == null || Boolean.FALSE.equals(tenantProperties.getEnable());
|
|
|
}
|
|
|
|
|
|
- private AuthLoginRespVO createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType) {
|
|
|
+ private AuthLoginRespVO createTokenAfterLoginSuccess(Long userId, String username, LoginLogTypeEnum logType, Long tenantId) {
|
|
|
// 插入登陆日志
|
|
|
createLoginLog(userId, username, logType, LoginResultEnum.SUCCESS);
|
|
|
// 创建访问令牌
|
|
|
OAuth2AccessTokenDO accessTokenDO = oauth2TokenService.createAccessToken(userId, getUserType().getValue(),
|
|
|
- OAuth2ClientConstants.CLIENT_ID_DEFAULT, null);
|
|
|
+ OAuth2ClientConstants.CLIENT_ID_DEFAULT, null, tenantId);
|
|
|
// 构建返回结果
|
|
|
return AuthConvert.INSTANCE.convert(accessTokenDO);
|
|
|
}
|