2020-01-21 19:19:18 +00:00
|
|
|
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2019-10-07 09:45:34 +00:00
|
|
|
const assert = require('assert');
|
|
|
|
const loopback = require('./loopback');
|
|
|
|
const debug = require('debug')('loopback:security:access-context');
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2017-03-27 12:58:01 +00:00
|
|
|
const DEFAULT_SCOPES = ['DEFAULT'];
|
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
/**
|
|
|
|
* Access context represents the context for a request to access protected
|
|
|
|
* resources
|
|
|
|
*
|
2016-12-06 14:24:49 +00:00
|
|
|
* NOTE While the method expects an array of principals in the AccessContext instance/object,
|
|
|
|
* it also accepts a single principal defined with the following properties:
|
|
|
|
* ```js
|
|
|
|
* {
|
|
|
|
* // AccessContext instance/object
|
|
|
|
* // ..
|
|
|
|
* principalType: 'somePrincipalType', // APP, ROLE, USER, or custom user model name
|
|
|
|
* principalId: 'somePrincipalId',
|
|
|
|
* }
|
|
|
|
* ```
|
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* @class
|
2016-12-06 14:24:49 +00:00
|
|
|
* @options {AccessContext|Object} context An AccessContext instance or an object
|
2013-12-12 00:03:48 +00:00
|
|
|
* @property {Principal[]} principals An array of principals
|
|
|
|
* @property {Function} model The model class
|
|
|
|
* @property {String} modelName The model name
|
2016-12-06 14:24:49 +00:00
|
|
|
* @property {*} modelId The model id
|
2013-12-12 00:03:48 +00:00
|
|
|
* @property {String} property The model property/method/relation name
|
|
|
|
* @property {String} method The model method to be invoked
|
2016-12-06 14:24:49 +00:00
|
|
|
* @property {String} accessType The access type: READ, REPLICATE, WRITE, or EXECUTE.
|
|
|
|
* @property {AccessToken} accessToken The access token resolved for the request
|
|
|
|
* @property {RemotingContext} remotingContext The request's remoting context
|
|
|
|
* @property {Registry} registry The application or global registry
|
2013-12-12 00:03:48 +00:00
|
|
|
* @returns {AccessContext}
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function AccessContext(context) {
|
|
|
|
if (!(this instanceof AccessContext)) {
|
|
|
|
return new AccessContext(context);
|
|
|
|
}
|
|
|
|
context = context || {};
|
|
|
|
|
2016-11-21 20:51:43 +00:00
|
|
|
assert(context.registry,
|
|
|
|
'Application registry is mandatory in AccessContext but missing in provided context');
|
|
|
|
this.registry = context.registry;
|
2013-12-12 00:03:48 +00:00
|
|
|
this.principals = context.principals || [];
|
2019-10-07 09:45:34 +00:00
|
|
|
let model = context.model;
|
2016-11-21 20:51:43 +00:00
|
|
|
model = ('string' === typeof model) ? this.registry.getModel(model) : model;
|
2013-12-12 00:03:48 +00:00
|
|
|
this.model = model;
|
|
|
|
this.modelName = model && model.modelName;
|
|
|
|
|
|
|
|
this.modelId = context.id || context.modelId;
|
|
|
|
this.property = context.property || AccessContext.ALL;
|
|
|
|
|
|
|
|
this.method = context.method;
|
2014-05-31 02:29:30 +00:00
|
|
|
this.sharedMethod = context.sharedMethod;
|
|
|
|
this.sharedClass = this.sharedMethod && this.sharedMethod.sharedClass;
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.sharedMethod) {
|
2014-10-16 22:54:40 +00:00
|
|
|
this.methodNames = this.sharedMethod.aliases.concat([this.sharedMethod.name]);
|
2014-05-31 02:29:30 +00:00
|
|
|
} else {
|
|
|
|
this.methodNames = [];
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.sharedMethod) {
|
2014-10-16 22:54:40 +00:00
|
|
|
this.accessType = this.model._getAccessTypeForMethod(this.sharedMethod);
|
2014-06-02 20:41:14 +00:00
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
this.accessType = context.accessType || AccessContext.ALL;
|
2017-03-27 12:58:01 +00:00
|
|
|
|
2014-10-13 08:23:35 +00:00
|
|
|
assert(loopback.AccessToken,
|
|
|
|
'AccessToken model must be defined before AccessContext model');
|
|
|
|
this.accessToken = context.accessToken || loopback.AccessToken.ANONYMOUS;
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const principalType = context.principalType || Principal.USER;
|
|
|
|
const principalId = context.principalId || undefined;
|
|
|
|
const principalName = context.principalName || undefined;
|
2017-11-15 18:49:13 +00:00
|
|
|
if (principalId != null) {
|
2013-12-12 00:03:48 +00:00
|
|
|
this.addPrincipal(principalType, principalId, principalName);
|
|
|
|
}
|
|
|
|
|
2018-03-20 13:15:44 +00:00
|
|
|
const token = this.accessToken;
|
2013-12-12 00:03:48 +00:00
|
|
|
|
2017-11-15 18:49:13 +00:00
|
|
|
if (token.userId != null) {
|
2018-03-20 13:15:44 +00:00
|
|
|
this.addPrincipal(token.principalType || Principal.USER, token.userId);
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
2017-11-15 18:49:13 +00:00
|
|
|
if (token.appId != null) {
|
2013-12-12 00:03:48 +00:00
|
|
|
this.addPrincipal(Principal.APPLICATION, token.appId);
|
|
|
|
}
|
2014-08-08 05:19:27 +00:00
|
|
|
this.remotingContext = context.remotingContext;
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Define constant for the wildcard
|
|
|
|
AccessContext.ALL = '*';
|
|
|
|
|
|
|
|
// Define constants for access types
|
|
|
|
AccessContext.READ = 'READ'; // Read operation
|
2015-04-03 14:41:32 +00:00
|
|
|
AccessContext.REPLICATE = 'REPLICATE'; // Replicate (pull) changes
|
2013-12-12 00:03:48 +00:00
|
|
|
AccessContext.WRITE = 'WRITE'; // Write operation
|
|
|
|
AccessContext.EXECUTE = 'EXECUTE'; // Execute operation
|
|
|
|
|
|
|
|
AccessContext.DEFAULT = 'DEFAULT'; // Not specified
|
|
|
|
AccessContext.ALLOW = 'ALLOW'; // Allow
|
|
|
|
AccessContext.ALARM = 'ALARM'; // Warn - send an alarm
|
|
|
|
AccessContext.AUDIT = 'AUDIT'; // Audit - record the access
|
|
|
|
AccessContext.DENY = 'DENY'; // Deny
|
|
|
|
|
|
|
|
AccessContext.permissionOrder = {
|
|
|
|
DEFAULT: 0,
|
|
|
|
ALLOW: 1,
|
|
|
|
ALARM: 2,
|
|
|
|
AUDIT: 3,
|
2016-04-01 09:14:26 +00:00
|
|
|
DENY: 4,
|
2013-12-12 00:03:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a principal to the context
|
|
|
|
* @param {String} principalType The principal type
|
|
|
|
* @param {*} principalId The principal id
|
|
|
|
* @param {String} [principalName] The principal name
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
AccessContext.prototype.addPrincipal = function(principalType, principalId, principalName) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const principal = new Principal(principalType, principalId, principalName);
|
|
|
|
for (let i = 0; i < this.principals.length; i++) {
|
|
|
|
const p = this.principals[i];
|
2013-12-12 00:03:48 +00:00
|
|
|
if (p.equals(principal)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.principals.push(principal);
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getUserId = function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const user = this.getUser();
|
2017-02-03 20:53:27 +00:00
|
|
|
return user && user.id;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getUser = function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
const BaseUser = this.registry.getModel('User');
|
|
|
|
for (let i = 0; i < this.principals.length; i++) {
|
|
|
|
const p = this.principals[i];
|
|
|
|
const isBuiltinPrincipal = p.type === Principal.APP ||
|
2016-11-21 20:51:43 +00:00
|
|
|
p.type === Principal.ROLE ||
|
|
|
|
p.type == Principal.SCOPE;
|
|
|
|
if (isBuiltinPrincipal) continue;
|
|
|
|
|
|
|
|
// the principalType must either be 'USER'
|
2013-12-12 00:03:48 +00:00
|
|
|
if (p.type === Principal.USER) {
|
2017-02-03 20:53:27 +00:00
|
|
|
return {id: p.id, principalType: p.type};
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
2016-11-21 20:51:43 +00:00
|
|
|
|
|
|
|
// or permit to resolve a valid user model
|
2019-10-07 09:45:34 +00:00
|
|
|
const userModel = this.registry.findModel(p.type);
|
2016-11-21 20:51:43 +00:00
|
|
|
if (!userModel) continue;
|
|
|
|
if (userModel.prototype instanceof BaseUser) {
|
2017-02-03 20:53:27 +00:00
|
|
|
return {id: p.id, principalType: p.type};
|
2016-11-21 20:51:43 +00:00
|
|
|
}
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the application id
|
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getAppId = function() {
|
2019-10-07 09:45:34 +00:00
|
|
|
for (let i = 0; i < this.principals.length; i++) {
|
|
|
|
const p = this.principals[i];
|
2013-12-12 00:03:48 +00:00
|
|
|
if (p.type === Principal.APPLICATION) {
|
|
|
|
return p.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the access context has authenticated principals
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.isAuthenticated = function() {
|
2017-11-15 18:49:13 +00:00
|
|
|
return this.getUserId() != null || this.getAppId() != null;
|
2013-12-12 00:03:48 +00:00
|
|
|
};
|
|
|
|
|
2017-03-27 12:58:01 +00:00
|
|
|
/**
|
|
|
|
* Get the list of scopes required by the current access context.
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.getScopes = function() {
|
|
|
|
if (!this.sharedMethod)
|
|
|
|
return DEFAULT_SCOPES;
|
|
|
|
|
|
|
|
// For backwards compatibility, methods with no scopes defined
|
|
|
|
// are assigned a single "DEFAULT" scope
|
|
|
|
const methodLevel = this.sharedMethod.accessScopes || DEFAULT_SCOPES;
|
|
|
|
|
|
|
|
// TODO add model-level and app-level scopes
|
|
|
|
|
|
|
|
debug('--Context scopes of %s()--', this.sharedMethod.stringName);
|
|
|
|
debug(' method-level: %j', methodLevel);
|
|
|
|
|
|
|
|
return methodLevel;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the scope required by the remote method is allowed
|
|
|
|
* by the scopes granted to the requesting access token.
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
AccessContext.prototype.isScopeAllowed = function() {
|
|
|
|
if (!this.accessToken) return false;
|
|
|
|
|
|
|
|
// For backwards compatibility, tokens with no scopes are treated
|
|
|
|
// as if they have "DEFAULT" scope granted
|
|
|
|
const tokenScopes = this.accessToken.scopes || DEFAULT_SCOPES;
|
|
|
|
|
|
|
|
const resourceScopes = this.getScopes();
|
|
|
|
|
|
|
|
// Scope is allowed when at least one of token's scopes
|
|
|
|
// is found in method's (resource's) scopes.
|
|
|
|
return Array.isArray(tokenScopes) && Array.isArray(resourceScopes) &&
|
|
|
|
resourceScopes.some(s => tokenScopes.indexOf(s) !== -1);
|
|
|
|
};
|
|
|
|
|
2014-11-04 00:26:33 +00:00
|
|
|
/*!
|
2013-12-17 02:12:13 +00:00
|
|
|
* Print debug info for access context.
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessContext.prototype.debug = function() {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (debug.enabled) {
|
2013-12-17 02:12:13 +00:00
|
|
|
debug('---AccessContext---');
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.principals && this.principals.length) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('principals:');
|
2013-12-17 02:12:13 +00:00
|
|
|
this.principals.forEach(function(principal) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('principal: %j', principal);
|
2013-12-17 02:12:13 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
debug('principals: %j', this.principals);
|
|
|
|
}
|
|
|
|
debug('modelName %s', this.modelName);
|
|
|
|
debug('modelId %s', this.modelId);
|
|
|
|
debug('property %s', this.property);
|
|
|
|
debug('method %s', this.method);
|
|
|
|
debug('accessType %s', this.accessType);
|
2017-03-27 12:58:01 +00:00
|
|
|
debug('accessScopes %j', this.getScopes());
|
2014-10-30 20:49:47 +00:00
|
|
|
if (this.accessToken) {
|
2014-10-16 22:54:40 +00:00
|
|
|
debug('accessToken:');
|
2013-12-17 02:12:13 +00:00
|
|
|
debug(' id %j', this.accessToken.id);
|
|
|
|
debug(' ttl %j', this.accessToken.ttl);
|
2017-03-27 12:58:01 +00:00
|
|
|
debug(' scopes %j', this.accessToken.scopes || DEFAULT_SCOPES);
|
2013-12-17 02:12:13 +00:00
|
|
|
}
|
|
|
|
debug('getUserId() %s', this.getUserId());
|
|
|
|
debug('isAuthenticated() %s', this.isAuthenticated());
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2013-12-17 02:12:13 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
/**
|
|
|
|
* This class represents the abstract notion of a principal, which can be used
|
|
|
|
* to represent any entity, such as an individual, a corporation, and a login id
|
|
|
|
* @param {String} type The principal type
|
2016-11-21 20:51:43 +00:00
|
|
|
* @param {*} id The principal id
|
2013-12-12 00:03:48 +00:00
|
|
|
* @param {String} [name] The principal name
|
2016-11-21 20:51:43 +00:00
|
|
|
* @param {String} modelName The principal model name
|
2013-12-12 00:03:48 +00:00
|
|
|
* @returns {Principal}
|
2013-12-20 01:49:47 +00:00
|
|
|
* @class
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
|
|
|
function Principal(type, id, name) {
|
|
|
|
if (!(this instanceof Principal)) {
|
|
|
|
return new Principal(type, id, name);
|
|
|
|
}
|
|
|
|
this.type = type;
|
|
|
|
this.id = id;
|
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define constants for principal types
|
|
|
|
Principal.USER = 'USER';
|
|
|
|
Principal.APP = Principal.APPLICATION = 'APP';
|
|
|
|
Principal.ROLE = 'ROLE';
|
|
|
|
Principal.SCOPE = 'SCOPE';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compare if two principals are equal
|
2014-03-14 00:25:21 +00:00
|
|
|
* Returns true if argument principal is equal to this principal.
|
2014-10-30 20:49:47 +00:00
|
|
|
* @param {Object} p The other principal
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
Principal.prototype.equals = function(p) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if (p instanceof Principal) {
|
|
|
|
return this.type === p.type && String(this.id) === String(p.id);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2014-03-14 00:25:21 +00:00
|
|
|
* A request to access protected resources.
|
2016-12-06 14:24:49 +00:00
|
|
|
*
|
|
|
|
* The method can either be called with the following signature or with a single
|
|
|
|
* argument: an AccessRequest instance or an object containing all the required properties.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @options {String|AccessRequest|Object} model|req The model name,<br>
|
|
|
|
* or an AccessRequest instance/object.
|
|
|
|
* @param {String} property The property/method/relation name
|
2013-12-12 00:03:48 +00:00
|
|
|
* @param {String} accessType The access type
|
2014-03-14 00:25:21 +00:00
|
|
|
* @param {String} permission The requested permission
|
2016-12-06 14:24:49 +00:00
|
|
|
* @param {String[]} methodNames The names of involved methods
|
|
|
|
* @param {Registry} registry The application or global registry
|
2013-12-12 00:03:48 +00:00
|
|
|
* @returns {AccessRequest}
|
|
|
|
*/
|
2016-12-06 14:24:49 +00:00
|
|
|
function AccessRequest(model, property, accessType, permission, methodNames, registry) {
|
2013-12-12 00:03:48 +00:00
|
|
|
if (!(this instanceof AccessRequest)) {
|
2016-12-06 14:24:49 +00:00
|
|
|
return new AccessRequest(model, property, accessType, permission, methodNames);
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
2014-03-19 22:09:20 +00:00
|
|
|
if (arguments.length === 1 && typeof model === 'object') {
|
|
|
|
// The argument is an object that contains all required properties
|
2019-10-07 09:45:34 +00:00
|
|
|
const obj = model || {};
|
2014-03-19 22:09:20 +00:00
|
|
|
this.model = obj.model || AccessContext.ALL;
|
|
|
|
this.property = obj.property || AccessContext.ALL;
|
|
|
|
this.accessType = obj.accessType || AccessContext.ALL;
|
|
|
|
this.permission = obj.permission || AccessContext.DEFAULT;
|
2016-12-06 14:24:49 +00:00
|
|
|
this.methodNames = obj.methodNames || [];
|
|
|
|
this.registry = obj.registry;
|
2014-03-19 22:09:20 +00:00
|
|
|
} else {
|
|
|
|
this.model = model || AccessContext.ALL;
|
|
|
|
this.property = property || AccessContext.ALL;
|
|
|
|
this.accessType = accessType || AccessContext.ALL;
|
|
|
|
this.permission = permission || AccessContext.DEFAULT;
|
2014-05-31 02:29:30 +00:00
|
|
|
this.methodNames = methodNames || [];
|
2016-12-06 14:24:49 +00:00
|
|
|
this.registry = registry;
|
2013-12-17 02:12:13 +00:00
|
|
|
}
|
2016-12-06 14:24:49 +00:00
|
|
|
// do not create AccessRequest without a registry
|
|
|
|
assert(this.registry,
|
|
|
|
'Application registry is mandatory in AccessRequest but missing in provided argument(s)');
|
2013-12-12 00:03:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-06-02 20:41:14 +00:00
|
|
|
* Does the request contain any wildcards?
|
|
|
|
*
|
|
|
|
* @returns {Boolean}
|
2013-12-12 00:03:48 +00:00
|
|
|
*/
|
2014-10-30 20:49:47 +00:00
|
|
|
AccessRequest.prototype.isWildcard = function() {
|
2013-12-12 00:03:48 +00:00
|
|
|
return this.model === AccessContext.ALL ||
|
|
|
|
this.property === AccessContext.ALL ||
|
|
|
|
this.accessType === AccessContext.ALL;
|
|
|
|
};
|
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
/**
|
|
|
|
* Does the given `ACL` apply to this `AccessRequest`.
|
|
|
|
*
|
|
|
|
* @param {ACL} acl
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessRequest.prototype.exactlyMatches = function(acl) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const matchesModel = acl.model === this.model;
|
|
|
|
const matchesProperty = acl.property === this.property;
|
|
|
|
const matchesMethodName = this.methodNames.indexOf(acl.property) !== -1;
|
|
|
|
const matchesAccessType = acl.accessType === this.accessType;
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2014-10-30 20:49:47 +00:00
|
|
|
if (matchesModel && matchesAccessType) {
|
2014-05-31 02:29:30 +00:00
|
|
|
return matchesProperty || matchesMethodName;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2016-12-06 14:24:49 +00:00
|
|
|
/**
|
|
|
|
* Settle the accessRequest's permission if DEFAULT
|
|
|
|
* In most situations, the default permission can be resolved from the nested model
|
|
|
|
* config. An default permission can also be explicitly provided to override it or
|
|
|
|
* cope with AccessRequest instances without a nested model (e.g. model is '*')
|
|
|
|
*
|
|
|
|
* @param {String} defaultPermission (optional) the default permission to apply
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessRequest.prototype.settleDefaultPermission = function(defaultPermission) {
|
|
|
|
if (this.permission !== 'DEFAULT')
|
|
|
|
return;
|
|
|
|
|
2019-10-07 09:45:34 +00:00
|
|
|
const modelName = this.model;
|
2016-12-06 14:24:49 +00:00
|
|
|
if (!defaultPermission) {
|
2019-10-07 09:45:34 +00:00
|
|
|
const modelClass = this.registry.findModel(modelName);
|
2016-12-06 14:24:49 +00:00
|
|
|
defaultPermission = modelClass && modelClass.settings.defaultPermission;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.permission = defaultPermission || 'ALLOW';
|
|
|
|
};
|
|
|
|
|
2014-05-31 02:29:30 +00:00
|
|
|
/**
|
|
|
|
* Is the request for access allowed?
|
2014-10-16 22:54:40 +00:00
|
|
|
*
|
2014-05-31 02:29:30 +00:00
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
|
|
|
|
AccessRequest.prototype.isAllowed = function() {
|
2014-10-13 08:55:08 +00:00
|
|
|
return this.permission !== loopback.ACL.DENY;
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
|
|
|
AccessRequest.prototype.debug = function() {
|
2014-10-30 20:49:47 +00:00
|
|
|
if (debug.enabled) {
|
2014-05-31 02:29:30 +00:00
|
|
|
debug('---AccessRequest---');
|
|
|
|
debug(' model %s', this.model);
|
|
|
|
debug(' property %s', this.property);
|
|
|
|
debug(' accessType %s', this.accessType);
|
|
|
|
debug(' permission %s', this.permission);
|
|
|
|
debug(' isWildcard() %s', this.isWildcard());
|
|
|
|
debug(' isAllowed() %s', this.isAllowed());
|
|
|
|
}
|
2014-10-16 22:54:40 +00:00
|
|
|
};
|
2014-05-31 02:29:30 +00:00
|
|
|
|
2013-12-12 00:03:48 +00:00
|
|
|
module.exports.AccessContext = AccessContext;
|
|
|
|
module.exports.Principal = Principal;
|
|
|
|
module.exports.AccessRequest = AccessRequest;
|
2017-03-27 12:58:01 +00:00
|
|
|
module.exports.DEFAULT_SCOPES = DEFAULT_SCOPES;
|