diff --git a/install/data/defaults.json b/install/data/defaults.json index 97c92bddf5..cd92bca23c 100644 --- a/install/data/defaults.json +++ b/install/data/defaults.json @@ -4,6 +4,7 @@ "loginDays": 14, "loginSeconds": 0, "loginAttempts": 5, + "lockoutDuration": 60, "adminReloginDuration": 60, "postDelay": 10, "initialPostDelay": 10, diff --git a/src/meta/configs.js b/src/meta/configs.js index 5b6392d8fc..b9f695a8ce 100644 --- a/src/meta/configs.js +++ b/src/meta/configs.js @@ -19,18 +19,24 @@ Meta.config = {}; function deserialize(config) { var deserialized = {}; Object.keys(config).forEach(function (key) { - if (typeof config[key] !== 'string') { - deserialized[key] = config[key]; - return; - } - - var number = parseFloat(config[key]); - if (!isNaN(number) && isFinite(config[key])) { - deserialized[key] = number; + const defaultType = typeof defaults[key]; + const type = typeof config[key]; + + if (defaultType === 'string' && type === 'number') { + deserialized[key] = String(config[key]); + } else if (defaultType === 'number' && type === 'string') { + const number = parseFloat(config[key]); + if (!isNaN(number) && isFinite(config[key])) { + deserialized[key] = number; + } else { + deserialized[key] = defaults[key]; + } } else if (config[key] === 'true') { deserialized[key] = true; } else if (config[key] === 'false') { deserialized[key] = false; + } else if (config[key] === null) { + deserialized[key] = defaults[key]; } else { deserialized[key] = config[key]; } diff --git a/src/user/auth.js b/src/user/auth.js index 53d0b60427..63e45e8334 100644 --- a/src/user/auth.js +++ b/src/user/auth.js @@ -26,15 +26,15 @@ module.exports = function (User) { } db.increment('loginAttempts:' + uid, next); }, - function (attemps, next) { - if (attemps <= meta.config.loginAttempts) { + function (attempts, next) { + if (attempts <= meta.config.loginAttempts) { return db.pexpire('loginAttempts:' + uid, 1000 * 60 * 60, callback); } // Lock out the account db.set('lockout:' + uid, '', next); }, function (next) { - var duration = 1000 * 60 * (meta.config.lockoutDuration || 60); + var duration = 1000 * 60 * meta.config.lockoutDuration; db.delete('loginAttempts:' + uid); db.pexpire('lockout:' + uid, duration); diff --git a/test/meta.js b/test/meta.js index c5024acb17..90e7d577f6 100644 --- a/test/meta.js +++ b/test/meta.js @@ -111,6 +111,72 @@ describe('meta', function () { }); }); + it('should get the correct type and default value', function (done) { + meta.configs.set('loginAttempts', '', function (err) { + assert.ifError(err); + meta.configs.get('loginAttempts', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, 5); + done(); + }); + }); + }); + + it('should get the correct type and correct value', function (done) { + meta.configs.set('loginAttempts', '0', function (err) { + assert.ifError(err); + meta.configs.get('loginAttempts', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, 0); + done(); + }); + }); + }); + + it('should get the correct value', function (done) { + meta.configs.set('title', 123, function (err) { + assert.ifError(err); + meta.configs.get('title', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, '123'); + done(); + }); + }); + }); + + it('should get the correct value', function (done) { + meta.configs.set('title', 0, function (err) { + assert.ifError(err); + meta.configs.get('title', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, '0'); + done(); + }); + }); + }); + + it('should get the correct value', function (done) { + meta.configs.set('title', '', function (err) { + assert.ifError(err); + meta.configs.get('title', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, ''); + done(); + }); + }); + }); + + it('should use default value if value is null', function (done) { + meta.configs.set('teaserPost', null, function (err) { + assert.ifError(err); + meta.configs.get('teaserPost', function (err, value) { + assert.ifError(err); + assert.strictEqual(value, 'last-reply'); + done(); + }); + }); + }); + it('should fail if field is invalid', function (done) { meta.configs.set('', 'someValue', function (err) { assert.equal(err.message, '[[error:invalid-data]]'); @@ -185,7 +251,7 @@ describe('meta', function () { assert.ifError(err); meta.configs.getFields(['stringField'], function (err, data) { assert.ifError(err); - assert.strictEqual(data.stringField, 123); + assert.strictEqual(data.stringField, '123'); done(); }); });