Merge pull request #14 from Getable/fix-permissions
Change: permissions must always be an array
This commit is contained in:
commit
a0aeaef3a9
20
README.md
20
README.md
@ -36,15 +36,19 @@ await register({
|
||||
// models defined for them. e.g.
|
||||
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
|
||||
bat: {
|
||||
methods: ['list'],
|
||||
config: { ... } // if provided, overrides the default config
|
||||
}
|
||||
}
|
||||
// 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']}
|
||||
]
|
||||
}
|
||||
});
|
||||
```
|
||||
|
42
src/crud.js
42
src/crud.js
@ -36,29 +36,33 @@ export default (server, model, { prefix, defaultConfig: config, models: permissi
|
||||
|
||||
if (!permissions) {
|
||||
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 });
|
||||
} else if (_.isPlainObject(permissions)) {
|
||||
const permittedModels = Object.keys(permissions);
|
||||
} else {
|
||||
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 });
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user