Skip to content

Commit

Permalink
Change type for private blog settings
Browse files Browse the repository at this point in the history
refs TryGhost#5614 and TryGhost#5503

- update private blog type, including update to settings.edit
- switch order of populate settings & update fixtures + populate all settings

Private blog settings should not be returned by public endpoints
therefore they need a type which is not `blog` or `theme`.
`core` doesn't suit either, as those settings don't usually have UI
To resolve this, I created a new type `private` which can be used
for any setting which has a UI but should not be public data
  • Loading branch information
ErisDS authored and sebgie committed Aug 31, 2015
1 parent 1f073c6 commit e8b5464
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 18 deletions.
2 changes: 1 addition & 1 deletion core/client/app/routes/settings/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default AuthenticatedRoute.extend(styleBody, CurrentUserSettings, {
},

model: function () {
return this.store.find('setting', {type: 'blog,theme'}).then(function (records) {
return this.store.find('setting', {type: 'blog,theme,private'}).then(function (records) {
return records.get('firstObject');
});
},
Expand Down
14 changes: 8 additions & 6 deletions core/server/data/default-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@
},
"navigation": {
"defaultValue": "[{\"label\":\"Home\", \"url\":\"/\"}]"
},
"isPrivate": {
"defaultValue": "false"
},
"password": {
"defaultValue": ""
}
},
"theme": {
Expand All @@ -86,5 +80,13 @@
"installedApps": {
"defaultValue": "[]"
}
},
"private": {
"isPrivate": {
"defaultValue": "false"
},
"password": {
"defaultValue": ""
}
}
}
32 changes: 26 additions & 6 deletions core/server/data/fixtures/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ logInfo = function logInfo(message) {
* Changes an admin user to have the owner role
* @returns {Promise|*}
*/
convertAdminToOwner = function () {
convertAdminToOwner = function convertAdminToOwner() {
var adminUser;

return models.User.findOne({role: 'Administrator'}).then(function (user) {
Expand All @@ -56,7 +56,7 @@ convertAdminToOwner = function () {
* Creates the user fixture and gives it the owner role
* @returns {Promise|*}
*/
createOwner = function () {
createOwner = function createOwner() {
var user = fixtures.users[0];

return models.Role.findOne({name: 'Owner'}).then(function (ownerRole) {
Expand All @@ -68,7 +68,7 @@ createOwner = function () {
});
};

populate = function () {
populate = function populate() {
var ops = [],
relations = [],
Post = models.Post,
Expand Down Expand Up @@ -122,7 +122,7 @@ populate = function () {
* Note: At the moment this is pretty adhoc & untestable, in future it would be better to have a config based system.
* @returns {Promise|*}
*/
to003 = function () {
to003 = function to003() {
var ops = [],
upgradeOp,
Role = models.Role,
Expand Down Expand Up @@ -161,7 +161,7 @@ to003 = function () {
* Update ghost_foot to include a CDN of jquery if the DB is migrating from
* @return {Promise}
*/
to004 = function () {
to004 = function to004() {
var value,
ops = [],
upgradeOp,
Expand Down Expand Up @@ -196,6 +196,26 @@ to004 = function () {
});
ops.push(upgradeOp);

// change `type` for protected blog `isPrivate` setting
upgradeOp = models.Settings.findOne('isPrivate').then(function (setting) {
if (setting) {
logInfo('Update isPrivate setting');
return models.Settings.edit({key: 'isPrivate', type: 'private'}, options);
}
return Promise.resolve();
});
ops.push(upgradeOp);

// change `type` for protected blog `password` setting
upgradeOp = models.Settings.findOne('password').then(function (setting) {
if (setting) {
logInfo('Update password setting');
return models.Settings.edit({key: 'password', type: 'private'}, options);
}
return Promise.resolve();
});
ops.push(upgradeOp);

// Update ghost-admin client fixture
// ghost-admin should exist from 003 version
upgradeOp = models.Client.findOne({slug: fixtures.clients[0].slug}).then(function (client) {
Expand All @@ -219,7 +239,7 @@ to004 = function () {
return Promise.all(ops);
};

update = function (fromVersion, toVersion) {
update = function update(fromVersion, toVersion) {
var ops = [];

logInfo('Updating fixtures');
Expand Down
8 changes: 5 additions & 3 deletions core/server/data/migration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ logInfo = function logInfo(message) {
populateDefaultSettings = function populateDefaultSettings() {
// Initialise the default settings
logInfo('Populating default settings');
return models.Settings.populateDefault('databaseVersion').then(function () {
return models.Settings.populateDefaults().then(function () {
logInfo('Complete');
});
};
Expand Down Expand Up @@ -178,9 +178,11 @@ migrateUp = function (fromVersion, toVersion) {
return sequence(migrateOps);
}
}).then(function () {
return fixtures.update(fromVersion, toVersion);
}).then(function () {
// Ensure all of the current default settings are created (these are fixtures, so should be inserted first)
return populateDefaultSettings();
}).then(function () {
// Finally, run any updates to the fixtures, including default settings
return fixtures.update(fromVersion, toVersion);
});
};

Expand Down
2 changes: 1 addition & 1 deletion core/server/data/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var db = {
uuid: {type: 'string', maxlength: 36, nullable: false, validations: {isUUID: true}},
key: {type: 'string', maxlength: 150, nullable: false, unique: true},
value: {type: 'text', maxlength: 65535, nullable: true},
type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'core', validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin']]}},
type: {type: 'string', maxlength: 150, nullable: false, defaultTo: 'core', validations: {isIn: [['core', 'blog', 'theme', 'app', 'plugin', 'private']]}},
created_at: {type: 'dateTime', nullable: false},
created_by: {type: 'integer', nullable: false},
updated_at: {type: 'dateTime', nullable: true},
Expand Down
11 changes: 10 additions & 1 deletion core/server/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,17 @@ Settings = ghostBookshelf.Model.extend({
item = self.filterData(item);

return Settings.forge({key: item.key}).fetch(options).then(function then(setting) {
var saveData = {};

if (setting) {
return setting.save({value: item.value}, options);
if (item.hasOwnProperty('value')) {
saveData.value = item.value;
}
// Internal context can overwrite type (for fixture migrations)
if (options.context.internal && item.hasOwnProperty('type')) {
saveData.type = item.type;
}
return setting.save(saveData, options);
}

return Promise.reject(new errors.NotFoundError('Unable to find setting to update: ' + item.key));
Expand Down

0 comments on commit e8b5464

Please sign in to comment.