Skip to content

Commit

Permalink
evolution: add auth object on CrudRequest - #11
Browse files Browse the repository at this point in the history
  • Loading branch information
GSI committed Mar 13, 2023
1 parent 7922373 commit f1c7c84
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 16 additions & 4 deletions packages/crud/src/interceptors/crud-request.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor

parser.parseQuery(req.query, crudOptions.operators?.custom);

let auth = null;
if (!isNil(ctrlOptions)) {
const search = this.getSearch(parser, crudOptions, action, req.params);
const auth = this.getAuth(parser, crudOptions, req);
auth = this.getAuth(parser, crudOptions, req);
parser.search = auth.or
? { $or: [auth.or, { $and: search }] }
: { $and: [auth.filter, ...search] };
} else {
parser.search = { $and: this.getSearch(parser, crudOptions, action) };
}

req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions);
req[PARSED_CRUD_REQUEST_KEY] = this.getCrudRequest(parser, crudOptions, auth?.auth);
}

return next.handle();
Expand All @@ -58,10 +59,10 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor
getCrudRequest(
parser: RequestQueryParser,
crudOptions: Partial<MergedCrudOptions>,
auth?: any,
): CrudRequest {
const parsed = parser.getParsed();
const { query, routes, params, operators } = crudOptions;

return {
parsed,
options: {
Expand All @@ -70,6 +71,7 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor
params,
operators,
},
auth,
};
}

Expand Down Expand Up @@ -159,7 +161,7 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor
parser: RequestQueryParser,
crudOptions: Partial<MergedCrudOptions>,
req: any,
): { filter?: any; or?: any } {
): { filter?: any; or?: any; auth?: any } {
const auth: any = {};

/* istanbul ignore else */
Expand All @@ -168,6 +170,16 @@ export class CrudRequestInterceptor extends CrudBaseInterceptor
? req[crudOptions.auth.property]
: req;

if (crudOptions.auth.property && req[crudOptions.auth.property]) {
if (typeof req[crudOptions.auth.property] === 'object') {
if (Object.keys(req[crudOptions.auth.property]).length > 0) {
auth.auth = req[crudOptions.auth.property];
}
} else {
auth.auth = req[crudOptions.auth.property];
}
}

if (isFunction(crudOptions.auth.or)) {
auth.or = crudOptions.auth.or(userOrRequest);
}
Expand Down
4 changes: 3 additions & 1 deletion packages/crud/src/interfaces/crud-request.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { ParsedRequestParams } from '@dataui/crud-request';

import { CrudRequestOptions } from '../interfaces';

export interface CrudRequest<EXTRA = {}> {
export interface CrudRequest<AUTH = {}, EXTRA = {}> {
parsed: ParsedRequestParams<EXTRA>;
options: CrudRequestOptions;
/** authenticated user's from request */
auth?: AUTH;
}

0 comments on commit f1c7c84

Please sign in to comment.