Make sure the backup option only appears when using sqlite

This commit is contained in:
Daniel García 2019-06-02 00:08:52 +02:00
parent 1322b876e9
commit 5710703c50
No known key found for this signature in database
GPG Key ID: FC8A7D14C3CD543A
2 changed files with 17 additions and 3 deletions

View File

@ -1,4 +1,5 @@
use serde_json::Value; use serde_json::Value;
use std::process::Command;
use rocket::http::{Cookie, Cookies, SameSite}; use rocket::http::{Cookie, Cookies, SameSite};
use rocket::request::{self, FlashMessage, Form, FromRequest, Request}; use rocket::request::{self, FlashMessage, Form, FromRequest, Request};
@ -9,7 +10,7 @@ use rocket_contrib::json::Json;
use crate::api::{ApiResult, EmptyResult, JsonResult}; use crate::api::{ApiResult, EmptyResult, JsonResult};
use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp}; use crate::auth::{decode_admin, encode_jwt, generate_admin_claims, ClientIp};
use crate::config::ConfigBuilder; use crate::config::ConfigBuilder;
use crate::db::{models::*, DbConn, backup_database}; use crate::db::{backup_database, models::*, DbConn};
use crate::error::Error; use crate::error::Error;
use crate::mail; use crate::mail;
use crate::CONFIG; use crate::CONFIG;
@ -34,6 +35,10 @@ pub fn routes() -> Vec<Route> {
] ]
} }
lazy_static! {
static ref CAN_BACKUP: bool = cfg!(feature = "sqlite") && Command::new("sqlite").arg("-version").status().is_ok();
}
#[get("/")] #[get("/")]
fn admin_disabled() -> &'static str { fn admin_disabled() -> &'static str {
"The admin panel is disabled, please configure the 'ADMIN_TOKEN' variable to enable it" "The admin panel is disabled, please configure the 'ADMIN_TOKEN' variable to enable it"
@ -102,6 +107,7 @@ struct AdminTemplateData {
version: Option<&'static str>, version: Option<&'static str>,
users: Vec<Value>, users: Vec<Value>,
config: Value, config: Value,
can_backup: bool,
} }
impl AdminTemplateData { impl AdminTemplateData {
@ -111,6 +117,7 @@ impl AdminTemplateData {
version: VERSION, version: VERSION,
users, users,
config: CONFIG.prepare_json(), config: CONFIG.prepare_json(),
can_backup: *CAN_BACKUP,
} }
} }
@ -207,7 +214,11 @@ fn delete_config(_token: AdminToken) -> EmptyResult {
#[post("/config/backup_db")] #[post("/config/backup_db")]
fn backup_db(_token: AdminToken) -> EmptyResult { fn backup_db(_token: AdminToken) -> EmptyResult {
backup_database() if *CAN_BACKUP {
backup_database()
} else {
err!("Can't back up current DB (either it's not SQLite or the 'sqlite' binary is not present)");
}
} }
pub struct AdminToken {} pub struct AdminToken {}

View File

@ -157,9 +157,11 @@
</div> </div>
</div> </div>
{{#if can_backup}}
<div class="card bg-light mb-3"> <div class="card bg-light mb-3">
<div class="card-header"><button type="button" class="btn btn-link collapsed" data-toggle="collapse" <div class="card-header"><button type="button" class="btn btn-link collapsed" data-toggle="collapse"
data-target="#g_database">Database</button></div> data-target="#g_database">Backup Database</button></div>
<div id="g_database" class="card-body collapse" data-parent="#config-form"> <div id="g_database" class="card-body collapse" data-parent="#config-form">
<div class="small mb-3"> <div class="small mb-3">
NOTE: A local installation of sqlite3 is required for this section to work. NOTE: A local installation of sqlite3 is required for this section to work.
@ -167,6 +169,7 @@
<button type="button" class="btn btn-primary" onclick="backupDatabase();">Backup Database</button> <button type="button" class="btn btn-primary" onclick="backupDatabase();">Backup Database</button>
</div> </div>
</div> </div>
{{/if}}
<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="deleteConf();">Reset defaults</button> <button type="button" class="btn btn-danger float-right" onclick="deleteConf();">Reset defaults</button>