Commit 7f6c219f6dc20542931bb55bd7a6591f09971545

Authored by lifeontrip
1 parent 9fdda43e

fix: 租户角色不存在时,第二次提交表单提示用户名已经存在的问题

1、回滚提交记录【租户角色不存在,会留存脏数据】
2、添加租户管理员时验证租户角色是否存在
@@ -185,9 +185,9 @@ public class YtUserController extends AbstractUserAccount { @@ -185,9 +185,9 @@ public class YtUserController extends AbstractUserAccount {
185 throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage()); 185 throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
186 } 186 }
187 userService.validateUserNameAndPhoneNumberAndEmail(userDTO); 187 userService.validateUserNameAndPhoneNumberAndEmail(userDTO);
  188 + userService.validateTenantRole(userDTO.getTenantId());
  189 + //租户角色不存在,会留存脏数据
188 TenantId tenantId = TenantId.fromUUID(UUID.fromString(userDTO.getTenantId())); 190 TenantId tenantId = TenantId.fromUUID(UUID.fromString(userDTO.getTenantId()));
189 - UserDTO returnUser = userService.saveTenantAdmin(  
190 - userDTO, getCurrentUser().isPtSysadmin(), tenantId.getId().toString());  
191 try { 191 try {
192 // 创建TB的租户管理员 192 // 创建TB的租户管理员
193 CustomerId customerId = new CustomerId(EntityId.NULL_UUID); 193 CustomerId customerId = new CustomerId(EntityId.NULL_UUID);
@@ -202,7 +202,8 @@ public class YtUserController extends AbstractUserAccount { @@ -202,7 +202,8 @@ public class YtUserController extends AbstractUserAccount {
202 throw handleException(e); 202 throw handleException(e);
203 } 203 }
204 204
205 - return returnUser; 205 + return userService.saveTenantAdmin(
  206 + userDTO, getCurrentUser().isPtSysadmin(), tenantId.getId().toString());
206 } 207 }
207 208
208 @DeleteMapping 209 @DeleteMapping
@@ -427,15 +427,7 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> @@ -427,15 +427,7 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User>
427 throw new YtDataValidationException("用户已存在"); 427 throw new YtDataValidationException("用户已存在");
428 } 428 }
429 baseMapper.insert(user); 429 baseMapper.insert(user);
430 - List<TenantRole> tenantRoleList =  
431 - tenantRoleMapper.selectList(  
432 - new QueryWrapper<TenantRole>()  
433 - .lambda()  
434 - .eq(TenantRole::getTenantId, userDTO.getTenantId()));  
435 - // 保存用户与角色的映射信息  
436 - if (null == tenantRoleList || tenantRoleList.size() == 0) {  
437 - throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());  
438 - } 430 + List<TenantRole> tenantRoleList =validateTenantRole(userDTO.getTenantId()) ;
439 for (TenantRole tenantRole : tenantRoleList) { 431 for (TenantRole tenantRole : tenantRoleList) {
440 roleMapper.saveUserRoleMapping(user.getId(), tenantRole.getRoleId()); 432 roleMapper.saveUserRoleMapping(user.getId(), tenantRole.getRoleId());
441 } 433 }
@@ -444,6 +436,20 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User> @@ -444,6 +436,20 @@ public class YtUserServiceImpl extends AbstractBaseService<UserMapper, User>
444 } 436 }
445 437
446 @Override 438 @Override
  439 + public List<TenantRole> validateTenantRole(String tenantId) {
  440 + List<TenantRole> tenantRoleList =
  441 + tenantRoleMapper.selectList(
  442 + new QueryWrapper<TenantRole>()
  443 + .lambda()
  444 + .eq(TenantRole::getTenantId, tenantId));
  445 + // 保存用户与角色的映射信息
  446 + if (null == tenantRoleList || tenantRoleList.size() == 0) {
  447 + throw new YtDataValidationException(ErrorMessage.INVALID_PARAMETER.getMessage());
  448 + }
  449 + return tenantRoleList;
  450 + }
  451 +
  452 + @Override
447 @Transactional 453 @Transactional
448 public void resetPassword(String userId, String tenantId, String password) { 454 public void resetPassword(String userId, String tenantId, String password) {
449 UserDTO userDTO = findUserInfoById(userId); 455 UserDTO userDTO = findUserInfoById(userId);
@@ -8,6 +8,7 @@ import org.thingsboard.server.common.data.yunteng.dto.request.AccountReqDTO; @@ -8,6 +8,7 @@ import org.thingsboard.server.common.data.yunteng.dto.request.AccountReqDTO;
8 import org.thingsboard.server.common.data.yunteng.dto.request.RoleOrOrganizationReqDTO; 8 import org.thingsboard.server.common.data.yunteng.dto.request.RoleOrOrganizationReqDTO;
9 import org.thingsboard.server.common.data.yunteng.dto.request.SendResetPasswordEmailMsg; 9 import org.thingsboard.server.common.data.yunteng.dto.request.SendResetPasswordEmailMsg;
10 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData; 10 import org.thingsboard.server.common.data.yunteng.utils.tools.YtPageData;
  11 +import org.thingsboard.server.dao.yunteng.entities.TenantRole;
11 import org.thingsboard.server.dao.yunteng.entities.User; 12 import org.thingsboard.server.dao.yunteng.entities.User;
12 13
13 import java.time.LocalDateTime; 14 import java.time.LocalDateTime;
@@ -48,6 +49,12 @@ public interface YtUserService { @@ -48,6 +49,12 @@ public interface YtUserService {
48 49
49 void validateUserNameAndPhoneNumberAndEmail(UserDTO userDTO); 50 void validateUserNameAndPhoneNumberAndEmail(UserDTO userDTO);
50 51
  52 + /**
  53 + * 验证租户角色是否有效
  54 + * @param tenantId
  55 + */
  56 + List<TenantRole> validateTenantRole(String tenantId);
  57 +
51 UserDTO findUserInfoById(String id); 58 UserDTO findUserInfoById(String id);
52 /** 59 /**
53 * 发送密码重置新消息 60 * 发送密码重置新消息