mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
refactor: 重构超管相关
This commit is contained in:
@@ -15,7 +15,7 @@ CREATE TABLE `sys_user` (
|
||||
`real_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名称',
|
||||
`avatar` varchar(500) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '头像路径(冗余字段)',
|
||||
`avatar_image_id` bigint NULL DEFAULT NULL COMMENT '头像文件ID',
|
||||
`role` enum('user','super') CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT 'user' COMMENT '关联角色(user-普通账户 super-超管账户)',
|
||||
`super` tinyint NOT NULL DEFAULT 0 COMMENT '是否超级管理员 (0-否 1-是)',
|
||||
`pwd_security_level` tinyint UNSIGNED NULL DEFAULT NULL COMMENT '密码安全级别(0-初始 1-很弱 2-弱 3-安全 4-很安全)',
|
||||
`pwd_update_time` timestamp NULL DEFAULT NULL COMMENT '密码最后更新时间',
|
||||
`last_login_ip` varchar(45) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '最后登录 ip',
|
||||
@@ -32,7 +32,7 @@ CREATE TABLE `sys_user` (
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `uk_login_code`(`login_code` ASC) USING BTREE,
|
||||
UNIQUE INDEX `uk_phone`(`phone` ASC) USING BTREE,
|
||||
INDEX `idx_role`(`role` ASC) USING BTREE,
|
||||
INDEX `idx_super`(`super` ASC) USING BTREE,
|
||||
INDEX `idx_status`(`status` ASC) USING BTREE,
|
||||
INDEX `fk_user_avatar_image`(`avatar_image_id` ASC) USING BTREE,
|
||||
INDEX `fk_user_last_company`(`last_company_id` ASC) USING BTREE,
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
-- ========================================
|
||||
-- sys_user.role(enum) -> sys_user.super(tinyint)
|
||||
-- 通过 super 字段判定是否为平台超管(0-否 1-是)
|
||||
-- ========================================
|
||||
|
||||
ALTER TABLE `sys_user`
|
||||
ADD COLUMN `super` tinyint NOT NULL DEFAULT 0 COMMENT '是否超级管理员 (0-否 1-是)' AFTER `avatar_image_id`;
|
||||
|
||||
UPDATE `sys_user` SET `super` = 1 WHERE `role` = 'super';
|
||||
|
||||
ALTER TABLE `sys_user` DROP INDEX `idx_role`;
|
||||
|
||||
ALTER TABLE `sys_user` DROP COLUMN `role`;
|
||||
|
||||
ALTER TABLE `sys_user` ADD INDEX `idx_super`(`super` ASC) USING BTREE;
|
||||
+2
-2
@@ -10,8 +10,8 @@ import java.lang.annotation.Target;
|
||||
* 用于标记需要权限验证的接口
|
||||
*
|
||||
* 权限判定逻辑:
|
||||
* - super 角色:直接放行(拥有所有权限)
|
||||
* - user 角色:从 org_position_permission 表查询岗位拥有的权限码进行判定
|
||||
* - 超管(Token.super=true):直接放行(拥有所有权限)
|
||||
* - 普通用户:从 org_position_permission 表查询岗位拥有的权限码进行判定
|
||||
*/
|
||||
@Target({ ElementType.METHOD, ElementType.TYPE })
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ import java.lang.annotation.Target;
|
||||
public @interface RequireRole {
|
||||
|
||||
/**
|
||||
* 允许的角色列表
|
||||
* 允许的角色列表(已废弃,保留仅为兼容;判定改用 allowSuper/allowCompany/allowUser)
|
||||
*/
|
||||
String[] value() default {};
|
||||
|
||||
|
||||
+15
-35
@@ -18,8 +18,8 @@ import java.lang.reflect.Method;
|
||||
* 用于拦截带有@RequirePermission注解的方法并进行权限验证
|
||||
* 权限判定逻辑:
|
||||
* - super:按 allowSuper 放行
|
||||
* - user + owner:仅校验平台分配给公司的权限(org_company_permission),不校验岗位
|
||||
* - user + member:公司权限 + 岗位权限双校验
|
||||
* - 非超管 + owner:仅校验平台分配给公司的权限(org_company_permission),不校验岗位
|
||||
* - 非超管 + member:公司权限 + 岗位权限双校验
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@@ -44,50 +44,30 @@ public class PermissionAspect {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
// 获取当前用户信息
|
||||
String role = currentUserContext.getCurrentUserPayload().getRole();
|
||||
boolean isSuper = Boolean.TRUE.equals(currentUserContext.getCurrentUserPayload().getIsSuper());
|
||||
String companyRole = currentUserContext.getCurrentUserPayload().getCompanyRole();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
Long positionId = currentUserContext.getCurrentUserPayload().getPositionId();
|
||||
|
||||
log.debug("权限验证 - 用户角色: {}, 公司角色: {}, 公司ID: {}, 岗位ID: {}, 权限码: {}",
|
||||
role, companyRole, companyId, positionId, annotation.value());
|
||||
log.debug("权限验证 - 超管: {}, 公司角色: {}, 公司ID: {}, 岗位ID: {}, 权限码: {}",
|
||||
isSuper, companyRole, companyId, positionId, annotation.value());
|
||||
|
||||
// 检查角色是否为空
|
||||
if (role == null) {
|
||||
throw new BusinessException("用户角色信息缺失");
|
||||
}
|
||||
|
||||
// 根据角色进行权限验证
|
||||
boolean hasPermission = false;
|
||||
|
||||
switch (role) {
|
||||
case "super":
|
||||
// 超级管理员拥有所有权限
|
||||
hasPermission = annotation.allowSuper();
|
||||
break;
|
||||
case "user":
|
||||
// owner:拥有平台分配给该公司的全部权限(不含岗位细分)
|
||||
if ("owner".equalsIgnoreCase(companyRole)) {
|
||||
hasPermission = permissionCheckService.hasCompanyAssignedPermission(
|
||||
companyId, annotation.value());
|
||||
} else {
|
||||
// member 走公司+岗位双校验
|
||||
hasPermission = permissionCheckService.hasPermissionByPosition(
|
||||
positionId, annotation.value(), companyId);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new BusinessException("未知的用户角色: " + role);
|
||||
boolean hasPermission;
|
||||
if (isSuper) {
|
||||
hasPermission = annotation.allowSuper();
|
||||
} else if ("owner".equalsIgnoreCase(companyRole)) {
|
||||
hasPermission = permissionCheckService.hasCompanyAssignedPermission(companyId, annotation.value());
|
||||
} else {
|
||||
hasPermission = permissionCheckService.hasPermissionByPosition(positionId, annotation.value(), companyId);
|
||||
}
|
||||
|
||||
if (!hasPermission) {
|
||||
log.warn("权限验证失败 - 用户角色: {}, 岗位ID: {}, 权限码: {}, 描述: {}",
|
||||
role, positionId, annotation.value(), annotation.description());
|
||||
log.warn("权限验证失败 - 超管: {}, 岗位ID: {}, 权限码: {}, 描述: {}",
|
||||
isSuper, positionId, annotation.value(), annotation.description());
|
||||
throw new BusinessException("权限不足: " + annotation.description());
|
||||
}
|
||||
|
||||
log.debug("权限验证通过 - 用户角色: {}, 权限码: {}", role, annotation.value());
|
||||
log.debug("权限验证通过 - 超管: {}, 权限码: {}", isSuper, annotation.value());
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
+18
-40
@@ -11,11 +11,10 @@ import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* 角色权限验证切面
|
||||
* 用于简单的角色权限控制
|
||||
* 通过 Token 中的 super 字段判定超管,其余按公司成员角色判定
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@@ -38,52 +37,31 @@ public class RoleAspect {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
// 获取当前用户系统角色
|
||||
String sysRole = currentUserContext.getCurrentUserPayload().getRole();
|
||||
boolean isSuper = Boolean.TRUE.equals(currentUserContext.getCurrentUserPayload().getIsSuper());
|
||||
|
||||
log.debug("角色权限验证 - 用户系统角色: {}, 注解配置: {}", sysRole, annotation.description());
|
||||
log.debug("角色权限验证 - 超管: {}, 注解配置: {}", isSuper, annotation.description());
|
||||
|
||||
// 检查系统角色是否为空
|
||||
if (sysRole == null) {
|
||||
throw new BusinessException("用户角色信息缺失");
|
||||
}
|
||||
|
||||
// 根据系统角色进行权限验证
|
||||
boolean hasPermission = false;
|
||||
|
||||
switch (sysRole) {
|
||||
case "super":
|
||||
hasPermission = annotation.allowSuper();
|
||||
break;
|
||||
case "user":
|
||||
// 普通用户检查公司成员角色,如果是 owner 则视为公司拥有者
|
||||
if (annotation.allowCompany()) {
|
||||
String companyRole = currentUserContext.getCurrentCompanyRole();
|
||||
if ("owner".equals(companyRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("角色权限验证 - 用户是公司拥有者");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
break;
|
||||
default:
|
||||
throw new BusinessException("未知的用户角色: " + sysRole);
|
||||
}
|
||||
|
||||
// 检查是否在允许的角色列表中
|
||||
if (annotation.value().length > 0) {
|
||||
hasPermission = hasPermission && Arrays.asList(annotation.value()).contains(sysRole);
|
||||
boolean hasPermission;
|
||||
if (isSuper) {
|
||||
hasPermission = annotation.allowSuper();
|
||||
} else if (annotation.allowCompany()) {
|
||||
String companyRole = currentUserContext.getCurrentCompanyRole();
|
||||
if ("owner".equals(companyRole)) {
|
||||
hasPermission = true;
|
||||
log.debug("角色权限验证 - 用户是公司拥有者");
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
} else {
|
||||
hasPermission = annotation.allowUser();
|
||||
}
|
||||
|
||||
if (!hasPermission) {
|
||||
log.warn("角色权限验证失败 - 用户系统角色: {}, 描述: {}", sysRole, annotation.description());
|
||||
log.warn("角色权限验证失败 - 超管: {}, 描述: {}", isSuper, annotation.description());
|
||||
throw new BusinessException("权限不足: " + annotation.description());
|
||||
}
|
||||
|
||||
log.debug("角色权限验证通过 - 用户系统角色: {}", sysRole);
|
||||
log.debug("角色权限验证通过 - 超管: {}", isSuper);
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -31,8 +31,8 @@ public class AccountFilterDTO {
|
||||
@Schema(description = "用户ID列表", example = "[1,2,3]")
|
||||
private java.util.List<Long> userIds;
|
||||
|
||||
@Schema(description = "角色", example = "user")
|
||||
private String role;
|
||||
@Schema(description = "是否超级管理员", example = "0")
|
||||
private Integer isSuper;
|
||||
|
||||
@Schema(description = "状态(0-正常 1-删除 2-停用 3-冻结)", example = "0")
|
||||
private Integer status;
|
||||
|
||||
+3
-3
@@ -44,9 +44,9 @@ public class SuperCreateAccountDTO {
|
||||
@Schema(description = "岗位ID", example = "1")
|
||||
private Long positionId;
|
||||
|
||||
@NotBlank(message = "角色不能为空")
|
||||
@Schema(description = "角色", example = "user", allowableValues = {"super", "admin", "user"})
|
||||
private String role;
|
||||
@NotNull(message = "是否超管不能为空")
|
||||
@Schema(description = "是否超级管理员 (0-否 1-是)", example = "0", allowableValues = {"0", "1"})
|
||||
private Integer isSuper;
|
||||
|
||||
@Schema(description = "头像图片ID", example = "1")
|
||||
private Long avatarImageId;
|
||||
|
||||
+2
-2
@@ -34,8 +34,8 @@ public class SuperUpdateAccountDTO {
|
||||
@Schema(description = "岗位ID", example = "1")
|
||||
private Long positionId;
|
||||
|
||||
@Schema(description = "角色", example = "user", allowableValues = {"super", "admin", "user"})
|
||||
private String role;
|
||||
@Schema(description = "是否超级管理员 (0-否 1-是)", example = "0", allowableValues = {"0", "1"})
|
||||
private Integer isSuper;
|
||||
|
||||
@Schema(description = "头像图片ID", example = "1")
|
||||
private Long avatarImageId;
|
||||
|
||||
+4
-4
@@ -48,11 +48,11 @@ public class CompanyAccountVO {
|
||||
@Schema(description = "头像图片ID", example = "1")
|
||||
private Long avatarImageId;
|
||||
|
||||
@Schema(description = "角色", example = "user")
|
||||
private String role;
|
||||
@Schema(description = "是否超级管理员", example = "0")
|
||||
private Integer isSuper;
|
||||
|
||||
@Schema(description = "角色名称", example = "普通用户")
|
||||
private String roleName;
|
||||
@Schema(description = "是否超级管理员名称", example = "普通用户")
|
||||
private String isSuperName;
|
||||
|
||||
@Schema(description = "密码安全级别", example = "3")
|
||||
private Integer pwdSecurityLevel;
|
||||
|
||||
+11
-17
@@ -102,7 +102,7 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
* 超管直接放行;普通用户须为该公司拥有者(company_member.role = owner)
|
||||
*/
|
||||
private void assertOwnerCanCreateAccount(Long companyId) {
|
||||
if ("super".equals(currentUserContext.getCurrentUserRole())) {
|
||||
if (currentUserContext.isCurrentUserSuper()) {
|
||||
return;
|
||||
}
|
||||
if (!companyMemberService.isOwner(currentUserContext.getCurrentUserId(), companyId)) {
|
||||
@@ -172,7 +172,7 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
BeanCopyUtils.copyProperties(createAccountDTO, user);
|
||||
user.setPassword(passwordEncoder.encode(createAccountDTO.getPassword()));
|
||||
user.setLoginCode(createAccountDTO.getPhone());
|
||||
user.setRole("user");
|
||||
user.setIsSuper(0);
|
||||
user.setStatus(1);
|
||||
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(createAccountDTO.getPassword()));
|
||||
user.setPwdUpdateTime(LocalDateTime.now());
|
||||
@@ -381,9 +381,9 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
wrapper.like(UserPO::getPhone, filterDTO.getPhone());
|
||||
}
|
||||
|
||||
// 角色
|
||||
if (StringUtils.hasText(filterDTO.getRole())) {
|
||||
wrapper.eq(UserPO::getRole, filterDTO.getRole());
|
||||
// 是否超管
|
||||
if (filterDTO.getIsSuper() != null) {
|
||||
wrapper.eq(UserPO::getIsSuper, filterDTO.getIsSuper());
|
||||
}
|
||||
|
||||
// 状态
|
||||
@@ -418,8 +418,9 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
// 设置状态名称
|
||||
accountVO.setStatusName(getStatusName(user.getStatus()));
|
||||
|
||||
// 设置角色名称
|
||||
accountVO.setRoleName(getRoleName(user.getRole()));
|
||||
// 设置是否超管展示名
|
||||
accountVO.setIsSuper(user.getIsSuper());
|
||||
accountVO.setIsSuperName(getIsSuperName(user.getIsSuper()));
|
||||
|
||||
// 设置公司ID(从公司成员表获取)
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
@@ -471,17 +472,10 @@ public class AccountServiceImpl extends ServiceImpl<UserMapper, UserPO> implemen
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色中文展示名
|
||||
* admin 为历史系统角色码,展示为「拥有者」
|
||||
* 获取超管标识中文展示名
|
||||
*/
|
||||
private String getRoleName(String role) {
|
||||
if (role == null) return "未知";
|
||||
return switch (role) {
|
||||
case "super" -> "超级管理员";
|
||||
case "admin" -> "拥有者";
|
||||
case "user" -> "普通用户";
|
||||
default -> "未知";
|
||||
};
|
||||
private String getIsSuperName(Integer isSuper) {
|
||||
return Integer.valueOf(1).equals(isSuper) ? "超级管理员" : "普通用户";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+14
-16
@@ -89,6 +89,9 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
|
||||
// 创建账户
|
||||
UserPO user = new UserPO();
|
||||
BeanCopyUtils.copyProperties(superCreateAccountDTO, user);
|
||||
if (user.getIsSuper() == null) {
|
||||
user.setIsSuper(0);
|
||||
}
|
||||
user.setPassword(passwordEncoder.encode(superCreateAccountDTO.getPassword()));
|
||||
user.setStatus(1); // 正常状态(与 sys_user 表定义一致:1=正常,0=停用)
|
||||
user.setPwdSecurityLevel(calculatePasswordSecurityLevel(superCreateAccountDTO.getPassword()));
|
||||
@@ -162,8 +165,8 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
|
||||
if (superUpdateAccountDTO.getAvatarImageId() != null) {
|
||||
user.setAvatarImageId(superUpdateAccountDTO.getAvatarImageId());
|
||||
}
|
||||
if (StringUtils.hasText(superUpdateAccountDTO.getRole())) {
|
||||
user.setRole(superUpdateAccountDTO.getRole());
|
||||
if (superUpdateAccountDTO.getIsSuper() != null) {
|
||||
user.setIsSuper(superUpdateAccountDTO.getIsSuper());
|
||||
}
|
||||
if (superUpdateAccountDTO.getStatus() != null) {
|
||||
user.setStatus(superUpdateAccountDTO.getStatus());
|
||||
@@ -348,9 +351,9 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
|
||||
}
|
||||
}
|
||||
|
||||
// 角色
|
||||
if (StringUtils.hasText(filterDTO.getRole())) {
|
||||
wrapper.eq(UserPO::getRole, filterDTO.getRole());
|
||||
// 是否超管
|
||||
if (filterDTO.getIsSuper() != null) {
|
||||
wrapper.eq(UserPO::getIsSuper, filterDTO.getIsSuper());
|
||||
}
|
||||
|
||||
// 状态
|
||||
@@ -383,8 +386,9 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
|
||||
// 设置状态名称
|
||||
accountVO.setStatusName(getStatusName(user.getStatus()));
|
||||
|
||||
// 设置角色名称
|
||||
accountVO.setRoleName(getRoleName(user.getRole()));
|
||||
// 设置是否超管展示名
|
||||
accountVO.setIsSuper(user.getIsSuper());
|
||||
accountVO.setIsSuperName(getIsSuperName(user.getIsSuper()));
|
||||
|
||||
// 设置公司名称(从公司成员表获取)
|
||||
List<CompanyMemberVO> companyMembers = companyMemberService.getByUserId(user.getId());
|
||||
@@ -434,16 +438,10 @@ public class SuperAccountServiceImpl extends ServiceImpl<UserMapper, UserPO> imp
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取角色名称
|
||||
* 获取超管标识中文展示名
|
||||
*/
|
||||
private String getRoleName(String role) {
|
||||
if (role == null) return "未知";
|
||||
return switch (role) {
|
||||
case "super" -> "超级管理员";
|
||||
case "admin" -> "拥有者";
|
||||
case "user" -> "普通用户";
|
||||
default -> "未知";
|
||||
};
|
||||
private String getIsSuperName(Integer isSuper) {
|
||||
return Integer.valueOf(1).equals(isSuper) ? "超级管理员" : "普通用户";
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+1
-1
@@ -149,7 +149,7 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
|
||||
ownerUser.setPhone(createCompanyDTO.getOwnerPhone());
|
||||
ownerUser.setRealName(createCompanyDTO.getOwnerName());
|
||||
ownerUser.setPassword(passwordEncoder.encode("123456")); // 默认密码
|
||||
ownerUser.setRole("user");
|
||||
ownerUser.setIsSuper(0);
|
||||
ownerUser.setStatus(1);
|
||||
ownerUser.setPwdSecurityLevel(1);
|
||||
ownerUser.setPwdUpdateTime(LocalDateTime.now());
|
||||
|
||||
+10
-20
@@ -65,30 +65,20 @@ public class AuthController {
|
||||
@GetMapping("/codes")
|
||||
public ApiResponse<List<String>> getAccessCodes() {
|
||||
try {
|
||||
String role = currentUserContext.getCurrentUserPayload().getRole();
|
||||
boolean isSuper = Boolean.TRUE.equals(currentUserContext.getCurrentUserPayload().getIsSuper());
|
||||
String companyRole = currentUserContext.getCurrentUserPayload().getCompanyRole();
|
||||
Long positionId = currentUserContext.getCurrentUserPayload().getPositionId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
|
||||
List<String> permissions;
|
||||
|
||||
// 根据角色返回不同的权限
|
||||
switch (role) {
|
||||
case "super":
|
||||
// 超级管理员拥有所有权限
|
||||
permissions = List.of("*");
|
||||
break;
|
||||
case "company":
|
||||
// 公司拥有者拥有公司相关权限
|
||||
permissions = List.of("company:*");
|
||||
break;
|
||||
case "user":
|
||||
// 普通用户根据岗位获取权限
|
||||
permissions = permissionCheckService.getPositionPermissions(positionId, companyId);
|
||||
break;
|
||||
default:
|
||||
permissions = List.of();
|
||||
if (isSuper) {
|
||||
permissions = List.of("*");
|
||||
} else if ("owner".equalsIgnoreCase(companyRole)) {
|
||||
permissions = List.of("company:*");
|
||||
} else {
|
||||
permissions = permissionCheckService.getPositionPermissions(positionId, companyId);
|
||||
}
|
||||
|
||||
|
||||
return ApiResponse.success("获取权限码成功", permissions);
|
||||
} catch (Exception e) {
|
||||
// 如果获取权限失败,返回空列表
|
||||
|
||||
+3
-3
@@ -27,11 +27,11 @@ public class SuperAuthController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换账户:以目标用户在指定公司下的身份登录,系统角色保持 super
|
||||
* 切换账户:以目标用户在指定公司下的身份登录,Token.super 保持为 true
|
||||
*/
|
||||
@Operation(summary = "切换账户", description = "超级管理员切换到指定用户在指定公司下的账户上下文,角色保持为super")
|
||||
@Operation(summary = "切换账户", description = "超级管理员切换到指定用户在指定公司下的账户上下文,超管标识保持不变")
|
||||
@PostMapping("/switch/company/{companyId}/user/{userId}")
|
||||
@RequireRole(value = {"super"}, allowCompany = false, allowUser = false, description = "切换账户")
|
||||
@RequireRole(allowCompany = false, allowUser = false, description = "切换账户")
|
||||
public ApiResponse<LoginVO> switchAccount(
|
||||
@Parameter(description = "目标公司ID") @PathVariable Long companyId,
|
||||
@Parameter(description = "目标用户ID") @PathVariable Long userId) {
|
||||
|
||||
+1
-1
@@ -131,7 +131,7 @@ public class SysInitService {
|
||||
user.setPhone(initProperties.getAdminPhone());
|
||||
user.setPassword(passwordEncoder.encode(rawPassword));
|
||||
user.setRealName(initProperties.getAdminRealName());
|
||||
user.setRole("super");
|
||||
user.setIsSuper(1);
|
||||
user.setStatus(initProperties.getStatus());
|
||||
user.setPwdSecurityLevel(3);
|
||||
user.setPwdUpdateTime(LocalDateTime.now());
|
||||
|
||||
+34
-39
@@ -70,48 +70,43 @@ public class UserMenuServiceImpl implements UserMenuService {
|
||||
throw new RuntimeException("用户状态异常");
|
||||
}
|
||||
|
||||
// 获取用户角色
|
||||
String userRole = user.getRole();
|
||||
|
||||
// 是否超级管理员(优先看库字段;切账户场景可被 Token.super 覆盖,此处按库判定菜单范围)
|
||||
boolean isSuper = Integer.valueOf(1).equals(user.getIsSuper())
|
||||
|| Boolean.TRUE.equals(currentUserContext.getCurrentUserPayload().getIsSuper());
|
||||
|
||||
List<MenuVO> menus;
|
||||
|
||||
switch (userRole) {
|
||||
case "super":
|
||||
// 超级管理员获取所有系统菜单,不需要公司上下文
|
||||
menus = menuService.getAllMenus();
|
||||
break;
|
||||
case "user":
|
||||
// 普通用户,根据公司成员角色判断
|
||||
Long companyIdForUser = currentUserContext.getCurrentCompanyId();
|
||||
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyIdForUser);
|
||||
if (member == null) {
|
||||
menus = List.of();
|
||||
} else if (member.getRole() == CompanyMemberRole.owner) {
|
||||
// owner 角色获取公司所有菜单
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyIdForUser);
|
||||
} else {
|
||||
// member 角色:公司菜单与岗位菜单取交集(均支持向下继承)
|
||||
if (member.getPositionId() == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
List<Long> companyAssigned = companyMenuService.getCompanyMenuIds(companyIdForUser);
|
||||
List<Long> positionAssigned = positionMenuService.getPositionMenuIds(member.getPositionId());
|
||||
|
||||
List<MenuVO> companyMenus = menuService.getMenusForRoute(companyAssigned);
|
||||
Set<Long> companyRouteMenuIds = companyMenus.stream()
|
||||
.map(MenuVO::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> positionMenus = menuService.getMenusForRoute(positionAssigned);
|
||||
menus = positionMenus.stream()
|
||||
.filter(menu -> companyRouteMenuIds.contains(menu.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (isSuper) {
|
||||
// 超级管理员获取所有系统菜单,不需要公司上下文
|
||||
menus = menuService.getAllMenus();
|
||||
} else {
|
||||
// 普通用户,根据公司成员角色判断
|
||||
Long companyIdForUser = currentUserContext.getCurrentCompanyId();
|
||||
CompanyMemberPO member = companyMemberService.getByUserIdAndCompanyId(userId, companyIdForUser);
|
||||
if (member == null) {
|
||||
menus = List.of();
|
||||
break;
|
||||
} else if (member.getRole() == CompanyMemberRole.owner) {
|
||||
// owner 角色获取公司所有菜单
|
||||
menus = companyMenuService.getCompanyMenusForRoute(companyIdForUser);
|
||||
} else {
|
||||
// member 角色:公司菜单与岗位菜单取交集(均支持向下继承)
|
||||
if (member.getPositionId() == null) {
|
||||
menus = List.of();
|
||||
} else {
|
||||
List<Long> companyAssigned = companyMenuService.getCompanyMenuIds(companyIdForUser);
|
||||
List<Long> positionAssigned = positionMenuService.getPositionMenuIds(member.getPositionId());
|
||||
|
||||
List<MenuVO> companyMenus = menuService.getMenusForRoute(companyAssigned);
|
||||
Set<Long> companyRouteMenuIds = companyMenus.stream()
|
||||
.map(MenuVO::getId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<MenuVO> positionMenus = menuService.getMenusForRoute(positionAssigned);
|
||||
menus = positionMenus.stream()
|
||||
.filter(menu -> companyRouteMenuIds.contains(menu.getId()))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为RouteVO
|
||||
|
||||
+4
-4
@@ -34,13 +34,13 @@ public interface PermissionCheckService {
|
||||
java.util.List<String> getPositionPermissions(Long positionId, Long companyId);
|
||||
|
||||
/**
|
||||
* 根据用户角色检查是否有权限
|
||||
* 根据是否超管检查是否有权限
|
||||
*
|
||||
* @param role 用户角色 (super/user)
|
||||
* @param isSuper 是否超级管理员
|
||||
* @param permissionCode 权限码
|
||||
* @param positionId 岗位ID (仅user角色需要)
|
||||
* @param positionId 岗位ID (非超管需要)
|
||||
* @param companyId 公司ID
|
||||
* @return 是否有权限
|
||||
*/
|
||||
boolean hasPermissionByRole(String role, String permissionCode, Long positionId, Long companyId);
|
||||
boolean hasPermissionBySuper(boolean isSuper, String permissionCode, Long positionId, Long companyId);
|
||||
}
|
||||
|
||||
+12
-17
@@ -93,31 +93,26 @@ public class PermissionCheckServiceImpl implements PermissionCheckService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据角色和权限代码检查是否有权限
|
||||
* @param role 角色 (super/user)
|
||||
* 根据是否超管检查是否有权限
|
||||
* @param isSuper 是否超级管理员
|
||||
* @param permissionCode 权限代码
|
||||
* @param positionId 岗位ID (仅 user 需要)
|
||||
* @param positionId 岗位ID (非超管需要)
|
||||
* @param companyId 公司ID
|
||||
* @return 是否有权限
|
||||
*/
|
||||
@Override
|
||||
public boolean hasPermissionByRole(String role, String permissionCode, Long positionId, Long companyId) {
|
||||
if (role == null || permissionCode == null) {
|
||||
public boolean hasPermissionBySuper(boolean isSuper, String permissionCode, Long positionId, Long companyId) {
|
||||
if (permissionCode == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (role) {
|
||||
case "super":
|
||||
// 超级管理员拥有所有权限
|
||||
return true;
|
||||
case "user":
|
||||
// 普通用户检查岗位权限
|
||||
if (positionId == null) {
|
||||
return false;
|
||||
}
|
||||
return hasPermissionByPosition(positionId, permissionCode, companyId);
|
||||
default:
|
||||
return false;
|
||||
if (isSuper) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (positionId == null) {
|
||||
return false;
|
||||
}
|
||||
return hasPermissionByPosition(positionId, permissionCode, companyId);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -29,10 +29,10 @@ public class UserFilterDTO {
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
* 是否超级管理员 (0-否 1-是)
|
||||
*/
|
||||
@Schema(description = "用户角色", example = "user", allowableValues = { "user", "admin", "super" })
|
||||
private String role;
|
||||
@Schema(description = "是否超级管理员", example = "0", allowableValues = { "0", "1" })
|
||||
private Integer isSuper;
|
||||
|
||||
/**
|
||||
* 公司ID
|
||||
|
||||
+3
-3
@@ -57,10 +57,10 @@ public class UserPO {
|
||||
private Long avatarImageId;
|
||||
|
||||
/**
|
||||
* 关联角色(user-普通账户 admin-公司拥有者账户 super-超管账户)
|
||||
* 是否超级管理员 (0-否 1-是)
|
||||
*/
|
||||
@TableField("role")
|
||||
private String role;
|
||||
@TableField("super")
|
||||
private Integer isSuper;
|
||||
|
||||
/**
|
||||
* 密码安全级别(0-初始 1-很弱 2-弱 3-安全 4-很安全)
|
||||
|
||||
+2
-2
@@ -56,9 +56,9 @@ public class UserVO {
|
||||
private String avatar;
|
||||
|
||||
/**
|
||||
* 关联角色(user-普通账户 company-公司账户 super-超管账户)
|
||||
* 是否超级管理员 (0-否 1-是)
|
||||
*/
|
||||
private String role;
|
||||
private Integer isSuper;
|
||||
|
||||
/**
|
||||
* 密码安全级别(0-初始 1-很弱 2-弱 3-安全 4-很安全)
|
||||
|
||||
+21
-17
@@ -101,7 +101,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
user.setPhone(registerDTO.getPhone());
|
||||
user.setPassword(passwordEncoder.encode(registerDTO.getPassword()));
|
||||
user.setRealName("用户" + System.currentTimeMillis()); // 默认用户名
|
||||
user.setRole("user"); // 默认角色
|
||||
user.setIsSuper(0); // 默认非超管
|
||||
user.setStatus(1); // 正常状态
|
||||
user.setPwdSecurityLevel(0); // 初始密码安全级别
|
||||
user.setPwdUpdateTime(LocalDateTime.now());
|
||||
@@ -360,15 +360,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
throw new BusinessException("用户状态异常,无法刷新令牌");
|
||||
}
|
||||
|
||||
// 生成新的登录响应,继承刷新token中的角色
|
||||
return generateLoginResponseWithRole(user, refreshPayload.getRole());
|
||||
// 生成新的登录响应,继承刷新 token 中的超管标识
|
||||
return generateLoginResponseWithSuper(user, refreshPayload.getIsSuper());
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换账户
|
||||
* <p>
|
||||
* 超管以目标用户身份登录,JWT 中 role 保持 super。
|
||||
* 公司上下文由 generateLoginResponseWithRole 决定:若传入 companyId 则优先使用该公司。
|
||||
* 超管以目标用户身份登录,JWT 中 super 保持为 true。
|
||||
* 公司上下文由 generateLoginResponseWithSuper 决定:若传入 companyId 则优先使用该公司。
|
||||
*
|
||||
* @param userId 目标用户ID
|
||||
* @param companyId 目标公司ID
|
||||
@@ -402,7 +402,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
targetUser.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(targetUser);
|
||||
|
||||
return generateLoginResponseWithRole(targetUser, "super");
|
||||
return generateLoginResponseWithSuper(targetUser, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -419,7 +419,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
|
||||
/**
|
||||
* 切换当前登录公司上下文
|
||||
* 更新 lastCompanyId 后按当前 token 角色重新签发 JWT
|
||||
* 更新 lastCompanyId 后按当前 token 超管标识重新签发 JWT
|
||||
*/
|
||||
@Override
|
||||
public LoginVO switchCompany(Long companyId) {
|
||||
@@ -458,8 +458,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
user.setUpdateBy(userId);
|
||||
this.updateById(user);
|
||||
|
||||
String role = currentUserContext.getCurrentUserPayload().getRole();
|
||||
return generateLoginResponseWithRole(user, role);
|
||||
Boolean isSuper = currentUserContext.getCurrentUserPayload().getIsSuper();
|
||||
return generateLoginResponseWithSuper(user, isSuper);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -489,9 +489,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
queryWrapper.eq(UserPO::getPhone, filter.getPhone());
|
||||
}
|
||||
|
||||
// 用户角色查询
|
||||
if (StringUtils.hasText(filter.getRole())) {
|
||||
queryWrapper.eq(UserPO::getRole, filter.getRole());
|
||||
// 是否超管查询
|
||||
if (filter.getIsSuper() != null) {
|
||||
queryWrapper.eq(UserPO::getIsSuper, filter.getIsSuper());
|
||||
}
|
||||
|
||||
// 公司ID查询(通过公司成员表)
|
||||
@@ -629,13 +629,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
}
|
||||
|
||||
private LoginVO generateLoginResponse(UserPO user) {
|
||||
return generateLoginResponseWithRole(user, user.getRole());
|
||||
return generateLoginResponseWithSuper(user, Integer.valueOf(1).equals(user.getIsSuper()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成登录响应(指定角色)
|
||||
* 生成登录响应(指定是否超管)
|
||||
*/
|
||||
private LoginVO generateLoginResponseWithRole(UserPO user, String role) {
|
||||
private LoginVO generateLoginResponseWithSuper(UserPO user, Boolean isSuper) {
|
||||
LoginVO loginVO = new LoginVO();
|
||||
|
||||
// 选择登录公司:优先使用 lastCompanyId(上次登录的公司),前提是该公司状态可用
|
||||
@@ -707,11 +707,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
}
|
||||
}
|
||||
|
||||
boolean superFlag = isSuper != null
|
||||
? Boolean.TRUE.equals(isSuper)
|
||||
: Integer.valueOf(1).equals(user.getIsSuper());
|
||||
|
||||
// 生成访问令牌负载信息
|
||||
AccessTokenPayload accessPayload = AccessTokenPayload.builder()
|
||||
.userId(user.getId().toString())
|
||||
.companyId(companyIdForToken)
|
||||
.role(role != null ? role : user.getRole())
|
||||
.isSuper(superFlag)
|
||||
.companyRole(companyRoleForToken)
|
||||
.positionId(positionIdForToken)
|
||||
.build();
|
||||
@@ -719,7 +723,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements
|
||||
// 生成刷新令牌负载信息
|
||||
RefreshTokenPayload refreshPayload = RefreshTokenPayload.builder()
|
||||
.userId(user.getId().toString())
|
||||
.role(role != null ? role : user.getRole())
|
||||
.isSuper(superFlag)
|
||||
.build();
|
||||
|
||||
// 生成访问令牌和刷新令牌
|
||||
|
||||
+2
-2
@@ -26,9 +26,9 @@ public class AccessTokenPayload {
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
* 是否超级管理员
|
||||
*/
|
||||
private String role;
|
||||
private Boolean isSuper;
|
||||
|
||||
/**
|
||||
* 公司成员角色 (owner/member)
|
||||
|
||||
+5
-7
@@ -38,7 +38,7 @@ public class CurrentUserContext {
|
||||
return TokenPayload.builder()
|
||||
.userId(String.valueOf(openPrincipal.operatorUserId()))
|
||||
.companyId(openPrincipal.companyId())
|
||||
.role("user")
|
||||
.isSuper(false)
|
||||
.companyRole("owner")
|
||||
.build();
|
||||
}
|
||||
@@ -52,7 +52,7 @@ public class CurrentUserContext {
|
||||
return TokenPayload.builder()
|
||||
.userId(String.valueOf(openApiAuthentication.getOperatorUserId()))
|
||||
.companyId(openApiAuthentication.getCompanyId())
|
||||
.role("user")
|
||||
.isSuper(false)
|
||||
.companyRole("owner")
|
||||
.build();
|
||||
}
|
||||
@@ -111,12 +111,10 @@ public class CurrentUserContext {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户角色
|
||||
*
|
||||
* @return 用户角色
|
||||
* 当前用户是否为平台超级管理员
|
||||
*/
|
||||
public String getCurrentUserRole() {
|
||||
return getCurrentUserPayload().getRole();
|
||||
public boolean isCurrentUserSuper() {
|
||||
return Boolean.TRUE.equals(getCurrentUserPayload().getIsSuper());
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -50,7 +50,7 @@ public class JwtUtils {
|
||||
return Jwts.builder()
|
||||
.setSubject(payload.getUserId())
|
||||
.claim("companyId", payload.getCompanyId())
|
||||
.claim("role", payload.getRole())
|
||||
.claim("super", Boolean.TRUE.equals(payload.getIsSuper()))
|
||||
.claim("companyRole", payload.getCompanyRole())
|
||||
.claim("positionId", payload.getPositionId())
|
||||
.claim("tokenType", payload.getTokenType())
|
||||
@@ -73,7 +73,7 @@ public class JwtUtils {
|
||||
|
||||
return Jwts.builder()
|
||||
.setSubject(payload.getUserId())
|
||||
.claim("role", payload.getRole())
|
||||
.claim("super", Boolean.TRUE.equals(payload.getIsSuper()))
|
||||
.claim("tokenType", payload.getTokenType())
|
||||
.setIssuedAt(now)
|
||||
.setExpiration(expiryDate)
|
||||
@@ -92,7 +92,7 @@ public class JwtUtils {
|
||||
AccessTokenPayload accessPayload = AccessTokenPayload.builder()
|
||||
.userId(payload.getUserId())
|
||||
.companyId(payload.getCompanyId())
|
||||
.role(payload.getRole())
|
||||
.isSuper(payload.getIsSuper())
|
||||
.companyRole(payload.getCompanyRole())
|
||||
.positionId(payload.getPositionId())
|
||||
.build();
|
||||
@@ -130,7 +130,7 @@ public class JwtUtils {
|
||||
|
||||
return RefreshTokenPayload.builder()
|
||||
.userId(claims.getSubject())
|
||||
.role(getStringFromClaims(claims, "role"))
|
||||
.isSuper(getBooleanFromClaims(claims, "super"))
|
||||
.tokenType(getStringFromClaims(claims, "tokenType"))
|
||||
.build();
|
||||
}
|
||||
@@ -152,7 +152,7 @@ public class JwtUtils {
|
||||
return TokenPayload.builder()
|
||||
.userId(claims.getSubject())
|
||||
.companyId(getLongFromClaims(claims, "companyId"))
|
||||
.role(getStringFromClaims(claims, "role"))
|
||||
.isSuper(getBooleanFromClaims(claims, "super"))
|
||||
.companyRole(getStringFromClaims(claims, "companyRole"))
|
||||
.positionId(getLongFromClaims(claims, "positionId"))
|
||||
.build();
|
||||
@@ -179,13 +179,13 @@ public class JwtUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
* 从JWT令牌中获取用户角色
|
||||
* 从JWT令牌中判断是否为超级管理员
|
||||
*
|
||||
* @param token JWT令牌
|
||||
* @return 用户角色
|
||||
* @return 是否超级管理员
|
||||
*/
|
||||
public String getRoleFromToken(String token) {
|
||||
return getPayloadFromToken(token).getRole();
|
||||
public boolean isSuperFromToken(String token) {
|
||||
return Boolean.TRUE.equals(getPayloadFromToken(token).getIsSuper());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,6 +213,23 @@ public class JwtUtils {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Claims中安全获取Boolean值
|
||||
*/
|
||||
private Boolean getBooleanFromClaims(Claims claims, String key) {
|
||||
Object value = claims.get(key);
|
||||
if (value == null) {
|
||||
return false;
|
||||
}
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return ((Number) value).intValue() == 1;
|
||||
}
|
||||
return Boolean.parseBoolean(value.toString()) || "1".equals(value.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* 从Claims中安全获取String值
|
||||
*/
|
||||
|
||||
+11
-40
@@ -32,27 +32,11 @@ public class PermissionUtils implements ApplicationContextAware {
|
||||
PermissionCheckService permissionCheckService = applicationContext.getBean(PermissionCheckService.class);
|
||||
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
||||
|
||||
String role = currentUserContext.getCurrentUserPayload().getRole();
|
||||
boolean isSuper = Boolean.TRUE.equals(currentUserContext.getCurrentUserPayload().getIsSuper());
|
||||
Long positionId = currentUserContext.getCurrentUserPayload().getPositionId();
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
return permissionCheckService.hasPermissionByRole(role, permissionCode, positionId, companyId);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前用户是否有指定角色
|
||||
*
|
||||
* @param role 角色
|
||||
* @return 是否有角色
|
||||
*/
|
||||
public static boolean hasRole(String role) {
|
||||
try {
|
||||
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
||||
String currentRole = currentUserContext.getCurrentUserPayload().getRole();
|
||||
return role.equals(currentRole);
|
||||
return permissionCheckService.hasPermissionBySuper(isSuper, permissionCode, positionId, companyId);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
@@ -64,7 +48,12 @@ public class PermissionUtils implements ApplicationContextAware {
|
||||
* @return 是否是超级管理员
|
||||
*/
|
||||
public static boolean isSuper() {
|
||||
return hasRole("super");
|
||||
try {
|
||||
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
||||
return currentUserContext.isCurrentUserSuper();
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -72,30 +61,12 @@ public class PermissionUtils implements ApplicationContextAware {
|
||||
*
|
||||
* @return 是否是公司拥有者
|
||||
*/
|
||||
public static boolean isCompany() {
|
||||
return hasRole("company");
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查当前用户是否是普通用户
|
||||
*
|
||||
* @return 是否是普通用户
|
||||
*/
|
||||
public static boolean isUser() {
|
||||
return hasRole("user");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户角色
|
||||
*
|
||||
* @return 用户角色
|
||||
*/
|
||||
public static String getCurrentRole() {
|
||||
public static boolean isCompanyOwner() {
|
||||
try {
|
||||
CurrentUserContext currentUserContext = applicationContext.getBean(CurrentUserContext.class);
|
||||
return currentUserContext.getCurrentUserPayload().getRole();
|
||||
return "owner".equalsIgnoreCase(currentUserContext.getCurrentCompanyRole());
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -21,9 +21,9 @@ public class RefreshTokenPayload {
|
||||
private String userId;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
* 是否超级管理员
|
||||
*/
|
||||
private String role;
|
||||
private Boolean isSuper;
|
||||
|
||||
/**
|
||||
* 令牌类型
|
||||
|
||||
@@ -25,9 +25,9 @@ public class TokenPayload {
|
||||
private Long companyId;
|
||||
|
||||
/**
|
||||
* 用户角色
|
||||
* 是否超级管理员
|
||||
*/
|
||||
private String role;
|
||||
private Boolean isSuper;
|
||||
|
||||
/**
|
||||
* 公司成员角色 (owner/member)
|
||||
|
||||
+6
-6
@@ -19,8 +19,8 @@ export namespace SuperAccountDTO {
|
||||
departmentId?: number;
|
||||
/** 岗位ID */
|
||||
positionId?: number;
|
||||
/** 角色 */
|
||||
role: string;
|
||||
/** 是否超级管理员(0-否 1-是) */
|
||||
isSuper: number;
|
||||
/** 头像图片ID */
|
||||
avatarImageId?: number;
|
||||
/** 备注 */
|
||||
@@ -41,8 +41,8 @@ export namespace SuperAccountDTO {
|
||||
departmentId?: number;
|
||||
/** 岗位ID */
|
||||
positionId?: number;
|
||||
/** 角色 */
|
||||
role?: string;
|
||||
/** 是否超级管理员(0-否 1-是) */
|
||||
isSuper?: number;
|
||||
/** 头像图片ID */
|
||||
avatarImageId?: number;
|
||||
/** 状态(0-正常 1-删除 2-停用 3-冻结) */
|
||||
@@ -65,8 +65,8 @@ export namespace SuperAccountDTO {
|
||||
departmentId?: number;
|
||||
/** 岗位ID */
|
||||
positionId?: number;
|
||||
/** 角色 */
|
||||
role?: string;
|
||||
/** 是否超级管理员(0-否 1-是) */
|
||||
isSuper?: number;
|
||||
/** 状态(0-正常 1-删除 2-停用 3-冻结) */
|
||||
status?: number;
|
||||
}
|
||||
|
||||
+4
-4
@@ -29,10 +29,10 @@ export namespace SuperAccountVO {
|
||||
avatar?: string;
|
||||
/** 头像图片ID */
|
||||
avatarImageId?: number;
|
||||
/** 角色 */
|
||||
role?: string;
|
||||
/** 角色名称 */
|
||||
roleName?: string;
|
||||
/** 是否超级管理员(0-否 1-是) */
|
||||
isSuper?: number;
|
||||
/** 是否超级管理员名称 */
|
||||
isSuperName?: string;
|
||||
/** 密码安全级别 */
|
||||
pwdSecurityLevel?: number;
|
||||
/** 密码最后更新时间 */
|
||||
|
||||
@@ -26,8 +26,8 @@ export namespace UserVO {
|
||||
realName: string;
|
||||
/** 头像路径 */
|
||||
avatar?: string;
|
||||
/** 关联角色(user-普通账户 company-公司账户 super-超管账户) */
|
||||
role: string;
|
||||
/** 是否超级管理员(0-否 1-是) */
|
||||
isSuper?: number;
|
||||
/** 密码安全级别(0-初始 1-很弱 2-弱 3-安全 4-很安全) */
|
||||
pwdSecurityLevel?: number;
|
||||
/** 密码最后更新时间 */
|
||||
|
||||
@@ -120,7 +120,7 @@ const menus = computed(() => {
|
||||
}
|
||||
|
||||
// 只有超级管理员才显示切换账户功能
|
||||
if (accessStore.userRole === 'super') {
|
||||
if (accessStore.isSuper) {
|
||||
menuItems.push({
|
||||
handler: () => {
|
||||
switchAccountModalRef.value?.open();
|
||||
|
||||
+10
-11
@@ -51,7 +51,7 @@ const formData = ref<
|
||||
companyId: 0,
|
||||
departmentId: undefined,
|
||||
positionId: undefined,
|
||||
role: '',
|
||||
isSuper: 0,
|
||||
avatarImageId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
@@ -73,7 +73,7 @@ function resetForm() {
|
||||
companyId: presetCompany.value?.id ?? 0,
|
||||
departmentId: undefined,
|
||||
positionId: undefined,
|
||||
role: '',
|
||||
isSuper: 0,
|
||||
avatarImageId: undefined,
|
||||
remark: '',
|
||||
status: 1,
|
||||
@@ -104,7 +104,7 @@ function open(
|
||||
companyId: accountData.companyId || presetCompany.value?.id || 0,
|
||||
departmentId: accountData.departmentId,
|
||||
positionId: accountData.positionId,
|
||||
role: accountData.role || '',
|
||||
isSuper: accountData.isSuper ?? 0,
|
||||
avatarImageId: accountData.avatarImageId,
|
||||
remark: accountData.remark || '',
|
||||
status: accountData.status ?? 1,
|
||||
@@ -253,17 +253,16 @@ defineExpose({
|
||||
</Form.Item>
|
||||
|
||||
<Form.Item
|
||||
label="角色"
|
||||
name="role"
|
||||
:rules="[{ required: true, message: '请输入角色' }]"
|
||||
label="是否超管"
|
||||
name="isSuper"
|
||||
:rules="[{ required: true, message: '请选择是否超管' }]"
|
||||
>
|
||||
<Select
|
||||
v-model:value="formData.role"
|
||||
placeholder="请选择角色"
|
||||
v-model:value="formData.isSuper"
|
||||
placeholder="请选择是否超管"
|
||||
:options="[
|
||||
{ label: '超级管理员', value: 'super' },
|
||||
{ label: '拥有者', value: 'admin' },
|
||||
{ label: '普通用户', value: 'user' },
|
||||
{ label: '否', value: 0 },
|
||||
{ label: '是', value: 1 },
|
||||
]"
|
||||
/>
|
||||
</Form.Item>
|
||||
|
||||
@@ -122,8 +122,8 @@ const gridOptions: VxeGridProps<SuperAccountVO.AccountInfoVO> = {
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
{
|
||||
field: 'role',
|
||||
title: '角色',
|
||||
field: 'isSuperName',
|
||||
title: '超管',
|
||||
minWidth: 100,
|
||||
showOverflow: 'tooltip' as const,
|
||||
},
|
||||
|
||||
@@ -52,9 +52,9 @@ interface AccessState {
|
||||
*/
|
||||
refreshTokenExpiresAt: number | null;
|
||||
/**
|
||||
* 用户角色
|
||||
* 是否平台超级管理员(来自 JWT claim: super)
|
||||
*/
|
||||
userRole: string | null;
|
||||
isSuper: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -153,22 +153,22 @@ export const useAccessStore = defineStore('core-access', {
|
||||
this.refreshTokenExpiresAt = refreshTokenExpiresAt;
|
||||
}
|
||||
|
||||
// 从accessToken中提取用户角色
|
||||
// 从 accessToken 中提取是否超管
|
||||
if (accessToken) {
|
||||
try {
|
||||
const parts = accessToken.split('.');
|
||||
if (parts.length === 3 && parts[1]) {
|
||||
const payload = JSON.parse(atob(parts[1]));
|
||||
this.userRole = payload.role || null;
|
||||
this.isSuper = payload.super === true || payload.super === 1;
|
||||
} else {
|
||||
this.userRole = null;
|
||||
this.isSuper = false;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn('解析token失败:', error);
|
||||
this.userRole = null;
|
||||
this.isSuper = false;
|
||||
}
|
||||
} else {
|
||||
this.userRole = null;
|
||||
this.isSuper = false;
|
||||
}
|
||||
},
|
||||
unlockScreen() {
|
||||
@@ -186,7 +186,7 @@ export const useAccessStore = defineStore('core-access', {
|
||||
'accessCodes',
|
||||
'isLockScreen',
|
||||
'lockScreenPassword',
|
||||
'userRole',
|
||||
'isSuper',
|
||||
],
|
||||
},
|
||||
state: (): AccessState => ({
|
||||
@@ -201,7 +201,7 @@ export const useAccessStore = defineStore('core-access', {
|
||||
loginExpired: false,
|
||||
refreshToken: null,
|
||||
refreshTokenExpiresAt: null,
|
||||
userRole: null,
|
||||
isSuper: false,
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user