Skip to content

Instantly share code, notes, and snippets.

@nullEuro
Created December 6, 2014 00:49
Show Gist options
  • Save nullEuro/6ea7c6c1de7206a44fd3 to your computer and use it in GitHub Desktop.
Save nullEuro/6ea7c6c1de7206a44fd3 to your computer and use it in GitHub Desktop.
loopback-component-push installation model access control
// ...
function checkBelongsTo(installationId, userId, cb) {
Installation.findById(installationId, function (err, inst) {
cb(err, !err && inst && inst.userId == userId);
});
}
var ERR_403 = new Error("Forbidden");
ERR_403.status = ERR_403.statusCode = 403;
// prevent users from creating installations for foreign accounts
Installation.beforeRemote('create', function(ctx, ign, next) {
ctx.req.body.userId = ctx.req.accessToken.userId;
next();
});
// prevent users from changing the userId of their own installations
// prevent users from changing foreign installations
Installation.beforeRemote('prototype.updateAttributes', function(ctx, ign, next) {
var currentUserId = ctx.req.accessToken.userId;
ctx.req.body.userId = currentUserId;
var installationId = ctx.req.params.id;
if (installationId) {
checkBelongsTo(installationId, currentUserId,
function(err, belongsToCurrentUser) {
next(err || (!belongsToCurrentUser ? ERR_403 : null));
});
} else {
next();
}
});
// ...
...
"acls": [
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$everyone",
"permission": "DENY"
},
{
"accessType": "WRITE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"property": "create"
},
{
"accessType": "EXECUTE",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW",
"property": "updateAttributes"
}
],
...
@Syhids
Copy link

Syhids commented Jan 12, 2015

How did you manage to get it working currently? I can't get the hooks working because of current issues about extending loopback models. Any hints?

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