2018-10-10 20:40:39 +02:00
|
|
|
use serde_json::Value;
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2024-04-27 22:09:00 +02:00
|
|
|
use super::{CollectionGroup, GroupUser, User, UserOrgStatus, UserOrgType, UserOrganization};
|
2024-01-25 22:02:07 +01:00
|
|
|
use crate::CONFIG;
|
2020-08-18 17:15:44 +02:00
|
|
|
|
|
|
|
db_object! {
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = collections)]
|
|
|
|
#[diesel(primary_key(uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct Collection {
|
|
|
|
pub uuid: String,
|
|
|
|
pub org_uuid: String,
|
|
|
|
pub name: String,
|
2023-06-28 20:37:13 +02:00
|
|
|
pub external_id: Option<String>,
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = users_collections)]
|
|
|
|
#[diesel(primary_key(user_uuid, collection_uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct CollectionUser {
|
|
|
|
pub user_uuid: String,
|
|
|
|
pub collection_uuid: String,
|
|
|
|
pub read_only: bool,
|
|
|
|
pub hide_passwords: bool,
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
#[derive(Identifiable, Queryable, Insertable)]
|
2022-05-20 23:39:47 +02:00
|
|
|
#[diesel(table_name = ciphers_collections)]
|
|
|
|
#[diesel(primary_key(cipher_uuid, collection_uuid))]
|
2020-08-18 17:15:44 +02:00
|
|
|
pub struct CollectionCipher {
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
pub collection_uuid: String,
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Collection {
|
2023-06-28 20:37:13 +02:00
|
|
|
pub fn new(org_uuid: String, name: String, external_id: Option<String>) -> Self {
|
|
|
|
let mut new_model = Self {
|
2018-12-07 14:32:40 +01:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-02-17 22:30:19 +01:00
|
|
|
org_uuid,
|
|
|
|
name,
|
2023-06-28 20:37:13 +02:00
|
|
|
external_id: None,
|
|
|
|
};
|
2023-06-28 22:11:26 +02:00
|
|
|
|
2023-06-28 20:37:13 +02:00
|
|
|
new_model.set_external_id(external_id);
|
|
|
|
new_model
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2018-10-10 20:40:39 +02:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-02-17 22:30:19 +01:00
|
|
|
json!({
|
2023-06-28 20:37:13 +02:00
|
|
|
"ExternalId": self.external_id,
|
2018-02-17 22:30:19 +01:00
|
|
|
"Id": self.uuid,
|
|
|
|
"OrganizationId": self.org_uuid,
|
|
|
|
"Name": self.name,
|
|
|
|
"Object": "collection",
|
|
|
|
})
|
|
|
|
}
|
2021-01-31 21:46:37 +01:00
|
|
|
|
2023-06-28 20:37:13 +02:00
|
|
|
pub fn set_external_id(&mut self, external_id: Option<String>) {
|
|
|
|
//Check if external id is empty. We don't want to have
|
|
|
|
//empty strings in the database
|
|
|
|
match external_id {
|
|
|
|
Some(external_id) => {
|
|
|
|
if external_id.is_empty() {
|
|
|
|
self.external_id = None;
|
|
|
|
} else {
|
|
|
|
self.external_id = Some(external_id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => self.external_id = None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-04 21:13:05 +02:00
|
|
|
pub async fn to_json_details(
|
|
|
|
&self,
|
|
|
|
user_uuid: &str,
|
|
|
|
cipher_sync_data: Option<&crate::api::core::CipherSyncData>,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2022-05-04 21:13:05 +02:00
|
|
|
) -> Value {
|
|
|
|
let (read_only, hide_passwords) = if let Some(cipher_sync_data) = cipher_sync_data {
|
|
|
|
match cipher_sync_data.user_organizations.get(&self.org_uuid) {
|
|
|
|
Some(uo) if uo.has_full_access() => (false, false),
|
|
|
|
Some(_) => {
|
|
|
|
if let Some(uc) = cipher_sync_data.user_collections.get(&self.uuid) {
|
|
|
|
(uc.read_only, uc.hide_passwords)
|
2023-02-17 13:34:57 +01:00
|
|
|
} else if let Some(cg) = cipher_sync_data.user_collections_groups.get(&self.uuid) {
|
|
|
|
(cg.read_only, cg.hide_passwords)
|
2022-05-04 21:13:05 +02:00
|
|
|
} else {
|
|
|
|
(false, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => (true, true),
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(!self.is_writable_by_user(user_uuid, conn).await, self.hide_passwords_for_user(user_uuid, conn).await)
|
|
|
|
};
|
|
|
|
|
2021-01-31 21:46:37 +01:00
|
|
|
let mut json_object = self.to_json();
|
|
|
|
json_object["Object"] = json!("collectionDetails");
|
2022-05-04 21:13:05 +02:00
|
|
|
json_object["ReadOnly"] = json!(read_only);
|
|
|
|
json_object["HidePasswords"] = json!(hide_passwords);
|
2021-01-31 21:46:37 +01:00
|
|
|
json_object
|
|
|
|
}
|
2024-04-27 22:09:00 +02:00
|
|
|
|
|
|
|
pub async fn can_access_collection(org_user: &UserOrganization, col_id: &str, conn: &mut DbConn) -> bool {
|
|
|
|
org_user.has_status(UserOrgStatus::Confirmed)
|
|
|
|
&& (org_user.has_full_access()
|
|
|
|
|| CollectionUser::has_access_to_collection_by_user(col_id, &org_user.user_uuid, conn).await
|
|
|
|
|| (CONFIG.org_groups_enabled()
|
|
|
|
&& (GroupUser::has_full_access_by_member(&org_user.org_uuid, &org_user.uuid, conn).await
|
|
|
|
|| GroupUser::has_access_to_collection_by_member(col_id, &org_user.uuid, conn).await)))
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2018-12-30 23:34:31 +01:00
|
|
|
use crate::db::DbConn;
|
2018-02-17 22:30:19 +01:00
|
|
|
|
2018-12-19 21:52:53 +01:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-17 22:30:19 +01:00
|
|
|
/// Database methods
|
|
|
|
impl Collection {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(&self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
self.update_users_revision(conn).await;
|
2019-09-12 22:12:22 +02:00
|
|
|
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
match diesel::replace_into(collections::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values(CollectionDb::to_db(self))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
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(collections::table)
|
|
|
|
.filter(collections::uuid.eq(&self.uuid))
|
|
|
|
.set(CollectionDb::to_db(self))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error saving collection")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error saving collection")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
let value = CollectionDb::to_db(self);
|
|
|
|
diesel::insert_into(collections::table)
|
|
|
|
.values(&value)
|
|
|
|
.on_conflict(collections::uuid)
|
|
|
|
.do_update()
|
|
|
|
.set(&value)
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
.map_res("Error saving collection")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
self.update_users_revision(conn).await;
|
|
|
|
CollectionCipher::delete_all_by_collection(&self.uuid, conn).await?;
|
|
|
|
CollectionUser::delete_all_by_collection(&self.uuid, conn).await?;
|
2022-10-20 15:31:53 +02:00
|
|
|
CollectionGroup::delete_all_by_collection(&self.uuid, conn).await?;
|
2018-05-17 00:05:50 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(collections::table.filter(collections::uuid.eq(self.uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting collection")
|
|
|
|
}}
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_organization(org_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
for collection in Self::find_by_organization(org_uuid, conn).await {
|
|
|
|
collection.delete(conn).await?;
|
2018-05-18 17:52:51 +02:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn update_users_revision(&self, conn: &mut DbConn) {
|
2021-11-16 17:07:55 +01:00
|
|
|
for user_org in UserOrganization::find_by_collection_and_org(&self.uuid, &self.org_uuid, conn).await.iter() {
|
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn).await;
|
|
|
|
}
|
2019-02-06 14:39:32 +01:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::uuid.eq(uuid))
|
|
|
|
.first::<CollectionDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-02-17 22:30:19 +01:00
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user_uuid(user_uuid: String, conn: &mut DbConn) -> Vec<Self> {
|
2024-01-25 22:02:07 +01:00
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
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.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)
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
2024-01-25 22:02:07 +01:00
|
|
|
.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()
|
|
|
|
)
|
|
|
|
)
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
2024-01-25 22:02:07 +01:00
|
|
|
.select(collections::all_columns)
|
|
|
|
.distinct()
|
|
|
|
.load::<CollectionDb>(conn).expect("Error loading collections").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.clone())
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
2022-10-20 15:31:53 +02:00
|
|
|
)
|
2024-01-25 22:02:07 +01:00
|
|
|
.filter(
|
|
|
|
users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection
|
|
|
|
users_organizations::access_all.eq(true) // access_all in Organization
|
2022-10-20 15:31:53 +02:00
|
|
|
)
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
2024-01-25 22:02:07 +01:00
|
|
|
.select(collections::all_columns)
|
|
|
|
.distinct()
|
|
|
|
.load::<CollectionDb>(conn).expect("Error loading collections").from_db()
|
|
|
|
}}
|
|
|
|
}
|
2018-05-04 19:25:50 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_organization_and_user_uuid(org_uuid: &str, user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
Self::find_by_user_uuid(user_uuid.to_owned(), conn)
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c.org_uuid == org_uuid)
|
|
|
|
.collect()
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_organization(org_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.load::<CollectionDb>(conn)
|
|
|
|
.expect("Error loading collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-05-11 20:08:02 +02:00
|
|
|
}
|
|
|
|
|
2023-02-28 20:43:22 +01:00
|
|
|
pub async fn count_by_org(org_uuid: &str, conn: &mut DbConn) -> i64 {
|
|
|
|
db_run! { conn: {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0)
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_uuid_and_org(uuid: &str, org_uuid: &str, conn: &mut DbConn) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::uuid.eq(uuid))
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.first::<CollectionDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-05-28 18:26:02 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_uuid_and_user(uuid: &str, user_uuid: String, conn: &mut DbConn) -> Option<Self> {
|
2024-01-25 22:02:07 +01:00
|
|
|
if CONFIG.org_groups_enabled() {
|
|
|
|
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())
|
2020-08-18 17:15:44 +02:00
|
|
|
)
|
2024-01-25 22:02:07 +01:00
|
|
|
))
|
|
|
|
.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)
|
|
|
|
.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()
|
|
|
|
}}
|
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
2018-05-09 12:55:05 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn is_writable_by_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
2023-01-05 17:04:11 +01:00
|
|
|
let user_uuid = user_uuid.to_string();
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.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(&self.uuid))
|
|
|
|
.filter(
|
|
|
|
users_collections::collection_uuid.eq(&self.uuid).and(users_collections::read_only.eq(false)).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().and(
|
|
|
|
collections_groups::read_only.eq(false))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0) != 0
|
|
|
|
}}
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
2021-01-31 21:46:37 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn hide_passwords_for_user(&self, user_uuid: &str, conn: &mut DbConn) -> bool {
|
2023-01-05 17:04:11 +01:00
|
|
|
let user_uuid = user_uuid.to_string();
|
|
|
|
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)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.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(&self.uuid))
|
|
|
|
.filter(
|
|
|
|
users_collections::collection_uuid.eq(&self.uuid).and(users_collections::hide_passwords.eq(true)).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().and(
|
|
|
|
collections_groups::hide_passwords.eq(true))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.count()
|
|
|
|
.first::<i64>(conn)
|
|
|
|
.ok()
|
|
|
|
.unwrap_or(0) != 0
|
|
|
|
}}
|
2021-01-31 21:46:37 +01:00
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 19:25:50 +02:00
|
|
|
/// Database methods
|
2018-05-17 00:05:50 +02:00
|
|
|
impl CollectionUser {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_organization_and_user_uuid(org_uuid: &str, user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::user_uuid.eq(user_uuid))
|
|
|
|
.inner_join(collections::table.on(collections::uuid.eq(users_collections::collection_uuid)))
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.load::<CollectionUserDb>(conn)
|
|
|
|
.expect("Error loading users_collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2019-09-12 22:12:22 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
pub async fn find_by_organization(org_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.inner_join(collections::table.on(collections::uuid.eq(users_collections::collection_uuid)))
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.inner_join(users_organizations::table.on(users_organizations::user_uuid.eq(users_collections::user_uuid)))
|
|
|
|
.select((users_organizations::uuid, users_collections::collection_uuid, users_collections::read_only, users_collections::hide_passwords))
|
|
|
|
.load::<CollectionUserDb>(conn)
|
|
|
|
.expect("Error loading users_collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2021-11-16 17:07:55 +01:00
|
|
|
pub async fn save(
|
2021-03-31 22:18:35 +02:00
|
|
|
user_uuid: &str,
|
|
|
|
collection_uuid: &str,
|
|
|
|
read_only: bool,
|
|
|
|
hide_passwords: bool,
|
2022-05-20 23:39:47 +02:00
|
|
|
conn: &mut DbConn,
|
2021-03-31 22:18:35 +02:00
|
|
|
) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(user_uuid, conn).await;
|
2018-08-21 13:20:55 +02:00
|
|
|
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
match diesel::replace_into(users_collections::table)
|
2020-08-18 17:15:44 +02:00
|
|
|
.values((
|
|
|
|
users_collections::user_uuid.eq(user_uuid),
|
|
|
|
users_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
users_collections::read_only.eq(read_only),
|
|
|
|
users_collections::hide_passwords.eq(hide_passwords),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
2020-09-22 12:13:02 +02:00
|
|
|
{
|
|
|
|
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(users_collections::table)
|
|
|
|
.filter(users_collections::user_uuid.eq(user_uuid))
|
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.set((
|
|
|
|
users_collections::user_uuid.eq(user_uuid),
|
|
|
|
users_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
users_collections::read_only.eq(read_only),
|
|
|
|
users_collections::hide_passwords.eq(hide_passwords),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding user to collection")
|
|
|
|
}
|
|
|
|
Err(e) => Err(e.into()),
|
|
|
|
}.map_res("Error adding user to collection")
|
2020-08-18 17:15:44 +02:00
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
diesel::insert_into(users_collections::table)
|
|
|
|
.values((
|
|
|
|
users_collections::user_uuid.eq(user_uuid),
|
|
|
|
users_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
users_collections::read_only.eq(read_only),
|
|
|
|
users_collections::hide_passwords.eq(hide_passwords),
|
|
|
|
))
|
|
|
|
.on_conflict((users_collections::user_uuid, users_collections::collection_uuid))
|
|
|
|
.do_update()
|
|
|
|
.set((
|
|
|
|
users_collections::read_only.eq(read_only),
|
|
|
|
users_collections::hide_passwords.eq(hide_passwords),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding user to collection")
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(self, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await;
|
2018-08-21 13:20:55 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::user_uuid.eq(&self.user_uuid))
|
|
|
|
.filter(users_collections::collection_uuid.eq(&self.collection_uuid)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing user from collection")
|
|
|
|
}}
|
2018-05-29 17:01:38 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_collection(collection_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.load::<CollectionUserDb>(conn)
|
|
|
|
.expect("Error loading users_collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-05-29 17:01:38 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 16:37:58 +01:00
|
|
|
pub async fn find_by_collection_swap_user_uuid_with_org_user_uuid(
|
|
|
|
collection_uuid: &str,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Vec<Self> {
|
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.inner_join(users_organizations::table.on(users_organizations::user_uuid.eq(users_collections::user_uuid)))
|
|
|
|
.select((users_organizations::uuid, users_collections::collection_uuid, users_collections::read_only, users_collections::hide_passwords))
|
|
|
|
.load::<CollectionUserDb>(conn)
|
|
|
|
.expect("Error loading users_collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_collection_and_user(
|
|
|
|
collection_uuid: &str,
|
|
|
|
user_uuid: &str,
|
|
|
|
conn: &mut DbConn,
|
|
|
|
) -> Option<Self> {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.filter(users_collections::user_uuid.eq(user_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.first::<CollectionUserDb>(conn)
|
|
|
|
.ok()
|
|
|
|
.from_db()
|
|
|
|
}}
|
2018-04-20 18:35:11 +02:00
|
|
|
}
|
2018-05-17 00:05:50 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn find_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<Self> {
|
2022-05-04 21:13:05 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::user_uuid.eq(user_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.load::<CollectionUserDb>(conn)
|
|
|
|
.expect("Error loading users_collections")
|
|
|
|
.from_db()
|
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_collection(collection_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
for collection in CollectionUser::find_by_collection(collection_uuid, conn).await.iter() {
|
|
|
|
User::update_uuid_revision(&collection.user_uuid, conn).await;
|
|
|
|
}
|
2018-08-21 13:20:55 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(users_collections::table.filter(users_collections::collection_uuid.eq(collection_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting users from collection")
|
|
|
|
}}
|
2018-05-17 00:05:50 +02:00
|
|
|
}
|
2018-05-18 17:52:51 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_user_and_org(user_uuid: &str, org_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
let collectionusers = Self::find_by_organization_and_user_uuid(org_uuid, user_uuid, conn).await;
|
2018-08-21 13:20:55 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
2020-11-07 23:03:02 +01:00
|
|
|
for user in collectionusers {
|
|
|
|
diesel::delete(users_collections::table.filter(
|
|
|
|
users_collections::user_uuid.eq(user_uuid)
|
|
|
|
.and(users_collections::collection_uuid.eq(user.collection_uuid))
|
|
|
|
))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing user from collections")?;
|
|
|
|
}
|
|
|
|
Ok(())
|
2020-08-18 17:15:44 +02:00
|
|
|
}}
|
2018-05-18 17:52:51 +02:00
|
|
|
}
|
2024-04-27 22:09:00 +02:00
|
|
|
|
|
|
|
pub async fn has_access_to_collection_by_user(col_id: &str, user_uuid: &str, conn: &mut DbConn) -> bool {
|
|
|
|
Self::find_by_collection_and_user(col_id, user_uuid, conn).await.is_some()
|
|
|
|
}
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl CollectionCipher {
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn save(cipher_uuid: &str, collection_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
Self::update_users_revision(collection_uuid, conn).await;
|
2019-09-12 22:12:22 +02:00
|
|
|
|
2020-09-22 12:13:02 +02:00
|
|
|
db_run! { conn:
|
2020-08-18 17:15:44 +02:00
|
|
|
sqlite, mysql {
|
2020-09-22 12:13:02 +02:00
|
|
|
// Not checking for ForeignKey Constraints here.
|
|
|
|
// Table ciphers_collections does not have ForeignKey Constraints which would cause conflicts.
|
|
|
|
// This table has no constraints pointing to itself, but only to others.
|
2020-08-18 17:15:44 +02:00
|
|
|
diesel::replace_into(ciphers_collections::table)
|
|
|
|
.values((
|
|
|
|
ciphers_collections::cipher_uuid.eq(cipher_uuid),
|
|
|
|
ciphers_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding cipher to collection")
|
|
|
|
}
|
|
|
|
postgresql {
|
|
|
|
diesel::insert_into(ciphers_collections::table)
|
|
|
|
.values((
|
|
|
|
ciphers_collections::cipher_uuid.eq(cipher_uuid),
|
|
|
|
ciphers_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
))
|
|
|
|
.on_conflict((ciphers_collections::cipher_uuid, ciphers_collections::collection_uuid))
|
|
|
|
.do_nothing()
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error adding cipher to collection")
|
|
|
|
}
|
|
|
|
}
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete(cipher_uuid: &str, collection_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2021-11-16 17:07:55 +01:00
|
|
|
Self::update_users_revision(collection_uuid, conn).await;
|
2020-09-22 12:13:02 +02:00
|
|
|
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(
|
|
|
|
ciphers_collections::table
|
|
|
|
.filter(ciphers_collections::cipher_uuid.eq(cipher_uuid))
|
|
|
|
.filter(ciphers_collections::collection_uuid.eq(collection_uuid)),
|
|
|
|
)
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error deleting cipher from collection")
|
|
|
|
}}
|
2018-05-09 12:55:05 +02:00
|
|
|
}
|
2018-05-15 18:27:53 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_cipher(cipher_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(ciphers_collections::table.filter(ciphers_collections::cipher_uuid.eq(cipher_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing cipher from collections")
|
|
|
|
}}
|
2018-05-15 18:27:53 +02:00
|
|
|
}
|
2018-05-17 00:05:50 +02:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn delete_all_by_collection(collection_uuid: &str, conn: &mut DbConn) -> EmptyResult {
|
2020-08-18 17:15:44 +02:00
|
|
|
db_run! { conn: {
|
|
|
|
diesel::delete(ciphers_collections::table.filter(ciphers_collections::collection_uuid.eq(collection_uuid)))
|
|
|
|
.execute(conn)
|
|
|
|
.map_res("Error removing ciphers from collection")
|
|
|
|
}}
|
2018-05-17 00:05:50 +02:00
|
|
|
}
|
2019-02-06 15:47:47 +01:00
|
|
|
|
2022-05-20 23:39:47 +02:00
|
|
|
pub async fn update_users_revision(collection_uuid: &str, conn: &mut DbConn) {
|
2021-11-16 17:07:55 +01:00
|
|
|
if let Some(collection) = Collection::find_by_uuid(collection_uuid, conn).await {
|
|
|
|
collection.update_users_revision(conn).await;
|
2019-02-06 15:47:47 +01:00
|
|
|
}
|
|
|
|
}
|
2018-12-30 23:34:31 +01:00
|
|
|
}
|