Skip to content

Commit

Permalink
Don't update non editable fields from the API
Browse files Browse the repository at this point in the history
  • Loading branch information
dani-garcia committed Jan 24, 2025
1 parent c0be36a commit e85a42a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/api/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ fn get_diagnostics_http(code: u16, _token: AdminToken) -> EmptyResult {
#[post("/config", data = "<data>")]
fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult {
let data: ConfigBuilder = data.into_inner();
if let Err(e) = CONFIG.update_config(data) {
if let Err(e) = CONFIG.update_config(data, true) {
err!(format!("Unable to save config: {e:?}"))
}
Ok(())
Expand Down
21 changes: 17 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ macro_rules! make_config {
serde_json::from_str(&config_str).map_err(Into::into)
}

fn clear_non_editable(&mut self) {
$($(
if !$editable {
self.$name = None;
}
)+)+
}

/// Merges the values of both builders into a new builder.
/// If both have the same element, `other` wins.
fn merge(&self, other: &Self, show_overrides: bool, overrides: &mut Vec<String>) -> Self {
Expand Down Expand Up @@ -677,7 +685,7 @@ make_config! {
/// Use Sendmail |> Whether to send mail via the `sendmail` command
use_sendmail: bool, true, def, false;
/// Sendmail Command |> Which sendmail command to use. The one found in the $PATH is used if not specified.
sendmail_command: String, true, option;
sendmail_command: String, false, option;
/// Host
smtp_host: String, true, option;
/// DEPRECATED smtp_ssl |> DEPRECATED - Please use SMTP_SECURITY
Expand Down Expand Up @@ -1146,12 +1154,17 @@ impl Config {
})
}

pub fn update_config(&self, other: ConfigBuilder) -> Result<(), Error> {
pub fn update_config(&self, other: ConfigBuilder, ignore_non_editable: bool) -> Result<(), Error> {
// Remove default values
//let builder = other.remove(&self.inner.read().unwrap()._env);

// TODO: Remove values that are defaults, above only checks those set by env and not the defaults
let builder = other;
let mut builder = other;

// Remove values that are not editable
if ignore_non_editable {
builder.clear_non_editable();
}

// Serialize now before we consume the builder
let config_str = serde_json::to_string_pretty(&builder)?;
Expand Down Expand Up @@ -1186,7 +1199,7 @@ impl Config {
let mut _overrides = Vec::new();
usr.merge(&other, false, &mut _overrides)
};
self.update_config(builder)
self.update_config(builder, false)
}

/// Tests whether an email's domain is allowed. A domain is allowed if it
Expand Down

0 comments on commit e85a42a

Please sign in to comment.