@@ -171,6 +171,11 @@ export const login = async (req, res) => {
171171 const user = await User . findOne ( { username : req . body . username } )
172172
173173 if ( user ) {
174+ if ( user . banned ) {
175+ res . status ( 403 ) . send ( 'User is banned' )
176+ return
177+ }
178+
174179 const matches = await bcrypt . compare ( req . body . password , user . password )
175180
176181 if ( matches ) {
@@ -414,6 +419,7 @@ export const fetchUser = async (req, res) => {
414419 role : 1 ,
415420 ...( req . userRole === 'admin' ? { email : 1 , invitedBy : 1 } : { } ) ,
416421 remainingInvites : 1 ,
422+ banned : 1 ,
417423 } ,
418424 } ,
419425 {
@@ -589,3 +595,51 @@ export const verifyUserEmail = async (req, res) => {
589595 res . status ( 400 ) . send ( 'Request must include token' )
590596 }
591597}
598+
599+ export const banUser = async ( req , res ) => {
600+ try {
601+ if ( req . userRole !== 'admin' ) {
602+ res . status ( 401 ) . send ( 'You do not have permission to ban a user' )
603+ return
604+ }
605+
606+ const user = await User . findOne ( { username : req . params . username } )
607+ if ( ! user ) {
608+ res . status ( 404 ) . send ( 'User does not exist' )
609+ return
610+ }
611+
612+ await User . findOneAndUpdate (
613+ { username : req . params . username } ,
614+ { $set : { banned : true } }
615+ )
616+
617+ res . sendStatus ( 200 )
618+ } catch ( e ) {
619+ res . status ( 500 ) . send ( e . message )
620+ }
621+ }
622+
623+ export const unbanUser = async ( req , res ) => {
624+ try {
625+ if ( req . userRole !== 'admin' ) {
626+ res . status ( 401 ) . send ( 'You do not have permission to unban a user' )
627+ return
628+ }
629+
630+ const user = await User . findOne ( { username : req . params . username } )
631+ if ( ! user ) {
632+ res . status ( 404 ) . send ( 'User does not exist' )
633+ return
634+ }
635+
636+ await User . findOneAndUpdate (
637+ { username : req . params . username } ,
638+ { $set : { banned : false } }
639+ )
640+
641+ res . sendStatus ( 200 )
642+ } catch ( e ) {
643+ res . status ( 500 ) . send ( e . message )
644+ }
645+ }
0 commit comments