diff --git a/server/src/entity-modules/__tests__/att-report-with-report-month.config.spec.ts b/server/src/entity-modules/__tests__/att-report-with-report-month.config.spec.ts index 3caf0026..623c447d 100644 --- a/server/src/entity-modules/__tests__/att-report-with-report-month.config.spec.ts +++ b/server/src/entity-modules/__tests__/att-report-with-report-month.config.spec.ts @@ -11,10 +11,7 @@ const mockBaseConstructor = jest.fn(); jest.mock('@shared/base-entity/base-entity.service', () => { class MockBaseEntityService { dataSource: any; - constructor( - public repo: any, - public mailSendService: any, - ) { + constructor(public repo: any, public mailSendService: any) { mockBaseConstructor(repo, mailSendService); } async createOne(req: any, dto: any) { @@ -83,6 +80,8 @@ describe('att-report-with-report-month.config', () => { // Mock getOne on the service instance itself since it calls this.getOne() service.getOne = jest.fn().mockResolvedValue({ id: 1, reportDate: '2022-01-01' }); + // By default every selected id is accessible to the caller + service.getManyByIds = jest.fn().mockImplementation((req, ids) => Promise.resolve(ids.map((id) => ({ id })))); mockBaseCreateOne.mockClear(); mockBaseCreateMany.mockClear(); @@ -150,6 +149,26 @@ describe('att-report-with-report-month.config', () => { expect(result).toBe('עודכנו 2 רשומות'); }); + it('bulkEdit should only update ids the caller can access', async () => { + service.getManyByIds.mockResolvedValueOnce([{ id: 1 }]); + const req = { parsed: { extra: { action: 'bulkEdit', ids: '1,99', absCount: '1' } } } as any; + + await service.doAction(req, {}); + + expect(service.getManyByIds).toHaveBeenCalledWith(req, [1, 99]); + expect(mockRepo.update.mock.calls[0][0].id.value).toEqual([1]); + }); + + it('bulkEdit should do nothing when no selected id is accessible', async () => { + service.getManyByIds.mockResolvedValueOnce([]); + const req = { parsed: { extra: { action: 'bulkEdit', ids: '99', absCount: '1' } } } as any; + + const result = await service.doAction(req, {}); + + expect(mockRepo.update).not.toHaveBeenCalled(); + expect(result).toBe('לא נבחרו רשומות'); + }); + it('bulkEdit should not update when no field is filled', async () => { const req = { parsed: { extra: { action: 'bulkEdit', ids: '1,2', reportDate: 'null' } } } as any; diff --git a/server/src/entity-modules/__tests__/student-klass.config.spec.ts b/server/src/entity-modules/__tests__/student-klass.config.spec.ts index 572165d4..4ce6f372 100644 --- a/server/src/entity-modules/__tests__/student-klass.config.spec.ts +++ b/server/src/entity-modules/__tests__/student-klass.config.spec.ts @@ -47,7 +47,7 @@ const createMockCrudRequest = (extra: any): CrudRequest => }, params: {}, }, - }) as CrudRequest; + } as CrudRequest); const createMockStudentKlass = (data: Partial = {}): StudentKlass => ({ id: 1, @@ -169,6 +169,7 @@ describe('StudentKlassConfig', () => { const Service = config.service; service = new Service(mockRepository as Repository, mockMailService); service.dataSource = mockDataSource as DataSource; + service.getManyByIds = jest.fn().mockImplementation((req, ids) => Promise.resolve(ids.map((id) => ({ id })))); }); describe('getReportData', () => { @@ -225,6 +226,16 @@ describe('StudentKlassConfig', () => { expect(mockRepository.save).toHaveBeenCalled(); }); + it('should only fix references of ids the caller can access', async () => { + const req = createMockCrudRequest({ action: 'fixReferences', ids: '1,99' }); + service.getManyByIds.mockResolvedValueOnce([{ id: 1 }]); + mockRepository.findBy.mockResolvedValueOnce([createMockStudentKlass({ id: 1, studentTz: '123', klassId: 1 })]); + + await service.doAction(req, {}); + + expect(mockRepository.findBy).toHaveBeenCalledWith({ id: In([1]) }); + }); + it('should call super.doAction for unknown action', async () => { const req = createMockCrudRequest({ action: 'unknownAction', diff --git a/server/src/entity-modules/att-report-with-report-month.config.ts b/server/src/entity-modules/att-report-with-report-month.config.ts index 2295147b..75cdde53 100644 --- a/server/src/entity-modules/att-report-with-report-month.config.ts +++ b/server/src/entity-modules/att-report-with-report-month.config.ts @@ -11,14 +11,7 @@ import { fixReferences } from '@shared/utils/entity/fixReference.util'; import { getHebrewDateFormatter } from '@shared/utils/formatting/formatter.util'; import { shouldShowTopic } from './att-report.config'; import { getBulkEditUpdate } from '@shared/utils/entity/bulkEdit.util'; -import { - getAsArray, - getAsDate, - getAsNumber, - getAsBoolean, - getAsString, - getAsNumberArray, -} from '@shared/utils/queryParam.util'; +import { getAsDate, getAsNumber, getAsBoolean, getAsString, getAsNumberArray } from '@shared/utils/queryParam.util'; function getConfig(): BaseEntityModuleOptions { return { @@ -114,20 +107,27 @@ class AttReportWithReportMonthService, body: any): Promise { switch (req.parsed.extra.action) { case 'bulkEdit': - return this.bulkEdit(req.parsed.extra); + return this.bulkEdit(req.parsed.extra, await this.getOwnedIds(req)); case 'bulkKnownAbsences': - return this.bulkKnownAbsences(req.parsed.extra); + return this.bulkKnownAbsences(req.parsed.extra, await this.getOwnedIds(req)); case 'fixStudentReferenceV2': - return this.fixStudentReferenceV2(req.parsed.extra); + return this.fixStudentReferenceV2(await this.getOwnedIds(req)); case 'fixReferences': - return this.fixReferences(req.parsed.extra); + return this.fixReferences(await this.getOwnedIds(req)); } return super.doAction(req, body); } - private async bulkEdit(extra: Record): Promise { - const ids = getAsNumberArray(extra.ids); - if (!ids || ids.length === 0) return 'לא נבחרו רשומות'; + // Keeps only the selected ids the caller may access (crudAuth filter), so a + // caller-supplied id from another user's data is never touched. + private async getOwnedIds(req: CrudRequest): Promise { + const ids = getAsNumberArray(req.parsed.extra.ids); + const entities = await this.getManyByIds(req, ids); + return entities.map((entity: any) => entity.id); + } + + private async bulkEdit(extra: Record, ids: number[]): Promise { + if (ids.length === 0) return 'לא נבחרו רשומות'; const update = getBulkEditUpdate(extra, { numbers: ['klassReferenceId', 'teacherReferenceId', 'lessonReferenceId', 'howManyLessons', 'absCount'], @@ -140,9 +140,8 @@ class AttReportWithReportMonthService): Promise { - const ids = getAsArray(extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + private async bulkKnownAbsences(extra: Record, ids: number[]): Promise { + if (ids.length === 0) return 'לא נבחרו רשומות'; const reports = await this.dataSource.getRepository(AttReport).findBy({ id: In(ids) }); const knownAbsences: Array> = reports.map((report) => ({ @@ -165,9 +164,8 @@ class AttReportWithReportMonthService): Promise { - const ids = getAsArray(extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + private async fixStudentReferenceV2(ids: number[]): Promise { + if (ids.length === 0) return 'לא נבחרו רשומות'; const reports = await this.dataSource.getRepository(AttReport).findBy({ id: In(ids) }); const reportsToSave = []; @@ -185,9 +183,8 @@ class AttReportWithReportMonthService): Promise { - const ids = getAsNumberArray(extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + private async fixReferences(ids: number[]): Promise { + if (ids.length === 0) return 'לא נבחרו רשומות'; const referenceFields = { studentTz: 'studentReferenceId', klassId: 'klassReferenceId', diff --git a/server/src/entity-modules/klass.config.ts b/server/src/entity-modules/klass.config.ts index 7cdff653..57cbb60b 100644 --- a/server/src/entity-modules/klass.config.ts +++ b/server/src/entity-modules/klass.config.ts @@ -26,8 +26,10 @@ class KlassService extends BaseEntityService { async doAction(req: CrudRequest, body: any): Promise { switch (req.parsed.extra.action) { case 'fixReferences': { - const ids = getAsNumberArray(req.parsed.extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + const ids = (await this.getManyByIds(req, getAsNumberArray(req.parsed.extra.ids))).map( + (entity: any) => entity.id, + ); + if (!ids.length) return 'לא נבחרו רשומות'; const referenceFields = { klassTypeId: 'klassTypeReferenceId', teacherId: 'teacherReferenceId', diff --git a/server/src/entity-modules/report-group-session.config.ts b/server/src/entity-modules/report-group-session.config.ts index 248856ac..a69dcb0b 100644 --- a/server/src/entity-modules/report-group-session.config.ts +++ b/server/src/entity-modules/report-group-session.config.ts @@ -38,14 +38,16 @@ class ReportGroupSessionService extends B async doAction(req: CrudRequest, body: any): Promise { switch (req.parsed.extra.action) { case 'adjustTime': { - const ids = getAsNumberArray(req.parsed.extra.ids); + const ids = (await this.getManyByIds(req, getAsNumberArray(req.parsed.extra.ids))).map( + (entity: any) => entity.id, + ); const hoursAdjustment = getAsNumber(req.parsed.extra.hoursAdjustment) || 0; if (hoursAdjustment === 0) { return 'לא בוצע שינוי - יש להזין מספר שעות'; } - if (!ids) return 'לא נבחרו מפגשים'; + if (!ids.length) return 'לא נבחרו מפגשים'; const sessions = await this.dataSource.getRepository(ReportGroupSession).find({ where: { id: In(ids) }, diff --git a/server/src/entity-modules/report-group.config.ts b/server/src/entity-modules/report-group.config.ts index 6318e39e..2662866c 100644 --- a/server/src/entity-modules/report-group.config.ts +++ b/server/src/entity-modules/report-group.config.ts @@ -40,8 +40,10 @@ class ReportGroupService extends BaseEntityServi async doAction(req: CrudRequest, body: any): Promise { switch (req.parsed.extra.action) { case 'updateSignatureData': { - const ids = getAsNumberArray(req.parsed.extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + const ids = (await this.getManyByIds(req, getAsNumberArray(req.parsed.extra.ids))).map( + (entity: any) => entity.id, + ); + if (!ids.length) return 'לא נבחרו רשומות'; const signatureData = getAsString(req.parsed.extra.signatureData); await this.updateSignatureData(ids, signatureData); return `עודכנו חתימות ל-${ids.length} קבוצות דוחות`; diff --git a/server/src/entity-modules/student-klass.config.ts b/server/src/entity-modules/student-klass.config.ts index 6d8c060d..38642b95 100644 --- a/server/src/entity-modules/student-klass.config.ts +++ b/server/src/entity-modules/student-klass.config.ts @@ -66,8 +66,10 @@ class StudentKlassService extends BaseEntitySer async doAction(req: CrudRequest, body: any): Promise { switch (req.parsed.extra.action) { case 'fixReferences': { - const ids = getAsNumberArray(req.parsed.extra.ids); - if (!ids) return 'לא נבחרו רשומות'; + const ids = (await this.getManyByIds(req, getAsNumberArray(req.parsed.extra.ids))).map( + (entity: any) => entity.id, + ); + if (!ids.length) return 'לא נבחרו רשומות'; const referenceFields = { studentTz: 'studentReferenceId', klassId: 'klassReferenceId', diff --git a/server/src/entity-modules/student.config.ts b/server/src/entity-modules/student.config.ts index bc5dcb05..cd31fb8e 100644 --- a/server/src/entity-modules/student.config.ts +++ b/server/src/entity-modules/student.config.ts @@ -10,7 +10,7 @@ import studentReportCardReact from 'src/reports/studentReportCardReact'; import { getUserIdFromUser } from '@shared/auth/auth.util'; import { generateStudentReportCard } from 'src/reports/reportGenerator'; import { In } from 'typeorm'; -import { getAsArray, getAsBoolean } from '@shared/utils/queryParam.util'; +import { getAsBoolean, getAsNumberArray } from '@shared/utils/queryParam.util'; function getConfig(): BaseEntityModuleOptions { return { @@ -47,9 +47,11 @@ class StudentService extends BaseEntityService { async doAction(req: CrudRequest, body: any): Promise { switch (req.parsed.extra.action) { case 'bulkUpdateActive': { - const ids = getAsArray(req.parsed.extra.ids); + const ids = (await this.getManyByIds(req, getAsNumberArray(req.parsed.extra.ids))).map( + (entity: any) => entity.id, + ); const isActive = getAsBoolean(req.parsed.extra.isActive) ?? false; - if (!ids) return 'לא נבחרו רשומות'; + if (!ids.length) return 'לא נבחרו רשומות'; const result = await this.dataSource.getRepository(Student).update({ id: In(ids) }, { isActive });