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