From 92c8e097dc5982b3c7dc9d21e8a60f393d5b4790 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Aug 2026 19:57:34 +0000 Subject: [PATCH 1/3] Fix 500 on attendance_cleanup_rule 'run now': scope job to the rule's own userId Production error: QueryFailedError: Field 'user_id' doesn't have a default value INSERT INTO `jobs`(..., `user_id`, ...) VALUES (..., DEFAULT, ...) runNow used getUserIdFromUser(req.auth) as the enqueued job's userId. That's wrong for this action: CrudAuthWithPermissionsFilter grants admins AND anyone with the attendanceCleanupRules permission full visibility (not just their own rows), so a requester can select rules owned by a different user - and for admin specifically, getUserIdFromUser always returns undefined (id -1 is a sentinel, not a real tenant), which is what produced the undefined user_id insert. Fix: load the selected rules and derive the job's userId from each rule's own userId, enqueuing one job per distinct owner so a multi-tenant selection is scoped correctly rather than crashing or silently misattributing the sweep. Reproduced the exact error against a real MySQL jobs table (enqueue(undefined, ...)) and confirmed the fix (enqueue(38, ...)) succeeds. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- .../attendance-cleanup-rule.config.spec.ts | 39 +++++++++++++++---- .../attendance-cleanup-rule.config.ts | 25 ++++++++---- 2 files changed, 50 insertions(+), 14 deletions(-) diff --git a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts index d3437f32..803dffd3 100644 --- a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts +++ b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts @@ -38,7 +38,7 @@ describe('AttendanceCleanupRuleConfig', () => { }); describe('doAction: runNow', () => { - function makeService() { + function makeService(rules: Partial[]) { const ServiceClass = attendanceCleanupRuleConfig.service as any; const mockRepository = { target: AttendanceCleanupRule, @@ -48,27 +48,52 @@ describe('AttendanceCleanupRuleConfig', () => { connection: { options: { type: 'mysql' } }, targetName: 'AttendanceCleanupRule', }, + find: jest.fn().mockResolvedValue(rules), } as any; const jobService = { enqueue: jest.fn().mockResolvedValue({ id: 42 }) }; const service = new ServiceClass(mockRepository, {} as any, jobService); - return { service, jobService }; + return { service, mockRepository, jobService }; } - it('enqueues an attendance-cleanup-sweep job scoped to the selected rule ids', async () => { - const { service, jobService } = makeService(); + it("enqueues a job scoped to the selected rules' own userId (not the requester's)", async () => { + // The requester (e.g. admin) has no userId of their own for this purpose - + // CrudAuthWithPermissionsFilter lets them select rules owned by anyone. + const { service, mockRepository, jobService } = makeService([ + { id: 1, userId: 38 }, + { id: 2, userId: 38 }, + ]); const req = { - auth: { id: 7 }, + auth: { id: -1, permissions: { admin: true } }, parsed: { extra: { action: 'runNow', ids: '1,2' } }, } as unknown as CrudRequest; const message = await service.doAction(req, {}); - expect(jobService.enqueue).toHaveBeenCalledWith(7, 'attendance-cleanup-sweep', { ruleIds: [1, 2] }); + expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything() } }); + expect(jobService.enqueue).toHaveBeenCalledTimes(1); + expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1, 2] }); expect(message).toContain('42'); }); + it('enqueues one job per distinct owner when selected rules belong to different users', async () => { + const { service, jobService } = makeService([ + { id: 1, userId: 38 }, + { id: 2, userId: 39 }, + ]); + const req = { + auth: { id: -1, permissions: { admin: true } }, + parsed: { extra: { action: 'runNow', ids: '1,2' } }, + } as unknown as CrudRequest; + + await service.doAction(req, {}); + + expect(jobService.enqueue).toHaveBeenCalledTimes(2); + expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1] }); + expect(jobService.enqueue).toHaveBeenCalledWith(39, 'attendance-cleanup-sweep', { ruleIds: [2] }); + }); + it('falls through to the base doAction for unknown actions', async () => { - const { service } = makeService(); + const { service } = makeService([]); const req = { auth: { id: 7 }, parsed: { extra: { action: 'somethingElse' } }, diff --git a/server/src/entity-modules/attendance-cleanup-rule.config.ts b/server/src/entity-modules/attendance-cleanup-rule.config.ts index 858d4291..da8d67be 100644 --- a/server/src/entity-modules/attendance-cleanup-rule.config.ts +++ b/server/src/entity-modules/attendance-cleanup-rule.config.ts @@ -1,6 +1,5 @@ import { CrudRequest } from '@dataui/crud'; -import { Repository } from 'typeorm'; -import { getUserIdFromUser } from '@shared/auth/auth.util'; +import { In, Repository } from 'typeorm'; import { CrudAuthWithPermissionsFilter } from '@shared/auth/crud-auth.filter'; import { BaseEntityService } from '@shared/base-entity/base-entity.service'; import { BaseEntityModuleOptions, Entity, InjectEntityRepository } from '@shared/base-entity/interface'; @@ -11,21 +10,33 @@ import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.ent class AttendanceCleanupRuleService extends BaseEntityService { constructor( - @InjectEntityRepository repo: Repository, + @InjectEntityRepository private readonly ruleRepo: Repository, mailSendService: MailSendService, private readonly jobService: JobService, ) { - super(repo, mailSendService); + super(ruleRepo, mailSendService); } async doAction(req: CrudRequest, body: any): Promise { const extra = req.parsed.extra as any; switch (extra.action) { case 'runNow': { - const userId = getUserIdFromUser(req.auth); const ruleIds = getAsNumberArray(extra.ids) ?? []; - const job = await this.jobService.enqueue(userId, 'attendance-cleanup-sweep', { ruleIds }); - return `נוצרה משימת ניקוי נוכחות (מזהה ${job.id})`; + // The job must be scoped to each rule's own userId, not the requester's - both + // admins and anyone else with the attendanceCleanupRules permission can see and + // select rules belonging to any user (CrudAuthWithPermissionsFilter grants full + // visibility, not just-your-own-rows), so the requester's userId (undefined for + // admin) is not a valid stand-in. + const rules = (await this.ruleRepo.find({ where: { id: In(ruleIds) } as any })) as unknown as AttendanceCleanupRule[]; + const userIds = [...new Set(rules.map((rule) => rule.userId))]; + const jobs = await Promise.all( + userIds.map((userId) => + this.jobService.enqueue(userId, 'attendance-cleanup-sweep', { + ruleIds: rules.filter((rule) => rule.userId === userId).map((rule) => rule.id), + }), + ), + ); + return `נוצרו ${jobs.length} משימות ניקוי נוכחות (מזהים: ${jobs.map((job) => job.id).join(', ')})`; } } return super.doAction(req, body); From 2aedfe8028b21494f5bad7d8ff624869c4dc3920 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Aug 2026 20:07:32 +0000 Subject: [PATCH 2/3] Close cross-tenant data exposure: use standard CrudAuthFilter, not permission-based full visibility Addresses Copilot review comment on this PR, and explicit user direction: CrudAuthWithPermissionsFilter granted ADMIN_FILTER (full visibility, not just-your-own-rows) to anyone with the attendanceCleanupRules permission - and doAction's runNow is a custom route that bypasses @dataui/crud's normal auth-filtered query building entirely, so it never consulted that filter in the first place. Together, any authenticated user could pass arbitrary rule ids to runNow and have jobs enqueued under other tenants' identities. Fix: - crudAuth is now the standard CrudAuthFilter (admin sees all, everyone else sees only their own rows) - same as most other entities. The attendanceCleanupRules permission remains a client-side visibility gate only; it no longer grants any server-side data access. - runNow explicitly applies crudAuth.filter(req.auth) to its id lookup, since this custom route doesn't go through the filtered query path - a non-owned id in the selection just doesn't resolve, instead of being trusted from the request. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- .../attendance-cleanup-rule.config.spec.ts | 66 +++++++++++-------- .../attendance-cleanup-rule.config.ts | 24 ++++--- 2 files changed, 53 insertions(+), 37 deletions(-) diff --git a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts index 803dffd3..fbe2dd10 100644 --- a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts +++ b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts @@ -1,5 +1,5 @@ import { CrudRequest } from '@dataui/crud'; -import { ADMIN_FILTER, NO_DATA_FILTER } from '@shared/auth/crud-auth.filter'; +import { ADMIN_FILTER, CrudAuthFilter } from '@shared/auth/crud-auth.filter'; import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity'; import attendanceCleanupRuleConfig from '../attendance-cleanup-rule.config'; @@ -16,24 +16,21 @@ describe('AttendanceCleanupRuleConfig', () => { }); describe('crudAuth', () => { - it('grants access to a user with the attendanceCleanupRules permission', () => { - const user = { permissions: { attendanceCleanupRules: true } }; - expect(attendanceCleanupRuleConfig.crudAuth.filter(user)).toEqual(ADMIN_FILTER); + // Same as most entities: admin sees everything, everyone else sees only their own + // rows. The attendanceCleanupRules permission gates client-side visibility only - it + // must not grant one user's data to another. + it('uses the standard CrudAuthFilter (admin sees all, others see only their own rows)', () => { + expect(attendanceCleanupRuleConfig.crudAuth).toBe(CrudAuthFilter); }); - it('grants access to an admin regardless of the dedicated permission', () => { + it('grants an admin full visibility', () => { const user = { permissions: { admin: true } }; expect(attendanceCleanupRuleConfig.crudAuth.filter(user)).toEqual(ADMIN_FILTER); }); - it('denies a user lacking the permission (no rows, not a 403)', () => { - const user = { permissions: {} }; - expect(attendanceCleanupRuleConfig.crudAuth.filter(user)).toEqual(NO_DATA_FILTER); - }); - - it('denies a user with no permissions object at all', () => { - const user = {}; - expect(attendanceCleanupRuleConfig.crudAuth.filter(user)).toEqual(NO_DATA_FILTER); + it('scopes a non-admin to their own rows', () => { + const user = { id: 38, permissions: {} }; + expect(attendanceCleanupRuleConfig.crudAuth.filter(user)).toEqual({ userId: 38 }); }); }); @@ -55,28 +52,37 @@ describe('AttendanceCleanupRuleConfig', () => { return { service, mockRepository, jobService }; } - it("enqueues a job scoped to the selected rules' own userId (not the requester's)", async () => { - // The requester (e.g. admin) has no userId of their own for this purpose - - // CrudAuthWithPermissionsFilter lets them select rules owned by anyone. - const { service, mockRepository, jobService } = makeService([ - { id: 1, userId: 38 }, - { id: 2, userId: 38 }, - ]); + it('applies crudAuth to the id lookup, so a non-admin can only run their own rules', async () => { + // The mocked find simulates the DB applying { userId: 7 } - a non-owned id in the + // selection simply doesn't come back, instead of being trusted from the request. + const { service, mockRepository, jobService } = makeService([{ id: 1, userId: 7 }]); const req = { - auth: { id: -1, permissions: { admin: true } }, - parsed: { extra: { action: 'runNow', ids: '1,2' } }, + auth: { id: 7, permissions: {} }, + parsed: { extra: { action: 'runNow', ids: '1,2' } }, // 2 belongs to another user } as unknown as CrudRequest; - const message = await service.doAction(req, {}); + await service.doAction(req, {}); - expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything() } }); + expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything(), userId: 7 } }); expect(jobService.enqueue).toHaveBeenCalledTimes(1); - expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1, 2] }); - expect(message).toContain('42'); + expect(jobService.enqueue).toHaveBeenCalledWith(7, 'attendance-cleanup-sweep', { ruleIds: [1] }); }); - it('enqueues one job per distinct owner when selected rules belong to different users', async () => { - const { service, jobService } = makeService([ + it('enqueues nothing when none of the selected ids resolve for this caller', async () => { + const { service, jobService } = makeService([]); + const req = { + auth: { id: 7, permissions: {} }, + parsed: { extra: { action: 'runNow', ids: '99' } }, // owned by someone else + } as unknown as CrudRequest; + + const message = await service.doAction(req, {}); + + expect(jobService.enqueue).not.toHaveBeenCalled(); + expect(message).toContain('0'); + }); + + it('enqueues one job per distinct owner when an admin selects rules across users', async () => { + const { service, mockRepository, jobService } = makeService([ { id: 1, userId: 38 }, { id: 2, userId: 39 }, ]); @@ -85,11 +91,13 @@ describe('AttendanceCleanupRuleConfig', () => { parsed: { extra: { action: 'runNow', ids: '1,2' } }, } as unknown as CrudRequest; - await service.doAction(req, {}); + const message = await service.doAction(req, {}); + expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything() } }); // no userId - admin sees all expect(jobService.enqueue).toHaveBeenCalledTimes(2); expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1] }); expect(jobService.enqueue).toHaveBeenCalledWith(39, 'attendance-cleanup-sweep', { ruleIds: [2] }); + expect(message).toContain('42'); }); it('falls through to the base doAction for unknown actions', async () => { diff --git a/server/src/entity-modules/attendance-cleanup-rule.config.ts b/server/src/entity-modules/attendance-cleanup-rule.config.ts index da8d67be..68d883ae 100644 --- a/server/src/entity-modules/attendance-cleanup-rule.config.ts +++ b/server/src/entity-modules/attendance-cleanup-rule.config.ts @@ -1,6 +1,6 @@ import { CrudRequest } from '@dataui/crud'; import { In, Repository } from 'typeorm'; -import { CrudAuthWithPermissionsFilter } from '@shared/auth/crud-auth.filter'; +import { CrudAuthFilter } from '@shared/auth/crud-auth.filter'; import { BaseEntityService } from '@shared/base-entity/base-entity.service'; import { BaseEntityModuleOptions, Entity, InjectEntityRepository } from '@shared/base-entity/interface'; import { MailSendService } from '@shared/utils/mail/mail-send.service'; @@ -8,6 +8,11 @@ import { JobService } from '@shared/utils/jobs/job.service'; import { getAsNumberArray } from '@shared/utils/queryParam.util'; import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity'; +// Same auth as most entities: admin sees everything, everyone else sees only their own +// rows. The attendanceCleanupRules permission only controls whether the client shows the +// resource - it must not grant cross-tenant data visibility. +const crudAuth = CrudAuthFilter; + class AttendanceCleanupRuleService extends BaseEntityService { constructor( @InjectEntityRepository private readonly ruleRepo: Repository, @@ -22,12 +27,15 @@ class AttendanceCleanupRuleService ext switch (extra.action) { case 'runNow': { const ruleIds = getAsNumberArray(extra.ids) ?? []; - // The job must be scoped to each rule's own userId, not the requester's - both - // admins and anyone else with the attendanceCleanupRules permission can see and - // select rules belonging to any user (CrudAuthWithPermissionsFilter grants full - // visibility, not just-your-own-rows), so the requester's userId (undefined for - // admin) is not a valid stand-in. - const rules = (await this.ruleRepo.find({ where: { id: In(ruleIds) } as any })) as unknown as AttendanceCleanupRule[]; + // doAction is a custom route - it does NOT go through @dataui/crud's normal + // query building, so crudAuth's filter has to be applied to this lookup + // explicitly or a caller could pass any rule id regardless of ownership. + const authFilter = crudAuth.filter(req.auth); + const rules = (await this.ruleRepo.find({ + where: { id: In(ruleIds), ...authFilter } as any, + })) as unknown as AttendanceCleanupRule[]; + // Admin's authFilter is {} (sees everyone), so a selection can still span + // multiple owners - scope each enqueued job to its own rule subset. const userIds = [...new Set(rules.map((rule) => rule.userId))]; const jobs = await Promise.all( userIds.map((userId) => @@ -46,7 +54,7 @@ class AttendanceCleanupRuleService ext function getConfig(): BaseEntityModuleOptions { return { entity: AttendanceCleanupRule, - crudAuth: CrudAuthWithPermissionsFilter(permissions => permissions?.attendanceCleanupRules), + crudAuth, query: { join: { lesson: { eager: false }, From 2bba03d80a5088d727926433eea2436ae6f1c31d Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 23 Aug 2026 20:17:00 +0000 Subject: [PATCH 3/3] Use the new BaseEntityService.getManyByIds for runNow's id lookup Simplifies the previous manual crudAuth.filter(req.auth) + raw find() into a call to the new shared helper (nra-server#43), which applies the same auth filtering through the real query-building path instead of duplicating it per action. bump server/shared to 24975d1 to pick up getManyByIds. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_011dEYULADLgGNfZ4nv6gz8s --- server/shared | 2 +- .../attendance-cleanup-rule.config.spec.ts | 61 ++++++++++--------- .../attendance-cleanup-rule.config.ts | 31 ++++------ 3 files changed, 47 insertions(+), 47 deletions(-) diff --git a/server/shared b/server/shared index e1113876..24975d13 160000 --- a/server/shared +++ b/server/shared @@ -1 +1 @@ -Subproject commit e1113876dcd26e239f87170a8a8448ccebe98600 +Subproject commit 24975d1365983fea61229ca0c8ba8d8417c7869a diff --git a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts index fbe2dd10..564f87d8 100644 --- a/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts +++ b/server/src/entity-modules/__tests__/attendance-cleanup-rule.config.spec.ts @@ -35,7 +35,7 @@ describe('AttendanceCleanupRuleConfig', () => { }); describe('doAction: runNow', () => { - function makeService(rules: Partial[]) { + function makeService() { const ServiceClass = attendanceCleanupRuleConfig.service as any; const mockRepository = { target: AttendanceCleanupRule, @@ -45,35 +45,44 @@ describe('AttendanceCleanupRuleConfig', () => { connection: { options: { type: 'mysql' } }, targetName: 'AttendanceCleanupRule', }, - find: jest.fn().mockResolvedValue(rules), } as any; const jobService = { enqueue: jest.fn().mockResolvedValue({ id: 42 }) }; const service = new ServiceClass(mockRepository, {} as any, jobService); - return { service, mockRepository, jobService }; + return { service, jobService }; } - it('applies crudAuth to the id lookup, so a non-admin can only run their own rules', async () => { - // The mocked find simulates the DB applying { userId: 7 } - a non-owned id in the - // selection simply doesn't come back, instead of being trusted from the request. - const { service, mockRepository, jobService } = makeService([{ id: 1, userId: 7 }]); + function makeReq(ids: string, rules: Partial[]) { + // getManyByIds is what actually applies crudAuth's filter (via createBuilder) - it's + // mocked here rather than re-testing that plumbing, which base-entity.service.spec.ts + // already covers directly. const req = { - auth: { id: 7, permissions: {} }, - parsed: { extra: { action: 'runNow', ids: '1,2' } }, // 2 belongs to another user + auth: { id: -1, permissions: { admin: true } }, + parsed: { extra: { action: 'runNow', ids } }, + options: {}, } as unknown as CrudRequest; + return req; + } - await service.doAction(req, {}); + it("enqueues a job scoped to the resolved rules' own userId", async () => { + const { service, jobService } = makeService(); + jest.spyOn(service, 'getManyByIds').mockResolvedValue([ + { id: 1, userId: 38 }, + { id: 2, userId: 38 }, + ] as any); + const req = makeReq('1,2', []); - expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything(), userId: 7 } }); + const message = await service.doAction(req, {}); + + expect(service.getManyByIds).toHaveBeenCalledWith(req, [1, 2]); expect(jobService.enqueue).toHaveBeenCalledTimes(1); - expect(jobService.enqueue).toHaveBeenCalledWith(7, 'attendance-cleanup-sweep', { ruleIds: [1] }); + expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1, 2] }); + expect(message).toContain('42'); }); it('enqueues nothing when none of the selected ids resolve for this caller', async () => { - const { service, jobService } = makeService([]); - const req = { - auth: { id: 7, permissions: {} }, - parsed: { extra: { action: 'runNow', ids: '99' } }, // owned by someone else - } as unknown as CrudRequest; + const { service, jobService } = makeService(); + jest.spyOn(service, 'getManyByIds').mockResolvedValue([]); + const req = makeReq('99', []); // e.g. owned by someone else, filtered out by crudAuth const message = await service.doAction(req, {}); @@ -81,27 +90,23 @@ describe('AttendanceCleanupRuleConfig', () => { expect(message).toContain('0'); }); - it('enqueues one job per distinct owner when an admin selects rules across users', async () => { - const { service, mockRepository, jobService } = makeService([ + it('enqueues one job per distinct owner when the resolved rules span multiple users', async () => { + const { service, jobService } = makeService(); + jest.spyOn(service, 'getManyByIds').mockResolvedValue([ { id: 1, userId: 38 }, { id: 2, userId: 39 }, - ]); - const req = { - auth: { id: -1, permissions: { admin: true } }, - parsed: { extra: { action: 'runNow', ids: '1,2' } }, - } as unknown as CrudRequest; + ] as any); + const req = makeReq('1,2', []); - const message = await service.doAction(req, {}); + await service.doAction(req, {}); - expect(mockRepository.find).toHaveBeenCalledWith({ where: { id: expect.anything() } }); // no userId - admin sees all expect(jobService.enqueue).toHaveBeenCalledTimes(2); expect(jobService.enqueue).toHaveBeenCalledWith(38, 'attendance-cleanup-sweep', { ruleIds: [1] }); expect(jobService.enqueue).toHaveBeenCalledWith(39, 'attendance-cleanup-sweep', { ruleIds: [2] }); - expect(message).toContain('42'); }); it('falls through to the base doAction for unknown actions', async () => { - const { service } = makeService([]); + const { service } = makeService(); const req = { auth: { id: 7 }, parsed: { extra: { action: 'somethingElse' } }, diff --git a/server/src/entity-modules/attendance-cleanup-rule.config.ts b/server/src/entity-modules/attendance-cleanup-rule.config.ts index 68d883ae..8f937f56 100644 --- a/server/src/entity-modules/attendance-cleanup-rule.config.ts +++ b/server/src/entity-modules/attendance-cleanup-rule.config.ts @@ -1,5 +1,5 @@ import { CrudRequest } from '@dataui/crud'; -import { In, Repository } from 'typeorm'; +import { Repository } from 'typeorm'; import { CrudAuthFilter } from '@shared/auth/crud-auth.filter'; import { BaseEntityService } from '@shared/base-entity/base-entity.service'; import { BaseEntityModuleOptions, Entity, InjectEntityRepository } from '@shared/base-entity/interface'; @@ -8,18 +8,13 @@ import { JobService } from '@shared/utils/jobs/job.service'; import { getAsNumberArray } from '@shared/utils/queryParam.util'; import { AttendanceCleanupRule } from 'src/db/entities/AttendanceCleanupRule.entity'; -// Same auth as most entities: admin sees everything, everyone else sees only their own -// rows. The attendanceCleanupRules permission only controls whether the client shows the -// resource - it must not grant cross-tenant data visibility. -const crudAuth = CrudAuthFilter; - class AttendanceCleanupRuleService extends BaseEntityService { constructor( - @InjectEntityRepository private readonly ruleRepo: Repository, + @InjectEntityRepository repo: Repository, mailSendService: MailSendService, private readonly jobService: JobService, ) { - super(ruleRepo, mailSendService); + super(repo, mailSendService); } async doAction(req: CrudRequest, body: any): Promise { @@ -27,15 +22,12 @@ class AttendanceCleanupRuleService ext switch (extra.action) { case 'runNow': { const ruleIds = getAsNumberArray(extra.ids) ?? []; - // doAction is a custom route - it does NOT go through @dataui/crud's normal - // query building, so crudAuth's filter has to be applied to this lookup - // explicitly or a caller could pass any rule id regardless of ownership. - const authFilter = crudAuth.filter(req.auth); - const rules = (await this.ruleRepo.find({ - where: { id: In(ruleIds), ...authFilter } as any, - })) as unknown as AttendanceCleanupRule[]; - // Admin's authFilter is {} (sees everyone), so a selection can still span - // multiple owners - scope each enqueued job to its own rule subset. + // getManyByIds applies crudAuth's filter (already merged into req.parsed.search + // by CrudRequestInterceptor) - a non-owned id in the selection simply doesn't + // come back, instead of being trusted from the request. + const rules = (await this.getManyByIds(req, ruleIds)) as unknown as AttendanceCleanupRule[]; + // Admin sees everyone, so a selection can still span multiple owners - scope + // each enqueued job to its own rule subset. const userIds = [...new Set(rules.map((rule) => rule.userId))]; const jobs = await Promise.all( userIds.map((userId) => @@ -54,7 +46,10 @@ class AttendanceCleanupRuleService ext function getConfig(): BaseEntityModuleOptions { return { entity: AttendanceCleanupRule, - crudAuth, + // Same auth as most entities: admin sees everything, everyone else sees only their + // own rows. The attendanceCleanupRules permission only controls whether the client + // shows the resource - it must not grant cross-tenant data visibility. + crudAuth: CrudAuthFilter, query: { join: { lesson: { eager: false },