Skip to content

Commit

Permalink
Add support for $filter based /canAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
thgreasi committed Sep 30, 2021
1 parent bf565ae commit afa4d76
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/sbvr-api/permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import * as memoize from 'memoizee';
import * as randomstring from 'randomstring';
import * as env from '../config-loader/env';
import * as sbvrUtils from '../sbvr-api/sbvr-utils';
import { HookReq, addPureHook, addHook } from './hooks';
import { HookReq, addPureHook, addHook, HookArgs } from './hooks';
import {
BadRequestError,
PermissionError,
Expand Down Expand Up @@ -1595,9 +1595,8 @@ export const setup = () => {
POSTPARSE: async ({
req,
request,
}: {
req: HookReq;
request: ODataRequest & { permissionType?: PermissionCheck };
}: HookArgs & {
request: { permissionType?: PermissionCheck };
}) => {
// If the abstract sql query is already generated then adding permissions will do nothing
if (request.abstractSqlQuery != null) {
Expand All @@ -1607,7 +1606,10 @@ export const setup = () => {
request.method === 'POST' &&
request.odataQuery.property?.resource === 'canAccess'
) {
if (request.odataQuery.key == null) {
const { key } = request.odataQuery;
const $filter = request.odataQuery.property.options?.$filter;
if ((key == null) === ($filter == null)) {
// Exactly one of key or $filter are allowed
throw new BadRequestError();
}
const { action, method } = request.values;
Expand Down Expand Up @@ -1638,7 +1640,12 @@ export const setup = () => {
const idField = resourceTable.idField;
request.odataQuery.options = {
$select: { properties: [{ name: idField }] },
$top: 1,
...(key != null
? { $top: 1 }
: {
$filter,
$orderby: { properties: [{ name: idField, order: 'asc' }] },
}),
};
request.odataQuery.resource = request.resourceName;
delete request.odataQuery.property;
Expand Down

0 comments on commit afa4d76

Please sign in to comment.