Skip to content

Instantly share code, notes, and snippets.

@mrvisser
Created September 12, 2012 21:21
Show Gist options
  • Save mrvisser/3710025 to your computer and use it in GitHub Desktop.
Save mrvisser/3710025 to your computer and use it in GitHub Desktop.
Sample plugability in Context
// oae-context/lib/api.js
/**
* A generic context object that represents a user execution context.
*
* @param {String} tenantId The unique id of the tenant in the context (e.g., 'cam')
* @param {String} userUuid The UUID of the active user in the context (e.g., 'u:cam:nicolaas')
*/
var Context = module.exports.Context = function(tenantId, userUuid) {
/**
* Get the tenant associated to the current context
*/
this.tenantId = function() {
return tenantId;
};
/**
* Get the user associated to the current context
*/
this.userUuid = function() {
return userUuid;
};
}
// oae-authz/lib/some-file.js
// oae-authz module would like to plug in context-aware functionality
Context.prototype.authz = function() {
var that = {};
// determine if the current user is allowed to do something
that.isAllowed = function(action, resourceUuid, callback) {
api.isAllowed(this.userUuid(), action, resourceUuid, callback);
};
return that;
}
// Usage:
var getContent = function(ctx, contentUuid, callback) {
ctx.authz().isAllowed('viewer', contentUuid, function(err, isAllowed) {
if (isAllowed) .....
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment