Created
December 6, 2014 00:49
-
-
Save nullEuro/6ea7c6c1de7206a44fd3 to your computer and use it in GitHub Desktop.
loopback-component-push installation model access control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ... | |
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(); | |
} | |
}); | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
"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" | |
} | |
], | |
... |
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
If you find a flaw in that solution please let me know.