feat(优化):年培训计划:已确认行的删除按钮、勾选框、批量删除均仅超级管理员可见/可用;提示文案随角色变化
deploy-hxtesting / build-and-deploy (push) Successful in 1m14s

课程管理:canDeleteCourse 超管不限状态/部门(已审核/已发布行可删,确认弹窗文案区分);批量删除按钮超管可见
月培训计划:已发布行的「删除」仅超管可见(普通维护角色仍只能删未发布计划),行内确认弹窗替代原 window.confirm,失败时透出后端文案
Training.vue:canManageTabs 加入 super_admin,纯超管(未兼其他角色)也能进入这三个页面执行删除
This commit is contained in:
runny committed 2026-11-26 17:06:37 +08:00
1 parent a448598df5
commit 2f1cda34da
19 files changed
+589 -278

No files matched your search

@@ -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))
}
@@ -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 时创建人/讲师放行)。
+4 -3
View File
@@ -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)
@@ -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)
}
@@ -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("仅可删除本部门的课程")
+31 -8
View File
@@ -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
}
+15 -9
View File
@@ -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
}
+10 -5
View File
@@ -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 空 = 全量)。
+19 -5
View File
@@ -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(课长/经理/人事/讲师),幂等:
+19 -8
View File
@@ -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) {