2018-10-10 18:40:39 +00:00
|
|
|
use serde_json::Value;
|
2018-02-17 21:30:19 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use super::{Organization, UserOrgStatus, UserOrgType, UserOrganization};
|
2018-02-17 21:30:19 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "collections"]
|
|
|
|
#[belongs_to(Organization, foreign_key = "org_uuid")]
|
|
|
|
#[primary_key(uuid)]
|
|
|
|
pub struct Collection {
|
|
|
|
pub uuid: String,
|
|
|
|
pub org_uuid: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
impl Collection {
|
|
|
|
pub fn new(org_uuid: String, name: String) -> Self {
|
|
|
|
Self {
|
2018-12-07 13:32:40 +00:00
|
|
|
uuid: crate::util::get_uuid(),
|
2018-02-17 21:30:19 +00:00
|
|
|
|
|
|
|
org_uuid,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-10 18:40:39 +00:00
|
|
|
pub fn to_json(&self) -> Value {
|
2018-02-17 21:30:19 +00:00
|
|
|
json!({
|
|
|
|
"Id": self.uuid,
|
|
|
|
"OrganizationId": self.org_uuid,
|
|
|
|
"Name": self.name,
|
|
|
|
"Object": "collection",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use crate::db::schema::*;
|
|
|
|
use crate::db::DbConn;
|
2018-02-17 21:30:19 +00:00
|
|
|
use diesel;
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
use crate::api::EmptyResult;
|
|
|
|
use crate::error::MapResult;
|
|
|
|
|
2018-02-17 21:30:19 +00:00
|
|
|
/// Database methods
|
|
|
|
impl Collection {
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
2019-02-06 13:39:32 +00:00
|
|
|
self.update_users_revision(conn);
|
2018-10-01 15:52:36 +00:00
|
|
|
|
|
|
|
diesel::replace_into(collections::table)
|
2018-12-30 22:34:31 +00:00
|
|
|
.values(&*self)
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error saving collection")
|
2018-02-17 21:30:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
2019-02-06 13:39:32 +00:00
|
|
|
self.update_users_revision(conn);
|
2018-05-16 22:05:50 +00:00
|
|
|
CollectionCipher::delete_all_by_collection(&self.uuid, &conn)?;
|
|
|
|
CollectionUser::delete_all_by_collection(&self.uuid, &conn)?;
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::delete(collections::table.filter(collections::uuid.eq(self.uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error deleting collection")
|
2018-02-17 21:30:19 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_organization(org_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-05-18 15:52:51 +00:00
|
|
|
for collection in Self::find_by_organization(org_uuid, &conn) {
|
|
|
|
collection.delete(&conn)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-06 13:39:32 +00:00
|
|
|
pub fn update_users_revision(&self, conn: &DbConn) {
|
|
|
|
UserOrganization::find_by_collection_and_org(&self.uuid, &self.org_uuid, conn)
|
|
|
|
.iter()
|
|
|
|
.for_each(|user_org| {
|
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-02-17 21:30:19 +00:00
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::uuid.eq(uuid))
|
2018-12-30 22:34:31 +00:00
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-02-17 21:30:19 +00:00
|
|
|
}
|
2018-04-20 16:35:11 +00:00
|
|
|
|
2018-05-04 17:25:50 +00:00
|
|
|
pub fn find_by_user_uuid(user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-12-27 17:56:01 +00:00
|
|
|
collections::table
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
users_collections::user_uuid.eq(user_uuid)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.left_join(users_organizations::table.on(
|
|
|
|
collections::org_uuid.eq(users_organizations::org_uuid).and(
|
|
|
|
users_organizations::user_uuid.eq(user_uuid)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.filter(
|
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32)
|
|
|
|
)
|
|
|
|
.filter(
|
|
|
|
users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection
|
|
|
|
users_organizations::access_all.eq(true) // access_all in Organization
|
|
|
|
)
|
|
|
|
).select(collections::all_columns)
|
|
|
|
.load::<Self>(&**conn).expect("Error loading collections")
|
2018-05-04 17:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_organization_and_user_uuid(org_uuid: &str, user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
2018-12-30 22:34:31 +00:00
|
|
|
Self::find_by_user_uuid(user_uuid, conn)
|
|
|
|
.into_iter()
|
|
|
|
.filter(|c| c.org_uuid == org_uuid)
|
|
|
|
.collect()
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-11 18:08:02 +00:00
|
|
|
pub fn find_by_organization(org_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
collections::table
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
2018-12-30 22:34:31 +00:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading collections")
|
2018-05-11 18:08:02 +00:00
|
|
|
}
|
|
|
|
|
2018-05-28 16:26:02 +00:00
|
|
|
pub fn find_by_uuid_and_org(uuid: &str, org_uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
collections::table
|
2018-12-30 22:34:31 +00:00
|
|
|
.filter(collections::uuid.eq(uuid))
|
|
|
|
.filter(collections::org_uuid.eq(org_uuid))
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-05-28 16:26:02 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 16:35:11 +00:00
|
|
|
pub fn find_by_uuid_and_user(uuid: &str, user_uuid: &str, conn: &DbConn) -> Option<Self> {
|
2018-05-21 16:31:46 +00:00
|
|
|
collections::table
|
|
|
|
.left_join(users_collections::table.on(
|
|
|
|
users_collections::collection_uuid.eq(collections::uuid).and(
|
|
|
|
users_collections::user_uuid.eq(user_uuid)
|
|
|
|
)
|
|
|
|
))
|
|
|
|
.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::type_.le(UserOrgType::Admin as i32) // Org admin or owner
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).select(collections::all_columns)
|
|
|
|
.first::<Self>(&**conn).ok()
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
2018-05-09 10:55:05 +00:00
|
|
|
|
|
|
|
pub fn is_writable_by_user(&self, user_uuid: &str, conn: &DbConn) -> bool {
|
|
|
|
match UserOrganization::find_by_user_and_org(&user_uuid, &self.org_uuid, &conn) {
|
|
|
|
None => false, // Not in Org
|
|
|
|
Some(user_org) => {
|
2018-12-30 22:34:31 +00:00
|
|
|
if user_org.access_all {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
users_collections::table
|
|
|
|
.inner_join(collections::table)
|
|
|
|
.filter(users_collections::collection_uuid.eq(&self.uuid))
|
|
|
|
.filter(users_collections::user_uuid.eq(&user_uuid))
|
|
|
|
.filter(users_collections::read_only.eq(false))
|
|
|
|
.select(collections::all_columns)
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
|
|
|
.is_some() // Read only or no access to collection
|
|
|
|
}
|
2018-05-09 10:55:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use super::User;
|
2018-04-20 16:35:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "users_collections"]
|
|
|
|
#[belongs_to(User, foreign_key = "user_uuid")]
|
|
|
|
#[belongs_to(Collection, foreign_key = "collection_uuid")]
|
|
|
|
#[primary_key(user_uuid, collection_uuid)]
|
2018-05-16 22:05:50 +00:00
|
|
|
pub struct CollectionUser {
|
2018-04-20 16:35:11 +00:00
|
|
|
pub user_uuid: String,
|
|
|
|
pub collection_uuid: String,
|
2018-05-04 18:10:35 +00:00
|
|
|
pub read_only: bool,
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 17:25:50 +00:00
|
|
|
/// Database methods
|
2018-05-16 22:05:50 +00:00
|
|
|
impl CollectionUser {
|
2018-05-11 18:08:02 +00:00
|
|
|
pub fn find_by_organization_and_user_uuid(org_uuid: &str, user_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
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)
|
2018-12-30 22:34:31 +00:00
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading users_collections")
|
2018-05-11 18:08:02 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
pub fn save(user_uuid: &str, collection_uuid: &str, read_only: bool, conn: &DbConn) -> EmptyResult {
|
2018-08-21 11:20:55 +00:00
|
|
|
User::update_uuid_revision(&user_uuid, conn);
|
|
|
|
|
2018-05-28 16:26:02 +00:00
|
|
|
diesel::replace_into(users_collections::table)
|
2018-12-30 22:34:31 +00:00
|
|
|
.values((
|
|
|
|
users_collections::user_uuid.eq(user_uuid),
|
|
|
|
users_collections::collection_uuid.eq(collection_uuid),
|
|
|
|
users_collections::read_only.eq(read_only),
|
|
|
|
))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error adding user to collection")
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
2018-08-21 11:20:55 +00:00
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::delete(
|
|
|
|
users_collections::table
|
|
|
|
.filter(users_collections::user_uuid.eq(&self.user_uuid))
|
|
|
|
.filter(users_collections::collection_uuid.eq(&self.collection_uuid)),
|
|
|
|
)
|
2018-12-19 20:52:53 +00:00
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error removing user from collection")
|
2018-05-29 15:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_collection(collection_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
users_collections::table
|
2018-12-30 22:34:31 +00:00
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.load::<Self>(&**conn)
|
|
|
|
.expect("Error loading users_collections")
|
2018-05-29 15:01:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_by_collection_and_user(collection_uuid: &str, user_uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
users_collections::table
|
2018-12-30 22:34:31 +00:00
|
|
|
.filter(users_collections::collection_uuid.eq(collection_uuid))
|
|
|
|
.filter(users_collections::user_uuid.eq(user_uuid))
|
|
|
|
.select(users_collections::all_columns)
|
|
|
|
.first::<Self>(&**conn)
|
|
|
|
.ok()
|
2018-04-20 16:35:11 +00:00
|
|
|
}
|
2018-05-16 22:05:50 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-08-21 11:20:55 +00:00
|
|
|
CollectionUser::find_by_collection(&collection_uuid, conn)
|
2018-12-30 22:34:31 +00:00
|
|
|
.iter()
|
2019-01-27 23:39:14 +00:00
|
|
|
.for_each(|collection| {
|
|
|
|
User::update_uuid_revision(&collection.user_uuid, conn);
|
|
|
|
});
|
2018-08-21 11:20:55 +00:00
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::delete(users_collections::table.filter(users_collections::collection_uuid.eq(collection_uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error deleting users from collection")
|
2018-05-16 22:05:50 +00:00
|
|
|
}
|
2018-05-18 15:52:51 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_user(user_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-08-21 11:20:55 +00:00
|
|
|
User::update_uuid_revision(&user_uuid, conn);
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::delete(users_collections::table.filter(users_collections::user_uuid.eq(user_uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error removing user from collections")
|
2018-05-18 15:52:51 +00:00
|
|
|
}
|
2018-05-09 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-30 22:34:31 +00:00
|
|
|
use super::Cipher;
|
2018-05-09 10:55:05 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable, Associations)]
|
|
|
|
#[table_name = "ciphers_collections"]
|
|
|
|
#[belongs_to(Cipher, foreign_key = "cipher_uuid")]
|
|
|
|
#[belongs_to(Collection, foreign_key = "collection_uuid")]
|
|
|
|
#[primary_key(cipher_uuid, collection_uuid)]
|
|
|
|
pub struct CollectionCipher {
|
|
|
|
pub cipher_uuid: String,
|
|
|
|
pub collection_uuid: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Database methods
|
|
|
|
impl CollectionCipher {
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn save(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-10-06 21:56:05 +00:00
|
|
|
diesel::replace_into(ciphers_collections::table)
|
2018-05-09 10:55:05 +00:00
|
|
|
.values((
|
|
|
|
ciphers_collections::cipher_uuid.eq(cipher_uuid),
|
|
|
|
ciphers_collections::collection_uuid.eq(collection_uuid),
|
2018-12-30 22:34:31 +00:00
|
|
|
))
|
|
|
|
.execute(&**conn)
|
2018-12-19 20:52:53 +00:00
|
|
|
.map_res("Error adding cipher to collection")
|
2018-05-09 10:55:05 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete(cipher_uuid: &str, collection_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-12-30 22:34:31 +00:00
|
|
|
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 10:55:05 +00:00
|
|
|
}
|
2018-05-15 16:27:53 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_cipher(cipher_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-12-30 22:34:31 +00:00
|
|
|
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 16:27:53 +00:00
|
|
|
}
|
2018-05-16 22:05:50 +00:00
|
|
|
|
2018-12-19 20:52:53 +00:00
|
|
|
pub fn delete_all_by_collection(collection_uuid: &str, conn: &DbConn) -> EmptyResult {
|
2018-12-30 22:34:31 +00:00
|
|
|
diesel::delete(ciphers_collections::table.filter(ciphers_collections::collection_uuid.eq(collection_uuid)))
|
|
|
|
.execute(&**conn)
|
|
|
|
.map_res("Error removing ciphers from collection")
|
2018-05-16 22:05:50 +00:00
|
|
|
}
|
2018-12-30 22:34:31 +00:00
|
|
|
}
|