Ghost/core/server/services/apps/permissions.js
Hannah Wolfe 882a2361ee
Moved apps to /services/ & moved individual tests (#9187)
refs #9178

* Moved app handling code into services/apps
  - Apps is a service, that allows for the App lifecycle 
  - /server/apps = contains internal apps 
   - /server/services/apps = contains code for managing/handling app life cycle, providing the proxy, etc
* Split apps service tests into separate files
* Moved internal app tests into test folders
    - Problem: Not all the tests in apps were unit tests, yet they were treated like they were in Gruntfile.js
    - Unit tests now live in /test/unit/apps
    - Route tests now live in /test/functional/routes/apps
    - Gruntfile.js has been updated to match
* Switch api.read usage for settingsCache
* Add tests to cover the basic App lifecycle
* Simplify some of the init logic
2017-10-30 12:31:04 +00:00

57 lines
1.7 KiB
JavaScript

var fs = require('fs'),
Promise = require('bluebird'),
path = require('path'),
parsePackageJson = require('../../utils/packages').parsePackageJSON;
function AppPermissions(appPath) {
this.appPath = appPath;
this.packagePath = path.join(this.appPath, 'package.json');
}
AppPermissions.prototype.read = function () {
var self = this;
return this.checkPackageContentsExists().then(function (exists) {
if (!exists) {
// If no package.json, return default permissions
return Promise.resolve(AppPermissions.DefaultPermissions);
}
// Read and parse the package.json
return self.getPackageContents().then(function (parsed) {
// If no permissions in the package.json then return the default permissions.
if (!(parsed.ghost && parsed.ghost.permissions)) {
return Promise.resolve(AppPermissions.DefaultPermissions);
}
// TODO: Validation on permissions object?
return Promise.resolve(parsed.ghost.permissions);
});
});
};
AppPermissions.prototype.checkPackageContentsExists = function () {
var self = this;
// Mostly just broken out for stubbing in unit tests
return new Promise(function (resolve) {
fs.stat(self.packagePath, function (err) {
var exists = !err;
resolve(exists);
});
});
};
// Get the contents of the package.json in the appPath root
AppPermissions.prototype.getPackageContents = function () {
return parsePackageJson(this.packagePath);
};
// Default permissions for an App.
AppPermissions.DefaultPermissions = {
posts: ['browse', 'read']
};
module.exports = AppPermissions;