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?
Sorry, I meant @ebarault. I just edited my comment.
@c3s4r thanks for bringing this mixin through essentially the difference is that the solution proposed in this discussion can automatically enable only the methods explicitly covered by static ACLs in the model configuration, so you don't have to enumerate them manually once more
yes, the idea is to port it as an npm module ultimately
I just finished creating a module for this. I changed the names of the options a little bit, and added unit tests. I manually tested on loopback 2 and 3, and it seems to be working fine.
https://www.npmjs.com/package/loopback-setup-remote-methods-mixin
I'm also thinking on adding an option for defining new remote methods as well. (Which will be deprecated when the Methods section is implemented for Loopback 3, but can be useful for Loopback 2).
Any comments or suggestions are welcome :)
As I said: I will be pushing the mixin as an npm module soon.
@c3s4r : although you most probably did this with all the best intentions :
- mention the contributors when publishing on npm, don't just publish as you own creation. I'm referring to the people who brought something from this discussion
- the way you refactored does not work for nonStatic methods named with
prototype.that are enabled from ACLs definition
@ebarault: Sorry, I did this on my best intentions, didn't want to steal the credit. I couldn't wait because I was needing it for a project, so I putted some effort on doing the unit tests, refactoring the code and publishing the module. Anyways,... let me know how you would like to proceed. I can mention you on the credits, or I can transfer you the project.
-> no pb @c3s4r : not my full credit at all either. We can add a list of contributors from this thread. It remains that the implementation is broken for now. I'll reach out on your github repo to discuss these, we can co-author it if you will. Just give me a moment to move on with other pending stuff.
I just fixed the broken implementation but haven't pushed yet. (Can do it tomorrow morning). Anyone that has contributed on the initial code please let me know your usernames on npm so I can add you as contributors.
@c3s4r I wouldn't mind being listed as "contributing", but I didn't do the heavy lifting (mainly started the mixin process). @ebarault really took my start and ran with it. NPM username is "drmikecrowe"
I just pushed the package to npm with the outstanding bug fixed. Also, I added @ebarault and @drmikecrowe as contributors and a credits section in the README file.
https://www.npmjs.com/package/loopback-setup-remote-methods-mixin
hi @c3s4r, this should be tinkered as it might lead to unexpected results since you make the choice to exclude both instance and static methods for a unique given name, let's continue the discussion on your github.
@ebarault I think it makes sense to create an issue for that at https://github.com/devsu/loopback-setup-remote-methods-mixin/issues
@ebarault: I'm aware of the limitation, I didn't see it like a big issue, anyways, if you think it should be changed, please open an issue on the other repo and let's continue the conversation there.
thanks all for the module, it is cool, but it is slow when you have many models and each of them has many relations. anything we can do about it?
@go2smartphone Something to deal with server.on('started', () => { ... }) ≈5 sec to process each model. Actually it works the same without looking if app is started (Loopback v2.36.0).
@go2smartphone @Hyperc0der Please open a new issue describing the problem you are facing in detail, ideally with a small app reproducing it (see http://loopback.io/doc/en/contrib/Reporting-issues.html#bug-report).