Change: permissions must always be an array #14

Merged
joeybaker merged 1 commits from fix-permissions into master 2016-07-22 18:55:26 +00:00
2 changed files with 35 additions and 27 deletions

View File

@ -36,15 +36,19 @@ await register({
// models defined for them. e.g. // models defined for them. e.g.
models: ['cat', 'dog'] // only the cat and dog models will have routes created models: ['cat', 'dog'] // only the cat and dog models will have routes created
// or // or
models: { models: [
// possible methods: list, get, scope, create, destroy, destroyAll, destroyScope, update // possible methods: list, get, scope, create, destroy, destroyAll, destroyScope, update
cat: ['get', 'list'], // the cat model only has get and list methods enabled // the cat model only has get and list methods enabled
dog: true, // the dog model has all methods enabled {model: 'cat', methods: ['get', 'list']},
bat: { // the dog model has all methods enabled
methods: ['list'], {model: 'dog'},
config: { ... } // if provided, overrides the default config // 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']}
]
} }
}); });
``` ```

View File

@ -36,29 +36,33 @@ export default (server, model, { prefix, defaultConfig: config, models: permissi
if (!permissions) { if (!permissions) {
createAll({ server, model, prefix, config }); createAll({ server, model, prefix, config });
} else if (Array.isArray(permissions) && permissions.includes(modelName)) { } else if (!Array.isArray(permissions)) {
throw new Error('hapi-sequelize-crud: `models` property must be an array');
} else if (permissions.includes(modelName)) {
createAll({ server, model, prefix, config }); createAll({ server, model, prefix, config });
} else if (_.isPlainObject(permissions)) { } else {
const permittedModels = Object.keys(permissions); const permissionOptions = permissions.filter((permission) => {
return permission.model === modelName;
});
if (permissions[modelName] === true) { permissionOptions.forEach((permissionOption) => {
createAll({ server, model, prefix, config }); if (_.isPlainObject(permissionOption)) {
} else if (permittedModels.includes(modelName)) { const permissionConfig = permissionOption.config || config;
if (Array.isArray(permissions[modelName])) {
permissions[modelName].forEach((method) => { if (permissionOption.methods) {
methods[method]({ server, model, prefix, config }); permissionOption.methods.forEach((method) => {
}); methods[method]({
} else if (_.isPlainObject(permissions[modelName])) { server,
permissions[modelName].methods.forEach((method) => { model,
methods[method]({ prefix,
server, config: permissionConfig,
model, });
prefix,
config: permissions[modelName].config || config,
}); });
}); } else {
createAll({ server, model, prefix, config: permissionConfig });
}
} }
} });
} }
}; };