Files
ecom-dmb/ecom-dmb-backend/src/main/java/top/xyquan/ecomdmbbackend/utils/CurrentUserContext.java
T

186 lines
6.0 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package top.xyquan.ecomdmbbackend.utils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.modules.open.api.context.OpenApiContextHolder;
import top.xyquan.ecomdmbbackend.modules.open.api.security.OpenApiAuthentication;
import top.xyquan.ecomdmbbackend.modules.open.api.security.OpenApiPrincipal;
import top.xyquan.ecomdmbbackend.modules.org.company.member.model.po.CompanyMemberPO;
import top.xyquan.ecomdmbbackend.modules.org.company.member.service.CompanyMemberService;
/**
* 当前用户上下文工具类
* 提供更安全的当前用户信息获取方式
*/
@Component
public class CurrentUserContext {
private final JwtUtils jwtUtils;
private final CompanyMemberService companyMemberService;
public CurrentUserContext(JwtUtils jwtUtils, @Lazy CompanyMemberService companyMemberService) {
this.jwtUtils = jwtUtils;
this.companyMemberService = companyMemberService;
}
/**
* 获取当前用户Token负载信息
*
* @return Token负载信息
* @throws BusinessException 如果无法获取用户信息
*/
public TokenPayload getCurrentUserPayload() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return TokenPayload.builder()
.userId(String.valueOf(openPrincipal.operatorUserId()))
.companyId(openPrincipal.companyId())
.role("user")
.companyRole("owner")
.build();
}
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
throw new BusinessException("用户未登录 - 认证信息为空");
}
if (authentication instanceof OpenApiAuthentication openApiAuthentication) {
return TokenPayload.builder()
.userId(String.valueOf(openApiAuthentication.getOperatorUserId()))
.companyId(openApiAuthentication.getCompanyId())
.role("user")
.companyRole("owner")
.build();
}
if (authentication.getCredentials() == null) {
throw new BusinessException("用户未登录 - Token信息为空");
}
String token = authentication.getCredentials().toString();
return jwtUtils.getPayloadFromToken(token);
}
/**
* 获取当前用户公司ID
* 改为从 org_company_member 表查(用当前 userId + 当前 token 中的 companyId)
*
* @return 公司ID
* @throws BusinessException 如果无法获取公司信息
*/
public Long getCurrentCompanyId() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return openPrincipal.companyId();
}
TokenPayload payload = getCurrentUserPayload();
if (payload.getCompanyId() == null) {
throw new BusinessException("无法获取公司信息");
}
return payload.getCompanyId();
}
/**
* 获取当前用户ID
*
* @return 用户ID
* @throws BusinessException 如果无法获取用户信息
*/
public Long getCurrentUserId() {
OpenApiPrincipal openPrincipal = OpenApiContextHolder.get();
if (openPrincipal != null) {
return openPrincipal.operatorUserId();
}
TokenPayload payload = getCurrentUserPayload();
try {
return Long.parseLong(payload.getUserId());
} catch (NumberFormatException e) {
throw new BusinessException("用户ID格式错误");
}
}
/**
* 是否为开放 API 请求(API Key 认证)
*/
public boolean isOpenApiRequest() {
return OpenApiContextHolder.isOpenApiRequest();
}
/**
* 获取当前用户角色
*
* @return 用户角色
*/
public String getCurrentUserRole() {
return getCurrentUserPayload().getRole();
}
/**
* 获取当前用户公司成员角色
*
* @return 公司成员角色 (owner/member)
*/
public String getCurrentCompanyRole() {
return getCurrentUserPayload().getCompanyRole();
}
/**
* 获取当前用户岗位ID
*
* @return 岗位ID
*/
public Long getCurrentUserPositionId() {
return getCurrentUserPayload().getPositionId();
}
/**
* 获取当前用户认证信息
*
* @return 认证信息
*/
public Authentication getCurrentAuthentication() {
return SecurityContextHolder.getContext().getAuthentication();
}
/**
* 检查是否已登录
*
* @return 是否已登录
*/
public boolean isAuthenticated() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && authentication.isAuthenticated();
}
/**
* 获取当前用户在公司中的成员信息
*
* @return CompanyMemberPO
* @throws BusinessException 如果无法获取成员信息
*/
public CompanyMemberPO getCurrentMember() {
Long userId = getCurrentUserId();
Long companyId = getCurrentCompanyId();
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyId);
if (member == null) {
throw new BusinessException("用户不属于当前公司");
}
return member;
}
/**
* 获取当前用户在公司中的成员角色
*
* @return "owner" 或 "member"
* @throws BusinessException 如果无法获取成员信息
*/
public String getCurrentMemberRole() {
CompanyMemberPO member = getCurrentMember();
return member.getRole().getCode();
}
}