This commit is contained in:
1 parent
9d4092bc7f
commit
78e9787793
11 files changed
+489
-85
No files matched your search
@@ -147,8 +147,10 @@ func (h *Handler) requireRoles(c *gin.Context, allowed ...string) (ok bool) {
|
||||
|
||||
// callerIsSuperAdmin 超级管理员判定:持有「超级管理员」职称角色(super_admin,
|
||||
// 配置中心可配、组织架构「设置角色」可分配)或空间 Owner 兜底;空间 Admin 不放行。
|
||||
// 已确认需求/已审核课程/已发布计划这类终态数据的删除仅对超级管理员开放,其余
|
||||
// 角色仍按原状态规则拦截,前端删除按钮同口径仅超级管理员可见。
|
||||
// 用于终态数据删除(已确认需求/已审核课程/已发布计划)、课程跨部门审核与需求
|
||||
// 核验确认的跨部门放行(000163);发布、查看及课程审核外的常规审批不经本函数,
|
||||
// 由 super_admin 继承全部权限组经 ExpandRoles 展开后在各 requireRoles/数据范围
|
||||
// 判定自动放行,前端删除/确认按钮按同口径放开。
|
||||
func (h *Handler) callerIsSuperAdmin(c *gin.Context) bool {
|
||||
if callerRole := types.TenantRoleFromContext(c.Request.Context()); callerRole.IsValid() &&
|
||||
callerRole == types.TenantRoleOwner {
|
||||
@@ -651,6 +653,10 @@ func (h *Handler) ListPosts(c *gin.Context) {
|
||||
// 部门口径(000158):全部行必须为导入人本人部门(org_employees.dept_id,全员
|
||||
// 严格,人事也不例外);部门列留空视为默认导入人部门,其余任一行不符即整批
|
||||
// 拒绝。导入人无部门档案时阻断,先由人事在组织中心维护。
|
||||
// 000163 权限调整:超级管理员放开跨部门导入 —— 无部门档案亦不阻断,行部门
|
||||
// 照实落库(自动匹配/新增的课程管理条目按行部门归属;部门列留空的行保持空
|
||||
// 部门,列表提示「未指定部门」,超管可后续补全/确认),与需求跨部门核验确认
|
||||
// 同口径。
|
||||
func (h *Handler) ImportDemands(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
if !h.requireRoles(c, types.TrainingRoleHR, types.TrainingRoleSupervisor) {
|
||||
@@ -669,41 +675,50 @@ func (h *Handler) ImportDemands(c *gin.Context) {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
if member == nil || trimSpace(member.DeptID) == "" {
|
||||
_ = c.Error(errors.NewBadRequestError("未找到您的部门档案,请联系人事在组织中心维护部门后再导入"))
|
||||
return
|
||||
}
|
||||
// 整批部门预检:任一行部门与导入人不符即拒绝,不做部分导入。
|
||||
// 通过口径:行部门 id 与本人一致,或部门列全空(默认导入人部门)。
|
||||
myDeptName := h.deptNameOf(ctx, tenantID, member.DeptID)
|
||||
violations := make([]string, 0, len(req.Demands))
|
||||
for i, item := range req.Demands {
|
||||
deptID, deptName := trimSpace(item.DeptID), trimSpace(item.DeptName)
|
||||
if deptID == "" && deptName == "" {
|
||||
continue
|
||||
// 000163:超级管理员不限部门,跳过本人部门档案校验与整批部门预检。
|
||||
bySuperAdmin := h.callerIsSuperAdmin(c)
|
||||
if !bySuperAdmin {
|
||||
if member == nil || trimSpace(member.DeptID) == "" {
|
||||
_ = c.Error(errors.NewBadRequestError("未找到您的部门档案,请联系人事在组织中心维护部门后再导入"))
|
||||
return
|
||||
}
|
||||
if deptID == member.DeptID {
|
||||
continue
|
||||
// 整批部门预检:任一行部门与导入人不符即拒绝,不做部分导入。
|
||||
// 通过口径:行部门 id 与本人一致,或部门列全空(默认导入人部门)。
|
||||
myDeptName := h.deptNameOf(ctx, tenantID, member.DeptID)
|
||||
violations := make([]string, 0, len(req.Demands))
|
||||
for i, item := range req.Demands {
|
||||
deptID, deptName := trimSpace(item.DeptID), trimSpace(item.DeptName)
|
||||
if deptID == "" && deptName == "" {
|
||||
continue
|
||||
}
|
||||
if deptID == member.DeptID {
|
||||
continue
|
||||
}
|
||||
if deptName == "" {
|
||||
deptName = h.deptNameOf(ctx, tenantID, deptID)
|
||||
}
|
||||
if deptName == "" {
|
||||
deptName = deptID
|
||||
}
|
||||
violations = append(violations, fmt.Sprintf("第%d行(%s)", i+1, deptName))
|
||||
}
|
||||
if deptName == "" {
|
||||
deptName = h.deptNameOf(ctx, tenantID, deptID)
|
||||
if len(violations) > 0 {
|
||||
_ = c.Error(errors.NewBadRequestError(fmt.Sprintf(
|
||||
"导入失败:第%s行部门与您所在部门(%s)不一致,仅可导入本人部门的需求表",
|
||||
strings.Join(violations, "、"), myDeptName)))
|
||||
return
|
||||
}
|
||||
if deptName == "" {
|
||||
deptName = deptID
|
||||
}
|
||||
violations = append(violations, fmt.Sprintf("第%d行(%s)", i+1, deptName))
|
||||
}
|
||||
if len(violations) > 0 {
|
||||
_ = c.Error(errors.NewBadRequestError(fmt.Sprintf(
|
||||
"导入失败:第%s行部门与您所在部门(%s)不一致,仅可导入本人部门的需求表",
|
||||
strings.Join(violations, "、"), myDeptName)))
|
||||
return
|
||||
}
|
||||
demands := make([]*types.TrainingDemand, 0, len(req.Demands))
|
||||
for _, item := range req.Demands {
|
||||
// 部门口径:非超管强制为导入人本人部门(部门列留空的行在此默认,其余已
|
||||
// 通过预检);超管按行部门照实落库,留空行保持空部门。
|
||||
deptID := trimSpace(item.DeptID)
|
||||
if !bySuperAdmin {
|
||||
deptID = member.DeptID
|
||||
}
|
||||
demands = append(demands, &types.TrainingDemand{
|
||||
// 部门强制为导入人本人部门:部门列留空的行在此默认,其余已通过预检。
|
||||
Year: item.Year, DeptID: member.DeptID, CourseName: item.CourseName,
|
||||
Year: item.Year, DeptID: deptID, CourseName: item.CourseName,
|
||||
Plant: item.Plant, Category: item.Category, SessionCount: item.SessionCount,
|
||||
Location: item.Location, PostScope: item.PostScope, LecturerID: item.LecturerID,
|
||||
LecturerExternal: trimSpace(item.LecturerExternal), TrainType: trimSpace(item.TrainType),
|
||||
@@ -747,7 +762,9 @@ const demandConfirmDenied = "暂无权限,请联系部门经理~"
|
||||
|
||||
// ConfirmDemand POST /training/demands/:id/confirm — 经理核验确认(部门+角色双校验)。
|
||||
// 仅「需求归属部门的成员且职称角色为经理」可核验(000154 收窄:人事/空间管理员/
|
||||
// 其他部门经理一律拒绝),统一提示「暂无权限,请联系部门经理~」;
|
||||
// 其他部门经理一律拒绝),统一提示「暂无权限,请联系部门经理~」;000163 权限调整:
|
||||
// 超级管理员(职称角色 super_admin/空间 Owner 兜底)放开跨部门,可确认全部部门
|
||||
// (含未补全部门)的需求,与课程跨部门审核/终态删除同口径。
|
||||
// 部门校验先于确认落库,避免越权时已改状态。
|
||||
func (h *Handler) ConfirmDemand(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
@@ -761,8 +778,11 @@ func (h *Handler) ConfirmDemand(c *gin.Context) {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
// 000163:超级管理员不限部门(职称角色经 ExpandRoles 命中 super_admin,空间
|
||||
// Owner 兜底),跳过下方角色与部门双校验,与课程跨部门审核同一口径。
|
||||
bySuperAdmin := h.callerIsSuperAdmin(c)
|
||||
roles := h.svc.ExpandTrainingRoles(ctx, tenantID, member.TrainingRoleList())
|
||||
if !types.HasTrainingRole(roles, types.TrainingRoleManager) {
|
||||
if !bySuperAdmin && !types.HasTrainingRole(roles, types.TrainingRoleManager) {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
@@ -771,8 +791,9 @@ func (h *Handler) ConfirmDemand(c *gin.Context) {
|
||||
_ = c.Error(err)
|
||||
return
|
||||
}
|
||||
// 本部门口径:仅可核验本部门需求(部门缺失的需求先由人事/提报人补全部门)。
|
||||
if existing.DeptID == "" || existing.DeptID != member.DeptID {
|
||||
// 本部门口径:仅可核验本部门需求(部门缺失的需求先由人事/提报人补全部门);
|
||||
// 超级管理员不限部门(含部门缺失的需求)。
|
||||
if !bySuperAdmin && (existing.DeptID == "" || existing.DeptID != member.DeptID) {
|
||||
_ = c.Error(errors.NewForbiddenError(demandConfirmDenied))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,272 @@
|
||||
package training
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"academy/internal/errors"
|
||||
"academy/internal/middleware"
|
||||
"academy/internal/types"
|
||||
"academy/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// stubDemandConfirm 只桩需求确认/批量导入链路触及的服务面(培训档案/角色展开/
|
||||
// 需求读写,列表与通知按空实现短路),验证 000163 超级管理员跨部门放行口径。
|
||||
type stubDemandConfirm struct {
|
||||
interfaces.TrainingService
|
||||
member *types.TrainingMember
|
||||
demand *types.TrainingDemand
|
||||
confirmedID string
|
||||
// imported 捕获 ImportDemands 收到的落库请求,供导入部门口径断言。
|
||||
imported []*types.TrainingDemand
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) CurrentMember(_ context.Context, _ uint64, userID string) (*types.TrainingMember, error) {
|
||||
if s.member == nil || s.member.UserID != userID {
|
||||
return nil, nil
|
||||
}
|
||||
return s.member, nil
|
||||
}
|
||||
|
||||
// ExpandTrainingRoles 按默认字典口径展开(000163):超管命中全部权限组,经理命中课长。
|
||||
func (s *stubDemandConfirm) ExpandTrainingRoles(_ context.Context, _ uint64, roles []string) []string {
|
||||
out := append([]string{}, roles...)
|
||||
if types.HasTrainingRole(out, types.TrainingRoleSuperAdmin) {
|
||||
out = append(out,
|
||||
string(types.TrainingRoleHR),
|
||||
string(types.TrainingRoleSupervisor),
|
||||
string(types.TrainingRoleLecturer),
|
||||
string(types.TrainingRoleManager),
|
||||
)
|
||||
}
|
||||
if types.HasTrainingRole(out, types.TrainingRoleManager) {
|
||||
out = append(out, string(types.TrainingRoleSupervisor))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) GetDemand(_ context.Context, _ uint64, id string) (*types.TrainingDemand, error) {
|
||||
if s.demand != nil && s.demand.ID == id {
|
||||
return s.demand, nil
|
||||
}
|
||||
return nil, errors.NewNotFoundError("需求不存在")
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ConfirmDemand(_ context.Context, _ uint64, id, _ string) (*types.TrainingDemand, error) {
|
||||
s.confirmedID = id
|
||||
demand := s.demand
|
||||
demand.Status = types.TrainingDemandConfirmed
|
||||
demand.CheckStatus = types.TrainingCheckPassed
|
||||
return demand, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ListCourses(_ context.Context, _ uint64, _ *interfaces.TrainingListQuery) ([]*types.TrainingCourse, int64, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ListMembers(_ context.Context, _ uint64, _ *interfaces.TrainingListQuery) ([]*types.TrainingMember, int64, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) BroadcastNotification(_ context.Context, _ uint64, _ []string, _, _, _, _, _, _ string) (int, error) {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ListDepts(_ context.Context, _ uint64) ([]*types.TrainingDept, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ListCourseLibraryItems(_ context.Context, _ uint64, _ *interfaces.TrainingListQuery) ([]*types.TrainingCourseLibrary, int64, error) {
|
||||
return nil, 0, nil
|
||||
}
|
||||
|
||||
func (s *stubDemandConfirm) ImportDemands(_ context.Context, _ uint64, demands []*types.TrainingDemand, _ string) ([]*types.TrainingDemand, int, error) {
|
||||
s.imported = demands
|
||||
return demands, 0, nil
|
||||
}
|
||||
|
||||
// serveTrainingRoute 以与线上一致的错误处理中间件执行目标路由,并注入认证上下文
|
||||
// (请求上下文空间角色 + gin 键身份),返回响应。
|
||||
func serveTrainingRoute(stub *stubDemandConfirm, spaceRole types.TenantRole, method, path, body string, route func(*gin.Engine, *Handler)) *httptest.ResponseRecorder {
|
||||
gin.SetMode(gin.TestMode)
|
||||
|
||||
const tenantID = uint64(100)
|
||||
h := NewHandler(stub, nil)
|
||||
r := gin.New()
|
||||
r.Use(func(c *gin.Context) {
|
||||
c.Set(types.TenantIDContextKey.String(), tenantID)
|
||||
c.Set(types.UserIDContextKey.String(), stub.member.UserID)
|
||||
c.Next()
|
||||
}, middleware.ErrorHandler())
|
||||
route(r, h)
|
||||
|
||||
var reqBody io.Reader
|
||||
if body != "" {
|
||||
reqBody = strings.NewReader(body)
|
||||
}
|
||||
req := httptest.NewRequest(method, path, reqBody)
|
||||
if body != "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
ctx := types.WithCaller(req.Context(), types.Caller{TenantID: tenantID, UserID: stub.member.UserID, Role: spaceRole})
|
||||
ctx = context.WithValue(ctx, types.TenantRoleContextKey, spaceRole)
|
||||
w := httptest.NewRecorder()
|
||||
r.ServeHTTP(w, req.WithContext(ctx))
|
||||
return w
|
||||
}
|
||||
|
||||
// runConfirmDemand 经由与线上一致的错误处理中间件执行确认请求,返回响应。
|
||||
func runConfirmDemand(t *testing.T, stub *stubDemandConfirm, spaceRole types.TenantRole) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
return serveTrainingRoute(stub, spaceRole, http.MethodPost, "/api/v1/training/demands/"+stub.demand.ID+"/confirm", "",
|
||||
func(r *gin.Engine, h *Handler) { r.POST("/api/v1/training/demands/:id/confirm", h.ConfirmDemand) })
|
||||
}
|
||||
|
||||
// runImportDemands 经由与线上一致的错误处理中间件执行批量导入请求,返回响应。
|
||||
func runImportDemands(t *testing.T, stub *stubDemandConfirm, spaceRole types.TenantRole, body string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
return serveTrainingRoute(stub, spaceRole, http.MethodPost, "/api/v1/training/demands/import", body,
|
||||
func(r *gin.Engine, h *Handler) { r.POST("/api/v1/training/demands/import", h.ImportDemands) })
|
||||
}
|
||||
|
||||
// TestConfirmDemandSuperAdminCrossDept 000163 权限调整:超级管理员职称角色
|
||||
// (继承经理权限组并放开部门校验)可核验确认任意部门的需求。
|
||||
func TestConfirmDemandSuperAdminCrossDept(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-super", DeptID: "dept-a",
|
||||
Roles: types.JSON(`["super_admin"]`)},
|
||||
demand: &types.TrainingDemand{ID: "d-1", TenantID: 100, Year: 2026,
|
||||
DeptID: "dept-b", CourseName: "安全生产培训",
|
||||
Status: types.TrainingDemandSubmitted, CheckStatus: types.TrainingCheckPending},
|
||||
}
|
||||
w := runConfirmDemand(t, stub, types.TenantRoleViewer)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
assert.Contains(t, w.Body.String(), `"success":true`)
|
||||
assert.Equal(t, "d-1", stub.confirmedID, "跨部门需求应被确认落库")
|
||||
}
|
||||
|
||||
// TestConfirmDemandSuperAdminEmptyDept 超级管理员可确认部门缺失的需求
|
||||
// (普通角色的口径是先由人事/提报人补全部门,超管不限)。
|
||||
func TestConfirmDemandSuperAdminEmptyDept(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-super", DeptID: "dept-a",
|
||||
Roles: types.JSON(`["super_admin"]`)},
|
||||
demand: &types.TrainingDemand{ID: "d-2", TenantID: 100, Year: 2026,
|
||||
CourseName: "新员工入职培训",
|
||||
Status: types.TrainingDemandSubmitted, CheckStatus: types.TrainingCheckPending},
|
||||
}
|
||||
w := runConfirmDemand(t, stub, types.TenantRoleViewer)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
assert.Equal(t, "d-2", stub.confirmedID)
|
||||
}
|
||||
|
||||
// TestConfirmDemandManagerCrossDeptDenied 000154 收窄保留:经理仅可确认本部门
|
||||
// 需求,跨部门确认依旧拒绝(超管旁路不放大到其他角色)。
|
||||
func TestConfirmDemandManagerCrossDeptDenied(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-exec", DeptID: "dept-a",
|
||||
Roles: types.JSON(`["exec"]`)},
|
||||
demand: &types.TrainingDemand{ID: "d-3", TenantID: 100, Year: 2026,
|
||||
DeptID: "dept-b", CourseName: "消防演练",
|
||||
Status: types.TrainingDemandSubmitted, CheckStatus: types.TrainingCheckPending},
|
||||
}
|
||||
w := runConfirmDemand(t, stub, types.TenantRoleViewer)
|
||||
|
||||
require.Equal(t, http.StatusForbidden, w.Code, w.Body.String())
|
||||
assert.Contains(t, w.Body.String(), demandConfirmDenied)
|
||||
assert.True(t, strings.Contains(w.Body.String(), "暂无权限,请联系部门经理~"))
|
||||
assert.Empty(t, stub.confirmedID, "越权确认不应落库")
|
||||
}
|
||||
|
||||
// TestConfirmDemandTraineeDenied 纯学员(无经理角色)确认一律拒绝。
|
||||
func TestConfirmDemandTraineeDenied(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-trainee", DeptID: "dept-b",
|
||||
Roles: types.JSON(`[]`)},
|
||||
demand: &types.TrainingDemand{ID: "d-4", TenantID: 100, Year: 2026,
|
||||
DeptID: "dept-b", CourseName: "办公软件进阶",
|
||||
Status: types.TrainingDemandSubmitted, CheckStatus: types.TrainingCheckPending},
|
||||
}
|
||||
w := runConfirmDemand(t, stub, types.TenantRoleViewer)
|
||||
|
||||
require.Equal(t, http.StatusForbidden, w.Code, w.Body.String())
|
||||
assert.Contains(t, w.Body.String(), demandConfirmDenied)
|
||||
assert.Empty(t, stub.confirmedID)
|
||||
}
|
||||
|
||||
// TestConfirmDemandSpaceOwnerFallback 空间 Owner 兜底口径(与 callerIsSuperAdmin
|
||||
// 判定一致):无职称角色培训档案的空间 Owner 亦可跨部门核验确认。
|
||||
func TestConfirmDemandSpaceOwnerFallback(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-owner", DeptID: "dept-a",
|
||||
Roles: types.JSON(`[]`)},
|
||||
demand: &types.TrainingDemand{ID: "d-5", TenantID: 100, Year: 2026,
|
||||
DeptID: "dept-b", CourseName: "质量体系宣贯",
|
||||
Status: types.TrainingDemandSubmitted, CheckStatus: types.TrainingCheckPending},
|
||||
}
|
||||
w := runConfirmDemand(t, stub, types.TenantRoleOwner)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
assert.Equal(t, "d-5", stub.confirmedID)
|
||||
}
|
||||
|
||||
// TestImportDemandsSuperAdminCrossDept 000163 权限调整:超级管理员批量导入不再
|
||||
// 限制本人部门——行部门照实落库,部门列留空的行保持空部门(列表提示「未指定
|
||||
// 部门」,超管可后续补全/确认)。
|
||||
func TestImportDemandsSuperAdminCrossDept(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-super", DeptID: "dept-a",
|
||||
Roles: types.JSON(`["super_admin"]`)},
|
||||
}
|
||||
body := `{"demands":[` +
|
||||
`{"course_name":"安全培训","dept_id":"dept-b","dept_name":"B部门"},` +
|
||||
`{"course_name":"质量培训","dept_id":"dept-a"},` +
|
||||
`{"course_name":"通用培训"}]}`
|
||||
w := runImportDemands(t, stub, types.TenantRoleViewer, body)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
require.Len(t, stub.imported, 3)
|
||||
assert.Equal(t, "dept-b", stub.imported[0].DeptID, "跨部门行应照实落库")
|
||||
assert.Equal(t, "dept-a", stub.imported[1].DeptID)
|
||||
assert.Empty(t, stub.imported[2].DeptID, "部门列留空行保持空部门")
|
||||
}
|
||||
|
||||
// TestImportDemandsSuperAdminWithoutDeptProfile 超管档案部门为空亦可导入,
|
||||
// 不再被「未找到您的部门档案」阻断。
|
||||
func TestImportDemandsSuperAdminWithoutDeptProfile(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-super", DeptID: "",
|
||||
Roles: types.JSON(`["super_admin"]`)},
|
||||
}
|
||||
body := `{"demands":[{"course_name":"入职培训","dept_id":"dept-b"}]}`
|
||||
w := runImportDemands(t, stub, types.TenantRoleViewer, body)
|
||||
|
||||
require.Equal(t, http.StatusOK, w.Code, w.Body.String())
|
||||
require.Len(t, stub.imported, 1)
|
||||
assert.Equal(t, "dept-b", stub.imported[0].DeptID)
|
||||
}
|
||||
|
||||
// TestImportDemandsManagerCrossDeptDenied 000158 收窄保留:非超管导入仍限本人
|
||||
// 部门,跨部门行整批拒绝(超管旁路不放大到其他角色)。
|
||||
func TestImportDemandsManagerCrossDeptDenied(t *testing.T) {
|
||||
stub := &stubDemandConfirm{
|
||||
member: &types.TrainingMember{UserID: "u-exec", DeptID: "dept-a",
|
||||
Roles: types.JSON(`["exec"]`)},
|
||||
}
|
||||
body := `{"demands":[{"course_name":"消防培训","dept_id":"dept-b"}]}`
|
||||
w := runImportDemands(t, stub, types.TenantRoleViewer, body)
|
||||
|
||||
require.Equal(t, http.StatusBadRequest, w.Code, w.Body.String())
|
||||
assert.Contains(t, w.Body.String(), "部门与您所在部门")
|
||||
assert.Empty(t, stub.imported, "越权导入不应落库")
|
||||
}
|
||||
@@ -274,6 +274,8 @@ func (h *Handler) SetCourseLibraryPublishStatus(c *gin.Context) {
|
||||
// callerVerifyDeptScope 核验/删除的部门口径(000157):一律按课长身份收窄本部门,
|
||||
// 不设全局旁路(人事/空间管理员均不例外)——核验权只属于「该部门课长/经理」。
|
||||
// 返回所在部门 id 与是否具备身份;无课长身份(或课长未分配部门)一律拒绝。
|
||||
// 超级管理员不经过本函数:调用点按 callerIsSuperAdmin 放开跨部门
|
||||
// (deptScope 传空,服务层按全局口径处理)。
|
||||
func (h *Handler) callerVerifyDeptScope(c *gin.Context) (deptID string, ok bool) {
|
||||
member, err := h.svc.CurrentMember(c.Request.Context(), callerTenantID(c), callerUserID(c))
|
||||
if err != nil || member == nil {
|
||||
@@ -297,20 +299,26 @@ type verifyCourseLibraryRequest struct {
|
||||
// (000163:核验升级为审核,审核通过即自动发布):仅本部门课长/经理可审核
|
||||
// (人事/讲师/学员/空间管理员均无跨部门审核权),待审核 → 已确认 + 自动发布;
|
||||
// 课程权限未设置整批拒绝;越权/已确认/不存在行跳过不阻断其余行。
|
||||
// 超级管理员(职称角色 super_admin/空间 Owner 兜底)放开跨部门:可审核全部
|
||||
// 部门的待审核课程(与终态删除同口径)。
|
||||
func (h *Handler) VerifyCourseLibraryItems(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
|
||||
}
|
||||
}
|
||||
var req verifyCourseLibraryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
_ = c.Error(errors.NewBadRequestError("请求参数不合法").WithDetails(err.Error()))
|
||||
return
|
||||
}
|
||||
deptID, ok := h.callerVerifyDeptScope(c)
|
||||
if !ok {
|
||||
_ = c.Error(errors.NewForbiddenError("仅本部门课长/经理可审核课程"))
|
||||
return
|
||||
}
|
||||
result, err := h.svc.VerifyCourseLibraryItems(c.Request.Context(), callerTenantID(c), req.IDs, callerUserID(c), deptID)
|
||||
if err != nil {
|
||||
_ = c.Error(err)
|
||||
|
||||
Reference in new issue
Block a user