完全移除模块级权限系统,迁移至接口级权限

后端:
- 删除 StudyRolePermission 模型和 project_permissions API 文件
- 重写 project_permissions.py core,移除所有模块级权限函数
- 重写 permission_cache.py,移除模块级权限缓存逻辑
- 新增权限模板功能(PermissionTemplate 模型、API、服务层)
- 新增 permission_templates 数据库迁移
- 迁移 8 个模块(attachments、audit_logs、dashboard、faqs、
  faq_categories、fees_attachments、knowledge_notes、
  material_equipments、overview、subject_histories、subject_pds)
  至接口级权限检查
- 删除所有模块级权限相关测试文件,新增权限模板测试

前端:
- 删除 ProjectPermissions.vue 和 ProjectPermissionsModule.vue
- 重写 projectRoutePermissions.ts,改为基于接口级权限格式
- 更新 store/study.ts、router/index.ts、AuditLogs.vue、Projects.vue
  中的权限 API 调用,从 fetchProjectRolePermissions 改为
  fetchApiEndpointPermissions
- 清理 types/api.ts 中的旧模块级权限类型定义
- 新增 PermissionTemplateSelector.vue 组件
- 更新权限管理页面,移除模块级权限 tab

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Cheng Zhou
2026-05-15 09:07:43 +08:00
parent 3b1bdc2070
commit 20ce6bccef
55 changed files with 1971 additions and 3030 deletions
@@ -18,47 +18,52 @@
<el-option
v-for="module in uniqueModules"
:key="module"
:label="module"
:label="moduleLabel(module)"
:value="module"
/>
</el-select>
<el-select v-model="filterAction" placeholder="筛选操作" clearable style="width: 120px">
<el-option label="全部操作" value="" />
<el-option label="创建" value="create" />
<el-option label="读取" value="read" />
<el-option label="写入" value="write" />
<el-option label="更新" value="update" />
<el-option label="删除" value="delete" />
<el-option label="导出" value="export" />
</el-select>
</div>
<!-- 权限矩阵表格 -->
<div class="api-permissions-table-wrapper">
<el-table :data="filteredOperations" border stripe>
<el-table-column prop="operation_key" label="权限操作" width="200">
<el-table :data="filteredOperations" border stripe :span-method="spanMethod">
<el-table-column prop="module" label="模块" width="140">
<template #default="{ row }">
<div class="operation-cell">
<el-tag :type="getActionType(row.action)" size="small">
{{ row.action === 'read' ? '读取' : '写入' }}
</el-tag>
<span class="operation-name">{{ row.operation_key }}</span>
</div>
<span class="module-label">{{ moduleLabel(row.module) }}</span>
</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 prop="operation_key" label="权限操作" width="200">
<template #default="{ row }">
<div class="operation-cell">
<el-tag :type="getActionType(row)" size="small">
{{ getActionLabel(row) }}
</el-tag>
<span class="operation-name">{{ row.description || row.operation_key }}</span>
</div>
</template>
</el-table-column>
<el-table-column
v-for="role in roles"
:key="role"
:label="role"
:width="100"
:width="90"
align="center"
>
<template #default="{ row }">
<el-checkbox
:model-value="isOperationAllowed(row.operation_key, role)"
@change="(val) => onPermissionChange(row.operation_key, role, val as boolean)"
@change="(val: boolean) => onPermissionChange(row.operation_key, role, val)"
/>
</template>
</el-table-column>
@@ -99,6 +104,30 @@ const filterModule = ref("");
const filterAction = ref("");
const operations = ref<Operation[]>([]);
const MODULE_LABELS: Record<string, string> = {
subjects: "参与者管理",
sites: "中心管理",
risk_issues: "风险问题",
monitoring_audit: "监查稽查",
project_members: "项目成员",
project_milestones: "项目里程碑",
project_overview: "项目总览",
fees: "合同费用",
materials: "物资管理",
material_equipments: "物资设备",
documents: "文件版本",
startup_ethics: "立项与伦理",
startup_auth: "启动与授权",
audit_export: "审计日志",
faq: "FAQ",
shared_library: "共享库",
attachments: "附件管理",
dashboard: "仪表板",
subject_histories: "参与者历史",
};
const moduleLabel = (module: string): string => MODULE_LABELS[module] || module;
// 从矩阵中提取角色列表
const roles = computed(() => {
if (!props.matrix) return [];
@@ -121,21 +150,48 @@ const uniqueModules = computed(() => {
return [...new Set(operations.value.map((o) => o.module))].sort();
});
// 过滤后的操作列表
// 过滤并按模块分组排序
const filteredOperations = computed(() => {
return operations.value.filter((operation) => {
const filtered = operations.value.filter((operation) => {
const matchSearch =
!searchText.value ||
operation.operation_key.toLowerCase().includes(searchText.value.toLowerCase()) ||
operation.description.toLowerCase().includes(searchText.value.toLowerCase());
operation.description.toLowerCase().includes(searchText.value.toLowerCase()) ||
moduleLabel(operation.module).includes(searchText.value);
const matchModule = !filterModule.value || operation.module === filterModule.value;
const matchAction = !filterAction.value || operation.action === filterAction.value;
const matchAction = !filterAction.value || getOperationVerb(operation) === filterAction.value;
return matchSearch && matchModule && matchAction;
});
// 按模块分组排序
filtered.sort((a, b) => {
if (a.module !== b.module) {
return moduleLabel(a.module).localeCompare(moduleLabel(b.module), "zh-CN");
}
return a.operation_key.localeCompare(b.operation_key);
});
return filtered;
});
// 模块列合并行
const spanMethod = ({ row, rowIndex, columnIndex }: { row: Operation; rowIndex: number; column: any; columnIndex: number }) => {
if (columnIndex === 0) {
const list = filteredOperations.value;
if (rowIndex === 0 || list[rowIndex - 1].module !== row.module) {
let count = 1;
for (let i = rowIndex + 1; i < list.length && list[i].module === row.module; i++) {
count++;
}
return { rowspan: count, colspan: 1 };
}
return { rowspan: 0, colspan: 0 };
}
return { rowspan: 1, colspan: 1 };
};
// 检查操作是否允许
const isOperationAllowed = (operation_key: string, role: string): boolean => {
if (!props.matrix || !props.matrix[role]) return false;
@@ -144,13 +200,44 @@ const isOperationAllowed = (operation_key: string, role: string): boolean => {
return typeof perm === "boolean" ? perm : perm.allowed;
};
// 获取操作的标签类型
const getActionType = (action: string): string => {
const typeMap: Record<string, string> = {
read: "info",
write: "success",
};
return typeMap[action] || "info";
// 从 operation_key 提取操作类型
const getOperationVerb = (row: Operation): string => {
const key = row.operation_key;
const suffix = key.split(":").pop() || "";
if (suffix === "create") return "create";
if (suffix === "read" || suffix === "list") return "read";
if (suffix === "update") return "update";
if (suffix === "delete") return "delete";
if (suffix === "export") return "export";
return row.action;
};
const ACTION_LABELS: Record<string, string> = {
create: "创建",
read: "读取",
update: "更新",
delete: "删除",
export: "导出",
write: "写入",
};
const ACTION_TAG_TYPES: Record<string, string> = {
create: "success",
read: "info",
update: "warning",
delete: "danger",
export: "",
write: "success",
};
const getActionLabel = (row: Operation): string => {
const verb = getOperationVerb(row);
return ACTION_LABELS[verb] || verb;
};
const getActionType = (row: Operation): string => {
const verb = getOperationVerb(row);
return ACTION_TAG_TYPES[verb] || "info";
};
// 权限变更处理
@@ -189,20 +276,20 @@ onMounted(() => {
overflow-x: auto;
}
.endpoint-cell {
.module-label {
font-weight: 600;
font-size: 13px;
color: #303133;
}
.operation-cell {
display: flex;
align-items: center;
gap: 10px;
gap: 8px;
:deep(.el-tag) {
min-width: 50px;
text-align: center;
.operation-name {
font-size: 13px;
color: #606266;
}
}
.endpoint-path {
font-family: monospace;
font-size: 12px;
color: #606266;
}
</style>
@@ -0,0 +1,400 @@
<template>
<div class="template-selector">
<div class="selector-header">
<h3>快速配置角色权限</h3>
<p class="selector-desc">选择预设模板一键应用到项目角色权限</p>
</div>
<div class="template-grid">
<div
v-for="template in templates"
:key="template.id"
class="template-card"
:class="{ selected: selectedId === template.id, system: template.is_system }"
@click="select(template)"
>
<div class="card-header">
<span class="card-icon">{{ roleIcon(template.category) }}</span>
<div class="card-title-wrap">
<span class="card-name">{{ template.name }}</span>
<el-tag v-if="template.is_system" size="small" type="info">系统预设</el-tag>
<el-tag v-else size="small" type="success">自定义</el-tag>
</div>
</div>
<p class="card-desc">{{ template.description }}</p>
<div class="card-stats">
<span class="stat">
<el-icon><Check /></el-icon>
{{ countEnabled(template) }} 项已启用
</span>
<span class="stat disabled">
<el-icon><Close /></el-icon>
{{ countDisabled(template) }} 项已禁用
</span>
</div>
</div>
<!-- 自定义模板入口 -->
<div class="template-card add-card" @click="showCreateDialog = true">
<el-icon class="add-icon"><Plus /></el-icon>
<span>保存当前配置为模板</span>
</div>
</div>
<!-- 预览面板 -->
<transition name="slide-up">
<div v-if="selected" class="preview-panel">
<div class="preview-header">
<div class="preview-title">
<span class="preview-icon">{{ roleIcon(selected.category) }}</span>
<strong>{{ selected.name }}</strong>
<span class="preview-desc">{{ selected.description }}</span>
</div>
<div class="preview-actions">
<el-button @click="selected = null">取消</el-button>
<el-button type="primary" :loading="applying" @click="applyTemplate">
<el-icon><Check /></el-icon>
应用此模板
</el-button>
</div>
</div>
<div class="preview-body">
<div v-for="(rolePerms, role) in selected.permissions" :key="role" class="role-preview">
<div class="role-label">{{ role }}</div>
<div class="perm-summary">
<el-tag type="success" size="small">启用 {{ countRoleEnabled(rolePerms) }}</el-tag>
<el-tag type="danger" size="small">禁用 {{ countRoleDisabled(rolePerms) }}</el-tag>
</div>
</div>
</div>
</div>
</transition>
<!-- 创建自定义模板对话框 -->
<el-dialog v-model="showCreateDialog" title="保存为自定义模板" width="480px">
<el-form :model="createForm" label-width="80px">
<el-form-item label="模板名称" required>
<el-input v-model="createForm.name" placeholder="请输入模板名称" />
</el-form-item>
<el-form-item label="描述">
<el-input v-model="createForm.description" type="textarea" :rows="2" placeholder="可选描述" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="showCreateDialog = false">取消</el-button>
<el-button type="primary" :loading="creating" @click="createTemplate">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { Check, Close, Plus } from "@element-plus/icons-vue";
import {
fetchPermissionTemplates,
createPermissionTemplate,
applyPermissionTemplate,
type PermissionTemplate,
} from "@/api/projectPermissions";
const props = defineProps<{
studyId: string;
currentPermissions?: Record<string, Record<string, boolean>>;
}>();
const emit = defineEmits<{
applied: [permissions: Record<string, Record<string, { allowed: boolean }>>];
}>();
const templates = ref<PermissionTemplate[]>([]);
const selectedId = ref<string | null>(null);
const selected = computed(() => templates.value.find((t) => t.id === selectedId.value) ?? null);
const applying = ref(false);
const creating = ref(false);
const showCreateDialog = ref(false);
const createForm = ref({ name: "", description: "" });
const roleIcon = (category: string | null) => {
const icons: Record<string, string> = {
PM: "👑",
CRA: "📋",
PV: "🔍",
MEDICAL_REVIEW: "🏥",
IMP: "📦",
QA: "✅",
};
return icons[category ?? ""] ?? "📄";
};
const countEnabled = (t: PermissionTemplate) => {
let n = 0;
for (const perms of Object.values(t.permissions) as Record<string, boolean>[]) {
n += (Object.values(perms) as boolean[]).filter(Boolean).length;
}
return n;
};
const countDisabled = (t: PermissionTemplate) => {
let n = 0;
for (const perms of Object.values(t.permissions) as Record<string, boolean>[]) {
n += (Object.values(perms) as boolean[]).filter((v) => !v).length;
}
return n;
};
const countRoleEnabled = (perms: Record<string, boolean>) =>
Object.values(perms).filter(Boolean).length;
const countRoleDisabled = (perms: Record<string, boolean>) =>
Object.values(perms).filter((v) => !v).length;
const select = (t: PermissionTemplate) => {
selectedId.value = selectedId.value === t.id ? null : t.id;
};
const applyTemplate = async () => {
if (!selected.value) return;
applying.value = true;
try {
const res = await applyPermissionTemplate(props.studyId, {
template_id: selected.value.id,
override: true,
});
ElMessage.success(`已应用模板「${selected.value.name}`);
emit("applied", res.data.permissions);
selectedId.value = null;
} catch {
ElMessage.error("应用模板失败");
} finally {
applying.value = false;
}
};
const createTemplate = async () => {
if (!createForm.value.name.trim()) {
ElMessage.warning("请输入模板名称");
return;
}
if (!props.currentPermissions) {
ElMessage.warning("当前没有权限配置可保存");
return;
}
creating.value = true;
try {
await createPermissionTemplate({
name: createForm.value.name,
description: createForm.value.description || undefined,
template_type: "CUSTOM",
permissions: props.currentPermissions,
});
ElMessage.success("模板保存成功");
showCreateDialog.value = false;
createForm.value = { name: "", description: "" };
await loadTemplates();
} catch {
ElMessage.error("保存模板失败");
} finally {
creating.value = false;
}
};
const loadTemplates = async () => {
try {
const res = await fetchPermissionTemplates();
templates.value = res.data;
} catch {
ElMessage.error("加载模板失败");
}
};
onMounted(loadTemplates);
</script>
<style scoped>
.template-selector {
padding: 16px 0;
}
.selector-header {
margin-bottom: 16px;
}
.selector-header h3 {
margin: 0 0 4px;
font-size: 15px;
font-weight: 600;
color: #1a1a1a;
}
.selector-desc {
margin: 0;
font-size: 13px;
color: #888;
}
.template-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 12px;
margin-bottom: 16px;
}
.template-card {
padding: 14px;
border: 1.5px solid #e4e7ed;
border-radius: 8px;
cursor: pointer;
transition: all 0.2s;
background: #fff;
}
.template-card:hover {
border-color: #409eff;
box-shadow: 0 2px 8px rgba(64, 158, 255, 0.15);
}
.template-card.selected {
border-color: #409eff;
background: #ecf5ff;
}
.card-header {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 6px;
}
.card-icon {
font-size: 20px;
line-height: 1;
}
.card-title-wrap {
display: flex;
flex-direction: column;
gap: 3px;
}
.card-name {
font-size: 14px;
font-weight: 600;
color: #303133;
}
.card-desc {
font-size: 12px;
color: #909399;
margin: 0 0 8px;
line-height: 1.4;
}
.card-stats {
display: flex;
gap: 10px;
font-size: 12px;
color: #67c23a;
}
.card-stats .stat {
display: flex;
align-items: center;
gap: 3px;
}
.card-stats .stat.disabled {
color: #f56c6c;
}
.add-card {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 8px;
border-style: dashed;
color: #909399;
font-size: 13px;
min-height: 110px;
}
.add-card:hover {
color: #409eff;
border-color: #409eff;
}
.add-icon {
font-size: 24px;
}
.preview-panel {
border: 1.5px solid #409eff;
border-radius: 8px;
background: #ecf5ff;
padding: 14px 16px;
}
.preview-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.preview-title {
display: flex;
align-items: center;
gap: 8px;
font-size: 14px;
}
.preview-icon {
font-size: 18px;
}
.preview-desc {
color: #909399;
font-size: 12px;
}
.preview-body {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.role-preview {
display: flex;
align-items: center;
gap: 8px;
background: #fff;
border-radius: 6px;
padding: 6px 10px;
}
.role-label {
font-size: 13px;
font-weight: 600;
color: #303133;
min-width: 60px;
}
.perm-summary {
display: flex;
gap: 6px;
}
.slide-up-enter-active,
.slide-up-leave-active {
transition: all 0.25s ease;
}
.slide-up-enter-from,
.slide-up-leave-to {
opacity: 0;
transform: translateY(8px);
}
</style>
@@ -1,95 +0,0 @@
<template>
<div class="module-permissions">
<div class="permissions-table-wrapper">
<el-table :data="tableData" border stripe>
<el-table-column prop="role" label="角色" width="120" />
<el-table-column
v-for="module in modules"
:key="module.key"
:label="module.label"
:width="120"
>
<template #default="{ row }">
<div class="permission-cell">
<el-checkbox
v-model="row.permissions[module.key].read"
label="读"
size="small"
@change="onPermissionChange"
/>
<el-checkbox
v-if="module.writable !== false"
v-model="row.permissions[module.key].write"
label="写"
size="small"
@change="onPermissionChange"
/>
</div>
</template>
</el-table-column>
</el-table>
</div>
</div>
</template>
<script setup lang="ts">
import { computed } from "vue";
import type { ProjectRolePermissionsResponse, ProjectPermissionModule } from "@/types/api";
interface Props {
project: any;
matrix: ProjectRolePermissionsResponse | null;
}
interface Emits {
(e: "update", matrix: ProjectRolePermissionsResponse): void;
}
const props = defineProps<Props>();
const emit = defineEmits<Emits>();
const modules = computed(() => {
if (!props.matrix) return [];
return props.matrix.modules || [];
});
const tableData = computed(() => {
if (!props.matrix) return [];
return Object.entries(props.matrix.roles).map(([role, permissions]) => ({
role,
permissions,
}));
});
const onPermissionChange = () => {
if (!props.matrix) return;
const updatedMatrix: ProjectRolePermissionsResponse = {
modules: props.matrix.modules,
roles: { ...props.matrix.roles },
};
emit("update", updatedMatrix);
};
</script>
<style scoped lang="scss">
.module-permissions {
padding: 20px 0;
}
.permissions-table-wrapper {
overflow-x: auto;
}
.permission-cell {
display: flex;
gap: 10px;
align-items: center;
:deep(.el-checkbox) {
margin: 0;
}
}
</style>