Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 11 additions & 5 deletions crates/defguard_core/src/db/models/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::collections::HashMap;
use sqlx::{PgExecutor, PgPool, Type, query, query_as};
use struct_patch::Patch;
use thiserror::Error;
use uuid::Uuid;

use crate::{enterprise::ldap::sync::SyncStatus, global_value, secret::SecretStringWrapper};

Expand Down Expand Up @@ -89,7 +90,7 @@ pub struct Settings {
pub enrollment_use_welcome_message_as_email: bool,
// Instance UUID needed for desktop client
#[serde(skip)]
pub uuid: uuid::Uuid,
pub uuid: Uuid,
// LDAP
pub ldap_url: Option<String>,
pub ldap_bind_username: Option<String>,
Expand Down Expand Up @@ -145,8 +146,8 @@ impl Settings {
ldap_group_search_base, ldap_user_search_base, ldap_user_obj_class, \
ldap_group_obj_class, ldap_username_attr, ldap_groupname_attr, \
ldap_group_member_attr, ldap_member_attr, openid_create_account, \
license, gateway_disconnect_notifications_enabled, ldap_use_starttls, ldap_tls_verify_cert, \
gateway_disconnect_notifications_inactivity_threshold, \
license, gateway_disconnect_notifications_enabled, ldap_use_starttls, \
ldap_tls_verify_cert, gateway_disconnect_notifications_inactivity_threshold, \
gateway_disconnect_notifications_reconnect_notification_enabled, \
ldap_sync_status \"ldap_sync_status: SyncStatus\", \
ldap_enabled, ldap_sync_enabled, ldap_is_authoritative, \
Expand All @@ -160,9 +161,14 @@ impl Settings {
}

/// Checks if given settings are correct
pub fn validate(&self) -> Result<(), SettingsValidationError> {
pub fn validate(&mut self) -> Result<(), SettingsValidationError> {
debug!("Validating settings: {self:?}");
// check if gateway disconnect notifications can be enabled, since it requires SMTP to be configured
if self.uuid.is_nil() {
warn!("Detected empty UUID in settings. Generating a new one.");
self.uuid = Uuid::new_v4();
}
// Check if gateway disconnect notifications can be enabled, since it requires SMTP to be
// configured.
if self.gateway_disconnect_notifications_enabled && !self.smtp_configured() {
warn!("Cannot enable gateway disconnect notifications. SMTP is not configured.");
return Err(SettingsValidationError::CannotEnableGatewayNotifications);
Expand Down
8 changes: 3 additions & 5 deletions crates/defguard_core/src/handlers/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,23 @@ pub async fn get_settings(_admin: AdminRole, State(appstate): State<AppState>) -
});
}
debug!("Retrieved settings");
Ok(ApiResponse {
json: json!({}),
status: StatusCode::OK,
})
Ok(ApiResponse::default())
}

pub async fn update_settings(
_admin: AdminRole,
session: SessionInfo,
context: ApiRequestContext,
State(appstate): State<AppState>,
Json(data): Json<Settings>,
Json(mut data): Json<Settings>,
) -> ApiResult {
debug!("User {} updating settings", session.user.username);

// fetch current settings for event
let before = Settings::get_current_settings();

update_cached_license(data.license.as_deref())?;
data.uuid = before.uuid;
data.validate()?;
// clone for event
let after = data.clone();
Expand Down
5 changes: 3 additions & 2 deletions crates/defguard_core/src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static MAIL_PASSWORD_RESET_START: &str =
include_str!("../templates/mail_password_reset_start.tera");
static MAIL_PASSWORD_RESET_SUCCESS: &str =
include_str!("../templates/mail_password_reset_success.tera");
static MAIL_DATETIME_FORMAT: &str = "%A, %B %d, %Y at %r";

#[derive(Error, Debug)]
pub enum TemplateError {
Expand Down Expand Up @@ -78,7 +79,7 @@ fn get_base_tera(
let now = Utc::now();
let current_year = format!("{:04}", now.year());
context.insert("current_year", &current_year);
context.insert("date_now", &now.format("%A, %B %d, %Y at %r").to_string());
context.insert("date_now", &now.format(MAIL_DATETIME_FORMAT).to_string());

if let Some(current_session) = session {
let device_info = &current_session.device_info;
Expand Down Expand Up @@ -239,7 +240,7 @@ pub fn new_device_login_mail(
tera.add_raw_template("mail_base", MAIL_BASE)?;
context.insert(
"date_now",
&created.format("%A, %B %d, %Y at %r").to_string(),
&created.format(MAIL_DATETIME_FORMAT).to_string(),
);

tera.add_raw_template("mail_new_device_login", MAIL_NEW_DEVICE_LOGIN)?;
Expand Down