2013-06-25 15:43:15 +04:00
|
|
|
var Settings,
|
2013-09-23 02:20:08 +04:00
|
|
|
ghostBookshelf = require('./base'),
|
|
|
|
validator = ghostBookshelf.validator,
|
2013-09-24 14:46:30 +04:00
|
|
|
uuid = require('node-uuid'),
|
2014-02-05 12:40:30 +04:00
|
|
|
_ = require('lodash'),
|
2013-09-24 14:46:30 +04:00
|
|
|
errors = require('../errorHandling'),
|
|
|
|
when = require('when'),
|
2014-02-19 17:57:26 +04:00
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
defaultSettings;
|
|
|
|
|
|
|
|
// For neatness, the defaults file is split into categories.
|
|
|
|
// It's much easier for us to work with it as a single level
|
|
|
|
// instead of iterating those categories every time
|
|
|
|
function parseDefaultSettings() {
|
|
|
|
var defaultSettingsInCategories = require('../data/default-settings.json'),
|
|
|
|
defaultSettingsFlattened = {};
|
|
|
|
|
|
|
|
|
|
|
|
_.each(defaultSettingsInCategories, function (settings, categoryName) {
|
|
|
|
_.each(settings, function (setting, settingName) {
|
|
|
|
setting.type = categoryName;
|
|
|
|
setting.key = settingName;
|
|
|
|
defaultSettingsFlattened[settingName] = setting;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return defaultSettingsFlattened;
|
|
|
|
}
|
|
|
|
defaultSettings = parseDefaultSettings();
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
// Each setting is saved as a separate row in the database,
|
|
|
|
// but the overlying API treats them as a single key:value mapping
|
2013-09-23 02:20:08 +04:00
|
|
|
Settings = ghostBookshelf.Model.extend({
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
tableName: 'settings',
|
2013-08-25 14:49:31 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
defaults: function () {
|
|
|
|
return {
|
|
|
|
uuid: uuid.v4(),
|
2013-09-14 22:04:41 +04:00
|
|
|
type: 'core'
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-08-25 14:49:31 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
// Validate default settings using the validator module.
|
|
|
|
// Each validation's key is a name and its value is an array of options
|
|
|
|
// Use true (boolean) if options aren't applicable
|
|
|
|
//
|
|
|
|
// eg:
|
|
|
|
// validations: { isUrl: true, len: [20, 40] }
|
|
|
|
//
|
|
|
|
// will validate that a setting's length is a URL between 20 and 40 chars,
|
|
|
|
// available validators: https://github.com/chriso/node-validator#list-of-validation-methods
|
2013-08-25 14:49:31 +04:00
|
|
|
validate: function () {
|
2013-09-02 05:49:08 +04:00
|
|
|
validator.check(this.get('key'), "Setting key cannot be blank").notEmpty();
|
|
|
|
validator.check(this.get('type'), "Setting type cannot be blank").notEmpty();
|
|
|
|
|
|
|
|
var matchingDefault = defaultSettings[this.get('key')];
|
|
|
|
|
|
|
|
if (matchingDefault && matchingDefault.validations) {
|
|
|
|
_.each(matchingDefault.validations, function (validationOptions, validationName) {
|
|
|
|
var validation = validator.check(this.get('value'));
|
|
|
|
|
|
|
|
if (validationOptions === true) {
|
|
|
|
validationOptions = null;
|
|
|
|
}
|
|
|
|
if (typeof validationOptions !== 'array') {
|
|
|
|
validationOptions = [validationOptions];
|
|
|
|
}
|
|
|
|
|
|
|
|
// equivalent of validation.isSomething(option1, option2)
|
|
|
|
validation[validationName].apply(validation, validationOptions);
|
|
|
|
}, this);
|
|
|
|
}
|
2013-10-07 21:02:57 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
saving: function () {
|
|
|
|
|
2014-01-07 00:17:20 +04:00
|
|
|
// disabling sanitization until we can implement a better version
|
|
|
|
// All blog setting keys that need their values to be escaped.
|
|
|
|
// if (this.get('type') === 'blog' && _.contains(['title', 'description', 'email'], this.get('key'))) {
|
|
|
|
// this.set('value', this.sanitize('value'));
|
|
|
|
// }
|
2013-10-07 21:02:57 +04:00
|
|
|
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.prototype.saving.apply(this, arguments);
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-10-07 21:02:57 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, {
|
|
|
|
read: function (_key) {
|
|
|
|
// Allow for just passing the key instead of attributes
|
|
|
|
if (!_.isObject(_key)) {
|
|
|
|
_key = { key: _key };
|
2013-06-15 18:10:30 +04:00
|
|
|
}
|
2013-09-23 02:20:08 +04:00
|
|
|
return ghostBookshelf.Model.read.call(this, _key);
|
2013-06-25 15:43:15 +04:00
|
|
|
},
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-11-21 00:36:02 +04:00
|
|
|
edit: function (_data, t) {
|
2013-06-25 15:43:15 +04:00
|
|
|
var settings = this;
|
|
|
|
if (!Array.isArray(_data)) {
|
|
|
|
_data = [_data];
|
2013-06-08 09:03:55 +04:00
|
|
|
}
|
2013-06-25 15:43:15 +04:00
|
|
|
return when.map(_data, function (item) {
|
|
|
|
// Accept an array of models as input
|
|
|
|
if (item.toJSON) { item = item.toJSON(); }
|
2013-11-21 00:36:02 +04:00
|
|
|
return settings.forge({ key: item.key }).fetch({transacting: t}).then(function (setting) {
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
if (setting) {
|
2013-11-21 00:36:02 +04:00
|
|
|
return setting.set('value', item.value).save(null, {transacting: t});
|
2013-09-02 05:49:08 +04:00
|
|
|
}
|
2013-11-21 00:36:02 +04:00
|
|
|
return settings.forge({ key: item.key, value: item.value }).save(null, {transacting: t});
|
2013-09-02 05:49:08 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
}, errors.logAndThrowError);
|
|
|
|
});
|
2013-09-02 05:49:08 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
populateDefaults: function () {
|
|
|
|
return this.findAll().then(function (allSettings) {
|
|
|
|
var usedKeys = allSettings.models.map(function (setting) { return setting.get('key'); }),
|
|
|
|
insertOperations = [];
|
|
|
|
|
2013-09-02 05:49:08 +04:00
|
|
|
_.each(defaultSettings, function (defaultSetting, defaultSettingKey) {
|
|
|
|
var isMissingFromDB = usedKeys.indexOf(defaultSettingKey) === -1;
|
2013-09-15 15:11:47 +04:00
|
|
|
// Temporary code to deal with old databases with currentVersion settings
|
|
|
|
if (defaultSettingKey === 'databaseVersion' && usedKeys.indexOf('currentVersion') !== -1) {
|
|
|
|
isMissingFromDB = false;
|
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
if (isMissingFromDB) {
|
2013-09-15 01:39:31 +04:00
|
|
|
defaultSetting.value = defaultSetting.defaultValue;
|
2013-09-02 05:49:08 +04:00
|
|
|
insertOperations.push(Settings.forge(defaultSetting).save());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return when.all(insertOperations);
|
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
}
|
2013-09-02 05:49:08 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
});
|
2013-06-08 09:03:55 +04:00
|
|
|
|
2013-06-25 15:43:15 +04:00
|
|
|
module.exports = {
|
|
|
|
Settings: Settings
|
2013-09-02 05:49:08 +04:00
|
|
|
};
|