Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Created December 2, 2014 03:27
Show Gist options
  • Save pulkitsinghal/43d4bae2467c686ec55b to your computer and use it in GitHub Desktop.
Save pulkitsinghal/43d4bae2467c686ec55b to your computer and use it in GitHub Desktop.
Strongloop/Loopback - Override User model via boot script
module.exports = function(app) {
var User = app.models.User;
// TODO: (1) find an example of how to add new "properties" to the built-in User mode via boot script
// (2) This is how you can add a "relationship" to the built-in User model via boot script
var SomeOtherModel = app.models.SomeOtherModel;
User.hasMany(SomeOtherModel, {as: 'someOtherModels', foreignKey: 'someOtherModelId'});
// (3) This is how you can add "remote methods" to the built-in User model via boot script
// (3a) either
User.greet = function(msg, cb) {
cb(null, 'Greetings... ' + msg);
}
// (3a) or
User.remoteMethod(
'greet',
{
accepts: {arg: 'msg', type: 'string'},
returns: {arg: 'greeting', type: 'string'}
}
);
// (4) This is a low-level/dynamic way to add "remoting" for any of the built-in models via boot script
// reference: https://groups.google.com/forum/#!topic/loopbackjs/0yHzU7PR19U
// *** UN-TESTED BY THE ORIGINAL AUTHOR OF THIS GIST ***
/*var remotes = app.remotes();
remotes.after('**', function(ctx, next, method) {
var req = ctx.req;
var Model = method.ctor;
var modelInstance = ctx.instance;
if (Model.modelName === 'MyModel') {
// ...
}
next();
});*/
// (5) This is how you can add "hooks" to the built-in User model via boot script
User.beforeCreate = function(next, modelInstance) {
console.log('inside User.beforeCreate');
// your code goes here
next();
};
// (6) This is how you can add "event observers" to the built-in User model via boot script
User.on('resetPasswordRequest', function (info) {
console.log('inside User.on.resetPasswordRequest');
// your code goes here
});
};
@coox
Copy link

coox commented Sep 9, 2015

Thanks a bunch for this Gist.

In practice, I found that extending the ACLs quickly becomes a requirement as soon as you do any kind of other customization on the built-in model.

There is a project to allow ACLs to be overriden in model-config.json that is being worked on and tracked in loopback issue #669, but in the meantime, I would suggest the approach below. In this example, read access is granted to a related model (as defined in section (2)):

// (7) This is how you can add ACLs to the built-in User model via boot script
var ACL = app.models.ACL;
ACL.create({
  model: 'User',
  accessType: 'READ',
  principalType: 'ROLE',
  principalId: '$owner',
  permission: 'ALLOW',
  property: '__get__someOtherModels',
}, function (err, acl) {
  console.log('ACL entry created: %j', acl);
});

If for any reason, this could be considered a bad idea, or if there is a better, sanctioned alternative, please let me know.

@TinOo512
Copy link

@coox I'm not sure which one si better but I do like that :

User.settings.acls.push({
  accessType: 'READ',
  principalType: 'ROLE',
  principalId: '$owner',
  permission: 'ALLOW',
  property: '__get__someOtherModels',
});

PS: it seems that loopback does the same way to apply the ACL specified in model.json

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