Skip to content

Commit

Permalink
feat(permissions): accept more then one role to check for permission
Browse files Browse the repository at this point in the history
  • Loading branch information
Viviane Dias committed Dec 4, 2020
1 parent 3c9ca94 commit 00fbbbc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
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]);
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

0 comments on commit 00fbbbc

Please sign in to comment.