From 2fffaec226e2dcfb9b013aa985049e368b88f368 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 3 Jun 2020 17:57:03 +0200 Subject: [PATCH] Added attachment info per user and some layout fix - Added the amount and size of the attachments per user - Changed the items count function a bit - Some small layout changes --- src/api/admin.rs | 8 +++++--- src/db/models/attachment.rs | 10 ++++++++++ src/db/models/cipher.rs | 3 ++- src/static/templates/admin/users.hbs | 9 ++++++++- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 46338a9..4921c30 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -253,13 +253,15 @@ fn get_users_json(_token: AdminToken, conn: DbConn) -> JsonResult { #[get("/users/overview")] fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { + use crate::util::get_display_size; + let users = User::get_all(&conn); let users_json: Vec = users.iter() .map(|u| { let mut usr = u.to_json(&conn); - if let Some(ciphers) = Cipher::count_owned_by_user(&u.uuid, &conn) { - usr["cipher_count"] = json!(ciphers); - }; + usr["cipher_count"] = json!(Cipher::count_owned_by_user(&u.uuid, &conn)); + usr["attachment_count"] = json!(Attachment::count_by_user(&u.uuid, &conn)); + usr["attachment_size"] = json!(get_display_size(Attachment::size_by_user(&u.uuid, &conn) as i32)); usr }).collect(); diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index f0efa93..58f893b 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -130,6 +130,16 @@ impl Attachment { result.unwrap_or(0) } + pub fn count_by_user(user_uuid: &str, conn: &DbConn) -> i64 { + attachments::table + .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) + .filter(ciphers::user_uuid.eq(user_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn size_by_org(org_uuid: &str, conn: &DbConn) -> i64 { let result: Option = attachments::table .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 94d7d1e..2fb09c0 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -355,12 +355,13 @@ impl Cipher { .load::(&**conn).expect("Error loading ciphers") } - pub fn count_owned_by_user(user_uuid: &str, conn: &DbConn) -> Option { + pub fn count_owned_by_user(user_uuid: &str, conn: &DbConn) -> i64 { ciphers::table .filter(ciphers::user_uuid.eq(user_uuid)) .count() .first::(&**conn) .ok() + .unwrap_or(0) } pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec { diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index 758031f..297a04f 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -10,7 +10,8 @@ User - Items + Items + Attachments Organizations Actions @@ -37,6 +38,12 @@ {{cipher_count}} + + Amount: {{attachment_count}} + {{#if attachment_count}} + Size: {{attachment_size}} + {{/if}} + {{#each Organizations}} {{Name}}