-
Notifications
You must be signed in to change notification settings - Fork 19
New endpoint to allow admins to get all access requests #2888
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JRB66955
wants to merge
20
commits into
main
Choose a base branch
from
dev/all-access-requests-for-admin-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
cf949ae
wip, initial endpoint to fetch all access requests
JRB66955 7823542
update findAccessRequest function to allow admins to get all accessRe…
JRB66955 3013b70
flatten if/else block and add more meaningful error msg
JRB66955 68e2694
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 9b3bf36
add initial tests for findAccessRequest func
JRB66955 fa88707
add tests for new getAccessRequests file
JRB66955 80f61ff
add back accidentally removed snapshot
JRB66955 6aa51aa
fix api routing issue, remove temp url to something more suitable whi…
JRB66955 5f643e0
re-order import to fix class extends value undefined error with authe…
JRB66955 1945c77
add additional mock getEntities func for auth
JRB66955 a7e992f
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 2affc33
wip commit, updating wrt to PR feedback, making endpoint more akin to…
JRB66955 f283092
update with new tests to reflect updated endpoint
JRB66955 26351ea
update one of the tests to exercise the full function, with added req…
JRB66955 85564c4
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 d21d80a
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 4da7e94
update with PR feedback, only include relevant ARs in auth connector,…
JRB66955 6640081
Merge branch 'main' into dev/all-access-requests-for-admin-endpoint
JRB66955 4703d4c
Slight tweak to new condition, try not to break python tests this time
JRB66955 84e76a4
rework of accessRequests, a new model/AR aggregation to loop/filter t…
JRB66955 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
backend/src/routes/v2/model/accessRequest/getAccessRequests.ts
JRB66955 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { Request, Response } from 'express' | ||
| import { z } from 'zod' | ||
|
|
||
| import { AuditInfo } from '../../../../connectors/audit/Base.js' | ||
| import audit from '../../../../connectors/audit/index.js' | ||
| import { AccessRequestInterface } from '../../../../models/AccessRequest.js' | ||
| import { findAccessRequest } from '../../../../services/accessRequest.js' | ||
| import { accessRequestInterfaceSchema, registerPath } from '../../../../services/specification.js' | ||
| import { coerceArray, parse, strictCoerceBoolean } from '../../../../utils/validate.js' | ||
|
|
||
| export const GetAccessRequestsSchema = z.object({ | ||
| query: z.object({ | ||
| modelId: coerceArray(z.array(z.string()).optional().default([])), | ||
| schemaId: z.string().optional().default(''), | ||
| mine: strictCoerceBoolean(z.boolean().optional().default(false)), | ||
| adminAccess: strictCoerceBoolean(z.boolean().optional().default(false)), | ||
| }), | ||
| }) | ||
|
|
||
| registerPath({ | ||
| method: 'get', | ||
| path: '/api/v2/access-requests/search', | ||
| tags: ['access-request'], | ||
| description: 'Get all access requests for all models.', | ||
| schema: GetAccessRequestsSchema, | ||
| responses: { | ||
| 200: { | ||
| description: 'An array of access request instances.', | ||
| content: { | ||
| 'application/json': { | ||
| schema: z.object({ | ||
| accessRequests: z.array(accessRequestInterfaceSchema), | ||
| }), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| interface GetAccessRequestsResponse { | ||
| accessRequests: Array<AccessRequestInterface> | ||
| } | ||
|
|
||
| export const getAccessRequests = [ | ||
| async (req: Request, res: Response<GetAccessRequestsResponse>): Promise<void> => { | ||
| req.audit = AuditInfo.ViewAccessRequests | ||
| const { | ||
| query: { modelId, schemaId, mine, adminAccess }, | ||
| } = parse(req, GetAccessRequestsSchema) | ||
|
|
||
| const accessRequests = await findAccessRequest(req.user, modelId, schemaId, mine, adminAccess) | ||
|
|
||
| await audit.onViewAccessRequests(req, accessRequests) | ||
|
|
||
| res.json({ accessRequests }) | ||
| }, | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,12 @@ | ||
| // eslint-disable-next-line simple-import-sort/imports | ||
| import { Validator } from 'jsonschema' | ||
| import { Types } from 'mongoose' | ||
|
|
||
| import authentication from '../connectors/authentication/index.js' | ||
| import { Roles } from '../connectors/authentication/Base.js' | ||
| import { AccessRequestAction } from '../connectors/authorisation/actions.js' | ||
| import authorisation from '../connectors/authorisation/index.js' | ||
| import { AccessRequestInterface } from '../models/AccessRequest.js' | ||
| import AccessRequestModel, { AccessRequestDoc, AccessRequestInterface } from '../models/AccessRequest.js' | ||
| import AccessRequest from '../models/AccessRequest.js' | ||
| import ResponseModel, { ResponseKind } from '../models/Response.js' | ||
| import ReviewModel from '../models/Review.js' | ||
|
|
@@ -23,6 +25,7 @@ import { removeResponsesByParentIds } from './response.js' | |
| import { createAccessRequestReviews, removeAccessRequestReviews } from './review.js' | ||
| import { getSchemaById } from './schema.js' | ||
| import { sendWebhooks } from './webhook.js' | ||
| import ModelModel from '../models/Model.js' | ||
|
|
||
| export type CreateAccessRequestParams = Pick<AccessRequestInterface, 'metadata' | 'schemaId'> | ||
| export async function createAccessRequest( | ||
|
|
@@ -131,6 +134,64 @@ export async function getAccessRequestById(user: UserInterface, accessRequestId: | |
| return accessRequest | ||
| } | ||
|
|
||
| export async function findAccessRequest( | ||
| user: UserInterface, | ||
| modelId: Array<string>, | ||
| schemaId: string, | ||
| filters: Array<string>, | ||
| adminAccess?: boolean, | ||
| ): Promise<Array<AccessRequestDoc>> { | ||
| if (adminAccess) { | ||
| if (!(await authentication.hasRole(user, Roles.Admin))) { | ||
| throw Forbidden('You do not have the required role.', { | ||
| userDn: user.dn, | ||
| requiredRole: Roles.Admin, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| const query: any = {} | ||
|
|
||
| if (modelId.length) { | ||
| query.modelId = { $all: modelId } | ||
| } | ||
|
|
||
| if (schemaId) { | ||
| query.schemaId = { $all: schemaId } | ||
| } | ||
|
|
||
| if (filters.length > 0) { | ||
| if (filters.includes('mine')) { | ||
| query.metadata.overview = { | ||
| $elemMatch: { | ||
| entity: { $in: await authentication.getEntities(user) }, | ||
| }, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const cursor = AccessRequestModel.find(query) | ||
|
|
||
| const results = await cursor | ||
| //Auth already checked, so just need to check if they require admin access | ||
| if (adminAccess) { | ||
| return results | ||
| } | ||
|
|
||
| // authorisation | ||
| const modelIds = results.map((result) => result.modelId) | ||
| let auths: any[] = [] | ||
|
||
| for (const modelId of modelIds) { | ||
| const modelDoc = await ModelModel.findOne({ id: modelId }) | ||
| if (!modelDoc) { | ||
| throw BadReq('Model cannot be found', { modelId }) | ||
| } | ||
| const model = modelDoc.toObject() | ||
| auths = auths.concat(await authorisation.accessRequests(user, model, results, AccessRequestAction.View)) | ||
| } | ||
| return results.filter((_, i) => auths[i].success) | ||
| } | ||
|
|
||
| export type UpdateAccessRequestParams = Pick<AccessRequestInterface, 'metadata'> | ||
| export async function updateAccessRequest( | ||
| user: UserInterface, | ||
|
|
||
25 changes: 25 additions & 0 deletions
25
backend/test/routes/model/accessRequest/__snapshots__/getAccessRequests.spec.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`routes > accessRequest > getAccessRequests > 200 > ok 1`] = ` | ||
| { | ||
| "accessRequests": { | ||
| "_id": "664e1aa8bda1f88c28e1c0ce", | ||
| "deleted": false, | ||
| "deletedAt": "", | ||
| "deletedBy": "", | ||
| "id": "test-access-request-13623", | ||
| "modelId": "test-model-4342", | ||
| }, | ||
| } | ||
| `; | ||
|
|
||
| exports[`routes > accessRequest > getAccessRequests > audit > expected call 1`] = ` | ||
| { | ||
| "_id": "664e1aa8bda1f88c28e1c0ce", | ||
| "deleted": false, | ||
| "deletedAt": "", | ||
| "deletedBy": "", | ||
| "id": "test-access-request-13623", | ||
| "modelId": "test-model-4342", | ||
| } | ||
| `; |
51 changes: 51 additions & 0 deletions
51
backend/test/routes/model/accessRequest/getAccessRequests.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import qs from 'qs' | ||
| import { describe, expect, test, vi } from 'vitest' | ||
|
|
||
| import audit from '../../../../src/connectors/audit/__mocks__/index.js' | ||
| import AccessRequestModel from '../../../../src/models/AccessRequest.js' | ||
| import { GetAccessRequestsSchema } from '../../../../src/routes/v2/model/accessRequest/getAccessRequests.js' | ||
| import { createFixture, testGet } from '../../../testUtils/routes.js' | ||
| import { testAccessRequest } from '../../../testUtils/testModels.js' | ||
|
|
||
| vi.mock('../../../../src/connectors/audit/index.js') | ||
|
|
||
| const accessRequestsMock = vi.hoisted(() => { | ||
| return { | ||
| findAccessRequest: vi.fn(() => undefined as any), | ||
| } | ||
| }) | ||
| vi.mock('../../../../src/services/accessRequest.js', () => accessRequestsMock) | ||
|
|
||
| const responseMock = vi.hoisted(() => { | ||
| return { | ||
| findResponses: vi.fn(() => undefined as any), | ||
| } | ||
| }) | ||
| vi.mock('../../../../src/services/response.js', () => responseMock) | ||
|
|
||
| describe('routes > accessRequest > getAccessRequests', () => { | ||
| test('200 > ok', async () => { | ||
| const accessRequestDoc = new AccessRequestModel({ ...testAccessRequest }) | ||
| accessRequestsMock.findAccessRequest.mockResolvedValueOnce(accessRequestDoc) | ||
| responseMock.findResponses.mockResolvedValue([testAccessRequest]) | ||
|
|
||
| const fixture = createFixture(GetAccessRequestsSchema) | ||
| const res = await testGet(`/api/v2/access-requests/search?${qs.stringify(fixture.query)}`) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(res.body).matchSnapshot() | ||
| }) | ||
|
|
||
| test('audit > expected call', async () => { | ||
| const accessRequestDoc = new AccessRequestModel({ ...testAccessRequest }) | ||
| accessRequestsMock.findAccessRequest.mockResolvedValueOnce(accessRequestDoc) | ||
| responseMock.findResponses.mockResolvedValue([testAccessRequest]) | ||
|
|
||
| const fixture = createFixture(GetAccessRequestsSchema) | ||
| const res = await testGet(`/api/v2/access-requests/search?${qs.stringify(fixture.query)}`) | ||
|
|
||
| expect(res.statusCode).toBe(200) | ||
| expect(audit.onViewAccessRequests).toBeCalled() | ||
| expect(audit.onViewAccessRequests.mock.calls.at(0)?.at(1)).toMatchSnapshot() | ||
| }) | ||
| }) |
12 changes: 12 additions & 0 deletions
12
backend/test/services/__snapshots__/accessRequest.spec.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this condition should remain as being for updating and deleting an access and request and a new condition should be added for viewing access requests where we reject if the user cannot view the model or is not named on the access request