Skip to content

Instantly share code, notes, and snippets.

@ebarault
Last active February 24, 2017 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ebarault/1c3e43e19735f03dee8260471f8d3545 to your computer and use it in GitHub Desktop.
Save ebarault/1c3e43e19735f03dee8260471f8d3545 to your computer and use it in GitHub Desktop.
// started from https://github.com/strongloop/loopback/issues/651
'use strict';
let _ = require('lodash');
module.exports = function (Model, options) {
if(Model && Model.sharedClass) {
let enableFromACLs = options.enableFromACLs === false? false: true;
let enabledMethodsNames = options.enable || [];
let disabledMethodsNames = options.disable || [];
// wait for all models to be attached so sharedClass.methods() returns all methods
Model.on('attached', function (server) {
server.on('started', function () {
if (enableFromACLs === true) {
// add names of methods explicitly authorized by static ACLs to array of enabled methods names and remove duplicates
enabledMethodsNames = _.uniq( enabledMethodsNames.concat( getAclMethods(Model)));
}
// remove explicitly hidden methods
if (disabledMethodsNames.length) {
enabledMethodsNames = _.difference(enabledMethodsNames, disabledMethodsNames);
}
// get all Model methods
let allMethods = Model.sharedClass.methods();
// compute final list of hidden methods names by substracting methods to enable from array of all methods
disabledMethodsNames = _.differenceWith(allMethods, enabledMethodsNames, function (method, enabledMethodName) {
return (method.name === enabledMethodName);
});
// extract methods names only and add prefix 'prototype.' to names of non static methods to disable
disabledMethodsNames = disabledMethodsNames.map(function (method) {
return method.isStatic? method.name: 'prototype.'+method.name;
});
if ( disabledMethodsNames.length ) {
console.log( 'Model <%s> - Hidding remote methods: ', Model.modelName, disabledMethodsNames.join( ', ' ));
}
// disable remote methods from computed list
disabledMethodsNames.map(Model.disableRemoteMethodByName, Model);
});
});
}
function getAclMethods (Model) {
let authorizedMethods = [];
let acls = Model.definition.settings.acls || [];
acls.forEach((acl) => {
if (acl.permission === 'ALLOW' && acl.property) {
if (Array.isArray(acl.property)) {
authorizedMethods = authorizedMethods.concat(acl.property);
}
else {
authorizedMethods.push(acl.property);
}
}
});
return authorizedMethods;
}
};
{
"name": "Example",
"base": "PersistentModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {},
"mixins":{
"DisableRemoteMethods":{
"enableFromACLs": true,
"enable":["findById"],
"disable":["create"]
}
}
}
'use strict';
var util = require('util');
var disableRemoteMethods = require('./disable-remote-methods');
module.exports = (0, util.deprecate) (
function (app) { return app.loopback.modelBuilder.mixins.define('disableRemoteMethods', disableRemoteMethods);},
'DEPRECATED: Use mixinSources, see https://github.com/.../loopback-import-mixin#mixinsources'
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment