Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process remove with form data asynchronously #514

Merged
merged 1 commit into from
Jan 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions controllers/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,49 +21,68 @@ exports.rm = function (aReq, aRes, aNext) {
var authedUser = aReq.session.user;

var form = null;
var reason = null;

// Check to make sure multipart form data submission header is present
if (!/multipart\/form-data/.test(aReq.headers['content-type'])) {
return aNext();
return statusCodePage(aReq, aRes, aNext, {
statusCode: 400,
statusMessage: 'Missing required header.'
});
}

form = new formidable.IncomingForm();
form.parse(aReq, function (aErr, aFields) {
reason = aFields.reason.trim();
});
var reason = aFields.reason;

if (!reason || reason === '' || /^User removed$/i.test(reason)) {
return statusCodePage(aReq, aRes, aNext, {
statusCode: 403,
statusMessage: 'Invalid reason for removal.'
});
}
// Check to make sure form submission has this name available.
// This occurs either when no reason is supplied,
// or a rare edge case if the view is missing the input name.
if (!reason) {
return statusCodePage(aReq, aRes, aNext, {
statusCode: 403,
statusMessage: 'Missing reason for removal.'
});
}

switch (type) {
case 'scripts':
case 'libs':
path += type === 'libs' ? '.js' : '.user.js';
Script.findOne({ installName: path }, function (aErr, aScript) {
removeLib.remove(Script, aScript, authedUser, reason, function (aRemoved) {
if (!aRemoved) { return aNext(); }
aRes.redirect('/');
});
// Simple error check for string null and reserved phrase
reason = reason.trim();
if (reason === '' || /^User removed$/i.test(reason)) {
return statusCodePage(aReq, aRes, aNext, {
statusCode: 403,
statusMessage: 'Invalid reason for removal.'
});
break;
case 'users':
User.findOne({ name: { $regex: new RegExp('^' + path + '$', "i") } },
function (aErr, aUser) {
removeLib.remove(User, aUser, authedUser, reason, function (aRemoved) {
if (!aRemoved) { return aNext(); }
}

// Destory all the sessions belonging to the removed user
destroySessions(aReq, aUser, function () {
aRes.redirect('/');
});
switch (type) {
case 'scripts':
case 'libs':
path += type === 'libs' ? '.js' : '.user.js';
Script.findOne({ installName: path }, function (aErr, aScript) {
removeLib.remove(Script, aScript, authedUser, reason, function (aRemoved) {
if (!aRemoved) {
return aNext();
}
aRes.redirect('/');
});
});
break;
default:
aNext();
}
break;
case 'users':
User.findOne({ name: { $regex: new RegExp('^' + path + '$', "i") } },
function (aErr, aUser) {
removeLib.remove(User, aUser, authedUser, reason, function (aRemoved) {
if (!aRemoved) {
return aNext();
}

// Destory all the sessions belonging to the removed user
destroySessions(aReq, aUser, function () {
aRes.redirect('/');
});
});
});
break;
default:
aNext();
}
});
};