From ef890d5f2639d7642720651f5f7daa9ef94d6813 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 13 Oct 2014 10:46:55 +0200 Subject: [PATCH] models: move Scope def into its own files --- common/models/acl.js | 43 ---------------------------------------- common/models/scope.js | 39 ++++++++++++++++++++++++++++++++++++ common/models/scope.json | 14 +++++++++++++ docs.json | 1 + lib/builtin-models.js | 5 ++++- 5 files changed, 58 insertions(+), 44 deletions(-) create mode 100644 common/models/scope.js create mode 100644 common/models/scope.json diff --git a/common/models/acl.js b/common/models/acl.js index 64f38219..0b210ad3 100644 --- a/common/models/acl.js +++ b/common/models/acl.js @@ -468,47 +468,4 @@ 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; diff --git a/common/models/scope.js b/common/models/scope.js new file mode 100644 index 00000000..4a96e4a3 --- /dev/null +++ b/common/models/scope.js @@ -0,0 +1,39 @@ +var assert = require('assert'); + +/** + * 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 Scope + */ + +module.exports = function(Scope) { + /** + * 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) { + var ACL = loopback.ACL; + assert(ACL, + 'ACL model must be defined before Scope.checkPermission is called'); + + 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); + } + }); + }; +}; diff --git a/common/models/scope.json b/common/models/scope.json new file mode 100644 index 00000000..7786c946 --- /dev/null +++ b/common/models/scope.json @@ -0,0 +1,14 @@ +{ + "name": "Scope", + "description": [ + "Schema for Scope which represents the permissions that are granted", + "to client applications by the resource owner" + ], + "properties": { + "name": { + "type": "string", + "required": true + }, + "description": "string" + } +} diff --git a/docs.json b/docs.json index 71f47537..b554576a 100644 --- a/docs.json +++ b/docs.json @@ -16,6 +16,7 @@ { "title": "Built-in models", "depth": 2 }, "common/models/access-token.js", "common/models/acl.js", + "common/models/scope.js", "common/models/application.js", "common/models/email.js", "common/models/role.js", diff --git a/lib/builtin-models.js b/lib/builtin-models.js index 23a27de3..d8df2fb2 100644 --- a/lib/builtin-models.js +++ b/lib/builtin-models.js @@ -16,7 +16,10 @@ module.exports = function(loopback) { loopback.Role = require('../common/models/role').Role; loopback.RoleMapping = require('../common/models/role').RoleMapping; loopback.ACL = require('../common/models/acl').ACL; - loopback.Scope = require('../common/models/acl').Scope; + + loopback.Scope = createModel( + require('../common/models/scope.json'), + require('../common/models/scope.js')); loopback.User = createModel( require('../common/models/user.json'),