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

Accept more then one role to check for permission #5

Merged
merged 2 commits into from
Dec 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions packages/activists-api/src/graphql-api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { mocked } from 'ts-jest/utils';
import fetch from './graphql-api/client';
import * as ActionsAPI from './graphql-api/actions';
Expand Down Expand Up @@ -28,11 +29,12 @@ describe('tests on api graphql', () => {
return NotificationsAPI
.send(input)
.then(({ data }) => {
// @ts-ignore
expect(fetchMocked).toBeCalledWith({
query: NotificationsAPI.queries.send,
variables: { input }
variables: { input },
});
expect(data).toEqual({ status: 'OK!' });
expect(data).toEqual({ status: "OK!" });
});
});

Expand All @@ -50,9 +52,10 @@ describe('tests on api graphql', () => {
return ActivistsAPI
.get_or_create(input)
.then((activist) => {
// @ts-ignore
expect(fetchMocked).toBeCalledWith({
query: ActivistsAPI.queries.get_or_create,
variables: { activist: input }
variables: { activist: input },
});
expect(activist).toEqual({ ...input, id: 2 });
});
Expand All @@ -70,9 +73,10 @@ describe('tests on api graphql', () => {
return WidgetsAPI
.get(widgetReturned.id)
.then((widget) => {
// @ts-ignore
expect(fetchMocked).toBeCalledWith({
query: WidgetsAPI.queries.get,
variables: { widget_id: widgetReturned.id }
variables: { widget_id: widgetReturned.id },
});
expect(widget).toEqual(widgetReturned);
});
Expand All @@ -91,9 +95,10 @@ describe('tests on api graphql', () => {
return ActionsAPI
.pressure(input)
.then((activist_pressure) => {
// @ts-ignore
expect(fetchMocked).toBeCalledWith({
query: ActionsAPI.queries.pressure,
variables: { input }
variables: { input },
});
expect(activist_pressure).toEqual({ id: 2 });
});
Expand All @@ -110,9 +115,10 @@ describe('tests on api graphql', () => {
return ActionsAPI
.pressure_sync_done(input)
.then((activist_pressure: any) => {
// @ts-ignore
expect(fetchMocked).toBeCalledWith({
query: ActionsAPI.queries.pressure_sync_done,
variables: input
variables: input,
});
expect(activist_pressure).toEqual({ id: 2 });
})
Expand Down
16 changes: 11 additions & 5 deletions packages/redes-api/src/resolvers/create_match.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ type Args = {
input: CreateMatch
}

const create_match = async (_: void, args: Args, context: Context): Promise<any> => {
const { input: { recipient, volunteer, agent, community_id } } = args
const create_match = async (
_: void,
args: Args,
context: Context
): Promise<any> => {
const {
input: { recipient, volunteer, agent, community_id },
} = args;
try {
const volunteerRes = await create_volunteer_ticket(undefined, {
input: {
Expand Down Expand Up @@ -63,11 +69,11 @@ const create_match = async (_: void, args: Args, context: Context): Promise<any>
status: "encaminhamento__realizado"
}

return await match.create(matchTicket)
} catch(e) {
return await match.create(matchTicket);
} catch (e) {
logger.error(e)
return undefined
}
}

export default check_user(create_match, Roles.USER)
export default check_user(create_match, [Roles.USER, Roles.ADMIN]);
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ const create_volunteer_ticket = async (_: void, args: Args, _context: Context):
}
}

export default check_user(create_volunteer_ticket, Roles.USER)
export default check_user(create_volunteer_ticket, [Roles.USER, Roles.ADMIN]);
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@ const update_recipient_ticket = async (_: void, args: Args, _context: Context):
}
}

export default check_user(update_recipient_ticket, Roles.USER)
export default check_user(update_recipient_ticket, [Roles.USER, Roles.ADMIN]);
20 changes: 9 additions & 11 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions utils/permissions-utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export enum Roles {
}

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export const handle_check_user = ({ fetch, logger }: Options) => (next: any, role: Roles) => async (_: void, args: any, context: Context) => {
export const handle_check_user = ({ fetch, logger }: Options) => (next: any, role: Roles | [Roles]) => async (_: void, args: any, context: Context) => {
const get_permission = handle_get_permission({ fetch, logger });
const { session }: Context = context;

Expand All @@ -27,7 +27,10 @@ export const handle_check_user = ({ fetch, logger }: Options) => (next: any, rol
// Get permission on API-GraphQL (Hasura)
const { permission, user } = await get_permission({ user_id: session.user_id, community_id });
// Execute only when role is permitted from relationship between community users
if (permission?.role === role || user.is_admin) return next(_, args, context);
const roleInArray = typeof role === 'number' ? [role] : role
if (
roleInArray.includes(permission?.role) || user.is_admin
) return next(_, args, context);
}
// Permission denied
throw new Error('invalid_permission');
Expand Down