refactor setDBAuth to not use step

This commit is contained in:
Eneko Lakasta 2018-02-07 12:40:36 +01:00
parent 232ff1ba33
commit c1535b1a12

View File

@ -21,42 +21,35 @@ module.exports = PgConnection;
// @param callback function(err)
//
PgConnection.prototype.setDBAuth = function(username, params, callback) {
var self = this;
var user_params = {};
var auth_user = global.environment.postgres_auth_user;
var auth_pass = global.environment.postgres_auth_pass;
step(
function getId() {
self.metadataBackend.getUserId(username, this);
},
function(err, user_id) {
assert.ifError(err);
user_params.user_id = user_id;
var dbuser = _.template(auth_user, user_params);
_.extend(params, {dbuser:dbuser});
// skip looking up user_password if postgres_auth_pass
// doesn't contain the "user_password" label
if (!auth_pass || ! auth_pass.match(/\buser_password\b/) ) {
return null;
}
self.metadataBackend.getUserDBPass(username, this);
},
function(err, user_password) {
assert.ifError(err);
user_params.user_password = user_password;
if ( auth_pass ) {
var dbpass = _.template(auth_pass, user_params);
_.extend(params, {dbpassword:dbpass});
}
return true;
},
function finish(err) {
callback(err);
this.metadataBackend.getUserId(username, (err, userId) => {
if (err) {
return callback(err);
}
);
const userParams = {
user_id: userId
};
_.extend(params, { dbuser: _.template(auth_user, userParams) });
if (!auth_pass || !auth_pass.match(/\buser_password\b/)) {
return callback();
}
this.metadataBackend.getUserDBPass(username, (err, userPassword) => {
if (err) {
return callback(err);
}
userParams.user_password = userPassword;
_.extend(params, { dbpassword: _.template(auth_pass, userParams) });
callback();
});
});
};
// Set db connection parameters to those for the given username