【7.6.0】更新登录用户临时身份切换方法

dev-7.6.0
fengshuonan 2023-11-16 21:58:03 +08:00
parent 039cbdc748
commit 9eff8a8a68
5 changed files with 157 additions and 7 deletions

View File

@ -27,6 +27,8 @@ package cn.stylefeng.roses.kernel.auth.api;
import cn.stylefeng.roses.kernel.auth.api.exception.AuthException;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import java.util.function.Consumer;
/**
*
*
@ -97,4 +99,35 @@ public interface LoginUserApi {
*/
boolean haveButton(String buttonCode);
/**
* id
* <p>
* 线使endSwitch()
*
* @param userId
* @author fengshuonan
* @since 2023/11/16 21:03
*/
LoginUser switchTo(Long userId);
/**
*
* <p>
* 线
*
* @author fengshuonan
* @since 2023/11/16 21:03
*/
void endSwitch();
/**
*
*
* @param userId id
* @param consumer
* @author fengshuonan
* @since 2023/11/16 21:12
*/
void switchTo(Long userId, Consumer<Long> consumer);
}

View File

@ -24,6 +24,9 @@
*/
package cn.stylefeng.roses.kernel.auth.auth;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.auth.api.LoginUserApi;
import cn.stylefeng.roses.kernel.auth.api.SessionManagerApi;
import cn.stylefeng.roses.kernel.auth.api.context.LoginUserHolder;
@ -32,9 +35,11 @@ import cn.stylefeng.roses.kernel.auth.api.exception.enums.AuthExceptionEnum;
import cn.stylefeng.roses.kernel.auth.api.loginuser.CommonLoginUserUtil;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.system.api.UserServiceApi;
import cn.stylefeng.roses.kernel.system.api.pojo.user.TempLoginUserInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.function.Consumer;
/**
*
@ -132,4 +137,42 @@ public class LoginUserImpl implements LoginUserApi {
}
}
@Override
public LoginUser switchTo(Long userId) {
if (ObjectUtil.isEmpty(userId)) {
return null;
}
// 获取用户的基本信息
TempLoginUserInfo tempLoginUserInfo = userServiceApi.createTempUserInfo(userId);
if (tempLoginUserInfo == null) {
return null;
}
// 创建临时登录用户
LoginUser loginUser = new LoginUser();
BeanUtil.copyProperties(tempLoginUserInfo, loginUser, CopyOptions.create().ignoreError());
// 设置用户的临时token
loginUser.setToken("tempToken");
// 放入线程变量中
LoginUserHolder.set(loginUser);
return loginUser;
}
@Override
public void endSwitch() {
LoginUserHolder.remove();
}
@Override
public void switchTo(Long userId, Consumer<Long> consumer) {
this.switchTo(userId);
consumer.accept(userId);
this.endSwitch();
}
}

View File

@ -25,10 +25,7 @@
package cn.stylefeng.roses.kernel.system.api;
import cn.stylefeng.roses.kernel.auth.api.pojo.login.LoginUser;
import cn.stylefeng.roses.kernel.system.api.pojo.user.OAuth2AuthUserDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.user.OnlineUserDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.user.SysUserDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.user.UserLoginInfoDTO;
import cn.stylefeng.roses.kernel.system.api.pojo.user.*;
import cn.stylefeng.roses.kernel.system.api.pojo.user.request.OnlineUserRequest;
import cn.stylefeng.roses.kernel.system.api.pojo.user.request.SysUserRequest;
@ -178,4 +175,12 @@ public interface UserServiceApi {
*/
SysUserDTO createAndSaveOAuth2User(OAuth2AuthUserDTO oAuth2AuthUserDTO);
/**
*
*
* @author fengshuonan
* @since 2023/11/16 21:39
*/
TempLoginUserInfo createTempUserInfo(Long userId);
}

View File

@ -0,0 +1,41 @@
package cn.stylefeng.roses.kernel.system.api.pojo.user;
import lombok.Data;
import java.util.Date;
/**
*
*
* @author fengshuonan
* @since 2023/11/16 21:38
*/
@Data
public class TempLoginUserInfo {
/**
* id
*/
private Long userId;
/**
*
*/
private String account;
/**
* id
*/
private Long tenantId;
/**
* IP
*/
private String loginIp;
/**
*
*/
private Date loginTime;
}

View File

@ -307,7 +307,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
// 更新枚举,更新只能更新未删除状态的
LambdaUpdateWrapper<SysUser> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.eq(SysUser::getUserId, id).and(i -> i.ne(SysUser::getDelFlag, YesOrNotEnum.Y.getCode())).set(SysUser::getStatusFlag, statusFlag);
updateWrapper.eq(SysUser::getUserId, id).and(i -> i.ne(SysUser::getDelFlag, YesOrNotEnum.Y.getCode()))
.set(SysUser::getStatusFlag, statusFlag);
boolean update = this.update(updateWrapper);
if (!update) {
@ -461,7 +462,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
sysUserRequest.setUserScopeIds(null);
}
// 如果是按部门数据划分
else if (dataScopeTypeEnums.contains(DataScopeTypeEnum.DEPT) || dataScopeTypeEnums.contains(DataScopeTypeEnum.DEPT_WITH_CHILD) || dataScopeTypeEnums.contains(DataScopeTypeEnum.DEFINE)) {
else if (dataScopeTypeEnums.contains(DataScopeTypeEnum.DEPT) || dataScopeTypeEnums.contains(
DataScopeTypeEnum.DEPT_WITH_CHILD) || dataScopeTypeEnums.contains(DataScopeTypeEnum.DEFINE)) {
sysUserRequest.setScopeOrgIds(dataScopeOrganizationIds);
sysUserRequest.setUserScopeIds(null);
}
@ -820,7 +822,8 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
Set<String> roleButtonCodes = roleServiceApi.getRoleButtonCodes(roleIds);
// 7. 组装响应结果
return UserLoginInfoFactory.userLoginInfoDTO(sysUser, roleResponseList, dataScopeResponse, userOrgInfo, resourceUrlsListByCodes, roleButtonCodes, userMenuType);
return UserLoginInfoFactory.userLoginInfoDTO(sysUser, roleResponseList, dataScopeResponse, userOrgInfo, resourceUrlsListByCodes,
roleButtonCodes, userMenuType);
}
@Override
@ -1032,6 +1035,31 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
return BeanUtil.copyProperties(oAuth2User, SysUserDTO.class);
}
@Override
public TempLoginUserInfo createTempUserInfo(Long userId) {
if (ObjectUtil.isEmpty(userId)) {
return null;
}
// 查询用户id对应的用户信息
LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(SysUser::getUserId, userId);
wrapper.select(SysUser::getUserId, SysUser::getAccount, SysUser::getLastLoginIp, SysUser::getLastLoginTime);
SysUser sysUser = this.getOne(wrapper);
if (sysUser == null) {
return null;
}
TempLoginUserInfo tempLoginUserInfo = new TempLoginUserInfo();
tempLoginUserInfo.setUserId(sysUser.getUserId());
tempLoginUserInfo.setAccount(sysUser.getAccount());
tempLoginUserInfo.setLoginTime(sysUser.getLastLoginTime());
tempLoginUserInfo.setLoginIp(sysUser.getLastLoginIp());
return tempLoginUserInfo;
}
/**
*
*