diff --git a/src/routes/admin.rs b/src/routes/admin.rs index aa177dd..d79e96e 100644 --- a/src/routes/admin.rs +++ b/src/routes/admin.rs @@ -13,17 +13,24 @@ use crate::{ auth::session::Admin, db::{admin, doll, schema::DollTagsDb, user}, ids::{id_public_to_db, PublicId}, - pages::{write_toast, CommonTemplateState}, + pages::{pop_toast, write_toast, CommonTemplateState}, }; use super::error_handlers::PageResult; #[get("/")] -pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) -> PageResult { +pub async fn index( + meta: CommonTemplateState, + mut db: DollTagsDb, + user: Admin, + jar: &'_ CookieJar<'_>, +) -> PageResult { let mut trx = db.begin().await?; let service_status = admin::get_service_status(&mut trx).await?; trx.commit().await?; + let toast = pop_toast(jar); + Ok(Template::render( "admin/index", context! { @@ -33,6 +40,7 @@ pub async fn index(meta: CommonTemplateState, mut db: DollTagsDb, user: Admin) - form: "", tag_handover: Context::default(), }, + toast, }, ) .into()) @@ -80,18 +88,11 @@ pub async fn handle_in_page_forms( mut db: DollTagsDb, user: Admin, mut form: Form>, + client_ip: IpAddr, ) -> PageResult { - println!("{:?}", form); - match form.form { SelectedForm::TagHandover => { if let Some(values) = &form.tag_handover.value.clone() { - // work - println!( - "woof handover of {} to {}", - values.tag_id, values.dest_account - ); - let target_user = user::get(&mut *db, values.dest_account).await?; let user_valid = match target_user { Some(user) => { @@ -130,10 +131,18 @@ pub async fn handle_in_page_forms( } if user_valid && target_tag.is_some() { - let id = target_tag.unwrap().id; + let tag = target_tag.unwrap(); + warn!( + "[audit|{}] [{}] beginning tag handover of tag {} to \"{}\" (previous user id: {})", + client_ip, + user.0.id.to_string(), + tag.id, + values.dest_account, + tag.bound_to_id, + ); return Ok(Redirect::to(uri!( "/admin", - show_confirm_tag_handover(PublicId(id), values.dest_account) + show_confirm_tag_handover(PublicId(tag.id), values.dest_account) )) .into()); } @@ -186,25 +195,34 @@ pub async fn handle_tag_handover<'a>( id: PublicId, dest_username: &'a str, ) -> PageResult { - let dest_user = match user::get(&mut *db, dest_username).await? { - Some(u) => u, - None => { + // note: there is currently no trace of the previous user in this handover code's audit log. + let id = id.0; + match user::get(&mut *db, dest_username).await? { + Some(u) => { + admin::handover_tag(&mut *db, id, &u.id).await?; warn!( - "[audit|{}] [{}] tried to hand over tag {} to nonexistent user {}", + "[audit|{}] [{}] handed over tag {} to \"{}\"", client_ip, user.0.id.to_string(), - id.0, + id, + dest_username + ); + write_toast( + jar, + format!("tag successfully handed over to \"{}\"", dest_username), + ); + } + None => { + warn!( + "[audit|{}] [{}] tried to hand over tag {} to nonexistent user \"{}\"", + client_ip, + user.0.id.to_string(), + id, dest_username ); write_toast(jar, String::from("this username doesn't exist")); - return Ok(Redirect::to("/admin").into()); } }; - admin::handover_tag(&mut *db, id.0, &dest_user.id).await?; - write_toast( - jar, - format!("tag successfully handed over to {}", dest_username), - ); - todo!("meow") + Ok(Redirect::to("/admin").into()) } diff --git a/templates/admin/index.html.tera b/templates/admin/index.html.tera index 38e9d81..010c3b0 100644 --- a/templates/admin/index.html.tera +++ b/templates/admin/index.html.tera @@ -8,6 +8,12 @@

+{% if toast %} +
+

{{toast|capitalize}}

+
+{% endif %} +

Service status

@@ -53,4 +59,4 @@
-{% endblock main %} +{% endblock main %} \ No newline at end of file