2013-06-25 15:43:15 +04:00
|
|
|
// canThis(someUser).edit.posts([id]|[[ids]])
|
|
|
|
// canThis(someUser).edit.post(somePost|somePostId)
|
|
|
|
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
when = require('when'),
|
|
|
|
Models = require('../models'),
|
|
|
|
objectTypeModelMap = require('./objectTypeModelMap'),
|
2014-02-12 07:40:39 +04:00
|
|
|
effectivePerms = require('./effective'),
|
2013-06-25 15:43:15 +04:00
|
|
|
PermissionsProvider = Models.Permission,
|
|
|
|
init,
|
|
|
|
refresh,
|
|
|
|
canThis,
|
|
|
|
CanThisResult,
|
|
|
|
exported;
|
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
function hasActionsMap() {
|
|
|
|
// Just need to find one key in the actionsMap
|
|
|
|
|
|
|
|
return _.any(exported.actionsMap, function (val, key) {
|
2013-10-31 22:02:34 +04:00
|
|
|
/*jslint unparam:true*/
|
2013-08-16 03:22:08 +04:00
|
|
|
return Object.hasOwnProperty(key);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
// TODO: Move this to its own file so others can use it?
|
|
|
|
function parseContext(context) {
|
|
|
|
// Parse what's passed to canThis.beginCheck for standard user and app scopes
|
|
|
|
var parsed = {
|
2014-03-21 03:48:06 +04:00
|
|
|
internal: false,
|
2014-02-12 07:40:39 +04:00
|
|
|
user: null,
|
|
|
|
app: null
|
|
|
|
};
|
2014-03-21 03:48:06 +04:00
|
|
|
|
|
|
|
if (context && (context === 'internal' || context.internal)) {
|
|
|
|
parsed.internal = true;
|
|
|
|
}
|
2014-02-12 07:40:39 +04:00
|
|
|
|
2014-03-21 03:48:06 +04:00
|
|
|
// @TODO: Refactor canThis() references to pass { user: id } explicitly instead of primitives.
|
|
|
|
if (context && context.id) {
|
|
|
|
// Handle passing of just user.id string
|
2014-02-12 07:40:39 +04:00
|
|
|
parsed.user = context.id;
|
|
|
|
} else if (_.isNumber(context)) {
|
2014-03-21 03:48:06 +04:00
|
|
|
// Handle passing of just user id number
|
2014-02-12 07:40:39 +04:00
|
|
|
parsed.user = context;
|
|
|
|
} else if (_.isObject(context)) {
|
|
|
|
// Otherwise, use the new hotness { user: id, app: id } format
|
|
|
|
parsed.user = context.user;
|
|
|
|
parsed.app = context.app;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Base class for canThis call results
|
|
|
|
CanThisResult = function () {
|
2014-02-12 07:40:39 +04:00
|
|
|
return;
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
CanThisResult.prototype.buildObjectTypeHandlers = function (obj_types, act_type, context, permissionLoad) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// Iterate through the object types, i.e. ['post', 'tag', 'user']
|
2014-02-12 07:40:39 +04:00
|
|
|
return _.reduce(obj_types, function (obj_type_handlers, obj_type) {
|
|
|
|
// Grab the TargetModel through the objectTypeModelMap
|
2013-06-25 15:43:15 +04:00
|
|
|
var TargetModel = objectTypeModelMap[obj_type];
|
|
|
|
|
|
|
|
// Create the 'handler' for the object type;
|
|
|
|
// the '.post()' in canThis(user).edit.post()
|
|
|
|
obj_type_handlers[obj_type] = function (modelOrId) {
|
|
|
|
var modelId;
|
|
|
|
|
2014-03-21 03:48:06 +04:00
|
|
|
// If it's an internal request, resolve immediately
|
|
|
|
if (context.internal) {
|
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
if (_.isNumber(modelOrId) || _.isString(modelOrId)) {
|
|
|
|
// It's an id already, do nothing
|
|
|
|
modelId = modelOrId;
|
|
|
|
} else if (modelOrId) {
|
|
|
|
// It's a model, get the id
|
|
|
|
modelId = modelOrId.id;
|
|
|
|
}
|
|
|
|
// Wait for the user loading to finish
|
2014-02-12 07:40:39 +04:00
|
|
|
return permissionLoad.then(function (loadedPermissions) {
|
2013-06-25 15:43:15 +04:00
|
|
|
// Iterate through the user permissions looking for an affirmation
|
2014-02-12 07:40:39 +04:00
|
|
|
var userPermissions = loadedPermissions.user,
|
|
|
|
appPermissions = loadedPermissions.app,
|
|
|
|
hasUserPermission,
|
|
|
|
hasAppPermission,
|
|
|
|
checkPermission = function (perm) {
|
|
|
|
var permObjId;
|
|
|
|
|
|
|
|
// Look for a matching action type and object type first
|
|
|
|
if (perm.get('action_type') !== act_type || perm.get('object_type') !== obj_type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Grab the object id (if specified, could be null)
|
|
|
|
permObjId = perm.get('object_id');
|
|
|
|
|
|
|
|
// If we didn't specify a model (any thing)
|
|
|
|
// or the permission didn't have an id scope set
|
|
|
|
// then the "thing" has permission
|
|
|
|
if (!modelId || !permObjId) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, check if the id's match
|
|
|
|
// TODO: String vs Int comparison possibility here?
|
|
|
|
return modelId === permObjId;
|
|
|
|
};
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
// Check user permissions for matching action, object and id.
|
|
|
|
if (!_.isEmpty(userPermissions)) {
|
|
|
|
hasUserPermission = _.any(userPermissions, checkPermission);
|
|
|
|
}
|
2013-06-09 03:39:24 +04:00
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
// Check app permissions if they were passed
|
|
|
|
hasAppPermission = true;
|
|
|
|
if (!_.isNull(appPermissions)) {
|
|
|
|
hasAppPermission = _.any(appPermissions, checkPermission);
|
|
|
|
}
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
if (hasUserPermission && hasAppPermission) {
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.resolve();
|
|
|
|
}
|
|
|
|
return when.reject();
|
|
|
|
}).otherwise(function () {
|
2014-04-08 17:40:33 +04:00
|
|
|
// Check for special permissions on the model directly
|
2013-06-25 15:43:15 +04:00
|
|
|
if (TargetModel && _.isFunction(TargetModel.permissable)) {
|
2014-04-08 17:40:33 +04:00
|
|
|
return TargetModel.permissable(modelId, context);
|
2013-06-05 07:47:11 +04:00
|
|
|
}
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.reject();
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
return obj_type_handlers;
|
|
|
|
}, {});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
CanThisResult.prototype.beginCheck = function (context) {
|
2013-06-25 15:43:15 +04:00
|
|
|
var self = this,
|
2014-02-12 07:40:39 +04:00
|
|
|
userPermissionLoad,
|
|
|
|
appPermissionLoad,
|
|
|
|
permissionsLoad;
|
|
|
|
|
|
|
|
// Get context.user and context.app
|
|
|
|
context = parseContext(context);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2013-08-16 03:22:08 +04:00
|
|
|
if (!hasActionsMap()) {
|
|
|
|
throw new Error("No actions map found, please call permissions.init() before use.");
|
|
|
|
}
|
|
|
|
|
2014-03-21 03:48:06 +04:00
|
|
|
// Kick off loading of effective user permissions if necessary
|
|
|
|
if (context.user) {
|
|
|
|
userPermissionLoad = effectivePerms.user(context.user);
|
|
|
|
} else {
|
|
|
|
// Resolve null if no context.user to prevent db call
|
|
|
|
userPermissionLoad = when.resolve(null);
|
|
|
|
}
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
|
|
|
|
// Kick off loading of effective app permissions if necessary
|
|
|
|
if (context.app) {
|
|
|
|
appPermissionLoad = effectivePerms.app(context.app);
|
|
|
|
} else {
|
|
|
|
// Resolve null if no context.app
|
|
|
|
appPermissionLoad = when.resolve(null);
|
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
|
2014-03-21 03:48:06 +04:00
|
|
|
// Wait for both user and app permissions to load
|
2014-02-12 07:40:39 +04:00
|
|
|
permissionsLoad = when.all([userPermissionLoad, appPermissionLoad]).then(function (result) {
|
|
|
|
return {
|
|
|
|
user: result[0],
|
|
|
|
app: result[1]
|
|
|
|
};
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// Iterate through the actions and their related object types
|
|
|
|
_.each(exported.actionsMap, function (obj_types, act_type) {
|
|
|
|
// Build up the object type handlers;
|
|
|
|
// the '.post()' parts in canThis(user).edit.post()
|
2014-02-12 07:40:39 +04:00
|
|
|
var obj_type_handlers = self.buildObjectTypeHandlers(obj_types, act_type, context, permissionsLoad);
|
2013-06-25 15:43:15 +04:00
|
|
|
|
|
|
|
// Define a property for the action on the result;
|
|
|
|
// the '.edit' in canThis(user).edit.post()
|
|
|
|
Object.defineProperty(self, act_type, {
|
|
|
|
writable: false,
|
|
|
|
enumerable: false,
|
|
|
|
configurable: false,
|
|
|
|
value: obj_type_handlers
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Return this for chaining
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
canThis = function (context) {
|
2013-06-25 15:43:15 +04:00
|
|
|
var result = new CanThisResult();
|
|
|
|
|
2014-02-12 07:40:39 +04:00
|
|
|
return result.beginCheck(context);
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
init = refresh = function () {
|
|
|
|
// Load all the permissions
|
|
|
|
return PermissionsProvider.browse().then(function (perms) {
|
|
|
|
var seenActions = {};
|
|
|
|
|
|
|
|
exported.actionsMap = {};
|
|
|
|
|
|
|
|
// Build a hash map of the actions on objects, i.e
|
|
|
|
/*
|
|
|
|
{
|
|
|
|
'edit': ['post', 'tag', 'user', 'page'],
|
|
|
|
'delete': ['post', 'user'],
|
|
|
|
'create': ['post', 'user', 'page']
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
_.each(perms.models, function (perm) {
|
|
|
|
var action_type = perm.get('action_type'),
|
|
|
|
object_type = perm.get('object_type');
|
|
|
|
|
|
|
|
exported.actionsMap[action_type] = exported.actionsMap[action_type] || [];
|
|
|
|
seenActions[action_type] = seenActions[action_type] || {};
|
|
|
|
|
|
|
|
// Check if we've already seen this action -> object combo
|
|
|
|
if (seenActions[action_type][object_type]) {
|
|
|
|
return;
|
|
|
|
}
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
exported.actionsMap[action_type].push(object_type);
|
|
|
|
seenActions[action_type][object_type] = true;
|
2013-06-05 07:47:11 +04:00
|
|
|
});
|
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
return when(exported.actionsMap);
|
|
|
|
});
|
|
|
|
};
|
2013-06-05 07:47:11 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = exported = {
|
|
|
|
init: init,
|
|
|
|
refresh: refresh,
|
|
|
|
canThis: canThis,
|
|
|
|
actionsMap: {}
|
2013-10-31 22:02:34 +04:00
|
|
|
};
|