@ -4,7 +4,7 @@ use serde_json::Value;
use std ::{ env , time ::Duration } ;
use rocket ::{
http ::{ Cookie , Cookies , SameSite },
http ::{ Cookie , Cookies , SameSite , Status },
request ::{ self , FlashMessage , Form , FromRequest , Outcome , Request } ,
response ::{ content ::Html , Flash , Redirect } ,
Route ,
@ -279,6 +279,14 @@ struct InviteData {
email : String ,
}
fn get_user_or_404 ( uuid : & str , conn : & DbConn ) -> ApiResult < User > {
if let Some ( user ) = User ::find_by_uuid ( uuid , conn ) {
Ok ( user )
} else {
err_code ! ( "User doesn't exist" , Status ::NotFound . code ) ;
}
}
#[ post( " /invite " , data = " <data> " ) ]
fn invite_user ( data : Json < InviteData > , _token : AdminToken , conn : DbConn ) -> JsonResult {
let data : InviteData = data . into_inner ( ) ;
@ -352,20 +360,20 @@ fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult<Html<String>> {
#[ get( " /users/<uuid> " ) ]
fn get_user_json ( uuid : String , _token : AdminToken , conn : DbConn ) -> JsonResult {
let user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let user = get_user_or_404 ( & uuid , & conn ) ? ;
Ok ( Json ( user . to_json ( & conn ) ) )
}
#[ post( " /users/<uuid>/delete " ) ]
fn delete_user ( uuid : String , _token : AdminToken , conn : DbConn ) -> EmptyResult {
let user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let user = get_user_or_404 ( & uuid , & conn ) ? ;
user . delete ( & conn )
}
#[ post( " /users/<uuid>/deauth " ) ]
fn deauth_user ( uuid : String , _token : AdminToken , conn : DbConn ) -> EmptyResult {
let mut user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let mut user = get_user_or_404 ( & uuid , & conn ) ? ;
Device ::delete_all_by_user ( & user . uuid , & conn ) ? ;
user . reset_security_stamp ( ) ;
@ -374,7 +382,7 @@ fn deauth_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
#[ post( " /users/<uuid>/disable " ) ]
fn disable_user ( uuid : String , _token : AdminToken , conn : DbConn ) -> EmptyResult {
let mut user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let mut user = get_user_or_404 ( & uuid , & conn ) ? ;
Device ::delete_all_by_user ( & user . uuid , & conn ) ? ;
user . reset_security_stamp ( ) ;
user . enabled = false ;
@ -384,7 +392,7 @@ fn disable_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
#[ post( " /users/<uuid>/enable " ) ]
fn enable_user ( uuid : String , _token : AdminToken , conn : DbConn ) -> EmptyResult {
let mut user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let mut user = get_user_or_404 ( & uuid , & conn ) ? ;
user . enabled = true ;
user . save ( & conn )
@ -392,7 +400,7 @@ fn enable_user(uuid: String, _token: AdminToken, conn: DbConn) -> EmptyResult {
#[ post( " /users/<uuid>/remove-2fa " ) ]
fn remove_2fa ( uuid : String , _token : AdminToken , conn : DbConn ) -> EmptyResult {
let mut user = User::find_by_uuid ( & uuid , & conn ) . map_res ( "User doesn't exist" ) ? ;
let mut user = get_user_or_404 ( & uuid , & conn ) ? ;
TwoFactor ::delete_all_by_user ( & user . uuid , & conn ) ? ;
user . totp_recover = None ;
user . save ( & conn )