Skip to content

Commit

Permalink
fix type mappings and review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Enubia committed Jun 7, 2021
1 parent 52f081d commit 2805448
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 8 additions & 8 deletions src/engine/action/Action.ts
Expand Up @@ -49,7 +49,7 @@ export type ActionSetup = {
output?: any;
resolve: ActionResolver;
type?: string;
permissions?: Function | Permission | Permission[];
permissions?: ((...args) => Permission | Permission[]) | Permission | Permission[];
// preProcessor?: Function;
// postProcessor?: Function;
preProcessor?: ActionPreProcessor;
Expand All @@ -70,7 +70,7 @@ export class Action {
resolve: ActionResolver;
type: string;
permissions: Permission | Permission[];
private _permissions: Function | Permission | Permission[];
private readonly _permissions: ((...args) => Permission | Permission[]) | Permission | Permission[];
private _defaultPermissions: Permission | Permission[];
descriptionPermissions: string | boolean;
preProcessor?: ActionPreProcessor;
Expand Down Expand Up @@ -152,7 +152,7 @@ export class Action {
}

if (isFunction(this.input)) {
const inputFn = this.input as Function;
const inputFn = this.input as (...args) => any;
this.input = inputFn();

passOrThrow(
Expand Down Expand Up @@ -198,7 +198,7 @@ export class Action {
}

if (isFunction(this.output)) {
const outputFn = this.output as Function;
const outputFn = this.output as (...args) => any;
this.output = outputFn();

passOrThrow(
Expand Down Expand Up @@ -237,8 +237,8 @@ export class Action {
_processPermissions(): null | Permission | Permission[] {
if (this._permissions) {
if (isFunction(this._permissions)) {
const permissionsFn = this._permissions as Function;
const permissions: Permission | Permission[] = permissionsFn();
const permissionsFn = this._permissions as (...args) => Permission | Permission[];
const permissions = permissionsFn();
return processActionPermissions(this, permissions);
}
const permissions = this._permissions as Permission | Permission[];
Expand All @@ -254,7 +254,7 @@ export class Action {
if (this.permissions) {
let permissions: Permission | Permission[];
if (isFunction(this._permissions)) {
const permissionsFn = this._permissions as Function;
const permissionsFn = this._permissions as (...args) => Permission | Permission[];
permissions = permissionsFn();
} else {
permissions = this._permissions as Permission | Permission[];
Expand All @@ -268,7 +268,7 @@ export class Action {
this._defaultPermissions = defaultPermissions;
}

getPermissions(): Permission | Permission[] {
getPermissions(): ((...args) => Permission | Permission[]) | Permission | Permission[] {
if ((!this._permissions && !this._defaultPermissions) || this.permissions) {
return this.permissions;
}
Expand Down
2 changes: 1 addition & 1 deletion src/engine/datatype/ObjectDataType.ts
Expand Up @@ -151,7 +151,7 @@ export class ObjectDataType extends ComplexDataType {
return resultAttributes;
}

validate: DataTypeValidateType = (value) => {
validate: DataTypeValidateType = ({ value }): void => {
if (value) {
if (!isMap(value)) {
throw new Error(`Object data type '${this.name}' expects an object`);
Expand Down
10 changes: 9 additions & 1 deletion src/graphqlProtocol/action.ts
Expand Up @@ -18,6 +18,7 @@ import { buildActionPermissionFilter } from '../engine/permission/Permission';
import { CustomError } from '../engine/CustomError';
import { Attribute } from '../engine/attribute/Attribute';
import { GraphRegistryType } from './graphRegistry';
import { isFunction } from '../index';

const AccessDeniedError = new CustomError(
'Access denied',
Expand Down Expand Up @@ -95,11 +96,18 @@ export const handlePermission = async (

const { userId, userRoles } = context;

let _permission;
if (!isFunction(permission)) {
_permission = () => permission;
} else {
_permission = permission;
}

const {
where: permissionWhere,
lookupPermissionEntity,
} = await buildActionPermissionFilter(
() => permission,
_permission,
userId,
userRoles,
action,
Expand Down

0 comments on commit 2805448

Please sign in to comment.