fix: M级人员获得考核调研管理权限

This commit is contained in:
江林铖 2026-07-28 19:44:36 +08:00
parent 1970c0f644
commit 60d249e876
2 changed files with 99 additions and 86 deletions

View File

@ -293,12 +293,33 @@ describe('performance survey service policies', () => {
expect(previewPerformanceSurveyEmployeeSync).not.toHaveBeenCalled()
})
test('系统管理员可执行只读人员同步预检', async () => {
test('素材库系统管理员但考核档案不是M级时不能执行人员同步预检', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({
...baseUser,
role: 'ADMIN',
isSuperAdmin: true,
})
;(prisma.performanceSurveyEmployeeProfile.findFirst as jest.Mock).mockResolvedValue({
levelType: 'P',
positionLevel: 'P5',
isLeadership: false,
})
await expect(previewPerformanceSurveyEmployeeProfiles(session, { limit: 200 }))
.rejects.toMatchObject({
message: '需要系统管理员权限',
status: 403,
})
expect(previewPerformanceSurveyEmployeeSync).not.toHaveBeenCalled()
expect(ensurePerformanceSurveyEmployeeProfileForUser).not.toHaveBeenCalled()
})
test('M级人员在考核调研内可执行系统管理员的只读预检', async () => {
;(prisma.performanceSurveyEmployeeProfile.findFirst as jest.Mock).mockResolvedValue({
levelType: 'M',
positionLevel: 'M1',
isLeadership: true,
})
;(previewPerformanceSurveyEmployeeSync as jest.Mock).mockResolvedValue({
mode: 'preview',
dryRun: true,
@ -314,10 +335,10 @@ describe('performance survey service policies', () => {
})
test('人员同步预检超时会返回中文错误且强调未写入', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({
...baseUser,
role: 'ADMIN',
isSuperAdmin: true,
;(prisma.performanceSurveyEmployeeProfile.findFirst as jest.Mock).mockResolvedValue({
levelType: 'M',
positionLevel: 'M1',
isLeadership: true,
})
;(previewPerformanceSurveyEmployeeSync as jest.Mock).mockRejectedValue(
Object.assign(new Error('request timeout'), { code: 'ETIMEOUT' }),
@ -537,7 +558,7 @@ describe('performance survey service policies', () => {
expect(getPerformanceSurveyAccountProfileWhere).not.toHaveBeenCalled()
})
test('M级详情权限只按完整组织路径及其子路径判断,不再按重名部门叶子判断', async () => {
test('M级作为考核系统管理员可跨组织查看人员考核明细', async () => {
;(ensurePerformanceSurveyEmployeeProfileForUser as jest.Mock).mockResolvedValue({
...baseEmployee,
levelType: 'M',
@ -557,33 +578,15 @@ describe('performance survey service policies', () => {
templateCode: 'course_share',
})
await expect(getPerformanceSurveyDashboardDetail(session, request)).rejects.toEqual(
new PerformanceSurveyApiError('无权限查看该人员考核明细', 403),
await expect(getPerformanceSurveyDashboardDetail(session, request)).resolves.toEqual(
expect.objectContaining({
employee: expect.objectContaining({ id: 'employee-other' }),
}),
)
const scopeWhere = (prisma.performanceSurveyEmployeeProfile.count as jest.Mock).mock.calls[0][0].where.AND[1]
expect(scopeWhere).toEqual({
OR: [
{ departmentPath: '集团A/品牌中心/策划部' },
{ departmentPath: { startsWith: '集团A/品牌中心/策划部/' } },
{
departmentMemberships: {
some: {
OR: [
{ departmentPath: '集团A/品牌中心/策划部' },
{ departmentPath: { startsWith: '集团A/品牌中心/策划部/' } },
],
},
},
},
],
})
expect(JSON.stringify(scopeWhere)).not.toContain('departmentName')
expect(JSON.stringify(scopeWhere)).not.toContain('contains')
expect(JSON.stringify(scopeWhere)).not.toContain('endsWith')
expect(prisma.performanceSurveyEmployeeProfile.count).not.toHaveBeenCalled()
})
test('M级档案缺少完整组织路径时默认拒绝打开他人详情', async () => {
test('M级作为考核系统管理员时不依赖本人组织路径查看他人详情', async () => {
;(ensurePerformanceSurveyEmployeeProfileForUser as jest.Mock).mockResolvedValue({
...baseEmployee,
departmentPath: null,
@ -602,7 +605,9 @@ describe('performance survey service policies', () => {
month: '2026-07',
employeeId: 'employee-other',
templateCode: 'course_share',
}))).rejects.toThrow('无权限查看该人员考核明细')
}))).resolves.toEqual(expect.objectContaining({
employee: expect.objectContaining({ id: 'employee-other' }),
}))
expect(prisma.performanceSurveyEmployeeProfile.count).not.toHaveBeenCalled()
})
@ -663,7 +668,7 @@ describe('performance survey service policies', () => {
consoleError.mockRestore()
})
test('M级不会因本组织员工只是评价人而获得整份调研管理权', async () => {
test('M级作为考核系统管理员可管理其他人员发起的调研', async () => {
;(ensurePerformanceSurveyEmployeeProfileForUser as jest.Mock).mockResolvedValue({
...baseEmployee,
levelType: 'M',
@ -675,51 +680,14 @@ describe('performance survey service policies', () => {
}))
await expect(
updatePerformanceSurvey(session, 'survey-1', { title: '试图越权修改' }),
).rejects.toThrow('无权限管理该考核调研')
updatePerformanceSurvey(session, 'survey-1', { title: '管理员修改' }),
).resolves.toEqual(expect.objectContaining({ id: 'survey-1' }))
const managerScope = (prisma.performanceSurvey.count as jest.Mock).mock.calls[0][0].where.AND[1]
expect(managerScope).toEqual({
AND: [
{ targets: { some: { targetType: 'employee' } } },
{
targets: {
none: {
targetType: 'employee',
OR: [
{ employeeProfileId: null },
{
employeeProfile: {
isNot: {
OR: [
{ departmentPath: '集团A/品牌中心/策划部' },
{ departmentPath: { startsWith: '集团A/品牌中心/策划部/' } },
{
departmentMemberships: {
some: {
OR: [
{ departmentPath: '集团A/品牌中心/策划部' },
{ departmentPath: { startsWith: '集团A/品牌中心/策划部/' } },
],
},
},
},
],
},
},
},
],
},
},
},
],
})
expect(JSON.stringify(managerScope)).not.toContain('assignments')
expect(JSON.stringify(managerScope)).not.toContain('evaluatorEmployee')
expect(JSON.stringify(managerScope)).not.toContain('creatorEmployeeId')
expect(prisma.performanceSurvey.count).not.toHaveBeenCalled()
expect(transactionClient.performanceSurvey.update).toHaveBeenCalled()
})
test('系统管理员身份不会扩大个人调研详情到其他被考核人', async () => {
test('素材库管理员身份不会扩大个人调研详情到其他被考核人', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({
...baseUser,
role: 'ADMIN',
@ -1095,15 +1063,14 @@ describe('performance survey service policies', () => {
expect(prisma.$transaction).not.toHaveBeenCalled()
})
test('系统管理员即使不在指定组织也可发起,canCreate 与创建接口保持一致', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({
...baseUser,
role: 'ADMIN',
})
test('考核档案M级即使不在指定组织也可发起,canCreate 与创建接口保持一致', async () => {
const adminEmployee = {
...baseEmployee,
departmentName: '视觉一部',
departmentPath: '集团A/其他中心/视觉一部',
levelType: 'M',
positionLevel: 'M1',
isLeadership: true,
}
const evaluator = {
...baseEmployee,
@ -1138,6 +1105,24 @@ describe('performance survey service policies', () => {
expect(prisma.performanceSurveyEmployeeProfile.findMany).toHaveBeenCalled()
})
test('M级人员在考核调研内按系统管理员授权', async () => {
;(ensurePerformanceSurveyEmployeeProfileForUser as jest.Mock).mockResolvedValue({
...baseEmployee,
levelType: 'M',
positionLevel: 'M1',
isLeadership: true,
})
await expect(getPerformanceSurveyMe(session)).resolves.toEqual(expect.objectContaining({
permissions: expect.objectContaining({
isSystemAdmin: true,
isDepartmentAdmin: true,
isLeadership: true,
canCreate: true,
}),
}))
})
test.each(['商品企划部', '空间视觉部'])(
'%s不再由部门名称硬编码禁止发起,资格交给岗位匹配规则',
async (departmentName) => {
@ -1153,12 +1138,14 @@ describe('performance survey service policies', () => {
},
)
test('系统管理员属于品牌中心未开放部门时仍保留全局发起能力', async () => {
;(prisma.user.findUnique as jest.Mock).mockResolvedValue({ ...baseUser, role: 'ADMIN' })
test('考核档案M级属于品牌中心未开放部门时仍保留全局发起能力', async () => {
;(ensurePerformanceSurveyEmployeeProfileForUser as jest.Mock).mockResolvedValue({
...baseEmployee,
departmentName: '商品企划部',
departmentPath: '集团A/品牌中心/商品企划部',
levelType: 'M',
positionLevel: 'M1',
isLeadership: true,
})
await expect(getPerformanceSurveyMe(session)).resolves.toEqual(expect.objectContaining({

View File

@ -889,14 +889,38 @@ async function ensureReadOnlySystemAdministrator(session: Session | null) {
const user = await prisma.user.findUnique({
where: { id: sessionUserId },
select: {
role: true,
isSuperAdmin: true,
dingtalkUserId: true,
email: true,
},
})
if (!user) {
throw new PerformanceSurveyApiError('登录用户不存在', 401)
}
if (!user.isSuperAdmin && user.role !== 'ADMIN' && user.role !== 'SYSTEM_ADMIN') {
const identityWhere: any[] = []
if (clean(user.dingtalkUserId)) {
identityWhere.push({ dingtalkUserId: clean(user.dingtalkUserId) })
}
if (clean(user.email)) {
identityWhere.push({
dingEmail: {
equals: clean(user.email),
mode: 'insensitive',
},
})
}
const employee = identityWhere.length > 0
? await prisma.performanceSurveyEmployeeProfile.findFirst({
where: { OR: identityWhere },
select: {
levelType: true,
positionLevel: true,
isLeadership: true,
},
})
: null
if (!isLeadershipProfile(employee)) {
throw new PerformanceSurveyApiError('需要系统管理员权限', 403)
}
}
@ -1064,9 +1088,11 @@ export async function getPerformanceSurveyContext(session: Session | null): Prom
displayName: getDisplayName(user),
})
const isSystemAdmin = Boolean(user.isSuperAdmin || user.role === 'ADMIN' || user.role === 'SYSTEM_ADMIN')
const isLeadership = isLeadershipProfile(employee)
const isDepartmentAdmin = Boolean(isSystemAdmin || user.role === 'DEPT_ADMIN' || isLeadership)
// 考核调研与素材库使用独立人员权限。这里只按考核人员档案判定,
// 不继承素材库 users.role / isSuperAdmin。
const isSystemAdmin = isLeadership
const isDepartmentAdmin = isLeadership
const managedDepartment = clean(user.managedDepartment)
const managedDepartmentParts = parseDepartmentPathParts(managedDepartment)
const scopeDepartment = managedDepartment