Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// # Settings API
|
2014-06-03 17:05:25 +04:00
|
|
|
// RESTful API for the Setting resource
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2013-12-06 12:51:35 +04:00
|
|
|
dataProvider = require('../models'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-05-07 04:49:25 +04:00
|
|
|
canThis = require('../permissions').canThis,
|
2014-05-09 14:11:29 +04:00
|
|
|
errors = require('../errors'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
utils = require('./utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
|
|
|
docName = 'settings',
|
2013-12-06 12:51:35 +04:00
|
|
|
settings,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
settingsCache = require('../settings/cache'),
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
settingsFilter,
|
2014-04-28 03:28:50 +04:00
|
|
|
settingsResult,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
canEditAllSettings,
|
2013-12-06 12:51:35 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// ## Helpers
|
2014-04-28 03:28:50 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Settings Filter
|
|
|
|
* Filters an object based on a given filter object
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settings
|
|
|
|
* @param {String} filter
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
settingsFilter = function (settings, filter) {
|
2016-06-11 21:23:27 +03:00
|
|
|
return _.fromPairs(_.filter(_.toPairs(settings), function (setting) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (filter) {
|
|
|
|
return _.some(filter.split(','), function (f) {
|
|
|
|
return setting[1].type === f;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}));
|
2014-02-23 16:32:35 +04:00
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
2017-03-03 01:00:01 +03:00
|
|
|
* ### Settings Result
|
2017-03-02 19:53:48 +03:00
|
|
|
*
|
2017-03-03 01:00:01 +03:00
|
|
|
* Takes a keyed JSON object
|
2017-03-02 19:53:48 +03:00
|
|
|
* E.g.
|
|
|
|
* dbHash: {
|
|
|
|
* id: '123abc',
|
|
|
|
* key: 'dbash',
|
|
|
|
* value: 'xxxx',
|
|
|
|
* type: 'core',
|
|
|
|
* timestamps
|
|
|
|
* }
|
|
|
|
*
|
2017-03-03 01:00:01 +03:00
|
|
|
* Performs a filter, based on the `type`
|
|
|
|
* And converts the remaining items to our API format by adding a `setting` and `meta` keys.
|
|
|
|
*
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @private
|
2017-03-03 01:00:01 +03:00
|
|
|
* @param {Object} settings - a keyed JSON object
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {String} type
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {{settings: *}}
|
|
|
|
*/
|
2017-03-03 01:00:01 +03:00
|
|
|
settingsResult = function settingsResult(settings, type) {
|
2014-04-28 03:28:50 +04:00
|
|
|
var filteredSettings = _.values(settingsFilter(settings, type)),
|
|
|
|
result = {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
settings: filteredSettings,
|
|
|
|
meta: {}
|
2014-04-28 03:28:50 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
if (type) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
result.meta.filters = {
|
|
|
|
type: type
|
2014-04-28 03:28:50 +04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Can Edit All Settings
|
|
|
|
* Check that this edit request is allowed for all settings requested to be updated
|
|
|
|
* @private
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} settingsInfo
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
|
|
|
canEditAllSettings = function (settingsInfo, options) {
|
2017-02-27 18:53:04 +03:00
|
|
|
var checkSettingPermissions = function checkSettingPermissions(setting) {
|
2014-06-17 19:36:47 +04:00
|
|
|
if (setting.type === 'core' && !(options.context && options.context.internal)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(
|
2016-10-06 15:27:35 +03:00
|
|
|
new errors.NoPermissionError({message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')})
|
2014-06-17 19:36:47 +04:00
|
|
|
);
|
|
|
|
}
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-07-30 05:25:53 +04:00
|
|
|
return canThis(options.context).edit.setting(setting.key).catch(function () {
|
2016-10-06 15:27:35 +03:00
|
|
|
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.api.settings.noPermissionToEditSettings')}));
|
2014-07-30 05:25:53 +04:00
|
|
|
});
|
2014-06-17 19:36:47 +04:00
|
|
|
},
|
|
|
|
checks = _.map(settingsInfo, function (settingInfo) {
|
2017-02-27 18:53:04 +03:00
|
|
|
var setting = settingsCache.get(settingInfo.key, {resolve: false});
|
2014-06-17 19:36:47 +04:00
|
|
|
|
|
|
|
if (!setting) {
|
2017-02-27 18:53:04 +03:00
|
|
|
return Promise.reject(new errors.NotFoundError(
|
|
|
|
{message: i18n.t('errors.api.settings.problemFindingSetting', {key: settingInfo.key})}
|
|
|
|
));
|
2014-06-17 19:36:47 +04:00
|
|
|
}
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2017-03-13 14:44:44 +03:00
|
|
|
if (setting.key === 'activeTheme') {
|
|
|
|
return Promise.reject(
|
|
|
|
new errors.BadRequestError({
|
|
|
|
message: i18n.t('errors.api.settings.activeThemeSetViaAPI.error'),
|
|
|
|
help: i18n.t('errors.api.settings.activeThemeSetViaAPI.help')
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-06-17 19:36:47 +04:00
|
|
|
return checkSettingPermissions(setting);
|
|
|
|
});
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(checks);
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
};
|
|
|
|
|
2014-06-03 17:05:25 +04:00
|
|
|
/**
|
|
|
|
* ## Settings API Methods
|
|
|
|
*
|
|
|
|
* **See:** [API Methods](index.js.html#api%20methods)
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
settings = {
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Browse
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
browse: function browse(options) {
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
options = options || {};
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
var result = settingsResult(settingsCache.getAll(), options.type);
|
2014-05-07 04:49:25 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// If there is no context, return only blog settings
|
|
|
|
if (!options.context) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(_.filter(result.settings, function (setting) { return setting.type === 'blog'; }));
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2014-05-07 04:49:25 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Otherwise return whatever this context is allowed to browse
|
|
|
|
return canThis(options.context).browse.setting().then(function () {
|
2014-05-07 04:49:25 +04:00
|
|
|
// Omit core settings unless internal request
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
if (!options.context.internal) {
|
2014-05-07 04:49:25 +04:00
|
|
|
result.settings = _.filter(result.settings, function (setting) { return setting.type !== 'core'; });
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Read
|
2014-09-10 08:06:24 +04:00
|
|
|
* @param {Object} options
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @returns {*}
|
|
|
|
*/
|
2013-12-06 12:51:35 +04:00
|
|
|
read: function read(options) {
|
|
|
|
if (_.isString(options)) {
|
2014-09-10 08:06:24 +04:00
|
|
|
options = {key: options};
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
var setting = settingsCache.get(options.key, {resolve: false}),
|
|
|
|
result = {};
|
2014-05-07 04:49:25 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
if (!setting) {
|
|
|
|
return Promise.reject(new errors.NotFoundError(
|
|
|
|
{message: i18n.t('errors.api.settings.problemFindingSetting', {key: options.key})}
|
|
|
|
));
|
|
|
|
}
|
2014-05-07 04:49:25 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
result[options.key] = setting;
|
2014-06-17 19:36:47 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
if (setting.type === 'core' && !(options.context && options.context.internal)) {
|
|
|
|
return Promise.reject(
|
|
|
|
new errors.NoPermissionError({message: i18n.t('errors.api.settings.accessCoreSettingFromExtReq')})
|
|
|
|
);
|
|
|
|
}
|
Consistency in model method naming
- The API has the BREAD naming for methods
- The model now has findAll, findOne, findPage (where needed), edit, add and destroy, meaning it is similar but with a bit more flexibility
- browse, read, update, create, and delete, which were effectively just aliases, have all been removed.
- added jsDoc for the model methods
2014-05-05 19:18:38 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
if (setting.type === 'blog') {
|
|
|
|
return Promise.resolve(settingsResult(result));
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
}
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2017-02-27 18:53:04 +03:00
|
|
|
return canThis(options.context).read.setting(options.key).then(function () {
|
|
|
|
return settingsResult(result);
|
|
|
|
}, function () {
|
|
|
|
return Promise.reject(new errors.NoPermissionError({message: i18n.t('errors.api.settings.noPermissionToReadSettings')}));
|
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
},
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
/**
|
|
|
|
* ### Edit
|
2015-12-02 10:28:36 +03:00
|
|
|
* Update properties of a setting
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
* @param {{settings: }} object Setting or a single string name
|
|
|
|
* @param {{id (required), include,...}} options (optional) or a single string value
|
|
|
|
* @return {Promise(Setting)} Edited Setting
|
|
|
|
*/
|
|
|
|
edit: function edit(object, options) {
|
|
|
|
options = options || {};
|
2014-04-03 17:03:09 +04:00
|
|
|
var self = this,
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
type;
|
2014-04-03 17:03:09 +04:00
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
// Allow shorthand syntax where a single key and value are passed to edit instead of object and options
|
|
|
|
if (_.isString(object)) {
|
2014-09-10 08:06:24 +04:00
|
|
|
object = {settings: [{key: object, value: options}]};
|
2013-12-06 12:51:35 +04:00
|
|
|
}
|
2014-04-28 03:28:50 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
// clean data
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
_.each(object.settings, function (setting) {
|
|
|
|
if (!_.isString(setting.value)) {
|
|
|
|
setting.value = JSON.stringify(setting.value);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
type = _.find(object.settings, function (setting) { return setting.key === 'type'; });
|
2014-05-06 22:52:11 +04:00
|
|
|
if (_.isObject(type)) {
|
|
|
|
type = type.value;
|
|
|
|
}
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
object.settings = _.reject(object.settings, function (setting) {
|
2017-03-02 19:53:48 +03:00
|
|
|
return setting.key === 'type';
|
2014-05-06 22:52:11 +04:00
|
|
|
});
|
|
|
|
|
Refactor API arguments
closes #2610, refs #2697
- cleanup API index.js, and add docs
- all API methods take consistent arguments: object & options
- browse, read, destroy take options, edit and add take object and options
- the context is passed as part of options, meaning no more .call
everywhere
- destroy expects an object, rather than an id all the way down to the model layer
- route params such as :id, :slug, and :key are passed as an option & used
to perform reads, updates and deletes where possible - settings / themes
may need work here still
- HTTP posts api can find a post by slug
- Add API utils for checkData
2014-05-08 16:41:19 +04:00
|
|
|
return canEditAllSettings(object.settings, options).then(function () {
|
|
|
|
return utils.checkObject(object, docName).then(function (checkedData) {
|
|
|
|
options.user = self.user;
|
|
|
|
return dataProvider.Settings.edit(checkedData.settings, options);
|
2017-03-03 01:00:01 +03:00
|
|
|
}).then(function (settingsModelsArray) {
|
|
|
|
// Instead of a standard bookshelf collection, Settings.edit returns an array of Settings Models.
|
|
|
|
// We convert this to JSON, by calling toJSON on each Model (using invokeMap for ease)
|
|
|
|
// We use keyBy to create an object that uses the 'key' as a key for each setting.
|
|
|
|
var settingsKeyedJSON = _.keyBy(_.invokeMap(settingsModelsArray, 'toJSON'), 'key');
|
|
|
|
return settingsResult(settingsKeyedJSON, type);
|
2014-05-06 22:52:11 +04:00
|
|
|
});
|
2013-12-06 12:51:35 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = settings;
|