fix: 修复权限相关问题

This commit is contained in:
ah--desktop
2026-04-08 00:09:49 +08:00
parent 9fe0e871ec
commit 534d052ae6
8 changed files with 48 additions and 35 deletions
+1 -1
View File
@@ -23,7 +23,7 @@ CREATE TABLE `sys_user` (
`last_company_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '最后登录公司ID', `last_company_id` bigint UNSIGNED NULL DEFAULT NULL COMMENT '最后登录公司ID',
`freeze_date` timestamp NULL DEFAULT NULL COMMENT '冻结时间', `freeze_date` timestamp NULL DEFAULT NULL COMMENT '冻结时间',
`freeze_cause` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '冻结原因', `freeze_cause` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '冻结原因',
`status` tinyint UNSIGNED NULL DEFAULT NULL COMMENT '状态(0-正常 1-删除 2-停用 3-冻结)', `status` tinyint UNSIGNED NOT NULL DEFAULT 1 COMMENT '状态(0-停用 1-正常)',
`remark` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注信息', `remark` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注信息',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
@@ -16,11 +16,10 @@ import java.lang.reflect.Method;
/** /**
* 权限验证切面 * 权限验证切面
* 用于拦截带有@RequirePermission注解的方法并进行权限验证 * 用于拦截带有@RequirePermission注解的方法并进行权限验证
*
* 权限判定逻辑: * 权限判定逻辑:
* - super 角色:直接放行(拥有所有权限) * - super:按 allowSuper 放行
* - company 角色:检查公司是否有该权限(同 user 逻辑,但只做公司级校验) * - user + owner:仅校验平台分配给公司的权限(org_company_permission),不校验岗位
* - user 角色:从 org_position_permission 表查询岗位拥有的权限码进行判定,同时需要公司有该权限 * - user + member:公司权限 + 岗位权限双校验
*/ */
@Aspect @Aspect
@Component @Component
@@ -47,11 +46,12 @@ public class PermissionAspect {
// 获取当前用户信息 // 获取当前用户信息
String role = currentUserContext.getCurrentUserPayload().getRole(); String role = currentUserContext.getCurrentUserPayload().getRole();
String companyRole = currentUserContext.getCurrentUserPayload().getCompanyRole();
Long companyId = currentUserContext.getCurrentCompanyId(); Long companyId = currentUserContext.getCurrentCompanyId();
Long positionId = currentUserContext.getCurrentUserPayload().getPositionId(); Long positionId = currentUserContext.getCurrentUserPayload().getPositionId();
log.debug("权限验证 - 用户角色: {}, 公司ID: {}, 岗位ID: {}, 权限码: {}", log.debug("权限验证 - 用户角色: {}, 公司角色: {}, 公司ID: {}, 岗位ID: {}, 权限码: {}",
role, companyId, positionId, annotation.value()); role, companyRole, companyId, positionId, annotation.value());
// 检查角色是否为空 // 检查角色是否为空
if (role == null) { if (role == null) {
@@ -66,24 +66,17 @@ public class PermissionAspect {
// 超级管理员拥有所有权限 // 超级管理员拥有所有权限
hasPermission = annotation.allowSuper(); hasPermission = annotation.allowSuper();
break; break;
case "company": case "user":
// 公司管理员角色,检查公司是否有该权限 // owner:拥有平台分配给该公司的全部权限(不含岗位细分)
// company 角色实际上是 member 中的 owner,有公司级权限 if ("owner".equalsIgnoreCase(companyRole)) {
// 走 user 逻辑,使用 positionId hasPermission = permissionCheckService.hasCompanyAssignedPermission(
if (positionId != null) { companyId, annotation.value());
} else {
// member 走公司+岗位双校验
hasPermission = permissionCheckService.hasPermissionByPosition( hasPermission = permissionCheckService.hasPermissionByPosition(
positionId, annotation.value(), companyId); positionId, annotation.value(), companyId);
} else {
// 如果没有职位,至少检查公司是否有权限
hasPermission = false;
} }
break; break;
case "user":
// 普通用户从 org_position_permission 表查询权限码进行判定
// 同时需要公司有该权限(两级校验)
hasPermission = permissionCheckService.hasPermissionByPosition(
positionId, annotation.value(), companyId);
break;
default: default:
throw new BusinessException("未知的用户角色: " + role); throw new BusinessException("未知的用户角色: " + role);
} }
@@ -214,8 +214,11 @@ public class CompanyMemberServiceImpl extends ServiceImpl<CompanyMemberMapper, C
// 设置状态名称 // 设置状态名称
vo.setStatusName(member.getStatus() != null && member.getStatus() == 1 ? "正常" : "禁用"); vo.setStatusName(member.getStatus() != null && member.getStatus() == 1 ? "正常" : "禁用");
// 设置角色名称 // VO 字段约定:
vo.setRole(member.getRole() != null ? member.getRole().getDescription() : "成员"); // - role: 存 code(owner/member),供鉴权和业务逻辑使用
// - roleName: 存中文展示名(公司拥有者/成员)
vo.setRole(member.getRole() != null ? member.getRole().getCode() : CompanyMemberRole.member.getCode());
vo.setRoleName(member.getRole() != null ? member.getRole().getDescription() : CompanyMemberRole.member.getDescription());
// 设置公司名称 // 设置公司名称
if (member.getCompanyId() != null) { if (member.getCompanyId() != null) {
@@ -26,10 +26,6 @@ public class CreateCompanyDTO {
@Schema(description = "公司地址", example = "北京市朝阳区xxx街道xxx号") @Schema(description = "公司地址", example = "北京市朝阳区xxx街道xxx号")
private String address; private String address;
/** /**
* 邮箱 * 邮箱
*/ */
@@ -121,8 +121,8 @@ public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, CompanyPO> im
ownerUser.setPhone(createCompanyDTO.getOwnerPhone()); ownerUser.setPhone(createCompanyDTO.getOwnerPhone());
ownerUser.setRealName(createCompanyDTO.getOwnerName()); ownerUser.setRealName(createCompanyDTO.getOwnerName());
ownerUser.setPassword(passwordEncoder.encode("123456")); // 默认密码 ownerUser.setPassword(passwordEncoder.encode("123456")); // 默认密码
ownerUser.setRole("user"); ownerUser.setRole("owner");
ownerUser.setStatus(0); ownerUser.setStatus(1);
ownerUser.setPwdSecurityLevel(1); ownerUser.setPwdSecurityLevel(1);
ownerUser.setPwdUpdateTime(LocalDateTime.now()); ownerUser.setPwdUpdateTime(LocalDateTime.now());
ownerUser.setCreateBy(currentUserContext.getCurrentUserId()); ownerUser.setCreateBy(currentUserContext.getCurrentUserId());
@@ -16,6 +16,14 @@ public interface PermissionCheckService {
*/ */
boolean hasPermissionByPosition(Long positionId, String permissionCode, Long companyId); boolean hasPermissionByPosition(Long positionId, String permissionCode, Long companyId);
/**
* 是否为公司已分配的权限(平台分配给公司的权限码,不校验岗位)
*
* @param companyId 公司ID
* @param permissionCode 权限码
*/
boolean hasCompanyAssignedPermission(Long companyId, String permissionCode);
/** /**
* 获取岗位的所有权限码 * 获取岗位的所有权限码
* *
@@ -28,7 +36,7 @@ public interface PermissionCheckService {
/** /**
* 根据用户角色检查是否有权限 * 根据用户角色检查是否有权限
* *
* @param role 用户角色 (super/company/user) * @param role 用户角色 (super/user)
* @param permissionCode 权限码 * @param permissionCode 权限码
* @param positionId 岗位ID (仅user角色需要) * @param positionId 岗位ID (仅user角色需要)
* @param companyId 公司ID * @param companyId 公司ID
@@ -13,7 +13,7 @@ import java.util.List;
/** /**
* 权限检查服务实现类 * 权限检查服务实现类
* 三级权限校验:平台(super) -> 公司(company) -> 职位(user) * 公司权限 + 岗位权限双重校验(member 场景)
*/ */
@Slf4j @Slf4j
@Service @Service
@@ -60,6 +60,20 @@ public class PermissionCheckServiceImpl implements PermissionCheckService {
} }
} }
@Override
public boolean hasCompanyAssignedPermission(Long companyId, String permissionCode) {
if (companyId == null || permissionCode == null) {
return false;
}
try {
List<String> codes = companyPermissionService.getCompanyPermissionCodes(companyId);
return codes.contains(permissionCode);
} catch (Exception e) {
log.error("公司权限验证异常", e);
return false;
}
}
/** /**
* 获取岗位权限列表(权限码列表) * 获取岗位权限列表(权限码列表)
* @param positionId 岗位ID * @param positionId 岗位ID
@@ -82,10 +96,10 @@ public class PermissionCheckServiceImpl implements PermissionCheckService {
/** /**
* 根据角色和权限代码检查是否有权限 * 根据角色和权限代码检查是否有权限
* @param role 角色 (super/company/user) * @param role 角色 (super/user)
* @param permissionCode 权限代码 * @param permissionCode 权限代码
* @param positionId 岗位ID (仅user角色需要) * @param positionId 岗位ID (仅 user 需要)
* @param companyId 公司ID (company角色需要) * @param companyId 公司ID
* @return 是否有权限 * @return 是否有权限
*/ */
@Override @Override
@@ -263,7 +263,6 @@ const loadOverviewData = async () => {
overviewData.value = data; overviewData.value = data;
} catch (error) { } catch (error) {
console.error('获取全店概览数据失败:', error); console.error('获取全店概览数据失败:', error);
message.error('获取全店概览数据失败');
} finally { } finally {
loading.value = false; loading.value = false;
} }