Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,37 @@ app.dataSources = app.datasources = {};
* Enable app wide authentication.
*/

app.enableAuth = function() {
app.enableAuth = function(options) {
var AUTH_MODELS = ['User', 'AccessToken', 'ACL', 'Role', 'RoleMapping'];

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going to happen if these models are sub-classed?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. This shortcut is intended only for the simple situations where you typically don't create subclasses. To keep the implementation simple, I can modify the condition determining whether a base model should be attached or not, and do not attach a base model if there is a subclassed model already attached?

From user perspective, if you create let's say Customer extending User:

  • If Customer is attached to a data source or the app, then the builtin User model is left unchanged.
  • If Customer is not attached to the app nor the datasource, then we attach the builtin User model for you.

Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good.


var remotes = this.remotes();
var app = this;

if (options && options.dataSource) {
var appModels = app.registry.modelBuilder.models;
AUTH_MODELS.forEach(function(m) {
var Model = app.registry.findModel(m);
if (!Model) {
throw new Error(
'Authentication requires model ' + m + ' to be defined.');
}

if (m.dataSource || m.app) return;

for (var name in appModels) {
var candidate = appModels[name];
var isSubclass = candidate.prototype instanceof Model;
var isAttached = !!candidate.dataSource || !!candidate.app;
if (isSubclass && isAttached) return;
}

app.model(Model, {
dataSource: options.dataSource,
public: m === 'User'
});
});
}

remotes.authorization = function(ctx, next) {
var method = ctx.method;
var req = ctx.req;
Expand Down
3 changes: 3 additions & 0 deletions lib/loopback.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ function createApplication(options) {
if (loopback.localRegistry || options && options.localRegistry === true) {
// setup the app registry
var registry = app.registry = new Registry();
if (options && options.loadBuiltinModels === true) {
require('./builtin-models')(registry);
}
} else {
app.registry = loopback.registry;
}
Expand Down
28 changes: 28 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,34 @@ describe('app', function() {
app.enableAuth();
expect(app.isAuthEnabled).to.equal(true);
});

it('auto-configures required models to provided dataSource', function() {
var AUTH_MODELS = ['User', 'ACL', 'AccessToken', 'Role', 'RoleMapping'];
var app = loopback({ localRegistry: true, loadBuiltinModels: true });
require('../lib/builtin-models')(app.registry);
var db = app.dataSource('db', { connector: 'memory' });

app.enableAuth({ dataSource: 'db' });

expect(Object.keys(app.models)).to.include.members(AUTH_MODELS);

AUTH_MODELS.forEach(function(m) {
var Model = app.models[m];
expect(Model.dataSource, m + '.dataSource').to.equal(db);
expect(Model.shared, m + '.shared').to.equal(m === 'User');
});
});

it('detects already configured subclass of a required model', function() {
var app = loopback({ localRegistry: true, loadBuiltinModels: true });
var db = app.dataSource('db', { connector: 'memory' });
var Customer = app.registry.createModel('Customer', {}, { base: 'User' });
app.model(Customer, { dataSource: 'db' });

app.enableAuth({ dataSource: 'db' });

expect(Object.keys(app.models)).to.not.include('User');
});
});

describe.onServer('app.get(\'/\', loopback.status())', function() {
Expand Down
18 changes: 18 additions & 0 deletions test/loopback.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ describe('loopback', function() {
});
});

describe('loopback(options)', function() {
it('supports localRegistry:true', function() {
var app = loopback({ localRegistry: true });
expect(app.registry).to.not.equal(loopback.registry);
});

it('does not load builtin models into the local registry', function() {
var app = loopback({ localRegistry: true });
expect(app.registry.findModel('User')).to.equal(undefined);
});

it('supports loadBuiltinModels:true', function() {
var app = loopback({ localRegistry: true, loadBuiltinModels: true });
expect(app.registry.findModel('User'))
.to.have.property('modelName', 'User');
});
});

describe('loopback.createDataSource(options)', function() {
it('Create a data source with a connector.', function() {
var dataSource = loopback.createDataSource({
Expand Down