2014-02-12 05:04:13 +04:00
|
|
|
var fs = require('fs'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2014-02-12 05:04:13 +04:00
|
|
|
path = require('path'),
|
2015-12-05 08:00:37 +03:00
|
|
|
parsePackageJson = require('../utils/parse-package-json');
|
2014-02-12 05:04:13 +04:00
|
|
|
|
|
|
|
function AppPermissions(appPath) {
|
|
|
|
this.appPath = appPath;
|
|
|
|
this.packagePath = path.join(this.appPath, 'package.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
AppPermissions.prototype.read = function () {
|
2014-08-17 10:17:23 +04:00
|
|
|
var self = this;
|
2014-02-12 05:04:13 +04:00
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return this.checkPackageContentsExists().then(function (exists) {
|
2014-09-10 08:06:24 +04:00
|
|
|
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)) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(AppPermissions.DefaultPermissions);
|
2014-02-12 05:04:13 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
// TODO: Validation on permissions object?
|
2014-02-12 05:04:13 +04:00
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
return Promise.resolve(parsed.ghost.permissions);
|
2014-08-17 10:17:23 +04:00
|
|
|
});
|
2014-09-10 08:06:24 +04:00
|
|
|
});
|
2014-02-12 05:04:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
AppPermissions.prototype.checkPackageContentsExists = function () {
|
2014-09-05 16:52:13 +04:00
|
|
|
var self = this;
|
|
|
|
|
2014-02-12 05:04:13 +04:00
|
|
|
// Mostly just broken out for stubbing in unit tests
|
2014-08-17 10:17:23 +04:00
|
|
|
return new Promise(function (resolve) {
|
2015-03-12 23:12:56 +03:00
|
|
|
fs.stat(self.packagePath, function (err) {
|
|
|
|
var exists = !err;
|
2014-08-17 10:17:23 +04:00
|
|
|
resolve(exists);
|
|
|
|
});
|
2014-02-12 05:04:13 +04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Get the contents of the package.json in the appPath root
|
|
|
|
AppPermissions.prototype.getPackageContents = function () {
|
2015-10-06 16:36:56 +03:00
|
|
|
return parsePackageJson(this.packagePath);
|
2014-02-12 05:04:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Default permissions for an App.
|
|
|
|
AppPermissions.DefaultPermissions = {
|
|
|
|
posts: ['browse', 'read']
|
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = AppPermissions;
|