Add reset user config button

This commit is contained in:
Daniel García 2019-02-06 17:32:13 +01:00
parent 89840790e7
commit ef63342e20
No known key found for this signature in database
GPG key ID: FC8A7D14C3CD543A
3 changed files with 55 additions and 19 deletions

View file

@ -27,6 +27,7 @@ pub fn routes() -> Vec<Route> {
delete_user, delete_user,
deauth_user, deauth_user,
post_config, post_config,
delete_config,
] ]
} }
@ -173,6 +174,11 @@ fn post_config(data: Json<ConfigBuilder>, _token: AdminToken) -> EmptyResult {
CONFIG.update_config(data) CONFIG.update_config(data)
} }
#[post("/config/delete")]
fn delete_config(_token: AdminToken) -> EmptyResult {
CONFIG.delete_user_config()
}
pub struct AdminToken {} pub struct AdminToken {}
impl<'a, 'r> FromRequest<'a, 'r> for AdminToken { impl<'a, 'r> FromRequest<'a, 'r> for AdminToken {

View file

@ -13,17 +13,14 @@ lazy_static! {
} }
macro_rules! make_config { macro_rules! make_config {
( ($(
$(#[doc = $groupdoc:literal])?
$group:ident $(: $group_enabled:ident)? {
$( $(
$(#[doc = $groupdoc:literal])? $(#[doc = $doc:literal])+
$group:ident $(: $group_enabled:ident)? { $name:ident : $ty:ty, $editable:literal, $none_action:ident $(, $default:expr)?;
$( )+},
$(#[doc = $doc:literal])+ )+) => {
$name:ident : $ty:ty, $editable:literal, $none_action:ident $(, $default:expr)?;
)+
},)+
) => {
pub struct Config { inner: RwLock<Inner> } pub struct Config { inner: RwLock<Inner> }
struct Inner { struct Inner {
@ -199,7 +196,7 @@ make_config! {
rsa_key_filename: String, false, auto, |c| format!("{}/{}", c.data_folder, "rsa_key"); rsa_key_filename: String, false, auto, |c| format!("{}/{}", c.data_folder, "rsa_key");
/// Web vault folder /// Web vault folder
web_vault_folder: String, false, def, "web-vault/".to_string(); web_vault_folder: String, false, def, "web-vault/".to_string();
}, },
ws { ws {
/// Enable websocket notifications /// Enable websocket notifications
websocket_enabled: bool, false, def, false; websocket_enabled: bool, false, def, false;
@ -208,7 +205,7 @@ make_config! {
/// Websocket port /// Websocket port
websocket_port: u16, false, def, 3012; websocket_port: u16, false, def, 3012;
}, },
/// General settings /// General settings
settings { settings {
/// Domain URL |> This needs to be set to the URL used to access the server, including 'http[s]://' and port, if it's different than the default. Some server functions don't work correctly without this value /// Domain URL |> This needs to be set to the URL used to access the server, including 'http[s]://' and port, if it's different than the default. Some server functions don't work correctly without this value
@ -236,7 +233,7 @@ make_config! {
}, },
/// Advanced settings /// Advanced settings
advanced { advanced {
/// Positive icon cache expiry |> Number of seconds to consider that an already cached icon is fresh. After this period, the icon will be redownloaded /// Positive icon cache expiry |> Number of seconds to consider that an already cached icon is fresh. After this period, the icon will be redownloaded
icon_cache_ttl: u64, true, def, 2_592_000; icon_cache_ttl: u64, true, def, 2_592_000;
/// Negative icon cache expiry |> Number of seconds before trying to download an icon that failed again. /// Negative icon cache expiry |> Number of seconds before trying to download an icon that failed again.
@ -355,6 +352,28 @@ impl Config {
Ok(()) Ok(())
} }
pub fn delete_user_config(&self) -> Result<(), Error> {
crate::util::delete_file(&CONFIG_FILE)?;
// Empty user config
let usr = ConfigBuilder::default();
// Config now is env + defaults
let config = {
let env = &self.inner.read().unwrap()._env;
env.build()
};
// Save configs
{
let mut writer = self.inner.write().unwrap();
writer.config = config;
writer._usr = usr;
}
Ok(())
}
pub fn private_rsa_key(&self) -> String { pub fn private_rsa_key(&self) -> String {
format!("{}.der", CONFIG.rsa_key_filename()) format!("{}.der", CONFIG.rsa_key_filename())
} }
@ -366,14 +385,11 @@ impl Config {
} }
pub fn mail_enabled(&self) -> bool { pub fn mail_enabled(&self) -> bool {
let inner = &self.inner.read().unwrap().config; let inner = &self.inner.read().unwrap().config;
inner._enable_smtp inner._enable_smtp && inner.smtp_host.is_some()
&& inner.smtp_host.is_some()
} }
pub fn yubico_enabled(&self) -> bool { pub fn yubico_enabled(&self) -> bool {
let inner = &self.inner.read().unwrap().config; let inner = &self.inner.read().unwrap().config;
inner._enable_yubico inner._enable_yubico && inner.yubico_client_id.is_some() && inner.yubico_secret_key.is_some()
&& inner.yubico_client_id.is_some()
&& inner.yubico_secret_key.is_some()
} }
pub fn render_template<T: serde::ser::Serialize>( pub fn render_template<T: serde::ser::Serialize>(

View file

@ -61,7 +61,7 @@
{{#each config}} {{#each config}}
{{#if groupdoc}} {{#if groupdoc}}
<div class="card bg-light mb-3"> <div class="card bg-light mb-3">
<div class="card-header"><button class="btn btn-link collapsed" type="button" data-toggle="collapse" <div class="card-header"><button type="button" class="btn btn-link collapsed" data-toggle="collapse"
data-target="#g_{{group}}">{{groupdoc}}</button></div> data-target="#g_{{group}}">{{groupdoc}}</button></div>
<div id="g_{{group}}" class="card-body collapse" data-parent="#config-form"> <div id="g_{{group}}" class="card-body collapse" data-parent="#config-form">
{{#each elements}} {{#each elements}}
@ -95,6 +95,7 @@
{{/if}} {{/if}}
{{/each}} {{/each}}
<button type="submit" class="btn btn-primary">Save</button> <button type="submit" class="btn btn-primary">Save</button>
<button type="button" class="btn btn-danger float-right" onclick="deleteConfig();">Reset defaults</button>
</form> </form>
</div> </div>
</div> </div>
@ -176,6 +177,19 @@
"Error saving config", data); "Error saving config", data);
return false; return false;
} }
function deleteConfig() {
var input = prompt("This will remove all user configurations, and restore the defaults and the " +
"values set by the environment. This operation could be dangerous. Type 'DELETE' to proceed:");
if (input === "DELETE") {
_post("/admin/config/delete",
"Config deleted correctly",
"Error deleting config");
} else {
alert("Wrong input, please try again")
}
return false;
}
function masterCheck(check_id, inputs_query) { function masterCheck(check_id, inputs_query) {
function toggleEnabled(check_id, inputs_query, enabled) { function toggleEnabled(check_id, inputs_query, enabled) {
$(inputs_query).prop("disabled", !enabled) $(inputs_query).prop("disabled", !enabled)