Disable all remote methods and enable only selected ones (white-list)
There is a bunch of existing open/closed issues with questions how to hide the method of the model. My question is about how can all the methods be disabled first and then enabled only those which are necessary? (basically, duplicates this comment: https://github.com/strongloop/loopback/issues/465#issuecomment-54851858)
Here is the simple function which hides all the methods of the model:
/**
* Hides all the methods besides those in 'methods'.
*
* @param Model model to be updated.
* @param methods array of methods to expose, e.g.: ['find', 'updateAttributes'].
*/
var setMethodsVisibility = function(Model, methods) {
methods = methods || [];
Model.sharedClass.methods().forEach(function(method) {
method.shared = methods.indexOf(method.name) > -1;
});
};
However, it doesn't hide two groups:
- remote methods, which have to be hidden like this: Model.disableRemoteMethod(name, isStatic)
- custom methods, like User.login or User.resetPassword (didn't find any way to hide them at all);
Could you please advise what would be the correct way to hide everything (without specific call for each method by its name explicitly) and allow only the methods which are necessary?
@bajtos Can you please clarify which part of this is docs and which is an enhancement request.
@voitau Could you please advise what would be the correct way to hide everything (without specific call for each method by its name explicitly) and allow only the methods which are necessary?
AFAIK, this is not possible at the moment. Let's keep this issue open to track your request for that. Note that the implementation will require changes in the strong-remoting module too.
However, it doesn't hide custom methods, like User.login or User.resetPassword (didn't find any way to hide them at all);
You have to call User.disableRemoteMethod before creating the explorer middleware. I have filled a new GH issue to fix that - see #686.
@crandmck Can you please clarify which part of this is docs and which is an enhancement request.
After a closer look, this is a pure enhancement. I am going to remove the "doc" label.
I'm able to hide my remote methods from the explorer when using @voitau's function in 2.17.2.
+1
great code @voitau, thanks! I'm new to loopback, what is the best place to put that code so I can use for all my models?
To hide ALL methods, I ended up doing this
function disableAllMethodsBut(model, methodsToExpose)
{
if(model && model.sharedClass)
{
methodsToExpose = methodsToExpose || [];
var modelName = model.sharedClass.name;
var methods = model.sharedClass.methods();
var relationMethods = [];
var hiddenMethods = [];
try
{
Object.keys(model.definition.settings.relations).forEach(function(relation)
{
relationMethods.push({ name: '__findById__' + relation, isStatic: false });
relationMethods.push({ name: '__destroyById__' + relation, isStatic: false });
relationMethods.push({ name: '__updateById__' + relation, isStatic: false });
relationMethods.push({ name: '__exists__' + relation, isStatic: false });
relationMethods.push({ name: '__link__' + relation, isStatic: false });
relationMethods.push({ name: '__get__' + relation, isStatic: false });
relationMethods.push({ name: '__create__' + relation, isStatic: false });
relationMethods.push({ name: '__update__' + relation, isStatic: false });
relationMethods.push({ name: '__destroy__' + relation, isStatic: false });
relationMethods.push({ name: '__unlink__' + relation, isStatic: false });
relationMethods.push({ name: '__count__' + relation, isStatic: false });
relationMethods.push({ name: '__delete__' + relation, isStatic: false });
});
} catch(err) {}
methods.concat(relationMethods).forEach(function(method)
{
var methodName = method.name;
if(methodsToExpose.indexOf(methodName) < 0)
{
hiddenMethods.push(methodName);
model.disableRemoteMethod(methodName, method.isStatic);
}
});
if(hiddenMethods.length > 0)
{
console.log('\nRemote mehtods hidden for', modelName, ':', hiddenMethods.join(', '), '\n');
}
}
};
FYI, I am working on a feature that enables/disables sharedMethods using settings in config.json/model-config.json. It's both a whitelister and blacklister. See https://github.com/strongloop/loopback/pull/1667
@superkhau Is there any sprint planned for these features?
@mrbatista Anything marked with a label is up for sprint planning. There is no guarantee when the issue will be pulled into the current sprint. See https://waffle.io/strongloop-internal/scrum-loopback.
@ericprieto In what file did you run that method?
@devonatdomandtom in any file... Just pass the model that you want the methods to be hidden. I usually run that on the model implementation.
@ericprieto thanks! did you include the function definition there as well?
This is what I'm doing to disable current methods:
YourModel.sharedClass.methods().forEach(function(method) {
YourModel.disableRemoteMethod(method.name, method.isStatic);
});
@devonatdomandtom no, I store this function in some helpers library
helpers.js
module.exports.disableAllMethods = function disableAllMethods(model, methodsToExpose)
{
if(model && model.sharedClass)
{
methodsToExpose = methodsToExpose || [];
var modelName = model.sharedClass.name;
var methods = model.sharedClass.methods();
var relationMethods = [];
var hiddenMethods = [];
try
{
Object.keys(model.definition.settings.relations).forEach(function(relation)
{
relationMethods.push({ name: '__findById__' + relation, isStatic: false });
relationMethods.push({ name: '__destroyById__' + relation, isStatic: false });
relationMethods.push({ name: '__updateById__' + relation, isStatic: false });
relationMethods.push({ name: '__exists__' + relation, isStatic: false });
relationMethods.push({ name: '__link__' + relation, isStatic: false });
relationMethods.push({ name: '__get__' + relation, isStatic: false });
relationMethods.push({ name: '__create__' + relation, isStatic: false });
relationMethods.push({ name: '__update__' + relation, isStatic: false });
relationMethods.push({ name: '__destroy__' + relation, isStatic: false });
relationMethods.push({ name: '__unlink__' + relation, isStatic: false });
relationMethods.push({ name: '__count__' + relation, isStatic: false });
relationMethods.push({ name: '__delete__' + relation, isStatic: false });
});
} catch(err) {}
methods.concat(relationMethods).forEach(function(method)
{
var methodName = method.name;
if(methodsToExpose.indexOf(methodName) < 0)
{
hiddenMethods.push(methodName);
model.disableRemoteMethod(methodName, method.isStatic);
}
});
if(hiddenMethods.length > 0)
{
console.log('\nRemote mehtods hidden for', modelName, ':', hiddenMethods.join(', '), '\n');
}
}
};
models/SomeModel.js
var disableAllMethods = require('../helpers.js').disableAllMethods;
module.exports = function(SomeModel)
{
disableAllMethods(SomeModel, [... methodsIDontWannaHide]);
...
};
@ericprieto Amazing sir. That was exceedingly helpful
@ericprieto thanks!
Anytime, guys :)
@alFReD-NSH thanks, ur code is simple.
Here is gist for model mixin to disable all remote methods and enable only selected in json model config. https://gist.github.com/ratik/5252e4c168a8c29329c0
Please @ratik ,give the proper credits, most of that code was posted here https://github.com/strongloop/loopback/issues/651#issuecomment-140879983