diff --git a/src/config/mod.rs b/src/config/mod.rs index 99550c0a..87154ed8 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -433,7 +433,6 @@ mod tests { connect_url = "sqlite://data.db?mode=rwc" [mail] - email_verification_enabled = false from = "example@email.com" reply_to = "noreply@email.com" diff --git a/src/config/v2/mail.rs b/src/config/v2/mail.rs index e171d41b..296b4d19 100644 --- a/src/config/v2/mail.rs +++ b/src/config/v2/mail.rs @@ -4,10 +4,6 @@ use serde::{Deserialize, Serialize}; /// SMTP configuration. #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct Mail { - /// Whether or not to enable email verification on signup. - #[serde(default = "Mail::default_email_verification_enabled")] - pub email_verification_enabled: bool, - /// The email address to send emails from. #[serde(default = "Mail::default_from")] pub from: Mailbox, @@ -24,7 +20,6 @@ pub struct Mail { impl Default for Mail { fn default() -> Self { Self { - email_verification_enabled: Self::default_email_verification_enabled(), from: Self::default_from(), reply_to: Self::default_reply_to(), smtp: Self::default_smtp(), @@ -33,10 +28,6 @@ impl Default for Mail { } impl Mail { - fn default_email_verification_enabled() -> bool { - false - } - fn default_from() -> Mailbox { "example@email.com".parse().expect("valid mailbox") } diff --git a/src/lib.rs b/src/lib.rs index 6a14843e..6b9e3d54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -191,7 +191,6 @@ //! connect_url = "sqlite://data.db?mode=rwc" //! //! [mail] -//! email_verification_enabled = false //! from = "example@email.com" //! reply_to = "noreply@email.com" //! diff --git a/src/services/authentication.rs b/src/services/authentication.rs index cd9dd9d3..f347a857 100644 --- a/src/services/authentication.rs +++ b/src/services/authentication.rs @@ -70,8 +70,12 @@ impl Service { let settings = self.configuration.settings.read().await; // Fail login if email verification is required and this email is not verified - if settings.mail.email_verification_enabled && !user_profile.email_verified { - return Err(ServiceError::EmailNotVerified); + if let Some(registration) = &settings.registration { + if let Some(email) = ®istration.email { + if email.verified && !user_profile.email_verified { + return Err(ServiceError::EmailNotVerified); + } + } } // Drop read lock on settings diff --git a/src/services/user.rs b/src/services/user.rs index 7185fe65..ff885dcd 100644 --- a/src/services/user.rs +++ b/src/services/user.rs @@ -125,18 +125,24 @@ impl RegistrationService { drop(self.user_repository.grant_admin_role(&user_id).await); } - if settings.mail.email_verification_enabled { - if let Some(email) = opt_email { - let mail_res = self - .mailer - .send_verification_mail(&email, ®istration_form.username, user_id, api_base_url) - .await; - - if mail_res.is_err() { - drop(self.user_repository.delete(&user_id).await); - return Err(ServiceError::FailedToSendVerificationEmail); + match ®istration.email { + Some(email) => { + if email.verified { + // Email verification is enabled + if let Some(email) = opt_email { + let mail_res = self + .mailer + .send_verification_mail(&email, ®istration_form.username, user_id, api_base_url) + .await; + + if mail_res.is_err() { + drop(self.user_repository.delete(&user_id).await); + return Err(ServiceError::FailedToSendVerificationEmail); + } + } } } + None => (), } Ok(user_id) diff --git a/src/web/api/client/v1/contexts/settings/mod.rs b/src/web/api/client/v1/contexts/settings/mod.rs index d5d148aa..fdc52518 100644 --- a/src/web/api/client/v1/contexts/settings/mod.rs +++ b/src/web/api/client/v1/contexts/settings/mod.rs @@ -66,7 +66,6 @@ pub struct Database { #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)] pub struct Mail { - pub email_verification_enabled: bool, pub from: String, pub reply_to: String, pub smtp: Smtp, @@ -179,7 +178,6 @@ impl From for Database { impl From for Mail { fn from(mail: DomainMail) -> Self { Self { - email_verification_enabled: mail.email_verification_enabled, from: mail.from.to_string(), reply_to: mail.reply_to.to_string(), smtp: Smtp::from(mail.smtp), diff --git a/src/web/api/server/v1/contexts/settings/mod.rs b/src/web/api/server/v1/contexts/settings/mod.rs index 4551b591..9d0e80a1 100644 --- a/src/web/api/server/v1/contexts/settings/mod.rs +++ b/src/web/api/server/v1/contexts/settings/mod.rs @@ -30,43 +30,55 @@ //! ```json //! { //! "data": { +//! "version": "2", +//! "logging": { +//! "threshold": "info" +//! }, //! "website": { //! "name": "Torrust" //! }, //! "tracker": { -//! "url": "udp://localhost:6969", -//! "mode": "public", //! "api_url": "http://localhost:1212/", -//! "token": "MyAccessToken", -//! "token_valid_seconds": 7257600 +//! "listed": false, +//! "private": false, +//! "token": "***", +//! "token_valid_seconds": 7257600, +//! "url": "udp://localhost:6969" //! }, //! "net": { -//! "port": 3001, -//! "base_url": null +//! "base_url": null, +//! "bind_address": "0.0.0.0:3001", +//! "tsl": null //! }, //! "auth": { -//! "min_password_length": 6, -//! "max_password_length": 64, -//! "secret_key": "MaxVerstappenWC2021" +//! "secret_key": "***", +//! "password_constraints": { +//! "max_password_length": 64, +//! "min_password_length": 6 +//! } //! }, //! "database": { -//! "connect_url": "sqlite://./storage/database/data.db?mode=rwc" +//! "connect_url": "sqlite://data.db?mode=rwc" //! }, //! "mail": { //! "email_verification_enabled": false, //! "from": "example@email.com", //! "reply_to": "noreply@email.com", -//! "username": "", -//! "password": "", -//! "server": "", -//! "port": 25 +//! "smtp": { +//! "port": 25, +//! "server": "", +//! "credentials": { +//! "password": "***", +//! "username": "" +//! } +//! } //! }, //! "image_cache": { -//! "max_request_timeout_ms": 1000, //! "capacity": 128000000, //! "entry_size_limit": 4000000, -//! "user_quota_period_seconds": 3600, -//! "user_quota_bytes": 64000000 +//! "max_request_timeout_ms": 1000, +//! "user_quota_bytes": 64000000, +//! "user_quota_period_seconds": 3600 //! }, //! "api": { //! "default_torrent_page_size": 10, @@ -79,8 +91,8 @@ //! } //! }, //! "tracker_statistics_importer": { +//! "port": 3002, //! "torrent_info_update_interval": 3600 -//! "port": 3002 //! } //! } //! } @@ -107,7 +119,7 @@ //! --header "Content-Type: application/json" \ //! --header "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyIjp7InVzZXJfaWQiOjEsInVzZXJuYW1lIjoiaW5kZXhhZG1pbiIsImFkbWluaXN0cmF0b3IiOnRydWV9LCJleHAiOjE2ODYyMTU3ODh9.4k8ty27DiWwOk4WVcYEhIrAndhpXMRWnLZ3i_HlJnvI" \ //! --request POST \ -//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"email_verification_enabled":false,"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ +//! --data '{"website":{"name":"Torrust"},"tracker":{"url":"udp://localhost:6969","mode":"public","api_url":"http://localhost:1212/","token":"MyAccessToken","token_valid_seconds":7257600},"net":{"port":3001,"base_url":null},"auth":{"min_password_length":6,"max_password_length":64,"secret_key":"MaxVerstappenWC2021"},"database":{"connect_url":"sqlite://./storage/database/data.db?mode=rwc"},"mail":{"from":"example@email.com","reply_to":"noreply@email.com","username":"","password":"","server":"","port":25},"image_cache":{"max_request_timeout_ms":1000,"capacity":128000000,"entry_size_limit":4000000,"user_quota_period_seconds":3600,"user_quota_bytes":64000000},"api":{"default_torrent_page_size":10,"max_torrent_page_size":30},"tracker_statistics_importer":{"torrent_info_update_interval":3600}}' \ //! "http://127.0.0.1:3001/v1/settings" //! ``` //! diff --git a/tests/common/contexts/settings/mod.rs b/tests/common/contexts/settings/mod.rs index 45f40e25..baa17610 100644 --- a/tests/common/contexts/settings/mod.rs +++ b/tests/common/contexts/settings/mod.rs @@ -72,7 +72,6 @@ pub struct Database { #[derive(Deserialize, Serialize, PartialEq, Debug, Clone)] pub struct Mail { - pub email_verification_enabled: bool, pub from: String, pub reply_to: String, pub smtp: Smtp, @@ -206,7 +205,6 @@ impl From for Database { impl From for Mail { fn from(mail: DomainMail) -> Self { Self { - email_verification_enabled: mail.email_verification_enabled, from: mail.from.to_string(), reply_to: mail.reply_to.to_string(), smtp: mail.smtp.into(),