From 4d341022d0546b121cef0da246e8b69eb79f76c7 Mon Sep 17 00:00:00 2001 From: Colin Reeder Date: Sun, 25 Oct 2020 22:29:12 -0600 Subject: Add UI for user suspension --- res/lang/en.ftl | 6 +++ src/resp_types.rs | 1 + src/routes/mod.rs | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) diff --git a/res/lang/en.ftl b/res/lang/en.ftl index 52c6764..b1892e1 100644 --- a/res/lang/en.ftl +++ b/res/lang/en.ftl @@ -137,6 +137,12 @@ user_edit_submit = Save user_edit_title = Edit Profile user_id_prompt = User ID: user_remote_note = This is a remote user, information on this page may be incomplete. +user_suspend = Suspend +user_suspend_title = Suspend User +user_suspend_question = Suspend this user? They will not be allowed to log in unless unsuspended. +user_suspend_undo = Unsuspend +user_suspend_yes = Yes, suspend +user_suspended_note = This user has been suspended. username_prompt = Username: view_at_source = View at Source view_more_comments = View More Comments diff --git a/src/resp_types.rs b/src/resp_types.rs index 63bfcc2..0382cc9 100644 --- a/src/resp_types.rs +++ b/src/resp_types.rs @@ -146,6 +146,7 @@ pub struct RespUserInfo<'a> { #[serde(flatten)] pub base: RespMinimalAuthorInfo<'a>, pub description: Cow<'a, str>, + pub suspended: Option, pub your_note: Option>, } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index aab5813..67d3ad4 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -1349,6 +1349,7 @@ async fn page_user( Some(render::rsx! {
{lang.tr("user_remote_note", None)} + {" "} {lang.tr("view_at_source", None)}{" ↗"}
}) @@ -1356,6 +1357,42 @@ async fn page_user( None // shouldn't ever happen } } + { + if base_data.is_site_admin() && user.as_ref().local { + Some(render::rsx! { + <> + { + if user.suspended == Some(true) { + Some(render::rsx! { +
+ {lang.tr("user_suspended_note", None)} + {" "} +
+ +
+
+ }) + } else { + None + } + } + { + if user.suspended == Some(false) { + Some(render::rsx! { +
+ {lang.tr("user_suspend", None)} +
+ }) + } else { + None + } + } + + }) + } else { + None + } + } { if let Some(your_note) = &user.your_note { Some(render::rsx! { @@ -1520,6 +1557,96 @@ async fn handler_user_edit_submit( .body("Successfully created.".into())?) } +async fn page_user_suspend( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (user_id,) = params; + + let lang = crate::get_lang_for_req(&req); + let cookies = get_cookie_map_for_req(&req)?; + + let base_data = + fetch_base_data(&ctx.backend_host, &ctx.http_client, req.headers(), &cookies).await?; + + let title = lang.tr("user_suspend_title", None); + + Ok(html_response(render::html! { + +

{title.as_ref()}

+

+ {lang.tr("user_suspend_question", None)} +

+
+ {lang.tr("no_cancel", None)} + {" "} + +
+
+ })) +} + +async fn handler_user_suspend_submit( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (user_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::patch(format!( + "{}/api/unstable/users/{}", + ctx.backend_host, user_id + )) + .body(r#"{"suspended":true}"#.into())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/users/{}", user_id)) + .body("Successfully suspended.".into())?) +} + +async fn handler_user_suspend_undo( + params: (i64,), + ctx: Arc, + req: hyper::Request, +) -> Result, crate::Error> { + let (user_id,) = params; + + let cookies = get_cookie_map_for_req(&req)?; + + res_to_error( + ctx.http_client + .request(for_client( + hyper::Request::patch(format!( + "{}/api/unstable/users/{}", + ctx.backend_host, user_id + )) + .body(r#"{"suspended":false}"#.into())?, + req.headers(), + &cookies, + )?) + .await?, + ) + .await?; + + Ok(hyper::Response::builder() + .status(hyper::StatusCode::SEE_OTHER) + .header(hyper::header::LOCATION, format!("/users/{}", user_id)) + .body("Successfully unsuspended.".into())?) +} + async fn page_user_your_note_edit( params: (i64,), ctx: Arc, @@ -1820,6 +1947,21 @@ pub fn route_root() -> crate::RouteNode<()> { .with_handler_async("POST", handler_user_edit_submit), ), ) + .with_child( + "suspend", + crate::RouteNode::new() + .with_handler_async("GET", page_user_suspend) + .with_child( + "submit", + crate::RouteNode::new() + .with_handler_async("POST", handler_user_suspend_submit), + ) + .with_child( + "undo", + crate::RouteNode::new() + .with_handler_async("POST", handler_user_suspend_undo), + ), + ) .with_child( "your_note/edit", crate::RouteNode::new() -- cgit v1.2.3