diff --git a/core/server/helpers/get.js b/core/server/helpers/get.js index 6d863c75ae..041ea91a87 100644 --- a/core/server/helpers/get.js +++ b/core/server/helpers/get.js @@ -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 ''; + }); + } + }); +}; diff --git a/core/server/middleware/auth.js b/core/server/middleware/auth.js index 79ff74826e..1878f3d669 100644 --- a/core/server/middleware/auth.js +++ b/core/server/middleware/auth.js @@ -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) { diff --git a/core/server/utils/labs.js b/core/server/utils/labs.js new file mode 100644 index 0000000000..3a0853b6d8 --- /dev/null +++ b/core/server/utils/labs.js @@ -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; diff --git a/core/test/unit/server_helpers/get_spec.js b/core/test/unit/server_helpers/get_spec.js index e45e73ed0d..36ec8ee918 100644 --- a/core/test/unit/server_helpers/get_spec.js +++ b/core/test/unit/server_helpers/get_spec.js @@ -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 () {