Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for setting registration fields from account schema #61

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
66 changes: 64 additions & 2 deletions lib/strategy/EnrichIntegrationFromRemoteConfigStrategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var async = require('async');
var extend = require('../helpers/clone-extend').extend;
var strings = require('../strings');
var util = require('util');

/**
* Retrieves Stormpath settings from the API service, and ensures the local
Expand Down Expand Up @@ -206,12 +207,72 @@ EnrichIntegrationFromRemoteConfigStrategy.prototype._enrichWithDirectoryPolicies

config.passwordPolicy = strength;

callback(null, null);
callback(null, directory);
}));
}));
}));
};

EnrichIntegrationFromRemoteConfigStrategy.prototype._enrichWithDirectoryAccountModel = function (client, config, directory, callback) {
if (!directory) {
return callback(null, null);
}

// Returns the callback immediately if there is an error.
// Continues processing if there isn't.
var stopIfError = function (process) {
return function (err, result) {
if (err) {
callback(err);
} else {
process(result);
}
};
};

var webConfig = config.web;

// Map the registration fields defined in the account schema.
directory.getAccountSchema(stopIfError(function (accountSchema) {
accountSchema.getFields(stopIfError(function (fields) {
var registrationFields = webConfig.register.form.fields;

fields.each(function (field, next) {
var fieldToConfigure = registrationFields[field.name];

if (fieldToConfigure) {
var localValue = registrationFields[field.name].required;
var remoteValue = field.required;

if (localValue === null) {
fieldToConfigure.required = remoteValue;
}

if (localValue === false && remoteValue === true){
console.warn(util.format(strings.ACCOUNT_SCHEMA_REMOTE_FIELD_REQUIRED_WARNING,directory.name,field.name));
fieldToConfigure.required = true;
}

if (localValue === true && remoteValue === false){
console.warn(util.format(strings.ACCOUNT_SCHEMA_LOCAL_FIELD_REQUIRED_WARNING,directory.name,field.name));
}
}

next();
}, function (err) {
// If a field is not configured, then don't require it.
for (var key in registrationFields) {
if (typeof registrationFields[key].required !== 'boolean') {
registrationFields[key].required = false;
}
}

callback(err, null);
});
}));
}));
};

EnrichIntegrationFromRemoteConfigStrategy.prototype.process = function (config, callback) {
var tasks = [];

Expand All @@ -228,7 +289,8 @@ EnrichIntegrationFromRemoteConfigStrategy.prototype.process = function (config,
this._enrichWithOAuthPolicy.bind(this),
this._enrichWithSocialProviders.bind(this, config),
this._resolveDirectoryHref.bind(this),
this._enrichWithDirectoryPolicies.bind(this, client, config)
this._enrichWithDirectoryPolicies.bind(this, client, config),
this._enrichWithDirectoryAccountModel.bind(this, client, config)
]);
}

Expand Down
2 changes: 2 additions & 0 deletions lib/strings.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions test/strategy/EnrichIntegrationFromRemoteConfigStrategyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,95 @@ describe('EnrichIntegrationFromRemoteConfigStrategy', function () {
});
});
});

describe('._enrichWithDirectoryAccountModel(client, config, directory, callback)', function() {
var testMethod;
var mockClient;
var mockConfig;
var mockDirectory;
var mockFields;
var sinonSandbox;

beforeEach(function () {
sinonSandbox = sinon.sandbox.create();

testMethod = EnrichIntegrationFromRemoteConfigStrategy.prototype._enrichWithDirectoryAccountModel;

mockClient = {};

mockConfig = {
web: {
register: {
form: {
fields: {
givenName: {
enabled: true,
required: false
},
surname: {
enabled: true,
required: false
}
}
}
}
}
};

mockFields = {
items: [{
name: 'givenName',
required: true
}, {
name: 'surname',
required: true
}],
each: function (iterator, onComplete) {
var self = this;
this.items.forEach(function (item, index) {
iterator(item, function () {
if (onComplete && index === self.items.length - 1) {
onComplete();
}
});
});
}
};

mockDirectory = {
getAccountSchema: function (callback) {
callback(null, {
getFields: function (callback) {
callback(null, mockFields);
}
});
}
};
});

afterEach(function () {
sinonSandbox.restore();
});

describe('when givenName and surname registration fields in config are not required', function () {
describe('but remote accountSchema has fields set to required', function () {
it('should warn and set fields in config as required', function (done) {
var testFormFields = mockConfig.web.register.form.fields;
var consoleMock = sinonSandbox.mock(console);

consoleMock.expects('warn').twice();

testMethod(mockClient, mockConfig, mockDirectory, function (err) {
assert.equal(err, null);
assert.equal(testFormFields.givenName.required, true);
assert.equal(testFormFields.surname.required, true);

consoleMock.verify();

done();
});
});
});
});
});
});