diff --git a/lib/loopback.js b/lib/loopback.js index e424a358..d5596a7a 100644 --- a/lib/loopback.js +++ b/lib/loopback.js @@ -217,3 +217,4 @@ loopback.Model = require('./models/model'); loopback.Email = require('./models/email'); loopback.User = require('./models/user'); loopback.Session = require('./models/session'); +loopback.Application = require('./models/application'); diff --git a/lib/models/application.js b/lib/models/application.js index 2d2bf598..cc423d25 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -1,10 +1,11 @@ +var loopback = require('../loopback'); var assert = require('assert'); // Authentication schemes var AuthenticationSchemeSchema = { scheme: String, // local, facebook, google, twitter, linkedin, github credential: Object // Scheme-specific credentials -} +}; var APNSSettingSchema = { pushOptions: {type: { @@ -27,13 +28,13 @@ var PushNotificationSettingSchema = { platform: {type: String, required: true}, // apns, gcm, mpns // configuration: {type: Object} // platform-specific configurations apns: APNSSettingSchema -} +}; /** * Data model for Application */ var ApplicationSchema = { - + id: {type: String, id: true, generated: true}, // Basic information name: {type: String, required: true}, // The name description: String, // The description @@ -59,7 +60,7 @@ var ApplicationSchema = { masterKey: String, // Push notification - pushSettings: [PushNotificationSettingSchema], + pushSettings: PushNotificationSettingSchema, // User Authentication authenticationEnabled: {type: Boolean, default: true}, @@ -90,111 +91,108 @@ function generateKey(hmacKey, algorithm, encoding) { return hmac.digest('base64'); } -module.exports = function (dataSource) { - dataSource = dataSource || new require('loopback-datasource-juggler').ModelBuilder(); +// var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema); +// ApplicationSchema.authenticationSchemes = [AuthenticationScheme]; - // var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema); - // ApplicationSchema.authenticationSchemes = [AuthenticationScheme]; +// var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema); +// ApplicationSchema.pushSettings = [PushNotificationSetting]; - // var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema); - // ApplicationSchema.pushSettings = [PushNotificationSetting]; +var Application = loopback.createModel('Application', ApplicationSchema); - var Application = dataSource.define('Application', ApplicationSchema); +// Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'}); +// Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'}); - // Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'}); - // Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'}); +Application.beforeCreate = function (next) { + // console.trace(); + var app = this; + // use data argument to update object + app.created = app.modified = new Date(); + app.id = generateKey('id', 'sha1'); + app.clientKey = generateKey('client'); + app.javaScriptKey = generateKey('javaScript'); + app.restApiKey = generateKey('restApi'); + app.windowsKey = generateKey('windows'); + app.masterKey = generateKey('master'); + next(); +}; - Application.beforeCreate = function (next) { - // console.trace(); - var app = this; - // use data argument to update object - app.created = app.modified = new Date(); - app.id = generateKey('id', 'sha1'); - app.clientKey = generateKey('client'); - app.javaScriptKey = generateKey('javaScript'); - app.restApiKey = generateKey('restApi'); - app.windowsKey = generateKey('windows'); - app.masterKey = generateKey('master'); - next(); - }; +/** + * Register a new application + * @param owner Owner's user id + * @param name Name of the application + * @param options Other options + * @param cb Callback function + */ +Application.register = function (owner, name, options, cb) { + assert(owner, 'owner is required'); + assert(name, 'name is required'); - /** - * Register a new application - * @param owner Owner's user id - * @param name Name of the application - * @param options Other options - * @param cb Callback function - */ - Application.register = function (owner, name, options, cb) { - assert(owner, 'owner is required'); - assert(name, 'name is required'); - - if(typeof options === 'function' && !cb) { - cb = options; - options = {}; + if(typeof options === 'function' && !cb) { + cb = options; + options = {}; + } + var props = {owner: owner, name: name}; + for(var p in options) { + if(!(p in props)) { + props[p] = options[p]; } - var props = {owner: owner, name: name}; - for(var p in options) { - if(!(p in props)) { - props[p] = options[p]; - } + } + Application.create(props, cb); +}; + +/** + * Reset keys for the application instance + * @param cb + */ +Application.prototype.resetKeys = function(cb) { + this.clientKey = generateKey('client'); + this.javaScriptKey = generateKey('javaScript'); + this.restApiKey = generateKey('restApi'); + this.windowsKey = generateKey('windows'); + this.masterKey = generateKey('master'); + this.modified = new Date(); + this.save(cb); +}; + +/** + * Reset keys for a given application by the appId + * @param appId + * @param cb + */ +Application.resetKeys = function(appId, cb) { + Application.findById(appId, function(err, app) { + if(err) { + cb && cb(err, app); + return; } - Application.create(props, cb); - } + app.resetKeys(cb); + }); +}; - /** - * Reset keys for the application instance - * @param cb - */ - Application.prototype.resetKeys = function(cb) { - this.clientKey = generateKey('client'); - this.javaScriptKey = generateKey('javaScript'); - this.restApiKey = generateKey('restApi'); - this.windowsKey = generateKey('windows'); - this.masterKey = generateKey('master'); - this.modified = new Date(); - this.save(cb); - } - - /** - * Reset keys for a given application by the appId - * @param appId - * @param cb - */ - Application.resetKeys = function(appId, cb) { - Application.findById(appId, function(err, app) { - if(err) { - cb && cb(err, app); - return; - } - app.resetKeys(cb); - }); - } - - /** - * - * @param appId - * @param key - * @param cb - */ - Application.authenticate = function(appId, key, cb) { - Application.findById(appId, function(err, app) { - if(err || !app) { - cb && cb(err, null); - return; - } - var matched = null; - ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'].forEach(function(k) { +/** + * + * @param appId + * @param key + * @param cb + */ +Application.authenticate = function(appId, key, cb) { + Application.findById(appId, function(err, app) { + if(err || !app) { + cb && cb(err, null); + return; + } + var matched = null; + ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'].forEach(function(k) { if(app[k] === key) { matched = k; } - }); - cb && cb(null, matched); - }); - } + }); + cb && cb(null, matched); + }); +}; + +module.exports = Application; - return Application; -} diff --git a/test/model.application.test.js b/test/model.application.test.js index f1d8cfce..4ff6dbdb 100644 --- a/test/model.application.test.js +++ b/test/model.application.test.js @@ -1,10 +1,10 @@ -var models = require('../lib/models'); var loopback = require(('../')); var assert = require('assert'); var dataSource = loopback.createDataSource('db', {connector: loopback.Memory}); -var Application = models.Application(dataSource); +var Application = loopback.Application; +Application.attachTo(dataSource); describe('Application', function () { var registeredApp = null;