Skip to content
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

Subscription permission #162

Merged
merged 3 commits into from May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/engine/action/Action.ts
Expand Up @@ -56,7 +56,7 @@ export class Action {
private _output: any;
resolve: Function;
type: string;
permissions: Function | Permission | Permission[];
permissions: Permission | Permission[];
private _permissions: Function | Permission | Permission[];
private _defaultPermissions: Permission | Permission[];
descriptionPermissions: string | boolean;
Expand Down Expand Up @@ -252,7 +252,7 @@ export class Action {
this._defaultPermissions = defaultPermissions;
}

getPermissions(): Function | Permission | Permission[] {
getPermissions(): Permission | Permission[] {
if ((!this._permissions && !this._defaultPermissions) || this.permissions) {
return this.permissions;
}
Expand Down
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
3 changes: 1 addition & 2 deletions src/engine/subscription/Subscription.spec.ts
Expand Up @@ -375,8 +375,7 @@ describe('Subscription', () => {
.map(attribute => input[attribute])
.reduce((acc, curr) => `${acc + delimiter + curr}`, '');

const topic = `${entitySubscription.name}${_entity.name}${filled}`;
return topic;
return filled;
}

return null;
Expand Down