Update naming & simplify filter

This commit is contained in:
Alexandre Bodin 2021-04-09 16:20:53 +02:00
parent 8fa8c9b4fa
commit d09fe6f492
3 changed files with 10 additions and 16 deletions

View File

@ -37,7 +37,7 @@ module.exports = {
const { sectionsBuilder, conditionProvider } = getService('permission'); const { sectionsBuilder, conditionProvider } = getService('permission');
const actions = await getService('action').getActionsByRoleId(roleId); const actions = await getService('action').getAllowedActionsForRole(roleId);
const conditions = conditionProvider.values(); const conditions = conditionProvider.values();
const sections = await sectionsBuilder.build(actions); const sections = await sectionsBuilder.build(actions);

View File

@ -1,6 +1,6 @@
'use strict'; 'use strict';
const { getActionsByRoleId } = require('../action'); const { getAllowedActionsForRole } = require('../action');
const { AUTHOR_CODE, PUBLISH_ACTION } = require('../constants'); const { AUTHOR_CODE, PUBLISH_ACTION } = require('../constants');
const fixtures = [ const fixtures = [
@ -16,7 +16,7 @@ const fixtures = [
]; ];
describe('Action', () => { describe('Action', () => {
describe('getActionByRoleId', () => { describe('getAllowedActionsForRole', () => {
test('returns every action if role is not provided', async () => { test('returns every action if role is not provided', async () => {
global.strapi = { global.strapi = {
admin: { admin: {
@ -32,7 +32,7 @@ describe('Action', () => {
}, },
}; };
const actions = await getActionsByRoleId(); const actions = await getAllowedActionsForRole();
expect(actions.length).toBe(fixtures.length); expect(actions.length).toBe(fixtures.length);
expect(actions).toEqual(expect.arrayContaining(fixtures)); expect(actions).toEqual(expect.arrayContaining(fixtures));
}); });
@ -58,7 +58,7 @@ describe('Action', () => {
}, },
}; };
const actions = await getActionsByRoleId(roleId); const actions = await getAllowedActionsForRole(roleId);
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId }); expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length); expect(actions.length).toBe(fixtures.length);
@ -86,7 +86,7 @@ describe('Action', () => {
}, },
}; };
const actions = await getActionsByRoleId(roleId); const actions = await getAllowedActionsForRole(roleId);
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId }); expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length - 1); expect(actions.length).toBe(fixtures.length - 1);

View File

@ -8,10 +8,10 @@ const { AUTHOR_CODE, PUBLISH_ACTION } = require('./constants');
/** /**
* Returns actions available for a role. * Returns actions available for a role.
* @param {object} roleId * @param {string|number} roleId
* @returns {object[]} * @returns {object[]}
*/ */
const getActionsByRoleId = async roleId => { const getAllowedActionsForRole = async roleId => {
const { actionProvider } = getService('permission'); const { actionProvider } = getService('permission');
if (!isNil(roleId)) { if (!isNil(roleId)) {
@ -22,13 +22,7 @@ const getActionsByRoleId = async roleId => {
} }
if (role.code === AUTHOR_CODE) { if (role.code === AUTHOR_CODE) {
return actionProvider.values().filter(action => { return actionProvider.values().filter(({ actionId }) => actionId !== PUBLISH_ACTION);
if (action.actionId !== PUBLISH_ACTION) {
return true;
}
return false;
});
} }
} }
@ -36,5 +30,5 @@ const getActionsByRoleId = async roleId => {
}; };
module.exports = { module.exports = {
getActionsByRoleId, getAllowedActionsForRole,
}; };