Skip to content

Commit

Permalink
feat: [#654] remove config option email_verification_enabled
Browse files Browse the repository at this point in the history
The config option:

```toml
[mail]
email_verification_enabled = false
```

was replaced with:

```toml
[registration]

  [registration.email]
  verified = true
```
  • Loading branch information
josecelano committed Jul 9, 2024
1 parent 29cc9dc commit e0dbdbe
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 46 deletions.
1 change: 0 additions & 1 deletion src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 0 additions & 9 deletions src/config/v2/mail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(),
Expand All @@ -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")
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
//!
Expand Down
8 changes: 6 additions & 2 deletions src/services/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) = &registration.email {
if email.verified && !user_profile.email_verified {
return Err(ServiceError::EmailNotVerified);
}
}
}

// Drop read lock on settings
Expand Down
26 changes: 16 additions & 10 deletions src/services/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &registration_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 &registration.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, &registration_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)
Expand Down
2 changes: 0 additions & 2 deletions src/web/api/client/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -179,7 +178,6 @@ impl From<DomainDatabase> for Database {
impl From<DomainMail> 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),
Expand Down
50 changes: 31 additions & 19 deletions src/web/api/server/v1/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -79,8 +91,8 @@
//! }
//! },
//! "tracker_statistics_importer": {
//! "port": 3002,
//! "torrent_info_update_interval": 3600
//! "port": 3002
//! }
//! }
//! }
Expand All @@ -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"
//! ```
//!
Expand Down
2 changes: 0 additions & 2 deletions tests/common/contexts/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -206,7 +205,6 @@ impl From<DomainDatabase> for Database {
impl From<DomainMail> 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(),
Expand Down

0 comments on commit e0dbdbe

Please sign in to comment.