|
|
|
@ -1,7 +1,7 @@
|
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
|
|
use super::{User, CollectionUser, Invitation};
|
|
|
|
|
use super::{User, CollectionUser};
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Identifiable, Queryable, Insertable)]
|
|
|
|
|
#[table_name = "organizations"]
|
|
|
|
@ -154,8 +154,6 @@ impl UserOrgType {
|
|
|
|
|
|
|
|
|
|
/// Local methods
|
|
|
|
|
impl Organization {
|
|
|
|
|
pub const VIRTUAL_ID: &'static str = "00000000-0000-0000-0000-000000000000";
|
|
|
|
|
|
|
|
|
|
pub fn new(name: String, billing_email: String) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
uuid: crate::util::get_uuid(),
|
|
|
|
@ -165,14 +163,6 @@ impl Organization {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_virtual() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
uuid: String::from(Organization::VIRTUAL_ID),
|
|
|
|
|
name: String::from("bitwarden_rs"),
|
|
|
|
|
billing_email: String::from("none@none.none")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn to_json(&self) -> Value {
|
|
|
|
|
json!({
|
|
|
|
|
"Id": self.uuid,
|
|
|
|
@ -216,20 +206,6 @@ impl UserOrganization {
|
|
|
|
|
type_: UserOrgType::User as i32,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn new_virtual(user_uuid: String, type_: UserOrgType, status: UserOrgStatus) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
uuid: user_uuid.clone(),
|
|
|
|
|
|
|
|
|
|
user_uuid,
|
|
|
|
|
org_uuid: String::from(Organization::VIRTUAL_ID),
|
|
|
|
|
|
|
|
|
|
access_all: true,
|
|
|
|
|
key: String::new(),
|
|
|
|
|
status: status as i32,
|
|
|
|
|
type_: type_ as i32,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -244,10 +220,6 @@ use crate::error::MapResult;
|
|
|
|
|
/// Database methods
|
|
|
|
|
impl Organization {
|
|
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
|
|
|
|
if self.uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
err!("diesel::result::Error::NotFound")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UserOrganization::find_by_org(&self.uuid, conn)
|
|
|
|
|
.iter()
|
|
|
|
|
.for_each(|user_org| {
|
|
|
|
@ -262,10 +234,6 @@ impl Organization {
|
|
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
|
|
|
|
use super::{Cipher, Collection};
|
|
|
|
|
|
|
|
|
|
if self.uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
err!("diesel::result::Error::NotFound")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Cipher::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
|
Collection::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
|
UserOrganization::delete_all_by_organization(&self.uuid, &conn)?;
|
|
|
|
@ -279,9 +247,6 @@ impl Organization {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find_by_uuid(uuid: &str, conn: &DbConn) -> Option<Self> {
|
|
|
|
|
if uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
return Some(Self::new_virtual())
|
|
|
|
|
};
|
|
|
|
|
organizations::table
|
|
|
|
|
.filter(organizations::uuid.eq(uuid))
|
|
|
|
|
.first::<Self>(&**conn).ok()
|
|
|
|
@ -371,9 +336,6 @@ impl UserOrganization {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn save(&mut self, conn: &DbConn) -> EmptyResult {
|
|
|
|
|
if self.org_uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
err!("diesel::result::Error::NotFound")
|
|
|
|
|
}
|
|
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
|
|
|
|
|
|
|
|
|
diesel::replace_into(users_organizations::table)
|
|
|
|
@ -382,9 +344,6 @@ impl UserOrganization {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn delete(self, conn: &DbConn) -> EmptyResult {
|
|
|
|
|
if self.org_uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
err!("diesel::result::Error::NotFound")
|
|
|
|
|
}
|
|
|
|
|
User::update_uuid_revision(&self.user_uuid, conn);
|
|
|
|
|
|
|
|
|
|
CollectionUser::delete_all_by_user(&self.user_uuid, &conn)?;
|
|
|
|
@ -449,22 +408,9 @@ impl UserOrganization {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find_by_org(org_uuid: &str, conn: &DbConn) -> Vec<Self> {
|
|
|
|
|
if org_uuid == Organization::VIRTUAL_ID {
|
|
|
|
|
User::get_all(&*conn).iter().map(|user| {
|
|
|
|
|
Self::new_virtual(
|
|
|
|
|
user.uuid.clone(),
|
|
|
|
|
UserOrgType::User,
|
|
|
|
|
if Invitation::find_by_mail(&user.email, &conn).is_some() {
|
|
|
|
|
UserOrgStatus::Invited
|
|
|
|
|
} else {
|
|
|
|
|
UserOrgStatus::Confirmed
|
|
|
|
|
})
|
|
|
|
|
}).collect()
|
|
|
|
|
} else {
|
|
|
|
|
users_organizations::table
|
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
|
.load::<Self>(&**conn).expect("Error loading user organizations")
|
|
|
|
|
}
|
|
|
|
|
users_organizations::table
|
|
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid))
|
|
|
|
|
.load::<Self>(&**conn).expect("Error loading user organizations")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn find_by_org_and_type(org_uuid: &str, type_: i32, conn: &DbConn) -> Vec<Self> {
|
|
|
|
|