@@ -21,49 +21,68 @@ exports.rm = function (aReq, aRes, aNext) {
2121 var authedUser = aReq . session . user ;
2222
2323 var form = null ;
24- var reason = null ;
2524
25+ // Check to make sure multipart form data submission header is present
2626 if ( ! / m u l t i p a r t \/ f o r m - d a t a / . test ( aReq . headers [ 'content-type' ] ) ) {
27- return aNext ( ) ;
27+ return statusCodePage ( aReq , aRes , aNext , {
28+ statusCode : 400 ,
29+ statusMessage : 'Missing required header.'
30+ } ) ;
2831 }
2932
3033 form = new formidable . IncomingForm ( ) ;
3134 form . parse ( aReq , function ( aErr , aFields ) {
32- reason = aFields . reason . trim ( ) ;
33- } ) ;
35+ var reason = aFields . reason ;
3436
35- if ( ! reason || reason === '' || / ^ U s e r r e m o v e d $ / i. test ( reason ) ) {
36- return statusCodePage ( aReq , aRes , aNext , {
37- statusCode : 403 ,
38- statusMessage : 'Invalid reason for removal.'
39- } ) ;
40- }
37+ // Check to make sure form submission has this name available.
38+ // This occurs either when no reason is supplied,
39+ // or a rare edge case if the view is missing the input name.
40+ if ( ! reason ) {
41+ return statusCodePage ( aReq , aRes , aNext , {
42+ statusCode : 403 ,
43+ statusMessage : 'Missing reason for removal.'
44+ } ) ;
45+ }
4146
42- switch ( type ) {
43- case 'scripts' :
44- case 'libs' :
45- path += type === 'libs' ? '.js' : '.user.js' ;
46- Script . findOne ( { installName : path } , function ( aErr , aScript ) {
47- removeLib . remove ( Script , aScript , authedUser , reason , function ( aRemoved ) {
48- if ( ! aRemoved ) { return aNext ( ) ; }
49- aRes . redirect ( '/' ) ;
50- } ) ;
47+ // Simple error check for string null and reserved phrase
48+ reason = reason . trim ( ) ;
49+ if ( reason === '' || / ^ U s e r r e m o v e d $ / i. test ( reason ) ) {
50+ return statusCodePage ( aReq , aRes , aNext , {
51+ statusCode : 403 ,
52+ statusMessage : 'Invalid reason for removal.'
5153 } ) ;
52- break ;
53- case 'users' :
54- User . findOne ( { name : { $regex : new RegExp ( '^' + path + '$' , "i" ) } } ,
55- function ( aErr , aUser ) {
56- removeLib . remove ( User , aUser , authedUser , reason , function ( aRemoved ) {
57- if ( ! aRemoved ) { return aNext ( ) ; }
54+ }
5855
59- // Destory all the sessions belonging to the removed user
60- destroySessions ( aReq , aUser , function ( ) {
61- aRes . redirect ( '/' ) ;
62- } ) ;
56+ switch ( type ) {
57+ case 'scripts' :
58+ case 'libs' :
59+ path += type === 'libs' ? '.js' : '.user.js' ;
60+ Script . findOne ( { installName : path } , function ( aErr , aScript ) {
61+ removeLib . remove ( Script , aScript , authedUser , reason , function ( aRemoved ) {
62+ if ( ! aRemoved ) {
63+ return aNext ( ) ;
64+ }
65+ aRes . redirect ( '/' ) ;
6366 } ) ;
6467 } ) ;
65- break ;
66- default :
67- aNext ( ) ;
68- }
68+ break ;
69+ case 'users' :
70+ User . findOne ( { name : { $regex : new RegExp ( '^' + path + '$' , "i" ) } } ,
71+ function ( aErr , aUser ) {
72+ removeLib . remove ( User , aUser , authedUser , reason , function ( aRemoved ) {
73+ if ( ! aRemoved ) {
74+ return aNext ( ) ;
75+ }
76+
77+ // Destory all the sessions belonging to the removed user
78+ destroySessions ( aReq , aUser , function ( ) {
79+ aRes . redirect ( '/' ) ;
80+ } ) ;
81+ } ) ;
82+ } ) ;
83+ break ;
84+ default :
85+ aNext ( ) ;
86+ }
87+ } ) ;
6988} ;
0 commit comments