Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix action permission as a function ( in resolver ) ; add typings
  • Loading branch information
getlarge committed May 14, 2020
1 parent a6b2917 commit c0b8782
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 66 deletions.
6 changes: 3 additions & 3 deletions src/engine/helpers.ts
@@ -1,10 +1,10 @@
import * as _ from 'lodash';
import { Entity } from '..';
import { Entity, Subscription } from '..';
import { Mutation } from './mutation/Mutation';

export const fillSystemAttributesDefaultValues = (
entity: Entity,
entityMutation: Mutation,
operation: Mutation | Subscription,
payload: any,
context: Record<string, any>,
): any => {
Expand All @@ -22,7 +22,7 @@ export const fillSystemAttributesDefaultValues = (
const attributeName = attribute.name;
const defaultValue = attribute.defaultValue;

const value = defaultValue(ret, entityMutation, entity, context);
const value = defaultValue(ret, operation, entity, context);
if (typeof value !== 'undefined') {
ret[attributeName] = value;
}
Expand Down
45 changes: 31 additions & 14 deletions src/engine/permission/Permission.ts
Expand Up @@ -499,8 +499,6 @@ export const buildUserAttributesPermissionFilter = ({
if (permission.userAttributes.length > 0) {
passOrThrow(userId, () => 'missing userId in permission object');
where = {};
// where = where || {};
// where.$or = where.$or || [];

permission.userAttributes.map(attributeName => {
const userAttrFilter = {
Expand Down Expand Up @@ -533,8 +531,6 @@ export const buildStatesPermissionFilter = ({
if (permission.states.length > 0) {
passOrThrow(entity, () => 'missing entity in permission object');
where = {};
// where = where || {};
// where.$or = where.$or || [];

const states = entity.getStates();
const stateIds = permission.states.map(stateName => {
Expand Down Expand Up @@ -573,8 +569,6 @@ export const buildValuesPermissionFilter = ({

if (permission.values.length > 0) {
where = {};
// where = where || {};
// where.$or = where.$or || [];

permission.values.map(({ attributeName, value }) => {
const filter = {
Expand Down Expand Up @@ -625,8 +619,6 @@ export const buildLookupsPermissionFilter = async ({

if (permission.lookups.length > 0) {
where = {};
// where = where || {};
// where.$or = where.$or || [];

await Promise.all(
permission.lookups.map(async ({ entity, lookupMap }) => {
Expand Down Expand Up @@ -812,10 +804,11 @@ export type ActionPermissionFilter = {
};

export const buildActionPermissionFilter = async (
_permissions: Permission | Permission[],
_permissions: Function | Permission | Permission[],
userId = null,
userRoles = [],
action: Action,
action: Action | Subscription,
// action: Action,
input?: any,
context?: any,
): Promise<
Expand All @@ -829,10 +822,22 @@ export const buildActionPermissionFilter = async (
return undefined;
}

// const permissions = isArray(_permissions) ? _permissions : [_permissions];
const permissions = isArray(_permissions as Permission[])
? (_permissions as Permission[])
: ([_permissions] as Permission[]);
let permissions: Permission[];
if (isFunction(_permissions)) {
const permissionFn = _permissions as Function;
const permissionResult = permissionFn();
permissions = isArray(permissionResult)
? permissionResult
: [permissionResult];
} else if (isArray(_permissions as Permission[])) {
permissions = _permissions as Permission[];
} else {
permissions = [_permissions] as Permission[];
}

// const permissions = isArray(_permissions as Permission[])
// ? (_permissions as Permission[])
// : ([_permissions] as Permission[]);

let foundSimplePermission = false;

Expand Down Expand Up @@ -1036,6 +1041,18 @@ export const findEmptyEntityPermissions = (permissions): string[] => {
});
}

if (permissions.subscriptions) {
const subscriptionNames = Object.keys(permissions.subscriptions);
subscriptionNames.map(subscriptionName => {
if (
permissions.subscriptions[subscriptionName] &&
hasEmptyPermissions(permissions.subscriptions[subscriptionName])
) {
emptyPermissionsIn.push(`subscriptions.${subscriptionName}`);
}
});
}

return emptyPermissionsIn;
};

Expand Down

0 comments on commit c0b8782

Please sign in to comment.