diff --git a/src/api.rs b/src/api.rs index a9af3bf..4eff209 100644 --- a/src/api.rs +++ b/src/api.rs @@ -30,20 +30,6 @@ use vfs::VFSSource; const CURRENT_MAJOR_VERSION: i32 = 2; const CURRENT_MINOR_VERSION: i32 = 1; -#[derive(Serialize)] -struct Version { - major: i32, - minor: i32, -} - -impl Version { - fn new(major: i32, minor: i32) -> Version { - Version { - major: major, - minor: minor, - } - } -} #[derive(Deserialize, Serialize)] struct Session { @@ -89,10 +75,18 @@ fn get_endpoints(db: Arc) -> Mount { let mut api_handler = Mount::new(); { - let db = db.clone(); api_handler.mount("/version/", self::version); - api_handler.mount("/auth/", - move |request: &mut Request| self::auth(request, db.deref())); + { + let db = db.clone(); + api_handler.mount("/auth/", + move |request: &mut Request| self::auth(request, db.deref())); + } + { + let db = db.clone(); + api_handler.mount("/initial_setup/", move |request: &mut Request| { + self::initial_setup(request, db.deref()) + }); + } } { @@ -207,13 +201,39 @@ impl Handler for AuthHandler { } fn version(_: &mut Request) -> IronResult { - let current_version = Version::new(CURRENT_MAJOR_VERSION, CURRENT_MINOR_VERSION); + #[derive(Serialize)] + struct Version { + major: i32, + minor: i32, + } + + let current_version = Version { + major: CURRENT_MAJOR_VERSION, + minor: CURRENT_MINOR_VERSION, + }; + match serde_json::to_string(¤t_version) { Ok(result_json) => Ok(Response::with((status::Ok, result_json))), Err(e) => Err(IronError::new(e, status::InternalServerError)), } } +fn initial_setup(_: &mut Request, db: &DB) -> IronResult { + #[derive(Serialize)] + struct InitialSetup { + has_any_users: bool, + }; + + let initial_setup = InitialSetup { + has_any_users: user::count(db)? > 0, + }; + + match serde_json::to_string(&initial_setup) { + Ok(result_json) => Ok(Response::with((status::Ok, result_json))), + Err(e) => Err(IronError::new(e, status::InternalServerError)), + } +} + fn auth(request: &mut Request, db: &DB) -> IronResult { let username; let password; diff --git a/src/user.rs b/src/user.rs index 1d8d2f4..93be791 100644 --- a/src/user.rs +++ b/src/user.rs @@ -1,5 +1,6 @@ use core::ops::Deref; use diesel; +use diesel::expression; use diesel::prelude::*; use rand; use ring::{digest, pbkdf2}; @@ -69,3 +70,13 @@ pub fn auth(db: &T, username: &str, password: &str) -> Result Err(e) => Err(e.into()), } } + +pub fn count(db: &T) -> Result + where T: ConnectionSource +{ + use db::users::dsl::*; + let connection = db.get_connection(); + let connection = connection.lock().unwrap(); + let connection = connection.deref(); + Ok(users.select(expression::count(name)).first(connection)?) +}