diff --git a/src/api/admin.rs b/src/api/admin.rs index af3fa521..1cddf629 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -21,27 +21,53 @@ pub fn routes() -> Vec { routes![admin_login, post_admin_login, admin_page, invite_user, delete_user] } -#[derive(FromForm)] -struct LoginForm { - token: String, -} - const COOKIE_NAME: &'static str = "BWRS_ADMIN"; const ADMIN_PATH: &'static str = "/admin"; +#[derive(Serialize)] +struct AdminTemplateData { + users: Vec, + page_content: String, + error: Option, +} + +impl AdminTemplateData { + fn login(error: Option) -> Self { + Self { + users: Vec::new(), + page_content: String::from("admin/admin_login"), + error, + } + } + + fn admin(users: Vec) -> Self { + Self { + users, + page_content: String::from("admin/admin_page"), + error: None, + } + } + + fn render(self) -> Result { + CONFIG.templates.render("admin/admin_base", &self).map_err(Into::into) + } +} + #[get("/", rank = 2)] fn admin_login(flash: Option) -> Result, Error> { // If there is an error, show it - let msg = flash - .map(|msg| format!("{}: {}", msg.name(), msg.msg())) - .unwrap_or_default(); - let error = json!({ "error": msg }); + let msg = flash.map(|msg| format!("{}: {}", msg.name(), msg.msg())); // Return the page - let text = CONFIG.templates.render("admin/admin_login", &error)?; + let text = AdminTemplateData::login(msg).render()?; Ok(Html(text)) } +#[derive(FromForm)] +struct LoginForm { + token: String, +} + #[post("/", data = "")] fn post_admin_login(data: Form, mut cookies: Cookies, ip: ClientIp) -> Result> { let data = data.into_inner(); @@ -74,19 +100,12 @@ fn _validate_token(token: &str) -> bool { } } -#[derive(Serialize)] -struct AdminTemplateData { - users: Vec, -} - #[get("/", rank = 1)] fn admin_page(_token: AdminToken, conn: DbConn) -> Result, Error> { let users = User::get_all(&conn); let users_json: Vec = users.iter().map(|u| u.to_json(&conn)).collect(); - let data = AdminTemplateData { users: users_json }; - - let text = CONFIG.templates.render("admin/admin_page", &data)?; + let text = AdminTemplateData::admin(users_json).render()?; Ok(Html(text)) } diff --git a/src/main.rs b/src/main.rs index 286824fa..028d26e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -352,6 +352,7 @@ fn load_templates(path: String) -> Handlebars { reg!("email/pw_hint_some"); reg!("email/send_org_invite"); + reg!("admin/admin_base"); reg!("admin/admin_login"); reg!("admin/admin_page"); diff --git a/src/static/templates/admin/admin_base.hbs b/src/static/templates/admin/admin_base.hbs new file mode 100644 index 00000000..a715f1b6 --- /dev/null +++ b/src/static/templates/admin/admin_base.hbs @@ -0,0 +1,48 @@ + + + + + + + Bitwarden_rs Admin Panel + + + + + + + + + + + + + {{> (page_content) }} + + + \ No newline at end of file diff --git a/src/static/templates/admin/admin_login.hbs b/src/static/templates/admin/admin_login.hbs index 82b883be..088c8bf0 100644 --- a/src/static/templates/admin/admin_login.hbs +++ b/src/static/templates/admin/admin_login.hbs @@ -1,54 +1,21 @@ - - - - - - - Bitwarden_rs Admin Panel - - - - - - - - - -
- {{#if error}} -
-
-
{{error}}
-
+
+ {{/if}} + +
+
+
Authentication key needed to continue
+ Please provide it below: + +
+ + +
- {{/if}} - -
-
-
Authentication key needed to continue
- Please provide it below: - -
- - -
-
-
-
- - - \ No newline at end of file + + \ No newline at end of file diff --git a/src/static/templates/admin/admin_page.hbs b/src/static/templates/admin/admin_page.hbs index c8e02e40..4b55cea3 100644 --- a/src/static/templates/admin/admin_page.hbs +++ b/src/static/templates/admin/admin_page.hbs @@ -1,124 +1,83 @@ - - +
+
+
Registered Users
- - - - Bitwarden_rs Admin Panel +
+ {{#each users}} +
+ +
+
+ {{Name}} + Delete User +
+ {{Email}} +
+
+ {{/each}} - - - - +
- + + Reload users + +
- - - - - -
-
-
Registered Users
- -
- {{#each users}} -
- {{!-- row.find(".tmp-icon").attr("src", identicon(user.Email)) --}} - -
-
- {{Name}} - Delete User -
- {{Email}} -
-
- {{/each}} - -
- - - Reload users - -
- -
-
-
Invite User
- Email: - -
- - -
-
-
-
- - - \ No newline at end of file + }); + \ No newline at end of file