From 6bcc2f80b8f31a6d295aeb29ad402cd20fe3c09a Mon Sep 17 00:00:00 2001 From: tony Date: Sun, 4 Jun 2023 00:12:35 +0200 Subject: [PATCH] add command to set the allow registration status Co-Authored-By: Matthias Ahouansou --- src/api/client_server/account.rs | 2 +- src/service/admin/mod.rs | 21 +++++++++++++++++++++ src/service/globals/mod.rs | 13 +++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/api/client_server/account.rs b/src/api/client_server/account.rs index 0226abc7..1027f145 100644 --- a/src/api/client_server/account.rs +++ b/src/api/client_server/account.rs @@ -75,7 +75,7 @@ pub async fn get_register_available_route( /// - Creates a new account and populates it with default account data /// - If `inhibit_login` is false: Creates a device and returns device id and access_token pub async fn register_route(body: Ruma) -> Result { - if !services().globals.allow_registration() && body.appservice_info.is_none() { + if !services().globals.allow_registration().await && body.appservice_info.is_none() { return Err(Error::BadRequest( ErrorKind::Forbidden, "Registration has been disabled.", diff --git a/src/service/admin/mod.rs b/src/service/admin/mod.rs index 345268de..0abce70d 100644 --- a/src/service/admin/mod.rs +++ b/src/service/admin/mod.rs @@ -160,6 +160,9 @@ enum AdminCommand { password: Option, }, + /// Temporarily toggle user registration by passing either true or false as an argument, does not persist between restarts + AllowRegistration { status: Option }, + /// Disables incoming federation handling for a room. DisableRoom { room_id: Box }, /// Enables incoming federation handling for a room again. @@ -656,6 +659,24 @@ impl Service { "Created user with user_id: {user_id} and password: {password}" )) } + AdminCommand::AllowRegistration { status } => { + if let Some(status) = status { + services().globals.set_registration(status).await; + RoomMessageEventContent::text_plain(if status { + "Registration is now enabled" + } else { + "Registration is now disabled" + }) + } else { + RoomMessageEventContent::text_plain( + if services().globals.allow_registration().await { + "Registration is currently enabled" + } else { + "Registration is currently disabled" + }, + ) + } + } AdminCommand::DisableRoom { room_id } => { services().rooms.metadata.disable_room(&room_id, true)?; RoomMessageEventContent::text_plain("Room disabled.") diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 2373a270..47c4f890 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -55,6 +55,7 @@ pub struct Service { pub actual_destination_cache: Arc>, // actual_destination, host pub tls_name_override: Arc>, pub config: Config, + allow_registration: RwLock, keypair: Arc, dns_resolver: TokioAsyncResolver, jwt_decoding_key: Option, @@ -184,6 +185,7 @@ impl Service { let unstable_room_versions = vec![RoomVersionId::V3, RoomVersionId::V4, RoomVersionId::V5]; let mut s = Self { + allow_registration: RwLock::new(config.allow_registration), db, config, keypair: Arc::new(keypair), @@ -285,8 +287,15 @@ impl Service { self.config.max_fetch_prev_events } - pub fn allow_registration(&self) -> bool { - self.config.allow_registration + /// Allows for the temporary (non-persistant) toggling of registration + pub async fn set_registration(&self, status: bool) { + let mut lock = self.allow_registration.write().await; + *lock = status; + } + + /// Checks whether user registration is allowed + pub async fn allow_registration(&self) -> bool { + *self.allow_registration.read().await } pub fn allow_encryption(&self) -> bool {