implemented full handover

This commit is contained in:
Artemis 2025-02-13 17:03:19 +01:00
parent ea202cf393
commit 1ebe51124a
2 changed files with 49 additions and 25 deletions

View file

@ -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<Forms<'_>>,
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())
}

View file

@ -8,6 +8,12 @@
</p>
</aside>
{% if toast %}
<div class="raised">
<p>{{toast|capitalize}}</p>
</div>
{% endif %}
<section>
<h2>Service status</h2>
@ -53,4 +59,4 @@
</form>
</section>
{% endblock main %}
{% endblock main %}