Skip to content

Instantly share code, notes, and snippets.

@ratik
Last active October 12, 2018 09:52
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save ratik/5252e4c168a8c29329c0 to your computer and use it in GitHub Desktop.
Save ratik/5252e4c168a8c29329c0 to your computer and use it in GitHub Desktop.
LoopbackJs disable all remote methods and enable only selected
/**
* Based on @ericprieto code https://github.com/strongloop/loopback/issues/651#issuecomment-140879983
* place this file into common/mixins/disableAllMethods.js
*
**/
module.exports = function(Model, options) {
if(Model && Model.sharedClass) {
var methodsToExpose = options.expose || [];
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');
// }
}
};
{
"name": "Some",
"base": "PersistedModel",
"idInjection": true,
"description":"",
"properties": {
"title": {
"type": "String",
"required": true,
"length": 1023,
"precision": null,
"scale": null,
"_selectable": false
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {},
"mixins":{
"DisableAllMethods":{
"expose":[
"findById"
]
}
}
}
Copy link

ghost commented Jan 25, 2016

Please, give the proper credits. I posted most of this code here
strongloop/loopback#651 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment