将接口级权限改为业务语言
1. 更新权限配置格式 - 从 "METHOD:/path" 改为 "module:operation" - 例如: "subjects:create", "visits:update", "risk_issues:delete" - 更易理解和管理 2. 新增权限操作列表 API - GET /api/v1/api-permissions/operations - 返回所有权限操作及其描述 3. 更新前端权限管理界面 - 显示业务语言的权限名称 - 按模块和操作类型筛选 - 改进用户体验 4. 修复导入错误 - 更新 MODULE_TO_ENDPOINTS 为 OPERATION_TO_ENDPOINTS - 禁用 API 端点注册表初始化 Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -24,6 +24,9 @@ export const fetchApiEndpointPermissions = (studyId: string) =>
|
||||
export const updateApiEndpointPermissions = (studyId: string, payload: ApiEndpointPermissionsUpdate) =>
|
||||
apiPut<ApiEndpointPermissionsResponse>(`/api/v1/studies/${studyId}/api-permissions`, payload);
|
||||
|
||||
export const fetchApiOperations = () =>
|
||||
apiGet<{ operations: Array<{ operation_key: string; module: string; action: string; description: string; default_roles: string[] }> }>(`/api/v1/api-permissions/operations`);
|
||||
|
||||
// 权限系统监控API(新增)
|
||||
export const fetchPermissionMetrics = () =>
|
||||
apiGet<PermissionMetricsResponse>(`/api/v1/permission-monitoring/metrics`);
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<div class="api-permissions-toolbar">
|
||||
<el-input
|
||||
v-model="searchText"
|
||||
placeholder="搜索端点..."
|
||||
placeholder="搜索权限..."
|
||||
clearable
|
||||
style="width: 250px"
|
||||
>
|
||||
@@ -23,29 +23,29 @@
|
||||
/>
|
||||
</el-select>
|
||||
|
||||
<el-select v-model="filterMethod" placeholder="筛选方法" clearable style="width: 120px">
|
||||
<el-option label="全部方法" value="" />
|
||||
<el-option label="GET" value="GET" />
|
||||
<el-option label="POST" value="POST" />
|
||||
<el-option label="PATCH" value="PATCH" />
|
||||
<el-option label="DELETE" value="DELETE" />
|
||||
<el-select v-model="filterAction" placeholder="筛选操作" clearable style="width: 120px">
|
||||
<el-option label="全部操作" value="" />
|
||||
<el-option label="读取" value="read" />
|
||||
<el-option label="写入" value="write" />
|
||||
</el-select>
|
||||
</div>
|
||||
|
||||
<!-- 权限矩阵表格 -->
|
||||
<div class="api-permissions-table-wrapper">
|
||||
<el-table :data="filteredEndpoints" border stripe>
|
||||
<el-table-column prop="endpoint_key" label="端点" width="250">
|
||||
<el-table :data="filteredOperations" border stripe>
|
||||
<el-table-column prop="operation_key" label="权限操作" width="200">
|
||||
<template #default="{ row }">
|
||||
<div class="endpoint-cell">
|
||||
<el-tag :type="getMethodType(row.method)" size="small">
|
||||
{{ row.method }}
|
||||
<div class="operation-cell">
|
||||
<el-tag :type="getActionType(row.action)" size="small">
|
||||
{{ row.action === 'read' ? '读取' : '写入' }}
|
||||
</el-tag>
|
||||
<span class="endpoint-path">{{ row.path }}</span>
|
||||
<span class="operation-name">{{ row.operation_key }}</span>
|
||||
</div>
|
||||
</template>
|
||||
</el-table-column>
|
||||
|
||||
<el-table-column prop="description" label="描述" width="200" show-overflow-tooltip />
|
||||
|
||||
<el-table-column prop="module" label="模块" width="100" />
|
||||
|
||||
<el-table-column
|
||||
@@ -57,8 +57,8 @@
|
||||
>
|
||||
<template #default="{ row }">
|
||||
<el-checkbox
|
||||
:model-value="isEndpointAllowed(row.endpoint_key, role)"
|
||||
@change="(val) => onPermissionChange(row.endpoint_key, role, val as boolean)"
|
||||
:model-value="isOperationAllowed(row.operation_key, role)"
|
||||
@change="(val) => onPermissionChange(row.operation_key, role, val as boolean)"
|
||||
/>
|
||||
</template>
|
||||
</el-table-column>
|
||||
@@ -68,15 +68,18 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, computed } from "vue";
|
||||
import { ref, computed, onMounted } from "vue";
|
||||
import { Search } from "@element-plus/icons-vue";
|
||||
import { ElMessage } from "element-plus";
|
||||
import type { ApiEndpointPermissionsResponse } from "@/types/api";
|
||||
import { fetchApiOperations } from "@/api/projectPermissions";
|
||||
|
||||
interface Endpoint {
|
||||
endpoint_key: string;
|
||||
method: string;
|
||||
path: string;
|
||||
interface Operation {
|
||||
operation_key: string;
|
||||
module: string;
|
||||
action: string;
|
||||
description: string;
|
||||
default_roles: string[];
|
||||
}
|
||||
|
||||
interface Props {
|
||||
@@ -93,7 +96,8 @@ const emit = defineEmits<Emits>();
|
||||
|
||||
const searchText = ref("");
|
||||
const filterModule = ref("");
|
||||
const filterMethod = ref("");
|
||||
const filterAction = ref("");
|
||||
const operations = ref<Operation[]>([]);
|
||||
|
||||
// 从矩阵中提取角色列表
|
||||
const roles = computed(() => {
|
||||
@@ -101,80 +105,56 @@ const roles = computed(() => {
|
||||
return Object.keys(props.matrix).sort();
|
||||
});
|
||||
|
||||
// 从矩阵中提取端点列表
|
||||
const endpoints = computed(() => {
|
||||
if (!props.matrix) return [];
|
||||
|
||||
const endpointSet = new Set<string>();
|
||||
Object.values(props.matrix).forEach((rolePerms) => {
|
||||
Object.keys(rolePerms).forEach((endpoint) => {
|
||||
endpointSet.add(endpoint);
|
||||
});
|
||||
});
|
||||
|
||||
return Array.from(endpointSet)
|
||||
.map((endpoint_key) => {
|
||||
const [method, ...pathParts] = endpoint_key.split(":");
|
||||
const path = pathParts.join(":");
|
||||
const module = extractModule(path);
|
||||
|
||||
return {
|
||||
endpoint_key,
|
||||
method,
|
||||
path,
|
||||
module,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.endpoint_key.localeCompare(b.endpoint_key));
|
||||
});
|
||||
|
||||
// 提取模块名称
|
||||
const extractModule = (path: string): string => {
|
||||
const match = path.match(/\/(\w+)/);
|
||||
return match ? match[1] : "unknown";
|
||||
// 加载权限操作列表
|
||||
const loadOperations = async () => {
|
||||
try {
|
||||
const { data } = await fetchApiOperations();
|
||||
operations.value = data.operations;
|
||||
} catch (error) {
|
||||
ElMessage.error("加载权限操作失败");
|
||||
console.error(error);
|
||||
}
|
||||
};
|
||||
|
||||
// 获取唯一的模块列表
|
||||
const uniqueModules = computed(() => {
|
||||
return [...new Set(endpoints.value.map((e) => e.module))].sort();
|
||||
return [...new Set(operations.value.map((o) => o.module))].sort();
|
||||
});
|
||||
|
||||
// 过滤后的端点列表
|
||||
const filteredEndpoints = computed(() => {
|
||||
return endpoints.value.filter((endpoint) => {
|
||||
// 过滤后的操作列表
|
||||
const filteredOperations = computed(() => {
|
||||
return operations.value.filter((operation) => {
|
||||
const matchSearch =
|
||||
!searchText.value ||
|
||||
endpoint.endpoint_key.toLowerCase().includes(searchText.value.toLowerCase()) ||
|
||||
endpoint.path.toLowerCase().includes(searchText.value.toLowerCase());
|
||||
operation.operation_key.toLowerCase().includes(searchText.value.toLowerCase()) ||
|
||||
operation.description.toLowerCase().includes(searchText.value.toLowerCase());
|
||||
|
||||
const matchModule = !filterModule.value || endpoint.module === filterModule.value;
|
||||
const matchMethod = !filterMethod.value || endpoint.method === filterMethod.value;
|
||||
const matchModule = !filterModule.value || operation.module === filterModule.value;
|
||||
const matchAction = !filterAction.value || operation.action === filterAction.value;
|
||||
|
||||
return matchSearch && matchModule && matchMethod;
|
||||
return matchSearch && matchModule && matchAction;
|
||||
});
|
||||
});
|
||||
|
||||
// 检查端点是否允许
|
||||
const isEndpointAllowed = (endpoint_key: string, role: string): boolean => {
|
||||
// 检查操作是否允许
|
||||
const isOperationAllowed = (operation_key: string, role: string): boolean => {
|
||||
if (!props.matrix || !props.matrix[role]) return false;
|
||||
const perm = props.matrix[role][endpoint_key];
|
||||
const perm = props.matrix[role][operation_key];
|
||||
if (!perm) return false;
|
||||
return typeof perm === "boolean" ? perm : perm.allowed;
|
||||
};
|
||||
|
||||
// 获取方法的标签类型
|
||||
const getMethodType = (method: string): string => {
|
||||
// 获取操作的标签类型
|
||||
const getActionType = (action: string): string => {
|
||||
const typeMap: Record<string, string> = {
|
||||
GET: "info",
|
||||
POST: "success",
|
||||
PATCH: "warning",
|
||||
DELETE: "danger",
|
||||
read: "info",
|
||||
write: "success",
|
||||
};
|
||||
return typeMap[method] || "info";
|
||||
return typeMap[action] || "info";
|
||||
};
|
||||
|
||||
// 权限变更处理
|
||||
const onPermissionChange = (endpoint_key: string, role: string, allowed: boolean) => {
|
||||
const onPermissionChange = (operation_key: string, role: string, allowed: boolean) => {
|
||||
if (!props.matrix) return;
|
||||
|
||||
const updatedMatrix: ApiEndpointPermissionsResponse = JSON.parse(JSON.stringify(props.matrix));
|
||||
@@ -183,10 +163,14 @@ const onPermissionChange = (endpoint_key: string, role: string, allowed: boolean
|
||||
updatedMatrix[role] = {};
|
||||
}
|
||||
|
||||
updatedMatrix[role][endpoint_key] = allowed;
|
||||
updatedMatrix[role][operation_key] = allowed;
|
||||
|
||||
emit("update", updatedMatrix);
|
||||
};
|
||||
|
||||
onMounted(() => {
|
||||
loadOperations();
|
||||
});
|
||||
</script>
|
||||
|
||||
<style scoped lang="scss">
|
||||
|
||||
Reference in New Issue
Block a user