Merge pull request #181 from strongloop/feature/user-token-rel

Make sure User/AccessToken relations are set up by default
This commit is contained in:
Raymond Feng 2014-02-14 11:36:17 -08:00
commit e56574fd13
5 changed files with 34 additions and 19 deletions

View File

@ -10,9 +10,7 @@ var express = require('express')
, proto = require('./application') , proto = require('./application')
, utils = require('express/node_modules/connect').utils , utils = require('express/node_modules/connect').utils
, DataSource = require('loopback-datasource-juggler').DataSource , DataSource = require('loopback-datasource-juggler').DataSource
, ModelBuilder = require('loopback-datasource-juggler').ModelBuilder , assert = require('assert');
, assert = require('assert')
, i8n = require('inflection');
/** /**
* `loopback` is the main entry for LoopBack core module. It provides static * `loopback` is the main entry for LoopBack core module. It provides static
@ -148,7 +146,7 @@ loopback.createModel = function (name, properties, options) {
} catch(e) {} } catch(e) {}
return model; return model;
} };
/** /**
* Add a remote method to a model. * Add a remote method to a model.
@ -164,7 +162,7 @@ loopback.remoteMethod = function (fn, options) {
}); });
} }
fn.http = fn.http || {verb: 'get'}; fn.http = fn.http || {verb: 'get'};
} };
/** /**
* Create a template helper. * Create a template helper.
@ -180,7 +178,7 @@ loopback.template = function (file) {
var templates = this._templates || (this._templates = {}); var templates = this._templates || (this._templates = {});
var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8')); var str = templates[file] || (templates[file] = fs.readFileSync(file, 'utf8'));
return ejs.compile(str); return ejs.compile(str);
} };
/** /**
* Get an in-memory data source. Use one if it already exists. * Get an in-memory data source. Use one if it already exists.
@ -202,7 +200,7 @@ loopback.memory = function (name) {
} }
return memory; return memory;
} };
/** /**
* Look up a model class by name from all models created by loopback.createModel() * Look up a model class by name from all models created by loopback.createModel()
@ -246,7 +244,7 @@ loopback.setDefaultDataSourceForType = function(type, dataSource) {
defaultDataSources[type] = dataSource; defaultDataSources[type] = dataSource;
return dataSource; return dataSource;
} };
/** /**
* Get the default `dataSource` for a given `type`. * Get the default `dataSource` for a given `type`.
@ -256,7 +254,7 @@ loopback.setDefaultDataSourceForType = function(type, dataSource) {
loopback.getDefaultDataSourceForType = function(type) { loopback.getDefaultDataSourceForType = function(type) {
return this.defaultDataSources && this.defaultDataSources[type]; return this.defaultDataSources && this.defaultDataSources[type];
} };
/** /**
* Attach any model that does not have a dataSource to * Attach any model that does not have a dataSource to
@ -275,7 +273,7 @@ loopback.autoAttach = function() {
loopback.autoAttachModel(ModelCtor); loopback.autoAttachModel(ModelCtor);
} }
}); });
} };
loopback.autoAttachModel = function(ModelCtor) { loopback.autoAttachModel = function(ModelCtor) {
if(ModelCtor.autoAttach) { if(ModelCtor.autoAttach) {
@ -287,7 +285,7 @@ loopback.autoAttachModel = function(ModelCtor) {
ModelCtor.attachTo(ds); ModelCtor.attachTo(ds);
} }
} };
/* /*
* Built in models / services * Built in models / services

View File

@ -53,7 +53,14 @@ var AccessToken = module.exports = Model.extend('AccessToken', properties, {
property: 'create', property: 'create',
permission: 'ALLOW' permission: 'ALLOW'
} }
] ],
relations: {
user: {
type: 'belongsTo',
model: 'User',
foreignKey: 'userId'
}
}
}); });
/** /**

View File

@ -53,7 +53,7 @@ var options = {
{ {
principalType: ACL.ROLE, principalType: ACL.ROLE,
principalId: Role.EVERYONE, principalId: Role.EVERYONE,
permission: ACL.DENY, permission: ACL.DENY
}, },
{ {
principalType: ACL.ROLE, principalType: ACL.ROLE,
@ -91,7 +91,14 @@ var options = {
permission: ACL.ALLOW, permission: ACL.ALLOW,
property: "updateAttributes" property: "updateAttributes"
} }
] ],
relations: {
accessTokens: {
type: 'hasMany',
model: 'AccessToken',
foreignKey: 'userId'
}
}
}; };
/** /**
@ -182,7 +189,7 @@ User.login = function (credentials, include, fn) {
fn(defaultError); fn(defaultError);
} }
}); });
} };
/** /**
* Logout a user with the given accessToken id. * Logout a user with the given accessToken id.

View File

@ -16,6 +16,8 @@ describe('role model', function () {
beforeEach(function() { beforeEach(function() {
ds = loopback.createDataSource({connector: 'memory'}); ds = loopback.createDataSource({connector: 'memory'});
// Re-attach the models so that they can have isolated store to avoid
// pollutions from other tests
User.attachTo(ds); User.attachTo(ds);
Role.attachTo(ds); Role.attachTo(ds);
RoleMapping.attachTo(ds); RoleMapping.attachTo(ds);

View File

@ -4,7 +4,7 @@ var passport = require('passport');
var MailConnector = require('../lib/connectors/mail'); var MailConnector = require('../lib/connectors/mail');
var userMemory = loopback.createDataSource({ var userMemory = loopback.createDataSource({
connector: loopback.Memory connector: 'memory'
}); });
describe('User', function(){ describe('User', function(){
@ -13,12 +13,13 @@ describe('User', function(){
User = loopback.User.extend('user'); User = loopback.User.extend('user');
User.email = loopback.Email.extend('email'); User.email = loopback.Email.extend('email');
loopback.autoAttach(); loopback.autoAttach();
// Update the AccessToken relation to use the subclass of User
AccessToken.belongsTo(User);
// allow many User.afterRemote's to be called // allow many User.afterRemote's to be called
User.setMaxListeners(0); User.setMaxListeners(0);
User.hasMany(AccessToken, {as: 'accessTokens', foreignKey: 'userId'});
AccessToken.belongsTo(User);
}); });
beforeEach(function (done) { beforeEach(function (done) {