improve handling of private settings

This commit is contained in:
Sacha Greif 2016-04-21 15:09:03 +09:00
parent 312a7b4f9e
commit 36a81acf8c
7 changed files with 144 additions and 20 deletions

View file

@ -11,6 +11,10 @@ function composer(props, onData) {
currentRoute: FlowRouter.current() currentRoute: FlowRouter.current()
} }
Meteor.call("settings.getJSON", (error, result) => {
Telescope.settings.settingsJSON = result;
});
if (!subscriptions.length || _.every(subscriptions, handle => handle.ready())) { if (!subscriptions.length || _.every(subscriptions, handle => handle.ready())) {
data.ready = true; data.ready = true;
onData(null, data); onData(null, data);

View file

@ -72,6 +72,7 @@ FormComponent.propTypes = {
name: React.PropTypes.string, name: React.PropTypes.string,
label: React.PropTypes.string, label: React.PropTypes.string,
value: React.PropTypes.any, value: React.PropTypes.any,
prefilledValue: React.PropTypes.any,
options: React.PropTypes.any, options: React.PropTypes.any,
control: React.PropTypes.any, control: React.PropTypes.any,
datatype: React.PropTypes.any, datatype: React.PropTypes.any,

View file

@ -251,6 +251,15 @@ class NovaForm extends Component{
// add value // add value
field.value = this.getDocument() && Utils.deepValue(this.getDocument(), fieldName) ? Utils.deepValue(this.getDocument(), fieldName) : ""; field.value = this.getDocument() && Utils.deepValue(this.getDocument(), fieldName) ? Utils.deepValue(this.getDocument(), fieldName) : "";
// replace value by prefilled value if value is empty
if (fieldSchema.autoform && fieldSchema.autoform.prefill) {
const prefilledValue = typeof fieldSchema.autoform.prefill === "function" ? fieldSchema.autoform.prefill.call(fieldSchema) : fieldSchema.autoform.prefill;
if (!!prefilledValue && !field.value) {
field.prefilledValue = prefilledValue;
field.value = prefilledValue;
}
}
// add options if they exist // add options if they exist
if (fieldSchema.autoform && fieldSchema.autoform.options) { if (fieldSchema.autoform && fieldSchema.autoform.options) {
field.options = typeof fieldSchema.autoform.options === "function" ? fieldSchema.autoform.options.call(fieldSchema) : fieldSchema.autoform.options; field.options = typeof fieldSchema.autoform.options === "function" ? fieldSchema.autoform.options.call(fieldSchema) : fieldSchema.autoform.options;

View file

@ -50,3 +50,31 @@ SimpleSchema.prototype.getProfileFields = function () {
}); });
return fields; return fields;
}; };
/**
* @summary Get a list of a schema's private fields
* @namespace Telescope.schemas
*/
Mongo.Collection.prototype.getPrivateFields = function () {
var schema = this.simpleSchema()._schema;
var fields = _.filter(_.keys(schema), function (fieldName) {
var field = schema[fieldName];
return field.publish !== true;
});
return fields;
};
/**
* @summary Get a list of a schema's public fields
* @namespace Telescope.schemas
*/
Mongo.Collection.prototype.getPublicFields = function () {
var schema = this.simpleSchema()._schema;
var fields = _.filter(_.keys(schema), function (fieldName) {
var field = schema[fieldName];
return field.publish === true;
});
return fields;
};

View file

@ -4,7 +4,12 @@
*/ */
const isInSettingsJSON = function () { const isInSettingsJSON = function () {
return typeof Telescope.settings.getFromJSON(this.name) !== "undefined"; // settings can either be in settings json's public, or in the special object we publish only for admins for private settings
return typeof Telescope.settings.getFromJSON(this.name) !== "undefined" || typeof Telescope.settings.settingsJSON[this.name] !== "undefined";
};
const getFromJSON = function () {
return Telescope.settings.getFromJSON(this.name) || Telescope.settings.settingsJSON[this.name];
}; };
Telescope.settings.collection = new Mongo.Collection("settings"); Telescope.settings.collection = new Mongo.Collection("settings");
@ -15,8 +20,10 @@ Telescope.settings.schema = new SimpleSchema({
optional: true, optional: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
publish: true,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
help: "Your site's title.", help: "Your site's title.",
group: "01_general" group: "01_general"
} }
@ -24,11 +31,13 @@ Telescope.settings.schema = new SimpleSchema({
siteUrl: { siteUrl: {
type: String, type: String,
optional: true, optional: true,
publish: true,
// regEx: SimpleSchema.RegEx.Url, // regEx: SimpleSchema.RegEx.Url,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "01_general", group: "01_general",
type: "bootstrap-url", type: "bootstrap-url",
help: 'Your site\'s URL (with trailing "/"). Will default to Meteor.absoluteUrl()' help: 'Your site\'s URL (with trailing "/"). Will default to Meteor.absoluteUrl()'
@ -37,20 +46,24 @@ Telescope.settings.schema = new SimpleSchema({
tagline: { tagline: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "01_general" group: "01_general"
} }
}, },
description: { description: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "01_general", group: "01_general",
rows: 5, rows: 5,
help: 'A short description used for SEO purposes.' help: 'A short description used for SEO purposes.'
@ -59,11 +72,13 @@ Telescope.settings.schema = new SimpleSchema({
siteImage: { siteImage: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
regEx: SimpleSchema.RegEx.Url, regEx: SimpleSchema.RegEx.Url,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "01_general", group: "01_general",
help: "URL to an image for the open graph image tag for all pages" help: "URL to an image for the open graph image tag for all pages"
} }
@ -71,11 +86,13 @@ Telescope.settings.schema = new SimpleSchema({
requireViewInvite: { requireViewInvite: {
type: Boolean, type: Boolean,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
control: "checkbox", control: "checkbox",
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: 'invites', group: 'invites',
leftLabel: 'Require View Invite' leftLabel: 'Require View Invite'
} }
@ -83,11 +100,13 @@ Telescope.settings.schema = new SimpleSchema({
requirePostInvite: { requirePostInvite: {
type: Boolean, type: Boolean,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
control: "checkbox", control: "checkbox",
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: 'invites', group: 'invites',
leftLabel: 'Require Post Invite' leftLabel: 'Require Post Invite'
} }
@ -95,11 +114,13 @@ Telescope.settings.schema = new SimpleSchema({
requirePostsApproval: { requirePostsApproval: {
type: Boolean, type: Boolean,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
control: "checkbox", control: "checkbox",
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "01_general", group: "01_general",
help: "Posts must be approved by admin", help: "Posts must be approved by admin",
leftLabel: "Require Posts Approval" leftLabel: "Require Posts Approval"
@ -108,11 +129,11 @@ Telescope.settings.schema = new SimpleSchema({
defaultEmail: { defaultEmail: {
type: String, type: String,
optional: true, optional: true,
private: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "06_email", group: "06_email",
help: 'The address all outgoing emails will be sent from.', help: 'The address all outgoing emails will be sent from.',
class: "private-field" class: "private-field"
@ -121,11 +142,11 @@ Telescope.settings.schema = new SimpleSchema({
mailUrl: { mailUrl: {
type: String, type: String,
optional: true, optional: true,
private: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "06_email", group: "06_email",
help: 'MAIL_URL environment variable (requires restart).', help: 'MAIL_URL environment variable (requires restart).',
class: "private-field" class: "private-field"
@ -135,11 +156,11 @@ Telescope.settings.schema = new SimpleSchema({
type: Number, type: Number,
optional: true, optional: true,
defaultValue: 30, defaultValue: 30,
private: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: '01_general', group: '01_general',
help: 'How often to recalculate scores, in seconds (default to 30)', help: 'How often to recalculate scores, in seconds (default to 30)',
class: "private-field" class: "private-field"
@ -148,11 +169,13 @@ Telescope.settings.schema = new SimpleSchema({
postInterval: { postInterval: {
type: Number, type: Number,
optional: true, optional: true,
publish: true,
defaultValue: 30, defaultValue: 30,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "02_posts", group: "02_posts",
help: 'Minimum time between posts, in seconds (defaults to 30)' help: 'Minimum time between posts, in seconds (defaults to 30)'
} }
@ -160,11 +183,13 @@ Telescope.settings.schema = new SimpleSchema({
RSSLinksPointTo: { RSSLinksPointTo: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
control: "radiogroup", control: "radiogroup",
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "02_posts", group: "02_posts",
options: [ options: [
{value: 'page', label: 'Discussion page'}, {value: 'page', label: 'Discussion page'},
@ -175,11 +200,13 @@ Telescope.settings.schema = new SimpleSchema({
commentInterval: { commentInterval: {
type: Number, type: Number,
optional: true, optional: true,
publish: true,
defaultValue: 15, defaultValue: 15,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "03_comments", group: "03_comments",
help: 'Minimum time between comments, in seconds (defaults to 15)' help: 'Minimum time between comments, in seconds (defaults to 15)'
} }
@ -187,11 +214,13 @@ Telescope.settings.schema = new SimpleSchema({
maxPostsPerDay: { maxPostsPerDay: {
type: Number, type: Number,
optional: true, optional: true,
publish: true,
defaultValue: 30, defaultValue: 30,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "02_posts", group: "02_posts",
help: 'Maximum number of posts a user can post in a day (default to 30).' help: 'Maximum number of posts a user can post in a day (default to 30).'
} }
@ -200,10 +229,12 @@ Telescope.settings.schema = new SimpleSchema({
type: Number, type: Number,
defaultValue: 3, defaultValue: 3,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: 'invites' group: 'invites'
} }
}, },
@ -211,50 +242,60 @@ Telescope.settings.schema = new SimpleSchema({
type: Number, type: Number,
defaultValue: 10, defaultValue: 10,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "02_posts" group: "02_posts"
} }
}, },
logoUrl: { logoUrl: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "04_logo" group: "04_logo"
} }
}, },
logoHeight: { logoHeight: {
type: Number, type: Number,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "04_logo" group: "04_logo"
} }
}, },
logoWidth: { logoWidth: {
type: Number, type: Number,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "04_logo" group: "04_logo"
} }
}, },
faviconUrl: { faviconUrl: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "04_logo" group: "04_logo"
} }
}, },
@ -281,30 +322,36 @@ Telescope.settings.schema = new SimpleSchema({
twitterAccount: { twitterAccount: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "07_integrations" group: "07_integrations"
} }
}, },
facebookPage: { facebookPage: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "07_integrations" group: "07_integrations"
} }
}, },
googleAnalyticsId: { googleAnalyticsId: {
type: String, type: String,
optional: true, optional: true,
publish: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "07_integrations" group: "07_integrations"
} }
}, },
@ -313,9 +360,9 @@ Telescope.settings.schema = new SimpleSchema({
optional: true, optional: true,
insertableIf: Users.is.admin, insertableIf: Users.is.admin,
editableIf: Users.is.admin, editableIf: Users.is.admin,
private: true,
autoform: { autoform: {
disabled: isInSettingsJSON, disabled: isInSettingsJSON,
prefill: getFromJSON,
group: "06_email", group: "06_email",
help: 'Content that will appear at the bottom of outgoing emails (accepts HTML).', help: 'Content that will appear at the bottom of outgoing emails (accepts HTML).',
rows: 5, rows: 5,

View file

@ -1,3 +1,36 @@
Telescope.settings.collection.smartMethods({ Telescope.settings.collection.smartMethods({
editName: "settings.edit" editName: "settings.edit"
}); });
Meteor.methods({
"settings.getJSON": function () {
if (Users.is.adminById(this.userId)) {
return Meteor.settings;
} else {
return {};
}
},
"settings.exportToJSON": function () {
if (Users.is.adminById(this.userId)) {
let settings = Telescope.settings.collection.findOne();
const schema = Telescope.settings.collection.simpleSchema()._schema;
const publicFields = Telescope.settings.collection.getPublicFields();
delete settings._id;
settings.public = {};
_.forEach(settings, (field, key) => {
if (_.contains(publicFields, key)) {
settings.public[key] = field;
delete settings[key];
}
});
console.log(JSON.stringify(settings, null, 2));
return settings;
}
},
"settings.clear": function () {
if (Users.is.adminById(this.userId)) {
const settings = Telescope.settings.collection.findOne();
Telescope.settings.collection.update(settings._id, {}, {validate: false});
}
}
})

View file

@ -1,19 +1,21 @@
Meteor.publish('settings', function() { // Meteor.publish('settings', function() {
var options = {}; // var options = {};
var privateFields = {}; // var privateFields = {};
// look at Settings.simpleSchema._schema to see which fields should be kept private // // look at Settings.simpleSchema._schema to see which fields should be kept private
_.each(Telescope.settings.collection.simpleSchema()._schema, (property, key) => { // _.each(Telescope.settings.collection.simpleSchema()._schema, (property, key) => {
if (property.private) // if (property.private)
privateFields[key] = false; // privateFields[key] = false;
}); // });
options = _.extend(options, { // options = _.extend(options, {
fields: privateFields // fields: privateFields
}); // });
return Telescope.settings.collection.find({}, options); // return Telescope.settings.collection.find({}, options);
}); // });
Telescope.settings.collection.smartPublish('settings');
Meteor.publish('settings.admin', function() { Meteor.publish('settings.admin', function() {
if (Users.is.adminById(this.userId)) { if (Users.is.adminById(this.userId)) {