Skip to main content

tuwunel_admin/user/
add_email.rs

1use ruma::{MilliSecondsSinceUnixEpoch, thirdparty::Medium};
2use tuwunel_core::{Err, Result};
3use tuwunel_service::threepid::canonicalize_email;
4
5use crate::{admin_command, utils::parse_active_local_user_id};
6
7#[admin_command]
8pub(super) async fn add_email(&self, username: String, address: String) -> Result {
9	let user_id = parse_active_local_user_id(self.services, &username).await?;
10
11	if user_id == self.services.globals.server_user {
12		return Err!("Not allowed to bind an email address to the server account.");
13	}
14
15	let email_canon = canonicalize_email(&address)?;
16
17	if self
18		.services
19		.threepid
20		.user_id_for_email(&email_canon)
21		.await?
22		.is_some_and(|bound| bound != user_id)
23	{
24		return Err!("Email {email_canon} is already bound to another user.");
25	}
26
27	let now = MilliSecondsSinceUnixEpoch::now();
28
29	self.services
30		.threepid
31		.put_binding(&user_id, &email_canon, Medium::Email, now, now)
32		.await;
33
34	write!(self, "Bound email {email_canon} to user {user_id}.").await
35}