Avoid unnecessary disk writes if user object has not changed, make updateUser async
This commit is contained in:
parent
085ede43df
commit
463a63aed3
@ -292,19 +292,15 @@ Client.prototype.setPassword = function(hash, callback) {
|
||||
client.manager.updateUser(client.name, {
|
||||
token: token,
|
||||
password: hash
|
||||
});
|
||||
|
||||
// re-read the hash off disk to ensure we use whatever is saved. this will
|
||||
// prevent situations where the password failed to save properly and so
|
||||
// a restart of the server would forget the change and use the old
|
||||
// password again.
|
||||
var user = client.manager.readUserConfig(client.name);
|
||||
if (user.password === hash) {
|
||||
client.config.password = hash;
|
||||
callback(true);
|
||||
} else {
|
||||
callback(false);
|
||||
}, function(err) {
|
||||
if (err) {
|
||||
log.error("Failed to update password of", client.name, err);
|
||||
return callback(false);
|
||||
}
|
||||
|
||||
client.config.password = hash;
|
||||
return callback(true);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -94,8 +94,8 @@ ClientManager.prototype.addUser = function(name, password) {
|
||||
return true;
|
||||
};
|
||||
|
||||
ClientManager.prototype.updateUser = function(name, opts) {
|
||||
var users = this.getUsers();
|
||||
ClientManager.prototype.updateUser = function(name, opts, callback) {
|
||||
const users = this.getUsers();
|
||||
if (users.indexOf(name) === -1) {
|
||||
return false;
|
||||
}
|
||||
@ -103,19 +103,25 @@ ClientManager.prototype.updateUser = function(name, opts) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var user = {};
|
||||
try {
|
||||
user = this.readUserConfig(name);
|
||||
let user = this.readUserConfig(name);
|
||||
const currentUser = JSON.stringify(user, null, "\t");
|
||||
_.assign(user, opts);
|
||||
fs.writeFileSync(
|
||||
Helper.getUserConfigPath(name),
|
||||
JSON.stringify(user, null, "\t")
|
||||
);
|
||||
} catch (e) {
|
||||
log.error("Failed to update user", e);
|
||||
return;
|
||||
const newUser = JSON.stringify(user, null, "\t");
|
||||
|
||||
// Do not touch the disk if object has not changed
|
||||
if (currentUser === newUser) {
|
||||
return callback ? callback() : true;
|
||||
}
|
||||
return true;
|
||||
|
||||
fs.writeFile(Helper.getUserConfigPath(name), newUser, (err) => {
|
||||
if (err) {
|
||||
log.error("Failed to update user", err);
|
||||
}
|
||||
|
||||
if (callback) {
|
||||
callback(err);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
ClientManager.prototype.readUserConfig = function(name) {
|
||||
|
@ -273,9 +273,7 @@ function localAuth(client, user, password, callback) {
|
||||
var hash = Helper.password.hash(password);
|
||||
|
||||
client.setPassword(hash, function(success) {
|
||||
if (!success) {
|
||||
log.error("Failed to update password of", client.name, "to match new security requirements");
|
||||
} else {
|
||||
if (success) {
|
||||
log.info("User", client.name, "logged in and their hashed password has been updated to match new security requirements");
|
||||
}
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user