diff --git a/migrations/mysql/2022-07-27-110000_add_group_support/down.sql b/migrations/mysql/2022-07-27-110000_add_group_support/down.sql index 33b110e7..0ec5c501 100644 --- a/migrations/mysql/2022-07-27-110000_add_group_support/down.sql +++ b/migrations/mysql/2022-07-27-110000_add_group_support/down.sql @@ -1,3 +1,3 @@ -DROP TABLE ´groups´; +DROP TABLE `groups`; DROP TABLE groups_users; -DROP TABLE collection_groups; \ No newline at end of file +DROP TABLE collections_groups; \ No newline at end of file diff --git a/migrations/mysql/2022-07-27-110000_add_group_support/up.sql b/migrations/mysql/2022-07-27-110000_add_group_support/up.sql index ce0622fa..ccfe600b 100644 --- a/migrations/mysql/2022-07-27-110000_add_group_support/up.sql +++ b/migrations/mysql/2022-07-27-110000_add_group_support/up.sql @@ -9,12 +9,12 @@ CREATE TABLE `groups` ( ); CREATE TABLE groups_users ( - groups_uuid CHAR(36) NOT NULL REFERENCES `groups` (uuid), + groups_uuid CHAR(36) NOT NULL REFERENCES `groups` (uuid), users_organizations_uuid VARCHAR(36) NOT NULL REFERENCES users_organizations (uuid), UNIQUE (groups_uuid, users_organizations_uuid) ); -CREATE TABLE collection_groups ( +CREATE TABLE collections_groups ( collections_uuid VARCHAR(40) NOT NULL REFERENCES collections (uuid), groups_uuid CHAR(36) NOT NULL REFERENCES `groups` (uuid), read_only BOOLEAN NOT NULL, diff --git a/migrations/postgresql/2022-07-27-110000_add_group_support/down.sql b/migrations/postgresql/2022-07-27-110000_add_group_support/down.sql index cf9f1c8c..9a12d694 100644 --- a/migrations/postgresql/2022-07-27-110000_add_group_support/down.sql +++ b/migrations/postgresql/2022-07-27-110000_add_group_support/down.sql @@ -1,3 +1,3 @@ DROP TABLE groups; DROP TABLE groups_users; -DROP TABLE collection_groups; \ No newline at end of file +DROP TABLE collections_groups; \ No newline at end of file diff --git a/migrations/postgresql/2022-07-27-110000_add_group_support/up.sql b/migrations/postgresql/2022-07-27-110000_add_group_support/up.sql index 1e0d06da..46df867e 100644 --- a/migrations/postgresql/2022-07-27-110000_add_group_support/up.sql +++ b/migrations/postgresql/2022-07-27-110000_add_group_support/up.sql @@ -9,12 +9,12 @@ CREATE TABLE groups ( ); CREATE TABLE groups_users ( - groups_uuid CHAR(36) NOT NULL REFERENCES groups (uuid), + groups_uuid CHAR(36) NOT NULL REFERENCES groups (uuid), users_organizations_uuid VARCHAR(36) NOT NULL REFERENCES users_organizations (uuid), PRIMARY KEY (group_uuid, users_organizations_uuid) ); -CREATE TABLE collection_groups ( +CREATE TABLE collections_groups ( collections_uuid VARCHAR(40) NOT NULL REFERENCES collections (uuid), groups_uuid CHAR(36) NOT NULL REFERENCES groups (uuid), read_only BOOLEAN NOT NULL, diff --git a/migrations/sqlite/2022-07-27-110000_add_group_support/down.sql b/migrations/sqlite/2022-07-27-110000_add_group_support/down.sql index cf9f1c8c..9a12d694 100644 --- a/migrations/sqlite/2022-07-27-110000_add_group_support/down.sql +++ b/migrations/sqlite/2022-07-27-110000_add_group_support/down.sql @@ -1,3 +1,3 @@ DROP TABLE groups; DROP TABLE groups_users; -DROP TABLE collection_groups; \ No newline at end of file +DROP TABLE collections_groups; \ No newline at end of file diff --git a/migrations/sqlite/2022-07-27-110000_add_group_support/up.sql b/migrations/sqlite/2022-07-27-110000_add_group_support/up.sql index 3f285b94..25ebdacd 100644 --- a/migrations/sqlite/2022-07-27-110000_add_group_support/up.sql +++ b/migrations/sqlite/2022-07-27-110000_add_group_support/up.sql @@ -9,12 +9,12 @@ CREATE TABLE groups ( ); CREATE TABLE groups_users ( - groups_uuid TEXT NOT NULL REFERENCES groups (uuid), + groups_uuid TEXT NOT NULL REFERENCES groups (uuid), users_organizations_uuid TEXT NOT NULL REFERENCES users_organizations (uuid), UNIQUE (groups_uuid, users_organizations_uuid) ); -CREATE TABLE collection_groups ( +CREATE TABLE collections_groups ( collections_uuid TEXT NOT NULL REFERENCES collections (uuid), groups_uuid TEXT NOT NULL REFERENCES groups (uuid), read_only BOOLEAN NOT NULL, diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index 859eb19b..3e0878b9 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -1675,7 +1675,7 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea _ => err!("Group could not be found!") }; - let collection_groups = CollectionGroup::find_by_group(&group_id, &conn).await + let collections_groups = CollectionGroup::find_by_group(&group_id, &conn).await .iter() .map(|entry| SelectionReadOnly::to_group_details_read_only(entry).to_json()) .collect::(); @@ -1686,7 +1686,7 @@ async fn get_group_details(_org_id: String, group_id: String, _headers: AdminHea "Name": group.name, "AccessAll": group.access_all, "ExternalId": group.external_id, - "Collections": collection_groups + "Collections": collections_groups }))) } diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index adcde616..7f0b93f4 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -467,17 +467,17 @@ impl Cipher { .inner_join(ciphers_collections::table.on( ciphers::uuid.eq(ciphers_collections::cipher_uuid) )) - .inner_join(collection_groups::table.on( - collection_groups::collections_uuid.eq(ciphers_collections::collection_uuid) + .inner_join(collections_groups::table.on( + collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid) )) .inner_join(groups_users::table.on( - groups_users::groups_uuid.eq(collection_groups::groups_uuid) + groups_users::groups_uuid.eq(collections_groups::groups_uuid) )) .inner_join(users_organizations::table.on( users_organizations::uuid.eq(groups_users::users_organizations_uuid) )) .filter(users_organizations::user_uuid.eq(user_uuid)) - .select((collection_groups::read_only, collection_groups::hide_passwords)) + .select((collections_groups::read_only, collections_groups::hide_passwords)) .load::<(bool, bool)>(conn) .expect("Error getting group access restrictions") }} @@ -563,14 +563,14 @@ impl Cipher { .left_join(groups::table.on( groups::uuid.eq(groups_users::groups_uuid) )) - .left_join(collection_groups::table.on( - collection_groups::collections_uuid.eq(ciphers_collections::collection_uuid) + .left_join(collections_groups::table.on( + collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid) )) .filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner .or_filter(users_organizations::access_all.eq(true)) // access_all in org .or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection .or_filter(groups::access_all.eq(true)) // Access via groups - .or_filter(collection_groups::collections_uuid.is_not_null()) // Access via groups + .or_filter(collections_groups::collections_uuid.is_not_null()) // Access via groups .into_boxed(); if !visible_only { @@ -702,14 +702,14 @@ impl Cipher { .left_join(groups::table.on( groups::uuid.eq(groups_users::groups_uuid) )) - .left_join(collection_groups::table.on( - collection_groups::collections_uuid.eq(ciphers_collections::collection_uuid) + .left_join(collections_groups::table.on( + collections_groups::collections_uuid.eq(ciphers_collections::collection_uuid) )) .or_filter(users_collections::user_uuid.eq(user_id)) // User has access to collection .or_filter(users_organizations::access_all.eq(true)) // User has access all .or_filter(users_organizations::atype.le(UserOrgType::Admin as i32)) // User is admin or owner .or_filter(groups::access_all.eq(true)) //Access via group - .or_filter(collection_groups::collections_uuid.is_not_null()) //Access via group + .or_filter(collections_groups::collections_uuid.is_not_null()) //Access via group .select(ciphers_collections::all_columns) .load::<(String, String)>(conn).unwrap_or_default() }} diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index d60e41ba..e7e5acbb 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -177,9 +177,9 @@ impl Collection { .left_join(groups::table.on( groups::uuid.eq(groups_users::groups_uuid) )) - .left_join(collection_groups::table.on( - collection_groups::groups_uuid.eq(groups_users::groups_uuid).and( - collection_groups::collections_uuid.eq(collections::uuid) + .left_join(collections_groups::table.on( + collections_groups::groups_uuid.eq(groups_users::groups_uuid).and( + collections_groups::collections_uuid.eq(collections::uuid) ) )) .filter( @@ -192,7 +192,7 @@ impl Collection { groups::access_all.eq(true) // access_all in groups ).or( // access via groups groups_users::users_organizations_uuid.eq(users_organizations::uuid).and( - collection_groups::collections_uuid.is_not_null() + collections_groups::collections_uuid.is_not_null() ) ) ) diff --git a/src/db/models/group.rs b/src/db/models/group.rs index cbf308b1..dfc6a025 100644 --- a/src/db/models/group.rs +++ b/src/db/models/group.rs @@ -16,7 +16,7 @@ db_object! { } #[derive(Identifiable, Queryable, Insertable)] - #[table_name = "collection_groups"] + #[table_name = "collections_groups"] #[primary_key(collections_uuid, groups_uuid)] pub struct CollectionGroup { pub collections_uuid: String, @@ -220,26 +220,26 @@ impl CollectionGroup { db_run! { conn: sqlite, mysql { - match diesel::replace_into(collection_groups::table) + match diesel::replace_into(collections_groups::table) .values(( - collection_groups::collections_uuid.eq(&self.collections_uuid), - collection_groups::groups_uuid.eq(&self.groups_uuid), - collection_groups::read_only.eq(&self.read_only), - collection_groups::hide_passwords.eq(&self.hide_passwords), + collections_groups::collections_uuid.eq(&self.collections_uuid), + collections_groups::groups_uuid.eq(&self.groups_uuid), + collections_groups::read_only.eq(&self.read_only), + collections_groups::hide_passwords.eq(&self.hide_passwords), )) .execute(conn) { Ok(_) => Ok(()), // Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first. Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => { - diesel::update(collection_groups::table) - .filter(collection_groups::collections_uuid.eq(&self.collections_uuid)) - .filter(collection_groups::groups_uuid.eq(&self.groups_uuid)) + diesel::update(collections_groups::table) + .filter(collections_groups::collections_uuid.eq(&self.collections_uuid)) + .filter(collections_groups::groups_uuid.eq(&self.groups_uuid)) .set(( - collection_groups::collections_uuid.eq(&self.collections_uuid), - collection_groups::groups_uuid.eq(&self.groups_uuid), - collection_groups::read_only.eq(&self.read_only), - collection_groups::hide_passwords.eq(&self.hide_passwords), + collections_groups::collections_uuid.eq(&self.collections_uuid), + collections_groups::groups_uuid.eq(&self.groups_uuid), + collections_groups::read_only.eq(&self.read_only), + collections_groups::hide_passwords.eq(&self.hide_passwords), )) .execute(conn) .map_res("Error adding group to collection") @@ -248,18 +248,18 @@ impl CollectionGroup { }.map_res("Error adding group to collection") } postgresql { - diesel::insert_into(collection_groups::table) + diesel::insert_into(collections_groups::table) .values(( - collection_groups::collections_uuid.eq(&self.collections_uuid), - collection_groups::groups_uuid.eq(&self.groups_uuid), - collection_groups::read_only.eq(self.read_only), - collection_groups::hide_passwords.eq(self.hide_passwords), + collections_groups::collections_uuid.eq(&self.collections_uuid), + collections_groups::groups_uuid.eq(&self.groups_uuid), + collections_groups::read_only.eq(self.read_only), + collections_groups::hide_passwords.eq(self.hide_passwords), )) - .on_conflict((collection_groups::collections_uuid, collection_groups::groups_uuid)) + .on_conflict((collections_groups::collections_uuid, collections_groups::groups_uuid)) .do_update() .set(( - collection_groups::read_only.eq(self.read_only), - collection_groups::hide_passwords.eq(self.hide_passwords), + collections_groups::read_only.eq(self.read_only), + collections_groups::hide_passwords.eq(self.hide_passwords), )) .execute(conn) .map_res("Error adding group to collection") @@ -269,8 +269,8 @@ impl CollectionGroup { pub async fn find_by_group (group_uuid: &str, conn: &DbConn) -> Vec { db_run! { conn: { - collection_groups::table - .filter(collection_groups::groups_uuid.eq(group_uuid)) + collections_groups::table + .filter(collections_groups::groups_uuid.eq(group_uuid)) .load::(conn) .expect("Error loading collection groups") .from_db() @@ -279,15 +279,15 @@ impl CollectionGroup { pub async fn find_by_user(user_uuid: &str, conn: &DbConn) -> Vec { db_run! { conn: { - collection_groups::table + collections_groups::table .inner_join(groups_users::table.on( - groups_users::groups_uuid.eq(collection_groups::groups_uuid) + groups_users::groups_uuid.eq(collections_groups::groups_uuid) )) .inner_join(users_organizations::table.on( users_organizations::uuid.eq(groups_users::users_organizations_uuid) )) .filter(users_organizations::user_uuid.eq(user_uuid)) - .select(collection_groups::all_columns) + .select(collections_groups::all_columns) .load::(conn) .expect("Error loading user collection groups") .from_db() @@ -296,9 +296,9 @@ impl CollectionGroup { pub async fn find_by_collection(collection_uuid: &str, conn: &DbConn) -> Vec { db_run! { conn: { - collection_groups::table - .filter(collection_groups::collections_uuid.eq(collection_uuid)) - .select(collection_groups::all_columns) + collections_groups::table + .filter(collections_groups::collections_uuid.eq(collection_uuid)) + .select(collections_groups::all_columns) .load::(conn) .expect("Error loading collection groups") .from_db() @@ -312,9 +312,9 @@ impl CollectionGroup { } db_run! { conn: { - diesel::delete(collection_groups::table) - .filter(collection_groups::collections_uuid.eq(&self.collections_uuid)) - .filter(collection_groups::groups_uuid.eq(&self.groups_uuid)) + diesel::delete(collections_groups::table) + .filter(collections_groups::collections_uuid.eq(&self.collections_uuid)) + .filter(collections_groups::groups_uuid.eq(&self.groups_uuid)) .execute(conn) .map_res("Error deleting collection group") }} @@ -327,8 +327,8 @@ impl CollectionGroup { } db_run! { conn: { - diesel::delete(collection_groups::table) - .filter(collection_groups::groups_uuid.eq(group_uuid)) + diesel::delete(collections_groups::table) + .filter(collections_groups::groups_uuid.eq(group_uuid)) .execute(conn) .map_res("Error deleting collection group") }} @@ -344,8 +344,8 @@ impl CollectionGroup { } db_run! { conn: { - diesel::delete(collection_groups::table) - .filter(collection_groups::collections_uuid.eq(collection_uuid)) + diesel::delete(collections_groups::table) + .filter(collections_groups::collections_uuid.eq(collection_uuid)) .execute(conn) .map_res("Error deleting collection group") }} @@ -426,8 +426,8 @@ impl GroupUser { } } - pub async fn delete_by_group_id_and_user_id(group_uuid: &str, user_uuid: &str, conn: &DbConn) -> EmptyResult { - match UserOrganization::find_by_uuid(user_uuid, conn).await { + pub async fn delete_by_group_id_and_user_id(group_uuid: &str, users_organizations_uuid: &str, conn: &DbConn) -> EmptyResult { + match UserOrganization::find_by_uuid(users_organizations_uuid, conn).await { Some(user) => User::update_uuid_revision(&user.user_uuid, conn).await, None => warn!("User could not be found!") }; @@ -435,7 +435,7 @@ impl GroupUser { db_run! { conn: { diesel::delete(groups_users::table) .filter(groups_users::groups_uuid.eq(group_uuid)) - .filter(groups_users::users_organizations_uuid.eq(user_uuid)) + .filter(groups_users::users_organizations_uuid.eq(users_organizations_uuid)) .execute(conn) .map_res("Error deleting group users") }} diff --git a/src/db/schemas/mysql/schema.rs b/src/db/schemas/mysql/schema.rs index b388e864..51875e11 100644 --- a/src/db/schemas/mysql/schema.rs +++ b/src/db/schemas/mysql/schema.rs @@ -240,7 +240,7 @@ table! { } table! { - collection_groups (collections_uuid, groups_uuid) { + collections_groups (collections_uuid, groups_uuid) { collections_uuid -> Text, groups_uuid -> Text, read_only -> Bool, @@ -270,8 +270,8 @@ joinable!(emergency_access -> users (grantor_uuid)); joinable!(groups -> organizations (organizations_uuid)); joinable!(groups_users -> users_organizations (users_organizations_uuid)); joinable!(groups_users -> groups (groups_uuid)); -joinable!(collection_groups -> collections (collections_uuid)); -joinable!(collection_groups -> groups (groups_uuid)); +joinable!(collections_groups -> collections (collections_uuid)); +joinable!(collections_groups -> groups (groups_uuid)); allow_tables_to_appear_in_same_query!( attachments, @@ -292,5 +292,5 @@ allow_tables_to_appear_in_same_query!( emergency_access, groups, groups_users, - collection_groups, + collections_groups, ); diff --git a/src/db/schemas/postgresql/schema.rs b/src/db/schemas/postgresql/schema.rs index 5ff0d60b..b2874f5e 100644 --- a/src/db/schemas/postgresql/schema.rs +++ b/src/db/schemas/postgresql/schema.rs @@ -240,7 +240,7 @@ table! { } table! { - collection_groups (collections_uuid, groups_uuid) { + collections_groups (collections_uuid, groups_uuid) { collections_uuid -> Text, groups_uuid -> Text, read_only -> Bool, @@ -270,8 +270,8 @@ joinable!(emergency_access -> users (grantor_uuid)); joinable!(groups -> organizations (organizations_uuid)); joinable!(groups_users -> users_organizations (users_organizations_uuid)); joinable!(groups_users -> groups (groups_uuid)); -joinable!(collection_groups -> collections (collections_uuid)); -joinable!(collection_groups -> groups (groups_uuid)); +joinable!(collections_groups -> collections (collections_uuid)); +joinable!(collections_groups -> groups (groups_uuid)); allow_tables_to_appear_in_same_query!( attachments, @@ -292,5 +292,5 @@ allow_tables_to_appear_in_same_query!( emergency_access, groups, groups_users, - collection_groups, + collections_groups, ); diff --git a/src/db/schemas/sqlite/schema.rs b/src/db/schemas/sqlite/schema.rs index 5ff0d60b..b2874f5e 100644 --- a/src/db/schemas/sqlite/schema.rs +++ b/src/db/schemas/sqlite/schema.rs @@ -240,7 +240,7 @@ table! { } table! { - collection_groups (collections_uuid, groups_uuid) { + collections_groups (collections_uuid, groups_uuid) { collections_uuid -> Text, groups_uuid -> Text, read_only -> Bool, @@ -270,8 +270,8 @@ joinable!(emergency_access -> users (grantor_uuid)); joinable!(groups -> organizations (organizations_uuid)); joinable!(groups_users -> users_organizations (users_organizations_uuid)); joinable!(groups_users -> groups (groups_uuid)); -joinable!(collection_groups -> collections (collections_uuid)); -joinable!(collection_groups -> groups (groups_uuid)); +joinable!(collections_groups -> collections (collections_uuid)); +joinable!(collections_groups -> groups (groups_uuid)); allow_tables_to_appear_in_same_query!( attachments, @@ -292,5 +292,5 @@ allow_tables_to_appear_in_same_query!( emergency_access, groups, groups_users, - collection_groups, + collections_groups, );