feat(otter): add working user removal route
							parent
							
								
									b946e1ce98
								
							
						
					
					
						commit
						5017bd1c5f
					
				|  | @ -14,4 +14,8 @@ impl<'a> AdminRepository<'a> { | ||||||
|     ) -> Result<Vec<models::User>, AuthErr> { |     ) -> Result<Vec<models::User>, AuthErr> { | ||||||
|         self.store.paginated_users(page, filter) |         self.store.paginated_users(page, filter) | ||||||
|     } |     } | ||||||
|  | 
 | ||||||
|  |     pub fn remove_user(&self, id: i64) -> Result<bool, AuthErr> { | ||||||
|  |         self.store.remove_user(id) | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -54,6 +54,9 @@ pub trait GpodderAuthStore { | ||||||
|     /// Update the user with the included ID with the new values
 |     /// Update the user with the included ID with the new values
 | ||||||
|     fn update_user(&self, user: User) -> Result<User, AuthErr>; |     fn update_user(&self, user: User) -> Result<User, AuthErr>; | ||||||
| 
 | 
 | ||||||
|  |     /// Remove the user with the given ID
 | ||||||
|  |     fn remove_user(&self, id: i64) -> Result<bool, AuthErr>; | ||||||
|  | 
 | ||||||
|     /// Create a new session for a user with the given session ID
 |     /// Create a new session for a user with the given session ID
 | ||||||
|     ///
 |     ///
 | ||||||
|     /// The `last_seen` timestamp's precision should be at least accurate to the second
 |     /// The `last_seen` timestamp's precision should be at least accurate to the second
 | ||||||
|  |  | ||||||
|  | @ -81,6 +81,16 @@ impl gpodder::GpodderAuthStore for SqliteRepository { | ||||||
|             .map_err(DbError::from)?) |             .map_err(DbError::from)?) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     fn remove_user(&self, id: i64) -> Result<bool, AuthErr> { | ||||||
|  |         let conn = &mut self.pool.get().map_err(DbError::from)?; | ||||||
|  | 
 | ||||||
|  |         match diesel::delete(users::table.filter(users::id.eq(id))).execute(conn) { | ||||||
|  |             Ok(0) => Ok(false), | ||||||
|  |             Ok(_) => Ok(true), | ||||||
|  |             Err(err) => Err(DbError::from(err).into()), | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     fn get_session(&self, session_id: i64) -> Result<Option<gpodder::models::Session>, AuthErr> { |     fn get_session(&self, session_id: i64) -> Result<Option<gpodder::models::Session>, AuthErr> { | ||||||
|         match sessions::table |         match sessions::table | ||||||
|             .inner_join(users::table) |             .inner_join(users::table) | ||||||
|  |  | ||||||
|  | @ -17,6 +17,7 @@ use crate::{ | ||||||
| pub fn router(ctx: Context) -> Router<Context> { | pub fn router(ctx: Context) -> Router<Context> { | ||||||
|     Router::new() |     Router::new() | ||||||
|         .route("/users", get(get_users)) |         .route("/users", get(get_users)) | ||||||
|  |         .route("/users/{id}", delete(delete_user)) | ||||||
|         .route_layer(axum::middleware::from_fn_with_state( |         .route_layer(axum::middleware::from_fn_with_state( | ||||||
|             ctx.clone(), |             ctx.clone(), | ||||||
|             super::auth::auth_web_middleware, |             super::auth::auth_web_middleware, | ||||||
|  | @ -70,3 +71,20 @@ async fn get_users( | ||||||
|         .authenticated(true) |         .authenticated(true) | ||||||
|         .response(&ctx.tera)) |         .response(&ctx.tera)) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | async fn delete_user( | ||||||
|  |     State(ctx): State<Context>, | ||||||
|  |     Extension(session): Extension<gpodder::Session>, | ||||||
|  |     Path(id): Path<i64>, | ||||||
|  | ) -> AppResult<()> { | ||||||
|  |     let deleted = | ||||||
|  |         tokio::task::spawn_blocking(move || ctx.store.admin(&session.user)?.remove_user(id)) | ||||||
|  |             .await | ||||||
|  |             .unwrap()?; | ||||||
|  | 
 | ||||||
|  |     if deleted { | ||||||
|  |         Ok(()) | ||||||
|  |     } else { | ||||||
|  |         Err(AppError::NotFound) | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue