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:
parent
7c2b146eed
commit
f9b997b65c
18
README.md
18
README.md
@ -32,13 +32,21 @@ await register({
|
|||||||
defaultConfig: { ... }, // passed as `config` to all routes created
|
defaultConfig: { ... }, // passed as `config` to all routes created
|
||||||
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']},
|
||||||
|
// 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: {
|
bat: {
|
||||||
methods: ['list'],
|
|
||||||
config: { ... } // if provided, overrides the default config
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
33
src/crud.js
33
src/crud.js
@ -30,36 +30,35 @@ models: {
|
|||||||
|
|
||||||
export default (server, model, { prefix, defaultConfig: config, models: permissions }) => {
|
export default (server, model, { prefix, defaultConfig: config, models: permissions }) => {
|
||||||
const modelName = model._singular;
|
const modelName = model._singular;
|
||||||
|
const permissionsIsArray = Array.isArray(permissions);
|
||||||
|
|
||||||
if (!permissions) {
|
if (!permissions) {
|
||||||
createAll({ server, model, prefix, config });
|
createAll({ server, model, prefix, config });
|
||||||
}
|
} else if (permissionsIsArray && permissions.includes(modelName)) {
|
||||||
else if (Array.isArray(permissions) && permissions.includes(modelName)) {
|
|
||||||
createAll({ server, model, prefix, config });
|
createAll({ server, model, prefix, config });
|
||||||
}
|
} else if (permissionsIsArray) {
|
||||||
else if (_.isPlainObject(permissions)) {
|
const permissionOptions = permissions.filter((permission) => {
|
||||||
const permittedModels = Object.keys(permissions);
|
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])) {
|
permissionOptions.forEach((permissionOption) => {
|
||||||
permissions[modelName].methods.forEach((method) => {
|
if (_.isPlainObject(permissionOption)) {
|
||||||
|
const permissionConfig = permissionOption.config || config;
|
||||||
|
|
||||||
|
if (permissionOption.methods) {
|
||||||
|
permissionOption.methods.forEach((method) => {
|
||||||
methods[method]({
|
methods[method]({
|
||||||
server,
|
server,
|
||||||
model,
|
model,
|
||||||
prefix,
|
prefix,
|
||||||
config: permissions[modelName].config || config,
|
config: permissionConfig,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
} else {
|
||||||
|
createAll({ server, model, prefix, config: permissionConfig });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user