feat(用户身份状态管理): Forge用户注销后微服务平台用户需如何处理

创建增量同步Forge上已注销账号信息定时任务(每隔五分钟执行一次):
1. 查询定时任务执行开始前十分钟到定时任务开始执行期间的所有Forge用户的账号注销记录
2. 遍历Forge用户注销记录列表,检查微服务平台是否存在有Forge注销记录但是在微服务平台未删除的用户
3. 若存在微服务平台未删除的用户则更新该用户所有身份状态为已注销并且逻辑删除该用户

Signed-off-by: OTTO <731554297@qq.com>
This commit is contained in:
OTTO 2024-10-14 10:40:42 +08:00
parent 20704028cd
commit afdddcb71e
8 changed files with 81 additions and 4 deletions

View File

@ -0,0 +1,16 @@
package com.microservices.system.domain.vo;
import lombok.Data;
import java.util.Date;
/**
* @author OTTO
*/
@Data
public class ForgeUserActionSearchVo {
private Date startTime;
private Date endTime;
// 操作类型为注销账户
private String actionType = "DestroyUser";
}

View File

@ -143,4 +143,12 @@ public interface SysUserDeptRoleMapper {
* @return 用户身份
*/
SysUserDeptRole selectUserIdentityByAllStatus(@Param("deptId") Long deptId, @Param("userId") Long userId);
/**
* 批量更新用户身份状态
*
* @param userId 用户Id
* @param state 状态
*/
void updateUserIdentifyStateByUserId(@Param("userId") Long userId, @Param("state") String state);
}

View File

@ -153,4 +153,11 @@ public interface ISysUserDeptRoleService {
List<SysUserDeptRole> selectZoneUserIdentityListByUser(Long userId, Boolean isContainMember, String deptName, String roleName);
SysUserDeptRole getAllStatusUserIdentityByDeptIdAndUserId(Long deptId, Long userId);
/**
* 注销用户时清理用户身份
*
* @param userId 用户Id
*/
void deleteSysUserDeptRoleByDeleteUser(@Param("userId") Long userId);
}

View File

@ -610,6 +610,11 @@ public class SysUserDeptRoleServiceImpl implements ISysUserDeptRoleService {
return sysUserDeptRoleMapper.selectUserIdentityByAllStatus(deptId, userId);
}
@Override
public void deleteSysUserDeptRoleByDeleteUser(Long userId) {
sysUserDeptRoleMapper.updateUserIdentifyStateByUserId(userId, UserIdentityStatus.DELETE.getState());
}
private SysUser selectSysUserById(Long userId) {
SysUser sysUser = sysUserMapper.selectUserById(userId);
if (sysUser == null) {

View File

@ -137,7 +137,15 @@ public class SysUserServiceImpl implements ISysUserService {
@Override
public SysUser selectUserByUserName(String userName)
{
return userMapper.selectUserByUserName(userName);
SysUser sysUser = userMapper.selectUserByUserName(userName);
return checkUserIsDelete(sysUser);
}
private SysUser checkUserIsDelete(SysUser sysUser) {
if (UserConstants.DEL_FLAG_TRUE.equals(sysUser.getDelFlag())) {
return sysUser.toDeleteUser();
}
return sysUser;
}
/**
@ -600,7 +608,7 @@ public class SysUserServiceImpl implements ISysUserService {
@Override
public SysUser getSysUserByGitLinkUserId(Long gitLinkUserId) {
return userMapper.selectUserByGitLinkUserId(gitLinkUserId);
return checkUserIsDelete(userMapper.selectUserByGitLinkUserId(gitLinkUserId));
}
@Override

View File

@ -1,10 +1,15 @@
package com.microservices.system.task;
import com.microservices.common.core.constant.UserConstants;
import com.microservices.common.core.utils.DateUtils;
import com.microservices.system.api.domain.SysUser;
import com.microservices.system.domain.ForgeUser;
import com.microservices.system.domain.ForgeUserAction;
import com.microservices.system.domain.vo.ForgeUserActionSearchVo;
import com.microservices.system.mapper.ForgeUserActionMapper;
import com.microservices.system.mapper.ForgeUserMapper;
import com.microservices.system.mapper.SysUserMapper;
import com.microservices.system.service.ISysUserDeptRoleService;
import com.microservices.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -12,6 +17,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@ -26,9 +32,13 @@ public class SyncGitLinkUserInfoJob {
@Autowired
private ForgeUserMapper forgeUserMapper;
@Autowired
private ForgeUserActionMapper forgeUserActionMapper;
@Autowired
private ISysUserService sysUserService;
@Autowired
private SysUserMapper sysUserMapper;
@Autowired
private ISysUserDeptRoleService sysUserDeptRoleService;
/**
* 每日凌晨两点执行一次全量用户从GitLink同步信息同时同步Forge上已注销用户信息
@ -39,11 +49,12 @@ public class SyncGitLinkUserInfoJob {
}
/**
* 每隔分钟对GitLink上五分钟内有更新的用户进行一次同步同时同步Forge上已注销用户信息
* 每隔分钟对GitLink上五分钟内有更新的用户进行一次同步同时同步Forge上已注销用户信息
*/
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.MINUTES)
public void syncIncrementGitLinkUserInfo() {
incrementSyncForgeUserInfo();
incrementSyncForgeDeleteUserInfo();
}
// 增量同步Forge用户信息
@ -69,7 +80,24 @@ public class SyncGitLinkUserInfoJob {
// 增量同步Forge上已注销账号信息
private void incrementSyncForgeDeleteUserInfo() {
ForgeUserActionSearchVo forgeUserActionSearchVo = new ForgeUserActionSearchVo();
// 查询定时任务执行开始前十分钟到定时任务开始执行期间的所有Forge用户的账号注销记录
Date now = DateUtils.getNowDate();
forgeUserActionSearchVo.setStartTime(DateUtils.addHours(now, -10));
forgeUserActionSearchVo.setEndTime(now);
List<ForgeUserAction> forgeUserActionList = forgeUserActionMapper.selectForgeUserActionList(forgeUserActionSearchVo);
// 遍历Forge用户注销记录列表检查微服务平台是否存在有Forge注销记录但是在微服务平台未删除的用户
for (ForgeUserAction forgeUserAction : forgeUserActionList) {
SysUser sysUser = sysUserMapper.selectUserByGitLinkUserId(forgeUserAction.getUserId());
if (sysUser != null && UserConstants.DEL_FLAG_FALSE.equals(sysUser.getDelFlag())) {
log.info("检测到用户{}在Forge端已注销将该用户在微服务平台的所有身份标记为已注销同时逻辑删除该用户", sysUser.getUserName());
// 更新用户所有身份为已注销
sysUserDeptRoleService.deleteSysUserDeptRoleByDeleteUser(sysUser.getUserId());
// 逻辑删除该用户
sysUserService.deleteUserById(sysUser.getUserId());
}
}
}
// 全量同步Forge用户信息

View File

@ -266,6 +266,11 @@
#{userId}
</foreach>
</update>
<update id="updateUserIdentifyStateByUserId">
update sys_user_dept_role
set state = #{state}
where user_id = #{userId}
</update>
<delete id="deleteSysUserDeptRoleByDeptId" parameterType="Long">
delete

View File

@ -163,7 +163,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
where u.user_name = #{userName}
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">