From a0796acbc73e8e7b9bcd94bab334cc82aa922835 Mon Sep 17 00:00:00 2001 From: Miroslav Prasil Date: Thu, 3 May 2018 17:47:27 +0100 Subject: [PATCH] Implement suggested improvements --- migrations/2018-04-27-155151_create_users_ciphers/up.sql | 4 ++-- src/api/core/ciphers.rs | 3 +-- src/api/identity.rs | 2 +- src/db/models/organization.rs | 4 ++-- src/db/models/user.rs | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/migrations/2018-04-27-155151_create_users_ciphers/up.sql b/migrations/2018-04-27-155151_create_users_ciphers/up.sql index a078879..12c096e 100644 --- a/migrations/2018-04-27-155151_create_users_ciphers/up.sql +++ b/migrations/2018-04-27-155151_create_users_ciphers/up.sql @@ -22,8 +22,8 @@ CREATE TABLE folders_ciphers ( PRIMARY KEY (cipher_uuid, folder_uuid) ); -INSERT INTO ciphers (uuid, created_at, updated_at, organization_uuid, type, name, notes, fields, data, favorite) -SELECT uuid, created_at, updated_at, organization_uuid, type, name, notes, fields, data, favorite FROM oldCiphers; +INSERT INTO ciphers (uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite) +SELECT uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite FROM oldCiphers; INSERT INTO folders_ciphers (cipher_uuid, folder_uuid) SELECT uuid, folder_uuid FROM oldCiphers WHERE folder_uuid IS NOT NULL; diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index a6b95b3..29cf2ae 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -253,8 +253,7 @@ fn post_ciphers_import(data: Json, headers: Headers, conn: DbConn) - if update_cipher_from_data(&mut cipher, cipher_data, &headers, &conn).is_err() { err!("Error creating cipher") } - //cipher.folder_uuid = folder_uuid; // TODO: This needs to create new folder-cipher mapping - + cipher.move_to_folder(folder_uuid, &headers.user.uuid.clone(), &conn).ok(); cipher.save(&conn); index += 1; } diff --git a/src/api/identity.rs b/src/api/identity.rs index 5bb7871..805c334 100644 --- a/src/api/identity.rs +++ b/src/api/identity.rs @@ -97,7 +97,7 @@ fn login(connect_data: Form, device_type: DeviceType, conn: DbConn) }; let user = User::find_by_uuid(&device.user_uuid, &conn).unwrap(); - let orgs = UserOrganization::find_by_user(&user.uuid, &conn).unwrap_or(vec![]); + let orgs = UserOrganization::find_by_user(&user.uuid, &conn); let (access_token, expires_in) = device.refresh_tokens(&user, orgs); device.save(&conn); diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 3f49280..3bfa3c7 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -222,10 +222,10 @@ impl UserOrganization { .first::(&**conn).ok() } - pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Option> { + pub fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec { users_organizations::table .filter(users_organizations::user_uuid.eq(user_uuid)) - .load::(&**conn).ok() + .load::(&**conn).unwrap_or(vec![]) } pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec { diff --git a/src/db/models/user.rs b/src/db/models/user.rs index be3d10b..0287d45 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -128,7 +128,7 @@ impl User { pub fn to_json(&self, conn: &DbConn) -> JsonValue { use super::UserOrganization; - let orgs = UserOrganization::find_by_user(&self.uuid, conn).unwrap_or(vec![]); + let orgs = UserOrganization::find_by_user(&self.uuid, conn); let orgs_json: Vec = orgs.iter().map(|c| c.to_json(&conn)).collect(); json!({