Make sure User/AccessToken relations are set up by default

User.login assumes the relation User.accessTokens exists
This commit is contained in:
Raymond Feng 2014-02-14 10:31:30 -08:00
parent 1dc4a3fa25
commit 46b579dc4a
5 changed files with 34 additions and 19 deletions

View File

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

View File

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

View File

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

View File

@ -16,6 +16,8 @@ describe('role model', function () {
beforeEach(function() {
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);
Role.attachTo(ds);
RoleMapping.attachTo(ds);

View File

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