|
|
@ -1,6 +1,7 @@
|
|
|
|
use serde_json::Value;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
|
|
|
|
use super::{CollectionGroup, User, UserOrgStatus, UserOrgType, UserOrganization};
|
|
|
|
use super::{CollectionGroup, User, UserOrgStatus, UserOrgType, UserOrganization};
|
|
|
|
|
|
|
|
use crate::CONFIG;
|
|
|
|
|
|
|
|
|
|
|
|
db_object! {
|
|
|
|
db_object! {
|
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
|
|
@ -181,47 +182,74 @@ impl Collection {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn find_by_user_uuid(user_uuid: String, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
pub async fn find_by_user_uuid(user_uuid: String, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
collections::table
|
|
|
|
db_run! { conn: {
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
collections::table
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
|
|
|
|
users_organizations::user_uuid.eq(user_uuid.clone())
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
|
|
|
|
groups::uuid.eq(groups_users::groups_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(
|
|
|
|
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
users_organizations::access_all.eq(true) // access_all in Organization
|
|
|
|
users_organizations::user_uuid.eq(user_uuid.clone())
|
|
|
|
).or(
|
|
|
|
|
|
|
|
groups::access_all.eq(true) // access_all in groups
|
|
|
|
|
|
|
|
).or( // access via groups
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid).and(
|
|
|
|
|
|
|
|
collections_groups::collections_uuid.is_not_null()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
.distinct()
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
.load::<CollectionDb>(conn).expect("Error loading collections").from_db()
|
|
|
|
))
|
|
|
|
}}
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
} else {
|
|
|
|
groups::uuid.eq(groups_users::groups_uuid)
|
|
|
|
db_run! { conn: {
|
|
|
|
))
|
|
|
|
collections::table
|
|
|
|
.left_join(collections_groups::table.on(
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
collections_groups::groups_uuid.eq(groups_users::groups_uuid).and(
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
collections_groups::collections_uuid.eq(collections::uuid)
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
|
|
|
|
users_organizations::user_uuid.eq(user_uuid.clone())
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
.filter(
|
|
|
|
users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
users_organizations::access_all.eq(true) // access_all in Organization
|
|
|
|
)
|
|
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection
|
|
|
|
|
|
|
|
users_organizations::access_all.eq(true) // access_all in Organization
|
|
|
|
|
|
|
|
).or(
|
|
|
|
|
|
|
|
groups::access_all.eq(true) // access_all in groups
|
|
|
|
|
|
|
|
).or( // access via groups
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid).and(
|
|
|
|
|
|
|
|
collections_groups::collections_uuid.is_not_null()
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.distinct()
|
|
|
|
.distinct()
|
|
|
|
.load::<CollectionDb>(conn).expect("Error loading collections").from_db()
|
|
|
|
.load::<CollectionDb>(conn).expect("Error loading collections").from_db()
|
|
|
|
}}
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Check if a user has access to a specific collection
|
|
|
|
// Check if a user has access to a specific collection
|
|
|
@ -277,45 +305,70 @@ impl Collection {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn find_by_uuid_and_user(uuid: &str, user_uuid: String, conn: &mut DbConn) -> Option<Self> {
|
|
|
|
pub async fn find_by_uuid_and_user(uuid: &str, user_uuid: String, conn: &mut DbConn) -> Option<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
collections::table
|
|
|
|
db_run! { conn: {
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
collections::table
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
)
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
|
|
|
|
users_organizations::user_uuid.eq(user_uuid)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
|
|
|
|
groups::uuid.eq(groups_users::groups_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(collections::uuid.eq(uuid))
|
|
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
users_collections::collection_uuid.eq(uuid).or( // Directly accessed collection
|
|
|
|
|
|
|
|
users_organizations::access_all.eq(true).or( // access_all in Organization
|
|
|
|
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin or owner
|
|
|
|
|
|
|
|
)).or(
|
|
|
|
|
|
|
|
groups::access_all.eq(true) // access_all in groups
|
|
|
|
|
|
|
|
).or( // access via groups
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid).and(
|
|
|
|
|
|
|
|
collections_groups::collections_uuid.is_not_null()
|
|
|
|
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
).select(collections::all_columns)
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
.first::<CollectionDb>(conn).ok()
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
.from_db()
|
|
|
|
users_organizations::user_uuid.eq(user_uuid)
|
|
|
|
}}
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups_users::table.on(
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(groups::table.on(
|
|
|
|
|
|
|
|
groups::uuid.eq(groups_users::groups_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(collections::uuid.eq(uuid))
|
|
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
users_collections::collection_uuid.eq(uuid).or( // Directly accessed collection
|
|
|
|
|
|
|
|
users_organizations::access_all.eq(true).or( // access_all in Organization
|
|
|
|
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin or owner
|
|
|
|
|
|
|
|
)).or(
|
|
|
|
|
|
|
|
groups::access_all.eq(true) // access_all in groups
|
|
|
|
|
|
|
|
).or( // access via groups
|
|
|
|
|
|
|
|
groups_users::users_organizations_uuid.eq(users_organizations::uuid).and(
|
|
|
|
|
|
|
|
collections_groups::collections_uuid.is_not_null()
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
).select(collections::all_columns)
|
|
|
|
|
|
|
|
.first::<CollectionDb>(conn).ok()
|
|
|
|
|
|
|
|
.from_db()
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
db_run! { conn: {
|
|
|
|
|
|
|
|
collections::table
|
|
|
|
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
|
|
|
|
users_collections::user_uuid.eq(user_uuid.clone())
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
|
|
|
|
users_organizations::user_uuid.eq(user_uuid)
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
.filter(collections::uuid.eq(uuid))
|
|
|
|
|
|
|
|
.filter(
|
|
|
|
|
|
|
|
users_collections::collection_uuid.eq(uuid).or( // Directly accessed collection
|
|
|
|
|
|
|
|
users_organizations::access_all.eq(true).or( // access_all in Organization
|
|
|
|
|
|
|
|
users_organizations::atype.le(UserOrgType::Admin as i32) // Org admin or owner
|
|
|
|
|
|
|
|
))
|
|
|
|
|
|
|
|
).select(collections::all_columns)
|
|
|
|
|
|
|
|
.first::<CollectionDb>(conn).ok()
|
|
|
|
|
|
|
|
.from_db()
|
|
|
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub async fn is_writable_by_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
|
|
|
pub async fn is_writable_by_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
|
|
|