fix(user): 修复设为开发者时角色替换和同步问题
- 替换用户角色为 developer,确保用户仅拥有该单一角色 - 在设为开发者后移除用户现有角色并绑定 developer 角色 - 角色替换失败时记录警告日志但不影响标记过程 - 保持通过MQ异步同步更新后的用户数据到 websopy - 增加相关日志以便追踪操作流程
This commit is contained in:
@@ -13,6 +13,7 @@ import com.gxwebsoft.common.core.web.PageResult;
|
|||||||
import com.gxwebsoft.common.system.entity.*;
|
import com.gxwebsoft.common.system.entity.*;
|
||||||
import com.gxwebsoft.common.system.mapper.UserMapper;
|
import com.gxwebsoft.common.system.mapper.UserMapper;
|
||||||
import com.gxwebsoft.common.system.param.LoginParam;
|
import com.gxwebsoft.common.system.param.LoginParam;
|
||||||
|
import com.gxwebsoft.common.system.param.RoleParam;
|
||||||
import com.gxwebsoft.common.system.param.UserParam;
|
import com.gxwebsoft.common.system.param.UserParam;
|
||||||
import com.gxwebsoft.common.mq.producer.SyncMessageProducer;
|
import com.gxwebsoft.common.mq.producer.SyncMessageProducer;
|
||||||
import com.gxwebsoft.common.system.service.*;
|
import com.gxwebsoft.common.system.service.*;
|
||||||
@@ -26,6 +27,7 @@ import org.springframework.transaction.annotation.Isolation;
|
|||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@@ -223,14 +225,36 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
|
|||||||
user.setUserId(userId);
|
user.setUserId(userId);
|
||||||
user.setIsDeveloper(true);
|
user.setIsDeveloper(true);
|
||||||
boolean result = updateById(user);
|
boolean result = updateById(user);
|
||||||
|
if (result) {
|
||||||
|
// 替换用户角色为 developer:先按用户所在租户找到 developer 角色,删除该用户现有角色后
|
||||||
|
// 只绑定 developer,保证用户最终只有「开发者」单一角色(不与原角色并存)
|
||||||
|
try {
|
||||||
|
User dbUser = getById(userId);
|
||||||
|
Integer tenantId = dbUser != null ? dbUser.getTenantId() : null;
|
||||||
|
RoleParam roleParam = new RoleParam();
|
||||||
|
roleParam.setRoleCode("developer");
|
||||||
|
if (tenantId != null) {
|
||||||
|
roleParam.setTenantId(tenantId);
|
||||||
|
}
|
||||||
|
Role developerRole = roleService.getByRoleCode(roleParam);
|
||||||
|
if (developerRole != null && developerRole.getRoleId() != null) {
|
||||||
|
userRoleService.remove(new LambdaUpdateWrapper<UserRole>().eq(UserRole::getUserId, userId));
|
||||||
|
userRoleService.saveBatch(userId, Collections.singletonList(developerRole.getRoleId()));
|
||||||
|
log.info("用户设为开发者并替换角色为 developer: userId={}, roleId={}", userId, developerRole.getRoleId());
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 角色替换失败不影响 is_developer 标记位落库
|
||||||
|
log.warn("替换用户角色为 developer 失败(不影响 is_developer 标记): userId={}", userId, e);
|
||||||
|
}
|
||||||
// 标记开发者后,通过MQ异步同步用户数据到 websopy
|
// 标记开发者后,通过MQ异步同步用户数据到 websopy
|
||||||
if (result && syncMessageProducer != null) {
|
if (syncMessageProducer != null) {
|
||||||
User updatedUser = getAllByUserId(String.valueOf(userId));
|
User updatedUser = getAllByUserId(String.valueOf(userId));
|
||||||
if (updatedUser != null) {
|
if (updatedUser != null) {
|
||||||
syncMessageProducer.sendUserSyncMessage("websopy", "UPDATE", updatedUser);
|
syncMessageProducer.sendUserSyncMessage("websopy", "UPDATE", updatedUser);
|
||||||
log.info("用户设为开发者后同步到websopy: userId={}", userId);
|
log.info("用户设为开发者后同步到websopy: userId={}", userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user