2013-12-20 01:49:47 +00:00
|
|
|
/*!
|
2013-11-10 06:22:16 +00:00
|
|
|
Schema ACL options
|
2013-06-26 23:25:51 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
Object level permissions, for example, an album owned by a user
|
2013-06-26 23:25:51 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
Factors to be authorized against:
|
2013-06-26 23:25:51 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
* model name: Album
|
|
|
|
* model instance properties: userId of the album, friends, shared
|
|
|
|
* methods
|
|
|
|
* app and/or user ids/roles
|
2013-07-01 18:51:28 +00:00
|
|
|
** loggedIn
|
|
|
|
** roles
|
|
|
|
** userId
|
|
|
|
** appId
|
|
|
|
** none
|
|
|
|
** everyone
|
|
|
|
** relations: owner/friend/granted
|
2013-06-26 23:25:51 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
Class level permissions, for example, Album
|
2013-07-01 18:51:28 +00:00
|
|
|
* model name: Album
|
|
|
|
* methods
|
2013-07-15 21:07:17 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
URL/Route level permissions
|
2013-07-18 18:44:25 +00:00
|
|
|
* url pattern
|
|
|
|
* application id
|
|
|
|
* ip addresses
|
|
|
|
* http headers
|
2013-07-15 21:07:17 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
Map to oAuth 2.0 scopes
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
var loopback = require('../loopback');
|
2013-11-20 21:31:30 +00:00
|
|
|
var async = require('async');
|
|
|
|
var assert = require('assert');
|
2013-12-12 03:15:19 +00:00
|
|
|
var debug = require('debug')('loopback:security:acl');
|
2013-11-20 21:31:30 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var ctx = require('./access-context');
|
|
|
|
var AccessContext = ctx.AccessContext;
|
|
|
|
var Principal = ctx.Principal;
|
|
|
|
var AccessRequest = ctx.AccessRequest;
|
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
var role = require('./role');
|
|
|
|
var Role = role.Role;
|
2013-11-10 06:22:16 +00:00
|
|
|
|
2013-11-12 06:16:51 +00:00
|
|
|
/**
|
2013-12-11 07:33:57 +00:00
|
|
|
* System grants permissions to principals (users/applications, can be grouped
|
|
|
|
* into roles).
|
2013-11-12 06:16:51 +00:00
|
|
|
*
|
2013-12-11 07:33:57 +00:00
|
|
|
* Protected resource: the model data and operations
|
|
|
|
* (model/property/method/relation/…)
|
2013-11-12 06:16:51 +00:00
|
|
|
*
|
2013-12-11 07:33:57 +00:00
|
|
|
* For a given principal, such as client application and/or user, is it allowed
|
|
|
|
* to access (read/write/execute)
|
2013-11-12 06:16:51 +00:00
|
|
|
* the protected resource?
|
|
|
|
*/
|
2013-07-18 18:44:25 +00:00
|
|
|
var ACLSchema = {
|
2013-11-10 06:22:16 +00:00
|
|
|
model: String, // The name of the model
|
|
|
|
property: String, // The name of the property, method, scope, or relation
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the access type - READ/WRITE/EXEC
|
|
|
|
*/
|
|
|
|
accessType: String,
|
|
|
|
|
|
|
|
/**
|
2013-12-11 07:33:57 +00:00
|
|
|
* ALARM - Generate an alarm, in a system dependent way, the access specified
|
|
|
|
* in the permissions component of the ACL entry.
|
2013-11-10 06:22:16 +00:00
|
|
|
* ALLOW - Explicitly grants access to the resource.
|
2013-12-11 07:33:57 +00:00
|
|
|
* AUDIT - Log, in a system dependent way, the access specified in the
|
|
|
|
* permissions component of the ACL entry.
|
2013-11-10 06:22:16 +00:00
|
|
|
* DENY - Explicitly denies access to the resource.
|
|
|
|
*/
|
|
|
|
permission: String,
|
|
|
|
/**
|
|
|
|
* Type of the principal - Application/User/Role
|
|
|
|
*/
|
|
|
|
principalType: String,
|
|
|
|
/**
|
|
|
|
* Id of the principal - such as appId, userId or roleId
|
|
|
|
*/
|
|
|
|
principalId: String
|
2013-10-28 17:44:05 +00:00
|
|
|
};
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/**
|
|
|
|
* A Model for access control meta data.
|
|
|
|
*
|
|
|
|
* @header ACL
|
|
|
|
* @class
|
|
|
|
* @inherits Model
|
|
|
|
*/
|
|
|
|
|
2013-11-04 21:19:02 +00:00
|
|
|
var ACL = loopback.createModel('ACL', ACLSchema);
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.ALL = AccessContext.ALL;
|
|
|
|
|
|
|
|
ACL.DEFAULT = AccessContext.DEFAULT; // Not specified
|
|
|
|
ACL.ALLOW = AccessContext.ALLOW; // Allow
|
|
|
|
ACL.ALARM = AccessContext.ALARM; // Warn - send an alarm
|
|
|
|
ACL.AUDIT = AccessContext.AUDIT; // Audit - record the access
|
|
|
|
ACL.DENY = AccessContext.DENY; // Deny
|
|
|
|
|
|
|
|
ACL.READ = AccessContext.READ; // Read operation
|
|
|
|
ACL.WRITE = AccessContext.WRITE; // Write operation
|
|
|
|
ACL.EXECUTE = AccessContext.EXECUTE; // Execute operation
|
|
|
|
|
|
|
|
ACL.USER = Principal.USER;
|
|
|
|
ACL.APP = ACL.APPLICATION = Principal.APPLICATION;
|
|
|
|
ACL.ROLE = Principal.ROLE;
|
|
|
|
ACL.SCOPE = Principal.SCOPE;
|
2013-11-10 06:22:16 +00:00
|
|
|
|
2013-12-09 23:26:53 +00:00
|
|
|
/**
|
|
|
|
* Calculate the matching score for the given rule and request
|
2013-12-12 00:03:48 +00:00
|
|
|
* @param {ACL} rule The ACL entry
|
|
|
|
* @param {AccessRequest} req The request
|
2013-12-09 23:26:53 +00:00
|
|
|
* @returns {number}
|
|
|
|
*/
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.getMatchingScore = function getMatchingScore(rule, req) {
|
2013-12-09 23:26:53 +00:00
|
|
|
var props = ['model', 'property', 'accessType'];
|
|
|
|
var score = 0;
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2013-12-09 23:26:53 +00:00
|
|
|
for (var i = 0; i < props.length; i++) {
|
|
|
|
// Shift the score by 4 for each of the properties as the weight
|
|
|
|
score = score * 4;
|
|
|
|
var val1 = rule[props[i]] || ACL.ALL;
|
|
|
|
var val2 = req[props[i]] || ACL.ALL;
|
2014-06-02 20:41:14 +00:00
|
|
|
var isMatchingMethodName = props[i] === 'property' && req.methodNames.indexOf(val1) !== -1;
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2014-06-02 20:41:14 +00:00
|
|
|
if (val1 === val2 || isMatchingMethodName) {
|
2013-12-09 23:26:53 +00:00
|
|
|
// Exact match
|
|
|
|
score += 3;
|
|
|
|
} else if (val1 === ACL.ALL) {
|
|
|
|
// Wildcard match
|
|
|
|
score += 2;
|
|
|
|
} else if (val2 === ACL.ALL) {
|
|
|
|
// Doesn't match at all
|
|
|
|
score += 1;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2014-03-19 22:09:20 +00:00
|
|
|
|
2014-03-19 23:24:50 +00:00
|
|
|
// Weigh against the principal type into 4 levels
|
|
|
|
// - user level (explicitly allow/deny a given user)
|
|
|
|
// - app level (explicitly allow/deny a given app)
|
|
|
|
// - role level (role based authorization)
|
|
|
|
// - other
|
|
|
|
// user > app > role > ...
|
2014-03-19 22:09:20 +00:00
|
|
|
score = score * 4;
|
|
|
|
switch(rule.principalType) {
|
|
|
|
case ACL.USER:
|
|
|
|
score += 4;
|
|
|
|
break;
|
|
|
|
case ACL.APP:
|
|
|
|
score += 3;
|
|
|
|
break;
|
|
|
|
case ACL.ROLE:
|
|
|
|
score += 2;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
score +=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weigh against the roles
|
2014-03-19 23:24:50 +00:00
|
|
|
// everyone < authenticated/unauthenticated < related < owner < ...
|
2014-03-19 22:09:20 +00:00
|
|
|
score = score * 8;
|
|
|
|
if(rule.principalType === ACL.ROLE) {
|
|
|
|
switch(rule.principalId) {
|
|
|
|
case Role.OWNER:
|
|
|
|
score += 4;
|
|
|
|
break;
|
|
|
|
case Role.RELATED:
|
|
|
|
score += 3;
|
|
|
|
break;
|
|
|
|
case Role.AUTHENTICATED:
|
|
|
|
case Role.UNAUTHENTICATED:
|
|
|
|
score += 2;
|
|
|
|
break;
|
|
|
|
case Role.EVERYONE:
|
|
|
|
score += 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
score += 5;
|
|
|
|
}
|
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
score = score * 4;
|
2013-12-12 00:03:48 +00:00
|
|
|
score += AccessContext.permissionOrder[rule.permission || ACL.ALLOW] - 1;
|
2013-12-09 23:26:53 +00:00
|
|
|
return score;
|
2013-12-12 00:03:48 +00:00
|
|
|
};
|
2013-11-12 18:10:32 +00:00
|
|
|
|
2014-06-02 20:41:14 +00:00
|
|
|
/**
|
|
|
|
* Get matching score for the given `AccessRequest`.
|
|
|
|
* @param {AccessRequest} req The request
|
|
|
|
* @returns {Number} score
|
|
|
|
*/
|
|
|
|
|
|
|
|
ACL.prototype.score = function(req) {
|
|
|
|
return this.constructor.getMatchingScore(this, req);
|
|
|
|
}
|
|
|
|
|
2013-11-15 17:41:26 +00:00
|
|
|
/*!
|
|
|
|
* Resolve permission from the ACLs
|
2013-12-09 23:26:53 +00:00
|
|
|
* @param {Object[]) acls The list of ACLs
|
|
|
|
* @param {Object} req The request
|
2013-12-20 01:49:47 +00:00
|
|
|
* @returns {AccessRequest} result The effective ACL
|
2013-11-15 17:41:26 +00:00
|
|
|
*/
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.resolvePermission = function resolvePermission(acls, req) {
|
2014-03-19 22:09:20 +00:00
|
|
|
if(!(req instanceof AccessRequest)) {
|
|
|
|
req = new AccessRequest(req);
|
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
// Sort by the matching score in descending order
|
|
|
|
acls = acls.sort(function (rule1, rule2) {
|
2013-12-12 00:03:48 +00:00
|
|
|
return ACL.getMatchingScore(rule2, req) - ACL.getMatchingScore(rule1, req);
|
2013-12-09 23:26:53 +00:00
|
|
|
});
|
|
|
|
var permission = ACL.DEFAULT;
|
|
|
|
var score = 0;
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2013-12-09 23:26:53 +00:00
|
|
|
for (var i = 0; i < acls.length; i++) {
|
2013-12-12 00:03:48 +00:00
|
|
|
score = ACL.getMatchingScore(acls[i], req);
|
2013-12-09 23:26:53 +00:00
|
|
|
if (score < 0) {
|
2014-06-02 20:41:14 +00:00
|
|
|
// the highest scored ACL did not match
|
2013-12-09 23:26:53 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
if (!req.isWildcard()) {
|
2013-12-09 23:26:53 +00:00
|
|
|
// We should stop from the first match for non-wildcard
|
2014-06-02 20:41:14 +00:00
|
|
|
permission = acls[i].permission;
|
2013-12-09 23:26:53 +00:00
|
|
|
break;
|
|
|
|
} else {
|
2014-05-31 02:29:30 +00:00
|
|
|
if(req.exactlyMatches(acls[i])) {
|
2014-06-02 20:41:14 +00:00
|
|
|
permission = acls[i].permission;
|
2013-12-09 23:26:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// For wildcard match, find the strongest permission
|
2013-12-12 00:03:48 +00:00
|
|
|
if(AccessContext.permissionOrder[acls[i].permission]
|
|
|
|
> AccessContext.permissionOrder[permission]) {
|
2014-06-02 20:41:14 +00:00
|
|
|
permission = acls[i].permission;
|
2013-11-15 17:41:26 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
}
|
2013-12-11 05:49:18 +00:00
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
if(debug.enabled) {
|
|
|
|
debug('The following ACLs were searched: ');
|
|
|
|
acls.forEach(function(acl) {
|
|
|
|
acl.debug();
|
2014-06-02 20:41:14 +00:00
|
|
|
debug('with score:', acl.score(req));
|
2014-05-31 02:29:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var res = new AccessRequest(req.model, req.property, req.accessType,
|
|
|
|
permission || ACL.DEFAULT);
|
|
|
|
return res;
|
|
|
|
};
|
2013-11-15 17:41:26 +00:00
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
/*!
|
2013-12-11 07:33:57 +00:00
|
|
|
* Get the static ACLs from the model definition
|
2013-11-20 21:31:30 +00:00
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {String} property The property/method/relation name
|
|
|
|
*
|
2013-12-09 23:26:53 +00:00
|
|
|
* @return {Object[]} An array of ACLs
|
2013-11-12 06:16:51 +00:00
|
|
|
*/
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.getStaticACLs = function getStaticACLs(model, property) {
|
2013-11-20 21:31:30 +00:00
|
|
|
var modelClass = loopback.getModel(model);
|
2013-11-15 17:41:26 +00:00
|
|
|
var staticACLs = [];
|
2013-11-20 21:31:30 +00:00
|
|
|
if (modelClass && modelClass.settings.acls) {
|
|
|
|
modelClass.settings.acls.forEach(function (acl) {
|
2013-12-12 00:03:48 +00:00
|
|
|
staticACLs.push(new ACL({
|
2013-11-20 21:31:30 +00:00
|
|
|
model: model,
|
|
|
|
property: acl.property || ACL.ALL,
|
|
|
|
principalType: acl.principalType,
|
|
|
|
principalId: acl.principalId, // TODO: Should it be a name?
|
2014-06-02 20:41:14 +00:00
|
|
|
accessType: acl.accessType || ACL.ALL,
|
2013-11-20 21:31:30 +00:00
|
|
|
permission: acl.permission
|
2013-12-12 00:03:48 +00:00
|
|
|
}));
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
var prop = modelClass &&
|
|
|
|
(modelClass.definition.properties[property] // regular property
|
2013-11-15 18:08:49 +00:00
|
|
|
|| (modelClass._scopeMeta && modelClass._scopeMeta[property]) // relation/scope
|
|
|
|
|| modelClass[property] // static method
|
|
|
|
|| modelClass.prototype[property]); // prototype method
|
2013-11-20 21:31:30 +00:00
|
|
|
if (prop && prop.acls) {
|
|
|
|
prop.acls.forEach(function (acl) {
|
2013-12-12 00:03:48 +00:00
|
|
|
staticACLs.push(new ACL({
|
2013-11-20 21:31:30 +00:00
|
|
|
model: modelClass.modelName,
|
|
|
|
property: property,
|
|
|
|
principalType: acl.principalType,
|
|
|
|
principalId: acl.principalId,
|
|
|
|
accessType: acl.accessType,
|
|
|
|
permission: acl.permission
|
2013-12-12 00:03:48 +00:00
|
|
|
}));
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
2013-11-15 17:41:26 +00:00
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
return staticACLs;
|
2013-12-12 00:03:48 +00:00
|
|
|
};
|
2013-11-15 17:41:26 +00:00
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
/**
|
|
|
|
* Check if the given principal is allowed to access the model/property
|
|
|
|
* @param {String} principalType The principal type
|
|
|
|
* @param {String} principalId The principal id
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {String} property The property/method/relation name
|
|
|
|
* @param {String} accessType The access type
|
|
|
|
* @param {Function} callback The callback function
|
|
|
|
*
|
|
|
|
* @callback callback
|
|
|
|
* @param {String|Error} err The error object
|
2013-12-20 01:49:47 +00:00
|
|
|
* @param {AccessRequest} result The access permission
|
2013-11-20 21:31:30 +00:00
|
|
|
*/
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.checkPermission = function checkPermission(principalType, principalId,
|
|
|
|
model, property, accessType,
|
|
|
|
callback) {
|
2014-04-29 01:34:48 +00:00
|
|
|
if(principalId !== null && principalId !== undefined && (typeof principalId !== 'string') ) {
|
2014-04-29 12:10:44 +00:00
|
|
|
principalId = principalId.toString();
|
2014-04-29 01:34:48 +00:00
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
property = property || ACL.ALL;
|
|
|
|
var propertyQuery = (property === ACL.ALL) ? undefined : {inq: [property, ACL.ALL]};
|
|
|
|
accessType = accessType || ACL.ALL;
|
|
|
|
var accessTypeQuery = (accessType === ACL.ALL) ? undefined : {inq: [accessType, ACL.ALL]};
|
2013-12-09 23:26:53 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var req = new AccessRequest(model, property, accessType);
|
2013-12-09 23:26:53 +00:00
|
|
|
|
2014-01-16 16:50:50 +00:00
|
|
|
var acls = this.getStaticACLs(model, property);
|
2013-12-09 23:26:53 +00:00
|
|
|
|
2014-01-16 16:50:50 +00:00
|
|
|
var resolved = this.resolvePermission(acls, req);
|
2013-12-09 23:26:53 +00:00
|
|
|
|
|
|
|
if(resolved && resolved.permission === ACL.DENY) {
|
2013-12-17 02:12:13 +00:00
|
|
|
debug('Permission denied by statically resolved permission');
|
|
|
|
debug(' Resolved Permission: %j', resolved);
|
2013-11-20 21:31:30 +00:00
|
|
|
process.nextTick(function() {
|
2013-12-09 23:26:53 +00:00
|
|
|
callback && callback(null, resolved);
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
2013-11-15 17:41:26 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-16 16:50:50 +00:00
|
|
|
var self = this;
|
|
|
|
this.find({where: {principalType: principalType, principalId: principalId,
|
2013-11-14 01:14:13 +00:00
|
|
|
model: model, property: propertyQuery, accessType: accessTypeQuery}},
|
2013-12-09 23:26:53 +00:00
|
|
|
function (err, dynACLs) {
|
2013-11-12 06:16:51 +00:00
|
|
|
if (err) {
|
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
acls = acls.concat(dynACLs);
|
2014-01-16 16:50:50 +00:00
|
|
|
resolved = self.resolvePermission(acls, req);
|
2013-12-09 23:26:53 +00:00
|
|
|
if(resolved && resolved.permission === ACL.DEFAULT) {
|
2013-11-20 21:31:30 +00:00
|
|
|
var modelClass = loopback.getModel(model);
|
2013-12-09 23:26:53 +00:00
|
|
|
resolved.permission = (modelClass && modelClass.settings.defaultPermission) || ACL.ALLOW;
|
2013-11-20 21:31:30 +00:00
|
|
|
}
|
2013-12-09 23:26:53 +00:00
|
|
|
callback && callback(null, resolved);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-12-17 02:12:13 +00:00
|
|
|
ACL.prototype.debug = function() {
|
|
|
|
if(debug.enabled) {
|
2014-01-16 16:50:50 +00:00
|
|
|
debug('---ACL---');
|
2013-12-17 02:12:13 +00:00
|
|
|
debug('model %s', this.model);
|
|
|
|
debug('property %s', this.property);
|
|
|
|
debug('principalType %s', this.principalType);
|
|
|
|
debug('principalId %s', this.principalId);
|
|
|
|
debug('accessType %s', this.accessType);
|
|
|
|
debug('permission %s', this.permission);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
/**
|
|
|
|
* Check if the request has the permission to access
|
|
|
|
* @param {Object} context
|
2013-12-09 23:26:53 +00:00
|
|
|
* @property {Object[]} principals An array of principals
|
|
|
|
* @property {String|Model} model The model name or model class
|
|
|
|
* @property {*} id The model instance id
|
|
|
|
* @property {String} property The property/method/relation name
|
|
|
|
* @property {String} accessType The access type
|
2013-11-20 21:31:30 +00:00
|
|
|
* @param {Function} callback
|
|
|
|
*/
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2014-04-21 18:13:03 +00:00
|
|
|
ACL.checkAccessForContext = function (context, callback) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if(!(context instanceof AccessContext)) {
|
|
|
|
context = new AccessContext(context);
|
|
|
|
}
|
2013-11-15 04:19:46 +00:00
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
var model = context.model;
|
|
|
|
var property = context.property;
|
|
|
|
var accessType = context.accessType;
|
2014-05-31 02:29:30 +00:00
|
|
|
var modelName = context.modelName;
|
2013-11-20 21:31:30 +00:00
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
var methodNames = context.methodNames;
|
|
|
|
var propertyQuery = (property === ACL.ALL) ? undefined : {inq: methodNames.concat([ACL.ALL])};
|
2013-11-20 21:31:30 +00:00
|
|
|
var accessTypeQuery = (accessType === ACL.ALL) ? undefined : {inq: [accessType, ACL.ALL]};
|
|
|
|
|
2014-06-02 20:41:14 +00:00
|
|
|
var req = new AccessRequest(modelName, property, accessType, ACL.DEFAULT, methodNames);
|
2013-12-09 23:26:53 +00:00
|
|
|
|
|
|
|
var effectiveACLs = [];
|
2014-01-16 16:50:50 +00:00
|
|
|
var staticACLs = this.getStaticACLs(model.modelName, property);
|
2013-11-20 21:31:30 +00:00
|
|
|
|
2014-01-16 16:50:50 +00:00
|
|
|
var self = this;
|
2014-01-23 22:26:45 +00:00
|
|
|
var roleModel = loopback.getModelByType(Role);
|
2014-01-16 16:50:50 +00:00
|
|
|
this.find({where: {model: model.modelName, property: propertyQuery,
|
2013-12-11 07:33:57 +00:00
|
|
|
accessType: accessTypeQuery}}, function (err, acls) {
|
2013-11-20 21:31:30 +00:00
|
|
|
if (err) {
|
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var inRoleTasks = [];
|
2013-12-11 05:49:18 +00:00
|
|
|
|
|
|
|
acls = acls.concat(staticACLs);
|
|
|
|
|
2013-11-20 21:31:30 +00:00
|
|
|
acls.forEach(function (acl) {
|
2013-12-12 00:03:48 +00:00
|
|
|
// Check exact matches
|
|
|
|
for (var i = 0; i < context.principals.length; i++) {
|
|
|
|
var p = context.principals[i];
|
|
|
|
if (p.type === acl.principalType
|
|
|
|
&& String(p.id) === String(acl.principalId)) {
|
2013-11-20 21:31:30 +00:00
|
|
|
effectiveACLs.push(acl);
|
2013-12-12 00:03:48 +00:00
|
|
|
return;
|
2013-11-20 21:31:30 +00:00
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check role matches
|
|
|
|
if (acl.principalType === ACL.ROLE) {
|
|
|
|
inRoleTasks.push(function (done) {
|
2014-01-23 22:26:45 +00:00
|
|
|
roleModel.isInRole(acl.principalId, context,
|
2013-12-12 00:03:48 +00:00
|
|
|
function (err, inRole) {
|
|
|
|
if (!err && inRole) {
|
|
|
|
effectiveACLs.push(acl);
|
|
|
|
}
|
|
|
|
done(err, acl);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
async.parallel(inRoleTasks, function (err, results) {
|
|
|
|
if(err) {
|
|
|
|
callback && callback(err, null);
|
|
|
|
return;
|
|
|
|
}
|
2014-01-16 16:50:50 +00:00
|
|
|
var resolved = self.resolvePermission(effectiveACLs, req);
|
2014-01-16 23:05:10 +00:00
|
|
|
if(resolved && resolved.permission === ACL.DEFAULT) {
|
|
|
|
resolved.permission = (model && model.settings.defaultPermission) || ACL.ALLOW;
|
|
|
|
}
|
2014-05-31 02:29:30 +00:00
|
|
|
debug('---Resolved---');
|
|
|
|
resolved.debug();
|
2013-12-09 23:26:53 +00:00
|
|
|
callback && callback(null, resolved);
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given access token can invoke the method
|
|
|
|
* @param {AccessToken} token The access token
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {*} modelId The model id
|
|
|
|
* @param {String} method The method name
|
2013-12-20 01:49:47 +00:00
|
|
|
* @end
|
|
|
|
* @callback {Function} callback
|
2013-11-20 21:31:30 +00:00
|
|
|
* @param {String|Error} err The error object
|
|
|
|
* @param {Boolean} allowed is the request allowed
|
|
|
|
*/
|
2013-12-12 00:03:48 +00:00
|
|
|
ACL.checkAccessForToken = function (token, model, modelId, method, callback) {
|
2013-11-20 21:31:30 +00:00
|
|
|
assert(token, 'Access token is required');
|
2013-12-07 01:04:47 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
var context = new AccessContext({
|
|
|
|
accessToken: token,
|
2013-11-20 21:31:30 +00:00
|
|
|
model: model,
|
|
|
|
property: method,
|
2013-12-12 00:03:48 +00:00
|
|
|
method: method,
|
|
|
|
modelId: modelId
|
|
|
|
});
|
2013-12-11 05:49:18 +00:00
|
|
|
|
2014-04-21 18:13:03 +00:00
|
|
|
this.checkAccessForContext(context, function (err, access) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if (err) {
|
2013-11-20 21:31:30 +00:00
|
|
|
callback && callback(err);
|
|
|
|
return;
|
|
|
|
}
|
2013-11-15 04:19:46 +00:00
|
|
|
callback && callback(null, access.permission !== ACL.DENY);
|
2013-11-20 21:31:30 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-01-23 22:46:02 +00:00
|
|
|
/*!
|
|
|
|
* Schema for Scope which represents the permissions that are granted to client
|
|
|
|
* applications by the resource owner
|
|
|
|
*/
|
|
|
|
var ScopeSchema = {
|
|
|
|
name: {type: String, required: true},
|
|
|
|
description: String
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Resource owner grants/delegates permissions to client applications
|
|
|
|
*
|
|
|
|
* For a protected resource, does the client application have the authorization
|
|
|
|
* from the resource owner (user or system)?
|
|
|
|
*
|
|
|
|
* Scope has many resource access entries
|
|
|
|
* @class
|
|
|
|
*/
|
|
|
|
var Scope = loopback.createModel('Scope', ScopeSchema);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the given scope is allowed to access the model/property
|
|
|
|
* @param {String} scope The scope name
|
|
|
|
* @param {String} model The model name
|
|
|
|
* @param {String} property The property/method/relation name
|
|
|
|
* @param {String} accessType The access type
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {String|Error} err The error object
|
|
|
|
* @param {AccessRequest} result The access permission
|
|
|
|
*/
|
|
|
|
Scope.checkPermission = function (scope, model, property, accessType, callback) {
|
|
|
|
this.findOne({where: {name: scope}}, function (err, scope) {
|
|
|
|
if (err) {
|
|
|
|
callback && callback(err);
|
|
|
|
} else {
|
|
|
|
var aclModel = loopback.getModelByType(ACL);
|
|
|
|
aclModel.checkPermission(ACL.SCOPE, scope.id, model, property, accessType, callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-11-12 18:10:32 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
module.exports.ACL = ACL;
|
|
|
|
module.exports.Scope = Scope;
|