From ac2723f898c45120ec023bbb2e0e66b26ad71a01 Mon Sep 17 00:00:00 2001
From: BlackDex <black.dex@gmail.com>
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<Route> {
@@ -253,8 +254,6 @@ fn get_users_json(_token: AdminToken, conn: DbConn) -> JsonResult {
 
 #[get("/users/overview")]
 fn users_overview(_token: AdminToken, conn: DbConn) -> ApiResult<Html<String>> {
-    use crate::util::get_display_size;
-
     let users = User::get_all(&conn);
     let users_json: Vec<Value> = 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<Html<String>> {
     let organizations = Organization::get_all(&conn);
-    let organizations_json: Vec<Value> = organizations.iter().map(|o| o.to_json()).collect();
+    let organizations_json: Vec<Value> = 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::<Self>(&**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::<i64>(&**conn)
+            .ok()
+            .unwrap_or(0)
+    }
+
     pub fn find_by_folder(folder_uuid: &str, conn: &DbConn) -> Vec<Self> {
         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::<i64>(&**conn)
+            .ok()
+            .unwrap_or(0)
+    }
+
     pub fn find_by_org_and_type(org_uuid: &str, atype: i32, conn: &DbConn) -> Vec<Self> {
         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 @@
     <div id="organizations-block" class="my-3 p-3 bg-white rounded shadow">
         <h6 class="border-bottom pb-2 mb-0">Organizations</h6>
 
-        <div id="organizations-list">
-            {{#each organizations}}
-            <div class="media pt-3">
-                <img class="mr-2 rounded identicon" data-src="{{Name}}_{{BillingEmail}}">
-                <div class="media-body pb-3 mb-0 small border-bottom">
-                    <div class="row justify-content-between">
-                        <div class="col">
+        <div class="table-responsive-xl small">
+            <table class="table table-sm table-striped table-hover">
+                <thead>
+                    <tr>
+                        <th style="width: 24px;" colspan="2">Organization</th>
+                        <th>Users</th>
+                        <th>Items</th>
+                        <th>Attachments</th>
+                    </tr>
+                </thead>
+                <tbody>
+                    {{#each organizations}}
+                    <tr>
+                        <td><img class="rounded identicon" data-src="{{Id}}"></td>
+                        <td>
                             <strong>{{Name}}</strong>
-                            {{#if Id}}
-                            <span class="badge badge-success ml-2">{{Id}}</span>
+                            <span class="mr-2">({{BillingEmail}})</span>
+                            <span class="d-block">
+                                <span class="badge badge-success">{{Id}}</span>
+                            </span>
+                        </td>
+                        <td>
+                            <span class="d-block">{{user_count}}</span>
+                        </td>
+                        <td>
+                            <span class="d-block">{{cipher_count}}</span>
+                        </td>
+                        <td>
+                            <span class="d-block"><strong>Amount:</strong> {{attachment_count}}</span>
+                            {{#if attachment_count}}
+                            <span class="d-block"><strong>Size:</strong> {{attachment_size}}</span>
                             {{/if}}
-                            <span class="d-block">{{BillingEmail}}</span>
-                        </div>
-                    </div>
-                </div>
-            </div>
-            {{/each}}
+                        </td>
+                    </tr>
+                    {{/each}}
+                </tbody>
+            </table>
         </div>
+        
     </div>
 </main>
 
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 @@
     <div id="users-block" class="my-3 p-3 bg-white rounded shadow">
         <h6 class="border-bottom pb-2 mb-0">Registered Users</h6>
 
-
-
         <div class="table-responsive-xl small">
             <table class="table table-sm table-striped table-hover">
                 <thead>