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