mirror of https://gitee.com/stylefeng/roses
【7.6.0】更新登录用户临时身份切换方法
parent
039cbdc748
commit
9eff8a8a68
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取系统用户
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue