diff --git a/frontend/src/api/training/index.ts b/frontend/src/api/training/index.ts index 01b917e..bbca06c 100644 --- a/frontend/src/api/training/index.ts +++ b/frontend/src/api/training/index.ts @@ -1250,22 +1250,28 @@ export const ROLE_DICT_RULE_KEY = 'role_dict' export const TRAINEE_LABEL = '学员' /** 系统默认职称角色(与后端 DefaultRoleDict 一致;字典缺失/损坏时兜底)。 - * 角色体系 v2:学员(隐式)/课长/经理/人事/讲师;经理继承课长全部权限并叠加审批决策。 */ + * 角色体系 v2:学员(隐式)/课长/经理/人事/讲师;经理继承课长全部权限并叠加审批决策。 + * v2.1 增补超级管理员:不继承任何权限组,仅承载终态数据(已确认需求/已审核课程/ + * 已发布计划)的删除权,组织架构「设置角色」可分配。 */ export const DEFAULT_ROLE_DICT: TrainingRoleDef[] = [ { key: 'hr', label: '人事', short: '人事', system: true }, { key: 'supervisor', label: '课长', short: '课长', system: true }, { key: 'lecturer', label: '讲师', short: '讲师', system: true }, { key: 'exec', label: '经理', short: '经理', system: true, inherits: ['supervisor'] }, + { key: 'super_admin', label: '超级管理员', short: '超管', system: true }, ] /** 存量角色别名:部门文员(clerk)已移除,按课长对待(与后端 LegacyRoleAlias 同构) */ const LEGACY_ROLE_ALIAS: Record = { clerk: 'supervisor' } -/** 角色体系 v2 升级判定:经理(exec)必须继承课长,否则视为旧字典(与后端 NeedsRoleDictUpgrade 同构) */ +/** 角色体系升级判定(与后端 NeedsRoleDictUpgrade 同构): + * 经理(exec)未继承课长视为 v1 旧字典;v2 口径但缺 v2.1 增补的超级管理员 + * 同样需要读时升级补齐(存量租户免迁移)。 */ export function needsRoleDictUpgrade(dict: TrainingRoleDef[]): boolean { const mgr = dict.find((r) => r.key === 'exec') if (!mgr) return true - return !(mgr.inherits || []).includes('supervisor') + if (!(mgr.inherits || []).includes('supervisor')) return true + return !dict.some((r) => r.key === 'super_admin') } /** v1 存量字典升级为 v2 视图(与后端 UpgradeRoleDictV2 同构,幂等): diff --git a/frontend/src/views/org-center/OrgCenter.vue b/frontend/src/views/org-center/OrgCenter.vue index 4a321b8..1687708 100644 --- a/frontend/src/views/org-center/OrgCenter.vue +++ b/frontend/src/views/org-center/OrgCenter.vue @@ -21,14 +21,17 @@ 部门 新增部门 - - - + +
+ + + +
@@ -66,32 +69,35 @@ 批量导入 - - - - - - - - + +
+ + + + + + + + +
@@ -854,15 +860,23 @@ onMounted(() => { } // 分栏:部门列收窄为固定宽度(000164,原 3fr 过宽且编码列留白明显), -// 员工档案占剩余全部宽度;窄屏退化为单列 +// 员工档案占剩余全部宽度;窄屏退化为单列。 +// 满屏布局(000164 布局诉求):栅格占满页身剩余高度,两面板各自内部滚动 —— +// 面板头(标题/操作/查找)/表头/分页固定,仅表格数据区滚动;窄屏单列回退整页滚动 .org-layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); + // 满屏关键:行高锁定为容器高(grid 行默认 auto 会被内容撑开,面板 height:100% + // 的百分比链随之失效,表头/分页固定就不生效了) + grid-template-rows: minmax(0, 1fr); gap: 12px; - align-items: start; + height: 100%; + min-height: 0; @media (max-width: 900px) { grid-template-columns: 1fr; + grid-template-rows: none; + height: auto; } // 000164 栅格子项收敛:员工档案表格/工具行较宽,缺省 min-width 会把 @@ -875,6 +889,31 @@ onMounted(() => { .panel-box { .training-panel-box(); + + .org-layout & { + display: flex; + flex-direction: column; + height: 100%; + + @media (max-width: 900px) { + height: auto; + } + } + + // 面板头固定,表格滚动区撑满剩余高度(表格 height=100% 启用 TDesign 固定表头; + // .t-table 根也要撑满 100%,否则百分比退化为内容高、表头固定不生效) + .org-layout & > .table-scroll { + flex: 1; + min-height: 0; + + > .t-table { + height: 100%; + } + } + + .org-layout & > .training-pagination { + flex-shrink: 0; + } } .form-hint { diff --git a/frontend/src/views/training/Training.vue b/frontend/src/views/training/Training.vue index 5f9341a..4259d56 100644 --- a/frontend/src/views/training/Training.vue +++ b/frontend/src/views/training/Training.vue @@ -124,11 +124,11 @@ const isAdmin = computed(() => myRoles.value.some((r) => r === 'hr') || authStore.hasRole('admin')) -// 培训管理页签(培训计划/培训课程/培训签到)门槛:课长/经理/人事/讲师 +// 培训管理页签(培训计划/培训课程/培训签到)门槛:课长/经理/人事/讲师/超级管理员 // (myRoles 已按权限组展开,自定义角色继承系统角色后自动生效)。 const canManageTabs = computed(() => isAdmin.value || - myRoles.value.some((r) => ['hr', 'supervisor', 'exec', 'lecturer'].includes(r))) + myRoles.value.some((r) => ['hr', 'supervisor', 'exec', 'lecturer', 'super_admin'].includes(r))) // 配置中心门槛(000158):人事/空间管理员之外,经理职称角色同权查看与维护。 const canManageConfig = computed(() => diff --git a/frontend/src/views/training/components/CoursePanel.vue b/frontend/src/views/training/components/CoursePanel.vue index 40eacdb..6a2f3f5 100644 --- a/frontend/src/views/training/components/CoursePanel.vue +++ b/frontend/src/views/training/components/CoursePanel.vue @@ -36,54 +36,64 @@ 共 {{ filteredCourses.length }} 门 - - - - - - - - - - - - - - - - + +
+ + + + + + + + + + + + + + + + +
@@ -585,6 +595,9 @@ const props = defineProps<{ roles: string[]; admin?: boolean }>() // 课程维护:人事/课长/讲师(roles 已按权限组展开,经理经课长继承自动命中); // 纯讲师的课程列表范围由后端按 lecturer_id 强制收窄。 const canManage = computed(() => props.admin || props.roles.some((r) => r === 'hr' || r === 'supervisor' || r === 'lecturer')) +// 超级管理员职称角色(super_admin):已发布计划的终态删除权唯一入口 +// (roles 已按权限组展开,自定义角色继承「超级管理员」权限组后同样命中) +const isSuperAdmin = computed(() => props.roles.includes('super_admin')) // 纯讲师视角隐藏「按讲师查询」筛选(列表已固定为自己的课程) const isLecturerOnly = computed(() => !canManage.value || (props.roles.includes('lecturer') && !props.roles.some((r) => r === 'hr' || r === 'supervisor' || r === 'exec'))) const router = useRouter() @@ -1619,13 +1632,13 @@ const onPublish = async (row: TrainingCourse) => { } const onDelete = async (row: TrainingCourse) => { - if (!window.confirm(`确认删除计划「${row.name}」?`)) return try { await deleteCourse(row.id) MessagePlugin.success('已删除') loadAll() - } catch { - MessagePlugin.error('删除失败') + } catch (e: any) { + // 已发布计划仅超级管理员可删,后端拦截时透出统一文案 + MessagePlugin.error(apiErrorMessage(e, '删除失败')) } } @@ -1914,6 +1927,12 @@ watch(() => route?.query?.courseId, openDetailFromRoute) .training-form-hints-below(); } +// 满屏列表布局(000164):页头固定,面板占满页身剩余高度, +// 筛选区/表头/分页固定,仅表格数据区滚动 +.course-panel { + .training-fill-panel(); +} + .form-hint { margin-top: 8px; color: var(--td-text-color-placeholder); diff --git a/frontend/src/views/training/components/LibraryPanel.vue b/frontend/src/views/training/components/LibraryPanel.vue index 7772ded..713e844 100644 --- a/frontend/src/views/training/components/LibraryPanel.vue +++ b/frontend/src/views/training/components/LibraryPanel.vue @@ -46,97 +46,103 @@ 已选 {{ selectedKeys.length }} 项 - - - - - - - - - - - - - - - - - - - - + + @@ -725,16 +731,19 @@ const hasPermSet = (row: TrainingLibraryCourse) => // 审核/删除权限(000157):严格限「本部门课长/经理」——人事/空间管理员一律无 // 跨部门审核/删除权(roles 已按权限组展开,经理经课长继承自动命中 supervisor)。 const canVerify = computed(() => props.roles.includes('supervisor')) -const canDelete = canVerify +// 超级管理员职称角色(super_admin):终态数据删除权唯一入口,已审核/已发布课程 +// 与跨部门均可删(后端 DeleteCourseLibraryItem 同口径兜底);审核权不放宽 +const isSuperAdmin = computed(() => props.roles.includes('super_admin')) +const canDelete = computed(() => canVerify.value || isSuperAdmin.value) // 仅本部门「待审核」课程可审核/删除;与后端 callerVerifyDeptScope 同口径: -// 无部门身份或课程所属部门与本人不一致时一律不可操作。 +// 无部门身份或课程所属部门与本人不一致时一律不可操作。超级管理员不限状态/部门。 const canVerifyCourse = (row: TrainingLibraryCourse) => canVerify.value && row.verify_status === 'pending' && Boolean(myDeptId.value) && Boolean(row.owner_dept_id) && row.owner_dept_id === myDeptId.value -const canDeleteCourse = canVerifyCourse +const canDeleteCourse = (row: TrainingLibraryCourse) => isSuperAdmin.value || canVerifyCourse(row) // 编辑权限:维护角色可编辑可见课程;无维护角色的学员仅可编辑「与我相关」课程 // (本人创建或本人授课,后端兜底校验同口径) @@ -1057,10 +1066,11 @@ const onBatchVerify = async () => { } const onBatchDelete = async () => { - // 与后端口径一致:仅本部门待审核行提交(已审核/越权行跳过) + // 与后端口径一致:普通角色仅本部门待审核行提交(已审核/越权行跳过); + // 超级管理员不限状态/部门(canDeleteCourse 按角色过滤,终态行可整批删除) const rows = filteredItems.value.filter((r) => selectedKeys.value.includes(r.id) && canDeleteCourse(r)) if (!rows.length) { - MessagePlugin.warning('请先勾选本部门待审核状态的课程') + MessagePlugin.warning(isSuperAdmin.value ? '请先勾选要删除的课程' : '请先勾选本部门待审核状态的课程') return } batchSaving.value = true @@ -1897,6 +1907,10 @@ onMounted(loadAll) .panel-box { .training-panel-box(); } + + // 满屏列表布局(000164):页头固定,面板占满页身剩余高度, + // 筛选区/表头/分页固定,仅表格数据区滚动 + .training-fill-panel(); } .library-filters { diff --git a/frontend/src/views/training/components/PlanPanel.vue b/frontend/src/views/training/components/PlanPanel.vue index 5add8cf..db45eb9 100644 --- a/frontend/src/views/training/components/PlanPanel.vue +++ b/frontend/src/views/training/components/PlanPanel.vue @@ -41,49 +41,52 @@ 查询 重置 批量确认 - 批量删除 - 已选 {{ selectedDemandKeys.length }} 项(可批量确认/删除未确认需求) + 批量删除 + 已选 {{ selectedDemandKeys.length }} 项(可批量确认/删除未确认需求{{ isSuperAdmin ? ';超级管理员可删除已确认需求' : '' }}) - - - - - - - - - - - - - - + +
+ + + + + + + + + + + + + + - + +
@@ -421,8 +432,14 @@ const deptDisplay = computed(() => { return myDeptName.value }) -const demandColumns = [ - { colKey: 'row-select', type: 'multiple', width: 44, disabled: ({ row }: { row: TrainingDemand }) => row.status === 'confirmed' }, +// 超级管理员职称角色(super_admin):终态数据(已确认需求等)删除权唯一入口; +// roles 已按权限组展开,自定义角色继承「超级管理员」权限组后同样命中 +const isSuperAdmin = computed(() => props.roles.includes('super_admin')) + +// 已确认行默认禁止勾选(批量确认/删除仅作用于未确认需求);超级管理员可勾选 +// 以批量删除,故勾选禁用按角色动态计算(computed 随 props.roles 就绪刷新) +const demandColumns = computed(() => [ + { colKey: 'row-select', type: 'multiple', width: 44, disabled: ({ row }: { row: TrainingDemand }) => row.status === 'confirmed' && !isSuperAdmin.value }, // 年计划编号(AP--####,创建时由服务端生成) { colKey: 'code', title: '编号', width: 130 }, { colKey: 'course_name', title: '课程名称' }, @@ -439,7 +456,7 @@ const demandColumns = [ // 状态/操作固定在列表右侧,横向滚动时始终可见 { colKey: 'check_status', title: '状态', width: 84, fixed: 'right' }, { colKey: 'op', title: '操作', width: 160, fixed: 'right' }, -] +]) // 批量确认/删除(仅作用于未确认需求,已确认行不可勾选) const selectedDemandKeys = ref([]) @@ -482,9 +499,11 @@ const onBatchConfirm = async () => { } const onBatchDelete = async () => { - const rows = demands.value.filter((d) => selectedDemandKeys.value.includes(d.id) && d.status !== 'confirmed') + // 勾选口径:未确认行全员可勾;已确认行仅超级管理员可勾(isSuperAdmin 放开勾选), + // 后端按行校验终态删除权,越权行整批报错提示 + const rows = demands.value.filter((d) => selectedDemandKeys.value.includes(d.id)) if (!rows.length) { - MessagePlugin.warning('请先勾选未确认的需求') + MessagePlugin.warning('请先勾选要删除的需求') return } batchSaving.value = true @@ -1043,6 +1062,12 @@ onMounted(() => { .training-form-hints-below(); } +// 满屏列表布局(000164):页头固定,面板占满页身剩余高度, +// 筛选区/表头/分页固定,仅表格数据区滚动 +.plan-panel { + .training-fill-panel(); +} + // 已确认行不做整行置灰:t-table 行选择列 disabled 命中时会加 __row--disabled // 类(整行 text-color-disabled),业务上已确认行仅禁止勾选,文字保持正常色 :deep(.t-table__row--disabled) { diff --git a/frontend/src/views/training/components/RulePanel.vue b/frontend/src/views/training/components/RulePanel.vue index 5121a4d..627a9da 100644 --- a/frontend/src/views/training/components/RulePanel.vue +++ b/frontend/src/views/training/components/RulePanel.vue @@ -1052,8 +1052,30 @@ watch(pushScene, () => loadPushLogs()) display: none; } -// ---- tab 化布局:tab 内容区与 panel-box 间距 ---- +// ---- tab 化布局:页头与页签条固定,仅 tab 内容区滚动(000164 布局诉求) ---- +.rule-panel { + height: 100%; + min-height: 0; + display: flex; + flex-direction: column; +} + .rule-tabs { + flex: 1; + min-height: 0; + display: flex; + flex-direction: column; + + :deep(.t-tabs__header) { + flex-shrink: 0; + } + + :deep(.t-tabs__content) { + flex: 1; + min-height: 0; + overflow-y: auto; + } + :deep(.t-tab-panel) { padding: 12px 0 0; } diff --git a/frontend/src/views/training/training.less b/frontend/src/views/training/training.less index d5bb107..a3b4d84 100644 --- a/frontend/src/views/training/training.less +++ b/frontend/src/views/training/training.less @@ -124,6 +124,45 @@ } } +// 列表页满屏布局(年培训计划/月培训计划/课程管理):页头(标题)固定不动, +// 面板占满页身剩余高度,内部「面板标题/筛选区/分页」固定,仅表格数据区滚动。 +// 约定:panel-box 收为 flex 列,表格外包一层 .table-scroll(flex:1 撑满剩余 +// 高度),表格传 height="100%" 启用 TDesign 固定表头(thead sticky + 数据行滚动)。 +.training-fill-panel() { + height: 100%; + min-height: 0; + display: flex; + flex-direction: column; + + > .panel-box { + flex: 1; + min-height: 0; + display: flex; + flex-direction: column; + } + + > .panel-box > .panel-box__header, + > .panel-box > .panel-box__sub { + flex-shrink: 0; + } + + > .panel-box > .table-scroll { + flex: 1; + min-height: 0; + + // 满屏关键:TDesign 的 height="100%" 落在内部 .t-table__content 上,而其父级 + // .t-table 根默认 auto 高,百分比会退化为内容高(表头固定不生效);给根也撑满 + // 100% 形成确定高度链,数据区才会真正滚动 + > .t-table { + height: 100%; + } + } + + > .panel-box > .training-pagination { + flex-shrink: 0; + } +} + // 内容卡片:对齐知识库 resource-card 规范(描边 + 8px 圆角 + 容器底色) .training-panel-box() { background: var(--td-bg-color-container); diff --git a/internal/application/repository/perm_sql_balance_test.go b/internal/application/repository/perm_sql_balance_test.go new file mode 100644 index 0000000..24e2450 --- /dev/null +++ b/internal/application/repository/perm_sql_balance_test.go @@ -0,0 +1,34 @@ +package repository + +import ( + "strings" + "testing" + + "academy/internal/types/interfaces" +) + +// TestCourseLibraryPermissionCondBalanced 2026-11-26 复测根因:权限 SQL 括号不配平 +// (少一个右括号)导致 scope=catalog / PermFilter 查询整体 500 +// 「syntax error at end of input」,前端 catch 后和信课程恒为空。 +// 本测试锁定:生成 SQL 括号必须配平,占位符数量与参数数量必须一致。 +func TestCourseLibraryPermissionCondBalanced(t *testing.T) { + q := &interfaces.TrainingListQuery{ + PermFilter: true, + PermDeptID: "dept-1", + PermUserID: "user-1", + CatalogDeptID: "dept-1", + CatalogDeptName: "资讯组", + PermIncludeCreator: true, + } + cond, args := courseLibraryPermissionCond(q) + opens := strings.Count(cond, "(") + closes := strings.Count(cond, ")") + if opens != closes { + t.Fatalf("括号不配平: '('=%d ')'=%d\nSQL=%s", opens, closes, cond) + } + placeholders := strings.Count(cond, "?") + if placeholders != len(args) { + t.Fatalf("占位符(%d)与参数(%d)数量不一致\nSQL=%s", placeholders, len(args), cond) + } + t.Logf("OK opens=%d closes=%d placeholders=%d args=%d", opens, closes, placeholders, len(args)) +} diff --git a/internal/application/repository/training_library.go b/internal/application/repository/training_library.go index f38307d..49ef967 100644 --- a/internal/application/repository/training_library.go +++ b/internal/application/repository/training_library.go @@ -31,7 +31,7 @@ const courseLibraryPermissionMatchSQL = "(tr_course_library.perm_dept_ids <> '' "tr_course_library.suitable_members = ? " + "OR (? <> '' AND ? = ANY(string_to_array(replace(replace(replace(replace(tr_course_library.suitable_members, ',', '、'), ',', '、'), ';', '、'), ';', '、'), '、'))) " + "OR (tr_course_library.suitable_members = '' AND (tr_course_library.owner_dept_id = '' " + - "OR (? <> '' AND tr_course_library.owner_dept_id = ?))))" + "OR (? <> '' AND tr_course_library.owner_dept_id = ?)))))" // courseLibraryPermissionCond 组装课程权限匹配条件与参数(000163;handler 注入 // 调用方部门 id/部门名/用户 id,PermIncludeCreator 时创建人/讲师放行)。 diff --git a/internal/application/service/training.go b/internal/application/service/training.go index d5f749d..c4f6ed9 100644 --- a/internal/application/service/training.go +++ b/internal/application/service/training.go @@ -599,13 +599,14 @@ func (s *trainingService) ConfirmDemand(ctx context.Context, tenantID uint64, id } // DeleteDemand soft-deletes one unconfirmed demand. -// 状态「已确认」的需求不可删除,避免误删已纳入年度排期的口径。 -func (s *trainingService) DeleteDemand(ctx context.Context, tenantID uint64, id string) error { +// 状态「已确认」的需求不可删除,避免误删已纳入年度排期的口径;仅超级管理员 +// (bySuperAdmin:super_admin 职称角色或空间 Owner 兜底)放开终态删除。 +func (s *trainingService) DeleteDemand(ctx context.Context, tenantID uint64, id string, bySuperAdmin bool) error { demand, err := s.repo.GetDemand(ctx, tenantID, id) if err != nil { return errors.NewNotFoundError("需求不存在") } - if demand.Status == types.TrainingDemandConfirmed { + if demand.Status == types.TrainingDemandConfirmed && !bySuperAdmin { return errors.NewBadRequestError("已确认的需求不可删除") } return s.repo.DeleteDemand(ctx, tenantID, id) diff --git a/internal/application/service/training_course.go b/internal/application/service/training_course.go index 962db84..4003242 100644 --- a/internal/application/service/training_course.go +++ b/internal/application/service/training_course.go @@ -422,7 +422,16 @@ func courseTypeLabel(t string) string { } // DeleteCourse soft-deletes one course. -func (s *trainingService) DeleteCourse(ctx context.Context, tenantID uint64, id string) error { +// 已发布计划不可普通删除(避免误删已排期/已通知的计划);仅超级管理员 +// (bySuperAdmin:super_admin 职称角色或空间 Owner 兜底)放开终态删除。 +func (s *trainingService) DeleteCourse(ctx context.Context, tenantID uint64, id string, bySuperAdmin bool) error { + course, err := s.GetCourse(ctx, tenantID, id) + if err != nil { + return err + } + if course.Status == types.TrainingCoursePublished && !bySuperAdmin { + return errors.NewBadRequestError("已发布的培训计划不可删除,仅超级管理员可删除") + } return s.repo.DeleteCourse(ctx, tenantID, id) } diff --git a/internal/application/service/training_library.go b/internal/application/service/training_library.go index 73ae5ef..39884f5 100644 --- a/internal/application/service/training_library.go +++ b/internal/application/service/training_library.go @@ -202,14 +202,15 @@ func (s *trainingService) SaveCourseLibraryItem(ctx context.Context, tenantID ui // DeleteCourseLibraryItem 软删课程管理条目(月培训计划已创建的历史计划不受影响)。 // 000157 收紧:仅「待核验」状态可删(已确认课程须走核验流程,不可绕过); -// deptScope 非空(课长/经理)时仅可删除本部门课程。 -func (s *trainingService) DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id, deptScope string) error { +// deptScope 非空(课长/经理)时仅可删除本部门课程。bySuperAdmin(超级管理员 +// 职称角色/空间 Owner 兜底)放开终态:已审核/已发布课程与跨部门均可删。 +func (s *trainingService) DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id, deptScope string, bySuperAdmin bool) error { item, err := s.repo.GetCourseLibraryItem(ctx, tenantID, id) if err != nil || item == nil { return werrors.NewNotFoundError("课程不存在") } - if item.VerifyStatus != types.CourseLibraryVerifyPending { - return werrors.NewBadRequestError("仅待核验状态的课程可删除,已确认课程不可删除") + if item.VerifyStatus != types.CourseLibraryVerifyPending && !bySuperAdmin { + return werrors.NewBadRequestError("仅待审核状态的课程可删除,已审核课程仅超级管理员可删除") } if deptScope != "" && item.OwnerDeptID != deptScope { return werrors.NewForbiddenError("仅可删除本部门的课程") diff --git a/internal/handler/training/training.go b/internal/handler/training/training.go index e4f7f14..9ba1982 100644 --- a/internal/handler/training/training.go +++ b/internal/handler/training/training.go @@ -145,6 +145,23 @@ func (h *Handler) requireRoles(c *gin.Context, allowed ...string) (ok bool) { return false } +// callerIsSuperAdmin 超级管理员判定:持有「超级管理员」职称角色(super_admin, +// 配置中心可配、组织架构「设置角色」可分配)或空间 Owner 兜底;空间 Admin 不放行。 +// 已确认需求/已审核课程/已发布计划这类终态数据的删除仅对超级管理员开放,其余 +// 角色仍按原状态规则拦截,前端删除按钮同口径仅超级管理员可见。 +func (h *Handler) callerIsSuperAdmin(c *gin.Context) bool { + if callerRole := types.TenantRoleFromContext(c.Request.Context()); callerRole.IsValid() && + callerRole == types.TenantRoleOwner { + return true + } + member, err := h.svc.CurrentMember(c.Request.Context(), callerTenantID(c), callerUserID(c)) + if err != nil || member == nil { + return false + } + roles := h.svc.ExpandTrainingRoles(c.Request.Context(), callerTenantID(c), member.TrainingRoleList()) + return types.HasTrainingRole(roles, types.TrainingRoleSuperAdmin) +} + // --------------------------------------------------------------------------- // 数据范围:课程/计划类列表按调用方职称角色收窄(权限模型 §数据范围) // --------------------------------------------------------------------------- @@ -584,7 +601,9 @@ func (h *Handler) SaveDemand(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true, "data": demand}) } -// DeleteDemand DELETE /training/demands/:id — 删除未确认需求。 +// DeleteDemand DELETE /training/demands/:id — 删除未确认需求;已确认需求仅 +// 超级管理员(职称角色 super_admin/空间 Owner 兜底)可删(前端行内删除按钮 +// 同口径仅超级管理员可见)。超级管理员删除时不再要求提报人/管理角色身份。 func (h *Handler) DeleteDemand(c *gin.Context) { ctx := c.Request.Context() demand, err := h.svc.GetDemand(ctx, callerTenantID(c), c.Param("id")) @@ -592,15 +611,16 @@ func (h *Handler) DeleteDemand(c *gin.Context) { _ = c.Error(err) return } - if demand.Status == types.TrainingDemandConfirmed { - _ = c.Error(errors.NewBadRequestError("已确认的需求不可删除")) + bySuperAdmin := h.callerIsSuperAdmin(c) + if demand.Status == types.TrainingDemandConfirmed && !bySuperAdmin { + _ = c.Error(errors.NewBadRequestError("已确认的需求不可删除,仅超级管理员可删除")) return } - if !h.callerCanMutateDemand(c, demand.SubmitterID) { + if !bySuperAdmin && !h.callerCanMutateDemand(c, demand.SubmitterID) { _ = c.Error(errors.NewForbiddenError("仅提报人或培训管理角色可删除该需求")) return } - if err := h.svc.DeleteDemand(ctx, callerTenantID(c), c.Param("id")); err != nil { + if err := h.svc.DeleteDemand(ctx, callerTenantID(c), c.Param("id"), bySuperAdmin); err != nil { _ = c.Error(err) return } @@ -1373,13 +1393,16 @@ func (h *Handler) PublishCourse(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"success": true, "data": published}) } -// DeleteCourse DELETE /training/courses/:id +// DeleteCourse DELETE /training/courses/:id — 已发布计划仅超级管理员(职称角色 +// super_admin/空间 Owner 兜底)可删,其余状态仍为人事/课长(服务层同口径兜底 +// 校验);超级管理员删除时不要求人事/课长身份。 func (h *Handler) DeleteCourse(c *gin.Context) { ctx := c.Request.Context() - if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) { + bySuperAdmin := h.callerIsSuperAdmin(c) + if !bySuperAdmin && !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) { return } - if err := h.svc.DeleteCourse(ctx, callerTenantID(c), c.Param("id")); err != nil { + if err := h.svc.DeleteCourse(ctx, callerTenantID(c), c.Param("id"), bySuperAdmin); err != nil { _ = c.Error(err) return } diff --git a/internal/handler/training/training_library.go b/internal/handler/training/training_library.go index a94c9bb..ba42375 100644 --- a/internal/handler/training/training_library.go +++ b/internal/handler/training/training_library.go @@ -209,17 +209,23 @@ func (h *Handler) canEditRelatedLibraryCourse(c *gin.Context, id string) bool { } // DeleteCourseLibraryItem DELETE /training/course-library/:id — 删除 -// (000157:仅本部门课长/经理,仅待核验状态;人事/学员/空间管理员无删除权限)。 +// (000157:仅本部门课长/经理,仅待审核状态;人事/学员/空间管理员无删除权限。 +// 超级管理员[职称角色 super_admin/空间 Owner 兜底]放开终态:已审核/已发布 +// 课程与跨部门均可删,且不要求课长/经理身份)。 func (h *Handler) DeleteCourseLibraryItem(c *gin.Context) { - if !h.requireRoles(c, types.TrainingRoleSupervisor, types.TrainingRoleManager) { - return + bySuperAdmin := h.callerIsSuperAdmin(c) + deptID := "" + if !bySuperAdmin { + if !h.requireRoles(c, types.TrainingRoleSupervisor, types.TrainingRoleManager) { + return + } + var ok bool + if deptID, ok = h.callerVerifyDeptScope(c); !ok { + _ = c.Error(errors.NewForbiddenError("仅本部门课长/经理可删除课程")) + return + } } - deptID, ok := h.callerVerifyDeptScope(c) - if !ok { - _ = c.Error(errors.NewForbiddenError("仅本部门课长/经理可删除课程")) - return - } - if err := h.svc.DeleteCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id"), deptID); err != nil { + if err := h.svc.DeleteCourseLibraryItem(c.Request.Context(), callerTenantID(c), c.Param("id"), deptID, bySuperAdmin); err != nil { _ = c.Error(err) return } diff --git a/internal/types/interfaces/training.go b/internal/types/interfaces/training.go index ac2688b..1bdb556 100644 --- a/internal/types/interfaces/training.go +++ b/internal/types/interfaces/training.go @@ -336,7 +336,9 @@ type TrainingService interface { // ImportDemands 批量导入需求;返回导入行与自动新增的待核验课程数(000158)。 ImportDemands(ctx context.Context, tenantID uint64, demands []*types.TrainingDemand, updaterID string) ([]*types.TrainingDemand, int, error) ConfirmDemand(ctx context.Context, tenantID uint64, id string, updaterID string) (*types.TrainingDemand, error) - DeleteDemand(ctx context.Context, tenantID uint64, id string) error + // DeleteDemand 删除需求;bySuperAdmin(超级管理员职称角色/空间 Owner 兜底) + // 时已确认需求同样可删,其余角色仅未确认需求。 + DeleteDemand(ctx context.Context, tenantID uint64, id string, bySuperAdmin bool) error // ListLecturers/ListPosts 需求提报表单下拉数据源(讲师/岗位)。 ListLecturers(ctx context.Context, tenantID uint64) ([]*types.TrainingLecturerOption, error) ListPosts(ctx context.Context, tenantID uint64) ([]string, error) @@ -356,7 +358,9 @@ type TrainingService interface { ListCourses(ctx context.Context, tenantID uint64, q *TrainingListQuery) ([]*types.TrainingCourse, int64, error) GetCourse(ctx context.Context, tenantID uint64, id string) (*types.TrainingCourse, error) SaveCourse(ctx context.Context, tenantID uint64, course *types.TrainingCourse, updaterID string) (*types.TrainingCourse, error) - DeleteCourse(ctx context.Context, tenantID uint64, id string) error + // DeleteCourse 删除培训计划;bySuperAdmin(超级管理员职称角色/空间 Owner 兜底) + // 时已发布计划同样可删,其余角色仅未发布计划。 + DeleteCourse(ctx context.Context, tenantID uint64, id string, bySuperAdmin bool) error PublishCourse(ctx context.Context, tenantID uint64, id, updaterID string) (*types.TrainingCourse, error) // ListCourseChangeLogs 课程信息修改版本记录(每次保存一条,变更明细可回溯)。 ListCourseChangeLogs(ctx context.Context, tenantID uint64, courseID string) ([]*types.TrainingCourseChangeLog, error) @@ -374,9 +378,10 @@ type TrainingService interface { VerifyCourseLibraryItems(ctx context.Context, tenantID uint64, ids []string, verifierID, deptScope string) (*CourseLibraryVerifyResult, error) // GetCourseLibraryItem 取单条课程条目(发布/下线后回显)。 GetCourseLibraryItem(ctx context.Context, tenantID uint64, id string) (*types.TrainingCourseLibrary, error) - // DeleteCourseLibraryItem 软删课程条目(仅待核验状态可删;deptScope 非空时 - // 校验所属部门匹配,000157 仅课长/经理限本部门)。 - DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id, deptScope string) error + // DeleteCourseLibraryItem 软删课程条目(仅待审核状态可删;deptScope 非空时 + // 校验所属部门匹配,000157 仅课长/经理限本部门;bySuperAdmin 超级管理员 + // 职称角色/空间 Owner 兜底时已审核/已发布课程与跨部门均放行)。 + DeleteCourseLibraryItem(ctx context.Context, tenantID uint64, id, deptScope string, bySuperAdmin bool) error // ImportCourseLibraryItems 批量导入(前端解析 Excel 后按行提交)。 ImportCourseLibraryItems(ctx context.Context, tenantID uint64, items []*types.TrainingCourseLibrary, updaterID string) (int, error) // ExportCourseLibrary 导出 Excel(excelize;ids 空 = 全量)。 diff --git a/internal/types/training.go b/internal/types/training.go index 5e2cd1d..06287cc 100644 --- a/internal/types/training.go +++ b/internal/types/training.go @@ -30,6 +30,10 @@ const ( // TrainingRoleManager 经理(课长全部权限 + 审批决策 + 管理驾驶舱 + 讲师结算)。 // 历史沿革:该角色前身是「高管(驾驶舱)」,key 保持 "exec" 不变以兼容存量数据。 TrainingRoleManager TrainingRole = "exec" + // TrainingRoleSuperAdmin 超级管理员(终态数据删除权):已确认需求/已审核课程/ + // 已发布计划这类终态数据仅该职称角色(及空间 Owner 兜底)可删,其余权限不做 + // 放宽——删除权单独收敛,不随人事/课长默认携带。 + TrainingRoleSuperAdmin TrainingRole = "super_admin" ) // legacyRoleClerk 部门文员(已移除的系统角色)。存量成员/自定义角色可能仍携带, @@ -66,12 +70,14 @@ func SystemTrainingRoles() []string { return []string{ TrainingRoleHR, TrainingRoleSupervisor, TrainingRoleLecturer, TrainingRoleManager, + TrainingRoleSuperAdmin, } } // DefaultRoleDict 系统默认职称角色(与 defaultRuleSeed 的 role_dict 一致)。 // 角色体系 v2:学员(隐式)/课长/经理/人事/讲师;经理继承课长全部权限并叠加 -// 审批决策、管理驾驶舱与讲师结算。 +// 审批决策、管理驾驶舱与讲师结算。超级管理员不继承任何权限组,仅承载终态 +// 数据(已确认需求/已审核课程/已发布计划)的删除权。 func DefaultRoleDict() []TrainingRoleDef { return []TrainingRoleDef{ {Key: TrainingRoleHR, Label: "人事", Short: "人事", System: true}, @@ -79,6 +85,7 @@ func DefaultRoleDict() []TrainingRoleDef { {Key: TrainingRoleLecturer, Label: "讲师", Short: "讲师", System: true}, {Key: TrainingRoleManager, Label: "经理", Short: "经理", System: true, Inherits: []string{TrainingRoleSupervisor}}, + {Key: TrainingRoleSuperAdmin, Label: "超级管理员", Short: "超管", System: true}, } } @@ -236,15 +243,22 @@ func hasString(list []string, want string) bool { return false } -// NeedsRoleDictUpgrade 判断存量字典是否为 v1 口径(角色体系调整前): -// v2 的硬标记是经理(exec)必须继承课长;凡不满足即视为 v1 需升级。 +// NeedsRoleDictUpgrade 判断存量字典是否需要读时升级: +// - v1 口径:经理(exec)未继承课长(角色体系调整前的字典); +// - v2 口径但缺后续新增的系统角色(如超级管理员):同样需要补齐, +// 使存量租户无需迁移即可在配置中心看到并分配新角色。 func NeedsRoleDictUpgrade(dict []TrainingRoleDef) bool { + mgrFound := false for _, r := range dict { if r.Key == TrainingRoleManager { - return !hasString(r.Inherits, TrainingRoleSupervisor) + mgrFound = true + if !hasString(r.Inherits, TrainingRoleSupervisor) { + return true + } } } - return true // 字典缺经理角色(异常/极旧数据),同样需要补齐 + // 缺经理(异常/极旧数据)或缺超级管理员,均视为需要升级 + return !mgrFound || !hasString(RoleDictKeys(dict), TrainingRoleSuperAdmin) } // UpgradeRoleDictV2 把 v1 存量字典升级为 v2(课长/经理/人事/讲师),幂等: diff --git a/internal/types/training_roles_test.go b/internal/types/training_roles_test.go index ad1c95f..9f54790 100644 --- a/internal/types/training_roles_test.go +++ b/internal/types/training_roles_test.go @@ -17,8 +17,8 @@ func marshalDict(t *testing.T, roles []TrainingRoleDef) []byte { func TestDefaultRoleDict(t *testing.T) { dict := DefaultRoleDict() - if len(dict) != 4 { - t.Fatalf("default dict should have 4 system roles, got %d", len(dict)) + if len(dict) != 5 { + t.Fatalf("default dict should have 5 system roles, got %d", len(dict)) } sysKeys := SystemTrainingRoles() for i, r := range dict { @@ -33,10 +33,15 @@ func TestDefaultRoleDict(t *testing.T) { } } // 经理必须继承课长(经理权限 = 课长全部 + 审批决策) - mgr := dict[len(dict)-1] + mgr := dict[3] if mgr.Key != TrainingRoleManager || !hasString(mgr.Inherits, TrainingRoleSupervisor) { t.Fatalf("manager should inherit supervisor, got %+v", mgr) } + // 超级管理员:纯删除权角色,不继承任何权限组 + super := dict[len(dict)-1] + if super.Key != TrainingRoleSuperAdmin || len(super.Inherits) != 0 { + t.Fatalf("super admin should have no inherits, got %+v", super) + } } func TestParseRoleDictValid(t *testing.T) { @@ -45,22 +50,23 @@ func TestParseRoleDictValid(t *testing.T) { {Key: TrainingRoleSupervisor, Label: "课长", Short: "课长", System: true}, {Key: TrainingRoleLecturer, Label: "讲师", Short: "讲师", System: true}, {Key: TrainingRoleManager, Label: "经理", Short: "经理", System: true, Inherits: []string{"", TrainingRoleSupervisor}}, + {Key: TrainingRoleSuperAdmin, Label: "超级管理员", Short: "超管", System: true}, {Key: "trainer_plus", Label: "资深讲师", Inherits: []string{TrainingRoleLecturer, TrainingRoleSupervisor}}, }) dict, err := ParseRoleDict(raw) if err != nil { t.Fatalf("valid dict rejected: %v", err) } - if len(dict) != 5 { - t.Fatalf("expect 5 roles, got %d", len(dict)) + if len(dict) != 6 { + t.Fatalf("expect 6 roles, got %d", len(dict)) } - last := dict[4] + last := dict[5] if len(last.Inherits) != 2 || last.Inherits[0] != TrainingRoleLecturer { t.Fatalf("inherits not preserved/cleaned: %v", last.Inherits) } // short 缺省回退 label - if dict[4].Short != "资深讲师" { - t.Fatalf("short should fall back to label, got %q", dict[4].Short) + if dict[5].Short != "资深讲师" { + t.Fatalf("short should fall back to label, got %q", dict[5].Short) } // manager 的空 inherit 被清理,课长继承保留 if len(dict[3].Inherits) != 1 || dict[3].Inherits[0] != TrainingRoleSupervisor { @@ -75,6 +81,7 @@ func TestParseRoleDictInvalid(t *testing.T) { {Key: TrainingRoleSupervisor, Label: "课长", Short: "课长", System: true}, {Key: TrainingRoleLecturer, Label: "讲师", Short: "讲师", System: true}, {Key: TrainingRoleManager, Label: "经理", Short: "经理", System: true, Inherits: []string{TrainingRoleSupervisor}}, + {Key: TrainingRoleSuperAdmin, Label: "超级管理员", Short: "超管", System: true}, } } cases := []struct { @@ -194,6 +201,10 @@ func TestNeedsRoleDictUpgrade(t *testing.T) { if NeedsRoleDictUpgrade(DefaultRoleDict()) { t.Fatal("v2 default dict should not need upgrade") } + // v2 口径但缺 v2.1 增补的超级管理员:同样需要读时升级补齐(存量租户免迁移) + if !NeedsRoleDictUpgrade(DefaultRoleDict()[:4]) { + t.Fatal("v2 dict missing super_admin should need upgrade") + } } func TestUpgradeRoleDictV2(t *testing.T) { diff --git a/scripts/check_perm_sql.js b/scripts/check_perm_sql.js new file mode 100644 index 0000000..0e98e84 --- /dev/null +++ b/scripts/check_perm_sql.js @@ -0,0 +1,43 @@ +// 校验课程权限 SQL 常量括号配平与占位符数量(2026-11-26 复测根因定位辅助) +const fs = require('fs') +const src = fs.readFileSync('internal/application/repository/training_library.go', 'utf8') +const start = src.indexOf('const courseLibraryPermissionMatchSQL') +const end = src.indexOf('// courseLibraryPermissionCond 组装') +const block = src.slice(start, end) +const parts = [] +let i = 0 +while (true) { + const q1 = block.indexOf('"', i) + if (q1 < 0) break + let j = q1 + 1 + let out = '' + while (j < block.length) { + if (block[j] === '\\') { + out += block[j + 1] + j += 2 + continue + } + if (block[j] === '"') break + out += block[j] + j += 1 + } + parts.push(out) + i = j + 1 +} +const sql = parts.join('') +const opens = (sql.match(/\(/g) || []).length +const closes = (sql.match(/\)/g) || []).length +const qs = (sql.match(/\?/g) || []).length +console.log('opens=', opens, 'closes=', closes, 'placeholders=', qs, 'BALANCED=', opens === closes) +// 括号深度轨迹:定位未闭合位置(深度未回 0 即缺失右括号) +let depth = 0 +for (let k = 0; k < sql.length; k++) { + if (sql[k] === '(') depth += 1 + if (sql[k] === ')') depth -= 1 +} +console.log('final depth =', depth, '(>0 表示缺少右括号)') +// 输出最后 120 个字符,便于人工核对末尾闭合 +console.log('--- tail ---') +console.log(sql.slice(-120)) +console.log('--- SQL ---') +console.log(sql)