From ac2723f898c45120ec023bbb2e0e66b26ad71a01 Mon Sep 17 00:00:00 2001 From: BlackDex Date: Wed, 3 Jun 2020 20:37:31 +0200 Subject: [PATCH] Updated Organizations overview - Changed HTML to match users overview - Added User count - Added Org cipher amount - Added Attachment count and size --- src/api/admin.rs | 12 +++-- src/db/models/attachment.rs | 10 ++++ src/db/models/cipher.rs | 9 ++++ src/db/models/organization.rs | 9 ++++ src/static/templates/admin/organizations.hbs | 51 ++++++++++++++------ src/static/templates/admin/users.hbs | 2 - 6 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/api/admin.rs b/src/api/admin.rs index 4921c30a..a897be1e 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -15,6 +15,7 @@ use crate::config::ConfigBuilder; use crate::db::{backup_database, models::*, DbConn}; use crate::error::Error; use crate::mail; +use crate::util::get_display_size; use crate::CONFIG; pub fn routes() -> Vec { @@ -253,8 +254,6 @@ 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| { @@ -312,7 +311,14 @@ fn update_revision_users(_token: AdminToken, conn: DbConn) -> EmptyResult { #[get("/organizations/overview")] fn organizations_overview(_token: AdminToken, conn: DbConn) -> ApiResult> { let organizations = Organization::get_all(&conn); - let organizations_json: Vec = organizations.iter().map(|o| o.to_json()).collect(); + let organizations_json: Vec = organizations.iter().map(|o| { + let mut org = o.to_json(); + org["user_count"] = json!(UserOrganization::count_by_org(&o.uuid, &conn)); + org["cipher_count"] = json!(Cipher::count_by_org(&o.uuid, &conn)); + org["attachment_count"] = json!(Attachment::count_by_org(&o.uuid, &conn)); + org["attachment_size"] = json!(get_display_size(Attachment::size_by_org(&o.uuid, &conn) as i32)); + org + }).collect(); let text = AdminTemplateData::organizations(organizations_json).render()?; Ok(Html(text)) diff --git a/src/db/models/attachment.rs b/src/db/models/attachment.rs index 58f893bc..b0e5003e 100644 --- a/src/db/models/attachment.rs +++ b/src/db/models/attachment.rs @@ -150,4 +150,14 @@ impl Attachment { result.unwrap_or(0) } + + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + attachments::table + .left_join(ciphers::table.on(ciphers::uuid.eq(attachments::cipher_uuid))) + .filter(ciphers::organization_uuid.eq(org_uuid)) + .count() + .first(&**conn) + .ok() + .unwrap_or(0) + } } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 2fb09c00..d0eb4e85 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -370,6 +370,15 @@ impl Cipher { .load::(&**conn).expect("Error loading ciphers") } + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + ciphers::table + .filter(ciphers::organization_uuid.eq(org_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec { folders_ciphers::table.inner_join(ciphers::table) .filter(folders_ciphers::folder_uuid.eq(folder_uuid)) diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 8ce476c6..fac5cad0 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -437,6 +437,15 @@ impl UserOrganization { .expect("Error loading user organizations") } + pub fn count_by_org(org_uuid: &str, conn: &DbConn) -> i64 { + users_organizations::table + .filter(users_organizations::org_uuid.eq(org_uuid)) + .count() + .first::(&**conn) + .ok() + .unwrap_or(0) + } + pub fn find_by_org_and_type(org_uuid: &str, atype: i32, conn: &DbConn) -> Vec { users_organizations::table .filter(users_organizations::org_uuid.eq(org_uuid)) diff --git a/src/static/templates/admin/organizations.hbs b/src/static/templates/admin/organizations.hbs index 6dbcbbbf..0646b4ea 100644 --- a/src/static/templates/admin/organizations.hbs +++ b/src/static/templates/admin/organizations.hbs @@ -2,24 +2,45 @@
Organizations
-
- {{#each organizations}} -
- -
-
-
+
+ + + + + + + + + + + {{#each organizations}} + + + + + + + + {{/each}} + +
OrganizationUsersItemsAttachments
{{Name}} - {{#if Id}} - {{Id}} + ({{BillingEmail}}) + + {{Id}} + + + {{user_count}} + + {{cipher_count}} + + Amount: {{attachment_count}} + {{#if attachment_count}} + Size: {{attachment_size}} {{/if}} - {{BillingEmail}} - - - - - {{/each}} +
+
diff --git a/src/static/templates/admin/users.hbs b/src/static/templates/admin/users.hbs index 297a04f0..5b99c854 100644 --- a/src/static/templates/admin/users.hbs +++ b/src/static/templates/admin/users.hbs @@ -2,8 +2,6 @@
Registered Users
- -