2
1
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2023-12-13 21:00:40 +01:00

Move get helper behind labs flag

issue #5976

- break out the labs check into a utility
- wrap the get helper in a labs check, so it only works if the checkbox is checked
- make the get helper output an error to both the server and browser console if used when not enabled
This commit is contained in:
Hannah Wolfe 2015-11-03 19:17:24 +00:00
parent d81ddc9310
commit df82895db7
4 changed files with 47 additions and 13 deletions

View file

@ -7,6 +7,7 @@ var _ = require('lodash'),
errors = require('../errors'),
api = require('../api'),
jsonpath = require('jsonpath'),
labs = require('../utils/labs'),
resources,
pathAliases,
get;
@ -140,4 +141,23 @@ get = function get(context, options) {
});
};
module.exports = get;
module.exports = function getWithLabs(context, options) {
var self = this,
errorMessages = [
'The {{get}} helper is not available.',
'Public API access must be enabled if you wish to use the {{get}} helper.',
'See http://support.ghost.org/public-api-beta'
];
return labs.isSet('publicAPI').then(function (publicAPI) {
if (publicAPI === true) {
// get helper is active
return get.call(self, context, options);
} else {
errors.logError.apply(this, errorMessages);
return Promise.resolve(function noGetHelper() {
return '<script>console.error("' + errorMessages.join(' ') + '");</script>';
});
}
});
};

View file

@ -3,7 +3,7 @@ var _ = require('lodash'),
url = require('url'),
errors = require('../errors'),
config = require('../config'),
api = require('../api'),
labs = require('../utils/labs'),
oauthServer,
auth;
@ -133,17 +133,8 @@ auth = {
// ### Require user depending on public API being activated.
requiresAuthorizedUserPublicAPI: function requiresAuthorizedUserPublicAPI(req, res, next) {
return api.settings.read({key: 'labs', context: {internal: true}}).then(function (response) {
var labs,
labsValue;
labs = _.find(response.settings, function (setting) {
return setting.key === 'labs';
});
labsValue = JSON.parse(labs.value);
if (labsValue.publicAPI && labsValue.publicAPI === true) {
return labs.isSet('publicAPI').then(function (publicAPI) {
if (publicAPI === true) {
return next();
} else {
if (req.user) {

20
core/server/utils/labs.js Normal file
View file

@ -0,0 +1,20 @@
var _ = require('lodash'),
api = require('../api'),
flagIsSet;
flagIsSet = function flagIsSet(flag) {
return api.settings.read({key: 'labs', context: {internal: true}}).then(function (response) {
var labs,
labsValue;
labs = _.find(response.settings, function (setting) {
return setting.key === 'labs';
});
labsValue = JSON.parse(labs.value);
return !!labsValue[flag] && labsValue[flag] === true;
});
};
module.exports.isSet = flagIsSet;

View file

@ -11,6 +11,8 @@ var should = require('should'),
helpers = require('../../../server/helpers'),
api = require('../../../server/api'),
labs = require('../../../server/utils/labs'),
sandbox = sinon.sandbox.create();
describe('{{#get}} helper', function () {
@ -23,6 +25,7 @@ describe('{{#get}} helper', function () {
beforeEach(function () {
fn = sandbox.spy();
inverse = sandbox.spy();
sandbox.stub(labs, 'isSet').returns(new Promise.resolve(true));
});
afterEach(function () {