Skip to content

Commit

Permalink
Sync remove controller with latest nomenclature and flag controller s…
Browse files Browse the repository at this point in the history
…ignature

* Streamline redirects for removing to point back to the author scripts list or general users
* One whitespace adjustment on flag controller

Applies to OpenUserJS#819

Originally applies to OpenUserJS#262 ... treads on OpenUserJS#262 (comment) *(preauth)*
  • Loading branch information
Martii committed Nov 17, 2015
1 parent da435f7 commit 52768bf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
1 change: 0 additions & 1 deletion controllers/flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ exports.flag = function (aReq, aRes, aNext) {
isLib = true;
// fallthrough
case 'scripts':

aReq.params.username = aReq.params[2];
aReq.params.scriptname = aReq.params[3]

Expand Down
41 changes: 28 additions & 13 deletions controllers/remove.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var Script = require('../models/script').Script;
var User = require('../models/user').User;

//--- Controller inclusions
var scriptStorage = require('./scriptStorage');

//--- Library inclusions
var removeLib = require('../libs/remove');
Expand Down Expand Up @@ -44,7 +45,10 @@ exports.rm = function (aReq, aRes, aNext) {
var reason = aFields.reason;

var type = aReq.params[0];
var path = aReq.params[1];
var isLib = null;

var installNameBase = null;
var username = null;

var authedUser = aReq.session.user;

Expand All @@ -70,21 +74,32 @@ exports.rm = function (aReq, aRes, aNext) {
}

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) {
aNext();
return;
}
aRes.redirect('/');
isLib = true;
// fallthrough
case 'scripts':
aReq.params.username = username = aReq.params[2];
aReq.params.scriptname = aReq.params[3]

installNameBase = scriptStorage.getInstallNameBase(aReq);

Script.findOne({
installName: scriptStorage.caseSensitive(installNameBase +
(isLib ? '.js' : '.user.js'))
}, function (aErr, aScript) {
removeLib.remove(Script, aScript, authedUser, reason, function (aRemoved) {
if (!aRemoved) {
aNext();
return;
}
aRes.redirect('/users/' + encodeURIComponent(username) + '/scripts');
});
});
});
break;
case 'users':
User.findOne({ name: { $regex: new RegExp('^' + path + '$', "i") } },
username = aReq.params[1];

User.findOne({ name: { $regex: new RegExp('^' + username + '$', "i") } },
function (aErr, aUser) {
removeLib.remove(User, aUser, authedUser, reason, function (aRemoved) {
if (!aRemoved) {
Expand All @@ -94,7 +109,7 @@ exports.rm = function (aReq, aRes, aNext) {

// Destroy all the sessions belonging to the removed user
destroySessions(aReq, aUser, function () {
aRes.redirect('/');
aRes.redirect('/users');
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ module.exports = function (aApp) {
aApp.route(/^\/flag\/(users|scripts|libs)\/((.+?)(?:\/(.+))?)$/).post(flag.flag);

// Remove route
aApp.route(/^\/remove\/(.+?)\/(.+)$/).post(remove.rm);
aApp.route(/^\/remove\/(users|scripts|libs)\/((.+?)(?:\/(.+))?)$/).post(remove.rm);

// Group routes
aApp.route('/groups').get(group.list);
Expand Down

0 comments on commit 52768bf

Please sign in to comment.