Change: permissions must always be an array

This allows us to set different configs per model. I should have thought
of this usecase when I first did permissions.
This commit is contained in:
Joey Baker 2016-07-22 11:24:08 -07:00
parent 7c2b146eed
commit f9b997b65c
2 changed files with 48 additions and 41 deletions

View File

@ -32,13 +32,21 @@ await register({
defaultConfig: { ... }, // passed as `config` to all routes created
models: ['cat', 'dog'] // only the cat and dog models will have routes created
// or
models: {
models: [
// possible methods: list, get, scope, create, destroy, destroyAll, destroyScope, update
cat: ['get', 'list'], // the cat model only has get and list methods enabled
dog: true, // the dog model has all methods enabled
// the cat model only has get and list methods enabled
{model: 'cat', methods: ['get', 'list']},
// the dog model has all methods enabled
{model: 'dog'},
// the cow model also has all methods enabled
'cow',
// the bat model as a custom config for the list method, but uses the default config for create.
// `config` if provided, overrides the default config
{model: 'bat', methods: ['list'], config: { ... }},
{model: 'bat', methods: ['create']}
]
models: {
bat: {
methods: ['list'],
config: { ... } // if provided, overrides the default config
}
}
}

View File

@ -4,9 +4,9 @@ import _ from 'lodash';
import { parseInclude, parseWhere } from './utils';
import { notFound } from 'boom';
const createAll = ({server, model, prefix, config}) => {
const createAll = ({ server, model, prefix, config }) => {
Object.keys(methods).forEach((method) => {
methods[method]({server, model, prefix, config});
methods[method]({ server, model, prefix, config });
});
};
@ -28,42 +28,41 @@ models: {
*/
export default (server, model, {prefix, defaultConfig: config, models: permissions}) => {
export default (server, model, { prefix, defaultConfig: config, models: permissions }) => {
const modelName = model._singular;
const permissionsIsArray = Array.isArray(permissions);
if (!permissions) {
createAll({server, model, prefix, config});
}
else if (Array.isArray(permissions) && permissions.includes(modelName)) {
createAll({server, model, prefix, config});
}
else if (_.isPlainObject(permissions)) {
const permittedModels = Object.keys(permissions);
createAll({ server, model, prefix, config });
} else if (permissionsIsArray && permissions.includes(modelName)) {
createAll({ server, model, prefix, config });
} else if (permissionsIsArray) {
const permissionOptions = permissions.filter((permission) => {
return permission.model === modelName;
});
if (permissions[modelName] === true) {
createAll({server, model, prefix, config});
}
else if (permittedModels.includes(modelName)) {
if (Array.isArray(permissions[modelName])) {
permissions[modelName].forEach((method) => {
methods[method]({server, model, prefix, config});
});
}
else if (_.isPlainObject(permissions[modelName])) {
permissions[modelName].methods.forEach((method) => {
methods[method]({
server,
model,
prefix,
config: permissions[modelName].config || config,
permissionOptions.forEach((permissionOption) => {
if (_.isPlainObject(permissionOption)) {
const permissionConfig = permissionOption.config || config;
if (permissionOption.methods) {
permissionOption.methods.forEach((method) => {
methods[method]({
server,
model,
prefix,
config: permissionConfig,
});
});
});
} else {
createAll({ server, model, prefix, config: permissionConfig });
}
}
}
});
}
};
export const list = ({server, model, prefix, config}) => {
export const list = ({ server, model, prefix, config }) => {
server.route({
method: 'GET',
path: `${prefix}/${model._plural}`,
@ -86,7 +85,7 @@ export const list = ({server, model, prefix, config}) => {
});
};
export const get = ({server, model, prefix, config}) => {
export const get = ({ server, model, prefix, config }) => {
server.route({
method: 'GET',
path: `${prefix}/${model._singular}/{id?}`,
@ -114,7 +113,7 @@ export const get = ({server, model, prefix, config}) => {
});
};
export const scope = ({server, model, prefix, config}) => {
export const scope = ({ server, model, prefix, config }) => {
const scopes = Object.keys(model.options.scopes);
server.route({
@ -140,7 +139,7 @@ export const scope = ({server, model, prefix, config}) => {
});
};
export const create = ({server, model, prefix, config}) => {
export const create = ({ server, model, prefix, config }) => {
server.route({
method: 'POST',
path: `${prefix}/${model._singular}`,
@ -156,7 +155,7 @@ export const create = ({server, model, prefix, config}) => {
});
};
export const destroy = ({server, model, prefix, config}) => {
export const destroy = ({ server, model, prefix, config }) => {
server.route({
method: 'DELETE',
path: `${prefix}/${model._singular}/{id?}`,
@ -177,7 +176,7 @@ export const destroy = ({server, model, prefix, config}) => {
});
};
export const destroyAll = ({server, model, prefix, config}) => {
export const destroyAll = ({ server, model, prefix, config }) => {
server.route({
method: 'DELETE',
path: `${prefix}/${model._plural}`,
@ -197,7 +196,7 @@ export const destroyAll = ({server, model, prefix, config}) => {
});
};
export const destroyScope = ({server, model, prefix, config}) => {
export const destroyScope = ({ server, model, prefix, config }) => {
const scopes = Object.keys(model.options.scopes);
server.route({
@ -225,7 +224,7 @@ export const destroyScope = ({server, model, prefix, config}) => {
});
};
export const update = ({server, model, prefix, config}) => {
export const update = ({ server, model, prefix, config }) => {
server.route({
method: 'PUT',
path: `${prefix}/${model._singular}/{id}`,