c70fbc2c7e
closes #8056 🎨 Collect together the package-related utils - read directory actually reads a directory of packages - parse package json is very tighly related to this 🎨 Move filterPaths -> packages.filterPackages - this function is related to packages, not settings - move the function to the new utils/packages - add 100% test coverage 🎨 Simplify filterPackages code 🎨 Simplify reading of packages & themes - This massively reduces all the complex code in the read packages & themes utils - Added full test coverage 🎨 Improve & clarify active prop in filterPackages - active is returned from API endpoints to combine data from multiple sources - see https://github.com/TryGhost/Ghost/pull/8064#discussion_r103514810 🎨 Better error handling 🔥 Temporarily remove custom error templates - we will reimplement this later when we have got a better concept of loading the active theme in place - refs #8079
57 lines
1.7 KiB
JavaScript
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;
|