From 224b500c3ac8e44a7d8110b380f799d24b162164 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Thu, 23 Jan 2014 14:46:02 -0800 Subject: [PATCH] Fix the Scope reference to models --- lib/models/acl.js | 82 +++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/lib/models/acl.js b/lib/models/acl.js index 1bb5b921..883661e7 100644 --- a/lib/models/acl.js +++ b/lib/models/acl.js @@ -44,47 +44,6 @@ var AccessRequest = ctx.AccessRequest; var role = require('./role'); var Role = role.Role; -/*! - * 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) { - Scope.findOne({where: {name: scope}}, function (err, scope) { - if (err) { - callback && callback(err); - } else { - ACL.checkPermission(ACL.SCOPE, scope.id, model, property, accessType, callback); - } - }); -}; - /** * System grants permissions to principals (users/applications, can be grouped * into roles). @@ -452,6 +411,47 @@ ACL.checkAccessForToken = function (token, model, modelId, method, callback) { }); }; +/*! + * 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); + } + }); +}; module.exports.ACL = ACL; module.exports.Scope = Scope;