Skip to content

Commit

Permalink
Hotfix for invalid project routes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrzapp committed Aug 11, 2017
1 parent 7358ddd commit 3bd5560
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 61 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hashbrown-cms",
"repository": "https://github.com/Putaitu/hashbrown-cms.git",
"version": "0.9.2",
"version": "0.9.3",
"description": "The pluggable CMS",
"main": "hashbrown.js",
"scripts": {
Expand Down
24 changes: 5 additions & 19 deletions src/Server/Controllers/ServerController.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class ServerController extends ApiController {
app.post('/api/server/backups/:project/:timestamp/restore', this.middleware({ needsAdmin: true, setProject: false }), this.postRestoreProjectBackup);
app.post('/api/server/settings/:project/:section', this.middleware({ needsAdmin: true, setProject: false }), this.postProjectSettings);
app.post('/api/server/migrate/:project/', this.middleware({ needsAdmin: true, setProject: false }), this.postMigrateContent);
app.post('/api/server/rename/:project/', this.middleware({ needsAdmin: true, setProject: false }), this.postRenameProject);

app.put('/api/server/projects/:project/:environment', this.middleware({ needsAdmin: true, setProject: false }), this.putEnvironment);

Expand Down Expand Up @@ -207,8 +206,11 @@ class ServerController extends ApiController {
*/
static postProjectSettings(req, res) {
let settings = req.body;

SettingsHelper.setSettings(req.params.project, null, req.params.section, settings)

ProjectHelper.checkProject(req.params.project)
.then(() => {
return SettingsHelper.setSettings(req.params.project, null, req.params.section, settings);
})
.then(() => {
res.status(200).send(settings);
})
Expand Down Expand Up @@ -477,22 +479,6 @@ class ServerController extends ApiController {
res.status(502).send(ServerController.printError(e));
});
}

/**
* Renames a project
*/
static postRenameProject(req, res) {
let oldName = req.params.project;
let newName = req.body.name;

ProjectHelper.renameProject(oldName, newName)
.then((msg) => {
res.status(200).send(msg);
})
.catch((e) => {
res.status(502).send(ServerController.printError(e));
});
}
}

module.exports = ServerController;
1 change: 1 addition & 0 deletions src/Server/Controllers/ViewController.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class ViewController extends Controller {
});
})
.catch((e) => {
debug.error(e, this);
res.status(403).redirect('/login');
});
});
Expand Down
94 changes: 53 additions & 41 deletions src/Server/Helpers/ProjectHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ class ProjectHelper {
});
}

/**
* Performs a check of the requested project
*
* @param {String} project
*
* @returns {Promise}
*/
static checkProject(
project = requiredParam('project')
) {
return this.projectExists(project)
.then((projectExists) => {
if(!projectExists) {
return Promise.reject(new Error('Project "' + project + '" could not be found'));
}

return Promise.resolve();
});
}

/**
* Gets a Project object
*
Expand All @@ -61,7 +81,10 @@ class ProjectHelper {
let users;
let backups;

return HashBrown.Helpers.SettingsHelper.getSettings(id)
return this.checkProject(id)
.then(() => {
return HashBrown.Helpers.SettingsHelper.getSettings(id);
})
.then((foundSettings) => {
settings = foundSettings || {};

Expand Down Expand Up @@ -98,8 +121,11 @@ class ProjectHelper {
* @returns {Promise(Array)} environments
*/
static getAllEnvironments(project) {
// First attempt to get remote environments
return SyncHelper.getResource(project, null, 'environments')
return this.checkProject(project)
.then(() => {
// First attempt to get remote environments
return SyncHelper.getResource(project, null, 'environments')
})
.then((environments) => {
// If remote environments were found, resolve immediately
if(environments && Array.isArray(environments)) {
Expand Down Expand Up @@ -145,20 +171,23 @@ class ProjectHelper {
* @returns {Promise} Promise
*/
static deleteProject(
id = requriedParam('id'),
id = requiredParam('id'),
makeBackup = true
) {
// Make backup first, if specified
if(makeBackup) {
return BackupHelper.createBackup(id)
.then(() => {
return MongoHelper.dropDatabase(id);
});
return this.checkProject(id)
.then(() => {
// Make backup first, if specified
if(makeBackup) {
return BackupHelper.createBackup(id)
.then(() => {
return MongoHelper.dropDatabase(id);
});

// If not, just drop the database
} else {
return MongoHelper.dropDatabase(id);
}
// If not, just drop the database
} else {
return MongoHelper.dropDatabase(id);
}
});
}

/**
Expand All @@ -173,8 +202,11 @@ class ProjectHelper {
project = requiredParam('project'),
environment = requiredParam('environment')
) {
// Check if project is synced first
return SettingsHelper.getSettings(project, null, 'sync')
return this.checkProject(project)
.then(() => {
// Check if project is synced first
return SettingsHelper.getSettings(project, null, 'sync');
})
.then((sync) => {
if(sync.enabled) {
return Promise.reject(new Error('Cannot add environments to a synced project'));
Expand All @@ -189,29 +221,6 @@ class ProjectHelper {
});
}

/**
* Renames a project
*
* @param {String} oldName
* @param {String} newName
*
* @return {Promise} Promise
*/
static renameProject(
oldName = requiredParam('oldName'),
newName = requiredParam('newName')
) {
// Check if project is synced first
return SettingsHelper.getSettings(project, null, 'sync')
.then((sync) => {
if(sync.enabled) {
return Promise.reject(new Error('Cannot rename a synced project'));
}

return MongoHelper.renameDatabase(oldName, newName);
});
}

/**
* Deletes an environment
*
Expand All @@ -224,8 +233,11 @@ class ProjectHelper {
project = requiredParam('project'),
environment = requiredParam('environment')
) {
// Check if project is synced first
return SettingsHelper.getSettings(project, null, 'sync')
return this.checkProject(project)
.then(() => {
// Check if project is synced first
return SettingsHelper.getSettings(project, null, 'sync');
})
.then((sync) => {
if(sync.enabled) {
return Promise.reject(new Error('Cannot delete environments from a synced project'));
Expand Down

0 comments on commit 3bd5560

Please sign in to comment.