mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
feat(auth): Token 负载增加 companyRole 字段
1. TokenPayload/AccessTokenPayload 增加 companyRole 字段 2. JwtUtils 生成和解析 Token 时包含 companyRole 3. UserServiceImpl 登录时从 org_company_member 获取 companyRole 写入 Token 4. CurrentUserContext 增加 getCurrentCompanyRole() 方法从 Token 获取 5. RoleAspect/PermissionAspect 改用 Token 中的 companyRole 直接判断权限
This commit is contained in:
+5
-12
@@ -63,18 +63,11 @@ public class PermissionAspect {
|
||||
case "user":
|
||||
// 普通用户检查公司成员角色
|
||||
if (annotation.allowCompany()) {
|
||||
try {
|
||||
String memberRole = currentUserContext.getCurrentMemberRole();
|
||||
if ("owner".equals(memberRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("权限验证 - 用户是公司拥有者,视为公司管理员");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser() &&
|
||||
permissionCheckService.hasPermissionByPosition(positionId, annotation.value(), companyId);
|
||||
}
|
||||
} catch (BusinessException e) {
|
||||
// 无法获取公司成员角色,降为普通用户权限
|
||||
log.debug("无法获取公司成员角色: {}", e.getMessage());
|
||||
String companyRole = currentUserContext.getCurrentCompanyRole();
|
||||
if ("owner".equals(companyRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("权限验证 - 用户是公司拥有者,视为公司管理员");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser() &&
|
||||
permissionCheckService.hasPermissionByPosition(positionId, annotation.value(), companyId);
|
||||
}
|
||||
|
||||
+5
-11
@@ -58,17 +58,11 @@ public class RoleAspect {
|
||||
case "user":
|
||||
// 普通用户检查公司成员角色,如果是 owner 则视为公司管理员
|
||||
if (annotation.allowCompany()) {
|
||||
try {
|
||||
String memberRole = currentUserContext.getCurrentMemberRole();
|
||||
if ("owner".equals(memberRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("角色权限验证 - 用户是公司拥有者,视为公司管理员");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
} catch (BusinessException e) {
|
||||
// 无法获取公司成员角色,降为普通用户权限
|
||||
log.debug("无法获取公司成员角色: {}", e.getMessage());
|
||||
String companyRole = currentUserContext.getCurrentCompanyRole();
|
||||
if ("owner".equals(companyRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("角色权限验证 - 用户是公司拥有者,视为公司管理员");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
} else {
|
||||
|
||||
+18
-1
@@ -19,6 +19,7 @@ import top.xyquan.ecomdmbbackend.modules.sys.user.model.dto.UpdateUserInfoDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.model.dto.UserFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.model.po.UserPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.company.model.po.CompanyPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.org.companymember.enums.CompanyMemberRole;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.auth.model.vo.LoginVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.model.vo.UserVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.user.service.UserService;
|
||||
@@ -536,7 +537,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// lastCompanyId 有效,直接从成员列表找对应的 positionId
|
||||
// lastCompanyId 有效,直接从成员列表找对应的 positionId 和 companyRole
|
||||
for (var member : memberships) {
|
||||
if (member.getCompanyId().equals(companyIdForToken)) {
|
||||
positionIdForToken = member.getPositionId();
|
||||
@@ -549,11 +550,27 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
throw new BusinessException("无可用公司,无法登录");
|
||||
}
|
||||
|
||||
// 从成员列表获取公司成员角色
|
||||
String companyRoleForToken = null;
|
||||
for (var member : memberships) {
|
||||
if (member.getCompanyId().equals(companyIdForToken)) {
|
||||
// MyBatis 可能将 ENUM 映射为 String,统一转 String 处理
|
||||
Object roleObj = member.getRole();
|
||||
if (roleObj instanceof CompanyMemberRole) {
|
||||
companyRoleForToken = ((CompanyMemberRole) roleObj).name();
|
||||
} else {
|
||||
companyRoleForToken = String.valueOf(roleObj);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 生成访问令牌负载信息
|
||||
AccessTokenPayload accessPayload = AccessTokenPayload.builder()
|
||||
.userId(user.getId().toString())
|
||||
.companyId(companyIdForToken)
|
||||
.role(role != null ? role : user.getRole())
|
||||
.companyRole(companyRoleForToken)
|
||||
.positionId(positionIdForToken)
|
||||
.build();
|
||||
|
||||
|
||||
@@ -30,6 +30,11 @@ public class AccessTokenPayload {
|
||||
*/
|
||||
private String role;
|
||||
|
||||
/**
|
||||
* 公司成员角色 (owner/member)
|
||||
*/
|
||||
private String companyRole;
|
||||
|
||||
/**
|
||||
* 岗位ID
|
||||
*/
|
||||
|
||||
@@ -82,6 +82,15 @@ public class CurrentUserContext {
|
||||
return getCurrentUserPayload().getRole();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户公司成员角色
|
||||
*
|
||||
* @return 公司成员角色 (owner/member)
|
||||
*/
|
||||
public String getCurrentCompanyRole() {
|
||||
return getCurrentUserPayload().getCompanyRole();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户岗位ID
|
||||
*
|
||||
|
||||
@@ -51,6 +51,7 @@ public class JwtUtils {
|
||||
.setSubject(payload.getUserId())
|
||||
.claim("companyId", payload.getCompanyId())
|
||||
.claim("role", payload.getRole())
|
||||
.claim("companyRole", payload.getCompanyRole())
|
||||
.claim("positionId", payload.getPositionId())
|
||||
.claim("tokenType", payload.getTokenType())
|
||||
.setIssuedAt(now)
|
||||
@@ -92,6 +93,7 @@ public class JwtUtils {
|
||||
.userId(payload.getUserId())
|
||||
.companyId(payload.getCompanyId())
|
||||
.role(payload.getRole())
|
||||
.companyRole(payload.getCompanyRole())
|
||||
.positionId(payload.getPositionId())
|
||||
.build();
|
||||
return generateAccessToken(accessPayload);
|
||||
@@ -151,6 +153,7 @@ public class JwtUtils {
|
||||
.userId(claims.getSubject())
|
||||
.companyId(getLongFromClaims(claims, "companyId"))
|
||||
.role(getStringFromClaims(claims, "role"))
|
||||
.companyRole(getStringFromClaims(claims, "companyRole"))
|
||||
.positionId(getLongFromClaims(claims, "positionId"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -29,6 +29,11 @@ public class TokenPayload {
|
||||
*/
|
||||
private String role;
|
||||
|
||||
/**
|
||||
* 公司成员角色 (owner/member)
|
||||
*/
|
||||
private String companyRole;
|
||||
|
||||
/**
|
||||
* 岗位ID
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user