36 lines
901 B
JavaScript
Raw Normal View History

2021-04-08 19:36:28 +02:00
'use strict';
const { isNil } = require('lodash/fp');
2021-10-27 18:54:58 +02:00
const { NotFoundError } = require('@strapi/utils').errors;
2021-04-08 19:36:28 +02:00
const { getService } = require('../utils');
const { AUTHOR_CODE, PUBLISH_ACTION } = require('./constants');
// TODO: move actionProvider here instead of in the permission service
/**
* Returns actions available for a role.
2021-04-09 16:20:53 +02:00
* @param {string|number} roleId
2021-04-08 19:36:28 +02:00
* @returns {object[]}
*/
2022-08-08 23:33:39 +02:00
const getAllowedActionsForRole = async (roleId) => {
2021-04-08 19:36:28 +02:00
const { actionProvider } = getService('permission');
if (!isNil(roleId)) {
const role = await getService('role').findOne({ id: roleId });
if (!role) {
2021-10-27 18:54:58 +02:00
throw new NotFoundError('role.notFound');
2021-04-08 19:36:28 +02:00
}
if (role.code === AUTHOR_CODE) {
2021-04-09 16:20:53 +02:00
return actionProvider.values().filter(({ actionId }) => actionId !== PUBLISH_ACTION);
2021-04-08 19:36:28 +02:00
}
}
return actionProvider.values();
};
module.exports = {
2021-04-09 16:20:53 +02:00
getAllowedActionsForRole,
2021-04-08 19:36:28 +02:00
};