From e9c86163aaf2a6524e605fbc03c37cce4bf72103 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 13 Oct 2014 11:20:04 +0200 Subject: [PATCH] models: move RoleMapping def into its own files --- common/models/role-mapping.js | 70 +++++++++++++++++++++++++ common/models/role-mapping.json | 23 +++++++++ common/models/role.js | 91 ++------------------------------- docs.json | 1 + lib/builtin-models.js | 5 +- 5 files changed, 101 insertions(+), 89 deletions(-) create mode 100644 common/models/role-mapping.js create mode 100644 common/models/role-mapping.json diff --git a/common/models/role-mapping.js b/common/models/role-mapping.js new file mode 100644 index 00000000..e2f0982d --- /dev/null +++ b/common/models/role-mapping.js @@ -0,0 +1,70 @@ +/** + * The `RoleMapping` model extends from the built in `loopback.Model` type. + * + * @property {String} id Generated ID. + * @property {String} name Name of the role. + * @property {String} Description Text description. + * + * @class RoleMapping + * @inherits {PersistedModel} + */ + +module.exports = function(RoleMapping) { +// Principal types + RoleMapping.USER = 'USER'; + RoleMapping.APP = RoleMapping.APPLICATION = 'APP'; + RoleMapping.ROLE = 'ROLE'; + + /** + * Get the application principal + * @callback {Function} callback + * @param {Error} err + * @param {Application} application + */ + RoleMapping.prototype.application = function (callback) { + if (this.principalType === RoleMapping.APPLICATION) { + var applicationModel = this.constructor.Application + || loopback.getModelByType(loopback.Application); + applicationModel.findById(this.principalId, callback); + } else { + process.nextTick(function () { + callback && callback(null, null); + }); + } + }; + + /** + * Get the user principal + * @callback {Function} callback + * @param {Error} err + * @param {User} user + */ + RoleMapping.prototype.user = function (callback) { + if (this.principalType === RoleMapping.USER) { + var userModel = this.constructor.User + || loopback.getModelByType(loopback.User); + userModel.findById(this.principalId, callback); + } else { + process.nextTick(function () { + callback && callback(null, null); + }); + } + }; + + /** + * Get the child role principal + * @callback {Function} callback + * @param {Error} err + * @param {User} childUser + */ + RoleMapping.prototype.childRole = function (callback) { + if (this.principalType === RoleMapping.ROLE) { + var roleModel = this.constructor.Role || loopback.getModelByType(Role); + roleModel.findById(this.principalId, callback); + } else { + process.nextTick(function () { + callback && callback(null, null); + }); + } + }; +}; diff --git a/common/models/role-mapping.json b/common/models/role-mapping.json new file mode 100644 index 00000000..592f2906 --- /dev/null +++ b/common/models/role-mapping.json @@ -0,0 +1,23 @@ +{ + "name": "RoleMapping", + "description": "Map principals to roles", + "properties": { + "id": { + "type": "string", + "id": true, + "generated": true + }, + "principalType": { + "type": "string", + "description": "The principal type, such as user, application, or role" + }, + "principalId": "string" + }, + "relations": { + "role": { + "type": "belongsTo", + "model": "Role", + "foreignKey": "roleId" + } + } +} diff --git a/common/models/role.js b/common/models/role.js index 1a588040..48fce120 100644 --- a/common/models/role.js +++ b/common/models/role.js @@ -16,94 +16,6 @@ var RoleSchema = { modified: {type: Date, default: Date} }; -/*! - * Map principals to roles - */ -var RoleMappingSchema = { - id: {type: String, id: true, generated: true}, // Id - // roleId: String, // The role id, to be injected by the belongsTo relation - principalType: String, // The principal type, such as user, application, or role - principalId: String // The principal id -}; - -/** - * The `RoleMapping` model extends from the built in `loopback.Model` type. - * - * @class - * @property {String} id Generated ID. - * @property {String} name Name of the role. - * @property {String} Description Text description. - * @inherits {Model} - */ - -var RoleMapping = loopback.createModel('RoleMapping', RoleMappingSchema, { - relations: { - role: { - type: 'belongsTo', - model: 'Role', - foreignKey: 'roleId' - } - } -}); - -// Principal types -RoleMapping.USER = 'USER'; -RoleMapping.APP = RoleMapping.APPLICATION = 'APP'; -RoleMapping.ROLE = 'ROLE'; - -/** - * Get the application principal - * @callback {Function} callback - * @param {Error} err - * @param {Application} application - */ -RoleMapping.prototype.application = function (callback) { - if (this.principalType === RoleMapping.APPLICATION) { - var applicationModel = this.constructor.Application - || loopback.getModelByType(loopback.Application); - applicationModel.findById(this.principalId, callback); - } else { - process.nextTick(function () { - callback && callback(null, null); - }); - } -}; - -/** - * Get the user principal - * @callback {Function} callback - * @param {Error} err - * @param {User} user - */ -RoleMapping.prototype.user = function (callback) { - if (this.principalType === RoleMapping.USER) { - var userModel = this.constructor.User - || loopback.getModelByType(loopback.User); - userModel.findById(this.principalId, callback); - } else { - process.nextTick(function () { - callback && callback(null, null); - }); - } -}; - -/** - * Get the child role principal - * @callback {Function} callback - * @param {Error} err - * @param {User} childUser - */ -RoleMapping.prototype.childRole = function (callback) { - if (this.principalType === RoleMapping.ROLE) { - var roleModel = this.constructor.Role || loopback.getModelByType(Role); - roleModel.findById(this.principalId, callback); - } else { - process.nextTick(function () { - callback && callback(null, null); - }); - } -}; - /** * The Role Model * @class @@ -118,6 +30,9 @@ var Role = loopback.createModel('Role', RoleSchema, { } }); +var RoleMapping = loopback.RoleMapping; +assert(RoleMapping, 'RoleMapping model must be defined before Role model'); + // Set up the connection to users/applications/roles once the model Role.once('dataSourceAttached', function () { var roleMappingModel = this.RoleMapping || loopback.getModelByType(RoleMapping); diff --git a/docs.json b/docs.json index b554576a..5971d085 100644 --- a/docs.json +++ b/docs.json @@ -19,6 +19,7 @@ "common/models/scope.js", "common/models/application.js", "common/models/email.js", + "common/models/role-mapping.js", "common/models/role.js", "common/models/user.js", "common/models/change.js" diff --git a/lib/builtin-models.js b/lib/builtin-models.js index e2cf4054..5f5e531a 100644 --- a/lib/builtin-models.js +++ b/lib/builtin-models.js @@ -13,8 +13,11 @@ module.exports = function(loopback) { require('../common/models/access-token.json'), require('../common/models/access-token.js')); + loopback.RoleMapping = createModel( + require('../common/models/role-mapping.json'), + require('../common/models/role-mapping.js')); + loopback.Role = require('../common/models/role').Role; - loopback.RoleMapping = require('../common/models/role').RoleMapping; loopback.ACL = createModel( require('../common/models/acl.json'),