Skip to main content

tuwunel_api/client/admin/users/
reset_password.rs

1use axum::extract::State;
2use futures::StreamExt;
3use synapse_admin_api::users::reset_password::v1 as reset_password;
4use tuwunel_core::{Err, Result, utils::stream::automatic_width};
5
6use crate::{
7	Ruma,
8	client::admin::{refuse_server_user, require_admin},
9};
10
11/// # `POST /_synapse/admin/v1/reset_password/{user_id}`
12pub(crate) async fn admin_reset_password_route(
13	State(services): State<crate::State>,
14	body: Ruma<reset_password::Request>,
15) -> Result<reset_password::Response> {
16	require_admin(&services, body.sender_user()).await?;
17	refuse_server_user(services, &body.user_id)?;
18
19	if !services.users.exists(&body.user_id).await {
20		return Err!(Request(NotFound("User not found")));
21	}
22
23	services
24		.users
25		.set_password(&body.user_id, Some(&body.new_password))
26		.await?;
27
28	if body.logout_devices {
29		services
30			.users
31			.all_device_ids(&body.user_id)
32			.map(ToOwned::to_owned)
33			.for_each_concurrent(automatic_width(), async |device_id| {
34				services
35					.users
36					.remove_device(&body.user_id, &device_id)
37					.await;
38			})
39			.await;
40	}
41
42	Ok(reset_password::Response::new())
43}