Browse Source

Table and two variables renamed

pull/2667/head
MFijak 3 years ago
committed by Maximilian Fijak
parent
commit
25764eb406
  1. 4
      migrations/mysql/2022-07-27-110000_add_group_support/down.sql
  2. 2
      migrations/mysql/2022-07-27-110000_add_group_support/up.sql
  3. 2
      migrations/postgresql/2022-07-27-110000_add_group_support/down.sql
  4. 2
      migrations/postgresql/2022-07-27-110000_add_group_support/up.sql
  5. 2
      migrations/sqlite/2022-07-27-110000_add_group_support/down.sql
  6. 2
      migrations/sqlite/2022-07-27-110000_add_group_support/up.sql
  7. 4
      src/api/core/organizations.rs
  8. 20
      src/db/models/cipher.rs
  9. 8
      src/db/models/collection.rs
  10. 78
      src/db/models/group.rs
  11. 8
      src/db/schemas/mysql/schema.rs
  12. 8
      src/db/schemas/postgresql/schema.rs
  13. 8
      src/db/schemas/sqlite/schema.rs

4
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;
DROP TABLE collections_groups;

2
migrations/mysql/2022-07-27-110000_add_group_support/up.sql

@ -14,7 +14,7 @@ CREATE TABLE groups_users (
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,

2
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;
DROP TABLE collections_groups;

2
migrations/postgresql/2022-07-27-110000_add_group_support/up.sql

@ -14,7 +14,7 @@ CREATE TABLE groups_users (
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,

2
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;
DROP TABLE collections_groups;

2
migrations/sqlite/2022-07-27-110000_add_group_support/up.sql

@ -14,7 +14,7 @@ CREATE TABLE groups_users (
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,

4
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::<Value>();
@ -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
})))
}

20
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()
}}

8
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()
)
)
)

78
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<Self> {
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::<CollectionGroupDb>(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<Self> {
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::<CollectionGroupDb>(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<Self> {
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::<CollectionGroupDb>(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")
}}

8
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,
);

8
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,
);

8
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,
);

Loading…
Cancel
Save