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

refactor: use functionsOfType to register multiple auth permissions functions #6009

Merged
Merged
Show file tree
Hide file tree
Changes from 4 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
16 changes: 14 additions & 2 deletions src/core/util/buildContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,20 @@ export default async function buildContext(context, request = {}) {

// authorization methods
if (userId) {
if (typeof context.auth.getHasPermissionFunctionForUser === "function") {
context.userHasPermission = await context.auth.getHasPermissionFunctionForUser(context);
if (context.getFunctionsOfType("getHasPermissionFunctionForUser") && context.getFunctionsOfType("getHasPermissionFunctionForUser").length) {
context.userHasPermission = async (...args) => {
// get all functions of type getHasPermissionFunctionForUser
const allAuthPluginFunctions = await context.getFunctionsOfType("getHasPermissionFunctionForUser");

const allPermissions = await Promise.all(allAuthPluginFunctions.map(async (func) => {
// call with context for currying
const result = await func(context)(...args);
return result;
}));

// userHasPermission if ALL permission checks are `true`
return allPermissions.every((permission) => permission === true);
};
} else {
context.userHasPermission = () => false;
}
Expand Down
15 changes: 9 additions & 6 deletions src/core/util/buildContext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const mockAccount = { _id: "accountId", userId: fakeUser._id };
const accountByUserId = jest.fn().mockName("accountByUserId").mockReturnValue(Promise.resolve(mockAccount));

const auth = {
accountByUserId,
getHasPermissionFunctionForUserLegacy: () => () => {}
accountByUserId
};

test("properly mutates the context object without user", async () => {
process.env.ROOT_URL = "http://localhost:3000";
const context = {
auth,
validatePermissions: mockContext.validatePermissions,
collections: mockContext.collections,
getFunctionsOfType: mockContext.getFunctionsOfType,
queries: {
primaryShopId: () => "PRIMARY_SHOP_ID"
}
},
validatePermissions: mockContext.validatePermissions
};

await buildContext(context, { user: undefined });
Expand All @@ -27,6 +27,7 @@ test("properly mutates the context object without user", async () => {
accountId: null,
auth,
collections: mockContext.collections,
getFunctionsOfType: mockContext.getFunctionsOfType,
isInternalCall: false,
queries: {
primaryShopId: jasmine.any(Function)
Expand All @@ -45,18 +46,20 @@ test("properly mutates the context object with user", async () => {

const context = {
auth,
validatePermissions: mockContext.validatePermissions,
collections: mockContext.collections,
getFunctionsOfType: mockContext.getFunctionsOfType,
queries: {
primaryShopId: () => "PRIMARY_SHOP_ID"
}
},
validatePermissions: mockContext.validatePermissions
};
await buildContext(context, { user: fakeUser });
expect(context).toEqual({
account: mockAccount,
accountId: mockAccount._id,
auth,
collections: mockContext.collections,
getFunctionsOfType: mockContext.getFunctionsOfType,
isInternalCall: false,
queries: {
primaryShopId: jasmine.any(Function)
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/legacy-authorization/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export default async function register(app) {
label: "Legacy Authorization",
name: "reaction-legacy-authorization",
version: app.context.appVersion,
auth: {
getHasPermissionFunctionForUser
functionsByType: {
getHasPermissionFunctionForUser: [getHasPermissionFunctionForUser]
}
});
}
2 changes: 1 addition & 1 deletion src/plugins/legacy-authorization/util/hasPermission.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default async function hasPermission(context, resource, action, authConte
return false;
}

const hasPermissionCurried = curryN(2, hasPermission);
const hasPermissionCurried = curryN(4, hasPermission);

/**
* @summary Get a `hasPermission` function bound to the current user context
Expand Down