|
|
@ -25,7 +25,7 @@ db_object! { |
|
|
|
#[derive(Identifiable, Queryable, Insertable, AsChangeset)] |
|
|
|
#[diesel(table_name = users_organizations)] |
|
|
|
#[diesel(primary_key(uuid))] |
|
|
|
pub struct UserOrganization { |
|
|
|
pub struct Membership { |
|
|
|
pub uuid: String, |
|
|
|
pub user_uuid: String, |
|
|
|
pub org_uuid: String, |
|
|
@ -51,7 +51,7 @@ db_object! { |
|
|
|
} |
|
|
|
|
|
|
|
// https://github.com/bitwarden/server/blob/b86a04cef9f1e1b82cf18e49fc94e017c641130c/src/Core/Enums/OrganizationUserStatusType.cs
|
|
|
|
pub enum UserOrgStatus { |
|
|
|
pub enum MembershipStatus { |
|
|
|
Revoked = -1, |
|
|
|
Invited = 0, |
|
|
|
Accepted = 1, |
|
|
@ -59,29 +59,29 @@ pub enum UserOrgStatus { |
|
|
|
} |
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, num_derive::FromPrimitive)] |
|
|
|
pub enum UserOrgType { |
|
|
|
pub enum MembershipType { |
|
|
|
Owner = 0, |
|
|
|
Admin = 1, |
|
|
|
User = 2, |
|
|
|
Manager = 3, |
|
|
|
} |
|
|
|
|
|
|
|
impl UserOrgType { |
|
|
|
impl MembershipType { |
|
|
|
pub fn from_str(s: &str) -> Option<Self> { |
|
|
|
match s { |
|
|
|
"0" | "Owner" => Some(UserOrgType::Owner), |
|
|
|
"1" | "Admin" => Some(UserOrgType::Admin), |
|
|
|
"2" | "User" => Some(UserOrgType::User), |
|
|
|
"3" | "Manager" => Some(UserOrgType::Manager), |
|
|
|
"0" | "Owner" => Some(MembershipType::Owner), |
|
|
|
"1" | "Admin" => Some(MembershipType::Admin), |
|
|
|
"2" | "User" => Some(MembershipType::User), |
|
|
|
"3" | "Manager" => Some(MembershipType::Manager), |
|
|
|
// HACK: We convert the custom role to a manager role
|
|
|
|
"4" | "Custom" => Some(UserOrgType::Manager), |
|
|
|
"4" | "Custom" => Some(MembershipType::Manager), |
|
|
|
_ => None, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl Ord for UserOrgType { |
|
|
|
fn cmp(&self, other: &UserOrgType) -> Ordering { |
|
|
|
impl Ord for MembershipType { |
|
|
|
fn cmp(&self, other: &MembershipType) -> Ordering { |
|
|
|
// For easy comparison, map each variant to an access level (where 0 is lowest).
|
|
|
|
static ACCESS_LEVEL: [i32; 4] = [ |
|
|
|
3, // Owner
|
|
|
@ -93,19 +93,19 @@ impl Ord for UserOrgType { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl PartialOrd for UserOrgType { |
|
|
|
fn partial_cmp(&self, other: &UserOrgType) -> Option<Ordering> { |
|
|
|
impl PartialOrd for MembershipType { |
|
|
|
fn partial_cmp(&self, other: &MembershipType) -> Option<Ordering> { |
|
|
|
Some(self.cmp(other)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl PartialEq<i32> for UserOrgType { |
|
|
|
impl PartialEq<i32> for MembershipType { |
|
|
|
fn eq(&self, other: &i32) -> bool { |
|
|
|
*other == *self as i32 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl PartialOrd<i32> for UserOrgType { |
|
|
|
impl PartialOrd<i32> for MembershipType { |
|
|
|
fn partial_cmp(&self, other: &i32) -> Option<Ordering> { |
|
|
|
if let Some(other) = Self::from_i32(*other) { |
|
|
|
return Some(self.cmp(&other)); |
|
|
@ -122,25 +122,25 @@ impl PartialOrd<i32> for UserOrgType { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl PartialEq<UserOrgType> for i32 { |
|
|
|
fn eq(&self, other: &UserOrgType) -> bool { |
|
|
|
impl PartialEq<MembershipType> for i32 { |
|
|
|
fn eq(&self, other: &MembershipType) -> bool { |
|
|
|
*self == *other as i32 |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl PartialOrd<UserOrgType> for i32 { |
|
|
|
fn partial_cmp(&self, other: &UserOrgType) -> Option<Ordering> { |
|
|
|
if let Some(self_type) = UserOrgType::from_i32(*self) { |
|
|
|
impl PartialOrd<MembershipType> for i32 { |
|
|
|
fn partial_cmp(&self, other: &MembershipType) -> Option<Ordering> { |
|
|
|
if let Some(self_type) = MembershipType::from_i32(*self) { |
|
|
|
return Some(self_type.cmp(other)); |
|
|
|
} |
|
|
|
None |
|
|
|
} |
|
|
|
|
|
|
|
fn lt(&self, other: &UserOrgType) -> bool { |
|
|
|
fn lt(&self, other: &MembershipType) -> bool { |
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Less) | None) |
|
|
|
} |
|
|
|
|
|
|
|
fn le(&self, other: &UserOrgType) -> bool { |
|
|
|
fn le(&self, other: &MembershipType) -> bool { |
|
|
|
matches!(self.partial_cmp(other), Some(Ordering::Less | Ordering::Equal) | None) |
|
|
|
} |
|
|
|
} |
|
|
@ -214,7 +214,7 @@ impl Organization { |
|
|
|
// It should also provide enough room for 100+ types, which i doubt will ever happen.
|
|
|
|
static ACTIVATE_REVOKE_DIFF: i32 = 128; |
|
|
|
|
|
|
|
impl UserOrganization { |
|
|
|
impl Membership { |
|
|
|
pub fn new(user_uuid: String, org_uuid: String) -> Self { |
|
|
|
Self { |
|
|
|
uuid: crate::util::get_uuid(), |
|
|
@ -224,15 +224,15 @@ impl UserOrganization { |
|
|
|
|
|
|
|
access_all: false, |
|
|
|
akey: String::new(), |
|
|
|
status: UserOrgStatus::Accepted as i32, |
|
|
|
atype: UserOrgType::User as i32, |
|
|
|
status: MembershipStatus::Accepted as i32, |
|
|
|
atype: MembershipType::User as i32, |
|
|
|
reset_password_key: None, |
|
|
|
external_id: None, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn restore(&mut self) -> bool { |
|
|
|
if self.status < UserOrgStatus::Invited as i32 { |
|
|
|
if self.status < MembershipStatus::Invited as i32 { |
|
|
|
self.status += ACTIVATE_REVOKE_DIFF; |
|
|
|
return true; |
|
|
|
} |
|
|
@ -240,7 +240,7 @@ impl UserOrganization { |
|
|
|
} |
|
|
|
|
|
|
|
pub fn revoke(&mut self) -> bool { |
|
|
|
if self.status > UserOrgStatus::Revoked as i32 { |
|
|
|
if self.status > MembershipStatus::Revoked as i32 { |
|
|
|
self.status -= ACTIVATE_REVOKE_DIFF; |
|
|
|
return true; |
|
|
|
} |
|
|
@ -249,7 +249,7 @@ impl UserOrganization { |
|
|
|
|
|
|
|
/// Return the status of the user in an unrevoked state
|
|
|
|
pub fn get_unrevoked_status(&self) -> i32 { |
|
|
|
if self.status <= UserOrgStatus::Revoked as i32 { |
|
|
|
if self.status <= MembershipStatus::Revoked as i32 { |
|
|
|
return self.status + ACTIVATE_REVOKE_DIFF; |
|
|
|
} |
|
|
|
self.status |
|
|
@ -307,8 +307,8 @@ impl Organization { |
|
|
|
err!(format!("BillingEmail {} is not a valid email address", self.billing_email.trim())) |
|
|
|
} |
|
|
|
|
|
|
|
for user_org in UserOrganization::find_by_org(&self.uuid, conn).await.iter() { |
|
|
|
User::update_uuid_revision(&user_org.user_uuid, conn).await; |
|
|
|
for member in Membership::find_by_org(&self.uuid, conn).await.iter() { |
|
|
|
User::update_uuid_revision(&member.user_uuid, conn).await; |
|
|
|
} |
|
|
|
|
|
|
|
db_run! { conn: |
|
|
@ -348,7 +348,7 @@ impl Organization { |
|
|
|
|
|
|
|
Cipher::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
Collection::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
UserOrganization::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
Membership::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
OrgPolicy::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
Group::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
|
OrganizationApiKey::delete_all_by_organization(&self.uuid, conn).await?; |
|
|
@ -376,13 +376,13 @@ impl Organization { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
impl UserOrganization { |
|
|
|
impl Membership { |
|
|
|
pub async fn to_json(&self, conn: &mut DbConn) -> Value { |
|
|
|
let org = Organization::find_by_uuid(&self.org_uuid, conn).await.unwrap(); |
|
|
|
|
|
|
|
// HACK: Convert the manager type to a custom type
|
|
|
|
// It will be converted back on other locations
|
|
|
|
let user_org_type = self.type_manager_as_custom(); |
|
|
|
let membership_type = self.type_manager_as_custom(); |
|
|
|
|
|
|
|
let permissions = json!({ |
|
|
|
// TODO: Add full support for Custom User Roles
|
|
|
@ -392,9 +392,9 @@ impl UserOrganization { |
|
|
|
"accessImportExport": false, |
|
|
|
"accessReports": false, |
|
|
|
// If the following 3 Collection roles are set to true a custom user has access all permission
|
|
|
|
"createNewCollections": user_org_type == 4 && self.access_all, |
|
|
|
"editAnyCollection": user_org_type == 4 && self.access_all, |
|
|
|
"deleteAnyCollection": user_org_type == 4 && self.access_all, |
|
|
|
"createNewCollections": membership_type == 4 && self.access_all, |
|
|
|
"editAnyCollection": membership_type == 4 && self.access_all, |
|
|
|
"deleteAnyCollection": membership_type == 4 && self.access_all, |
|
|
|
"manageGroups": false, |
|
|
|
"managePolicies": false, |
|
|
|
"manageSso": false, // Not supported
|
|
|
@ -459,7 +459,7 @@ impl UserOrganization { |
|
|
|
"userId": self.user_uuid, |
|
|
|
"key": self.akey, |
|
|
|
"status": self.status, |
|
|
|
"type": user_org_type, |
|
|
|
"type": membership_type, |
|
|
|
"enabled": true, |
|
|
|
|
|
|
|
"object": "profileOrganization", |
|
|
@ -476,8 +476,8 @@ impl UserOrganization { |
|
|
|
|
|
|
|
// Because BitWarden want the status to be -1 for revoked users we need to catch that here.
|
|
|
|
// We subtract/add a number so we can restore/activate the user to it's previous state again.
|
|
|
|
let status = if self.status < UserOrgStatus::Revoked as i32 { |
|
|
|
UserOrgStatus::Revoked as i32 |
|
|
|
let status = if self.status < MembershipStatus::Revoked as i32 { |
|
|
|
MembershipStatus::Revoked as i32 |
|
|
|
} else { |
|
|
|
self.status |
|
|
|
}; |
|
|
@ -519,12 +519,12 @@ impl UserOrganization { |
|
|
|
.into_iter() |
|
|
|
.filter_map(|c| { |
|
|
|
let (read_only, hide_passwords, can_manage) = if self.has_full_access() { |
|
|
|
(false, false, self.atype >= UserOrgType::Manager) |
|
|
|
(false, false, self.atype >= MembershipType::Manager) |
|
|
|
} else if let Some(cu) = cu.get(&c.uuid) { |
|
|
|
( |
|
|
|
cu.read_only, |
|
|
|
cu.hide_passwords, |
|
|
|
self.atype == UserOrgType::Manager && !cu.read_only && !cu.hide_passwords, |
|
|
|
self.atype == MembershipType::Manager && !cu.read_only && !cu.hide_passwords, |
|
|
|
) |
|
|
|
// If previous checks failed it might be that this user has access via a group, but we should not return those elements here
|
|
|
|
// Those are returned via a special group endpoint
|
|
|
@ -548,11 +548,11 @@ impl UserOrganization { |
|
|
|
|
|
|
|
// HACK: Convert the manager type to a custom type
|
|
|
|
// It will be converted back on other locations
|
|
|
|
let user_org_type = self.type_manager_as_custom(); |
|
|
|
let membership_type = self.type_manager_as_custom(); |
|
|
|
|
|
|
|
// HACK: Only return permissions if the user is of type custom and has access_all
|
|
|
|
// Else Bitwarden will assume the defaults of all false
|
|
|
|
let permissions = if user_org_type == 4 && self.access_all { |
|
|
|
let permissions = if membership_type == 4 && self.access_all { |
|
|
|
json!({ |
|
|
|
// TODO: Add full support for Custom User Roles
|
|
|
|
// See: https://bitwarden.com/help/article/user-types-access-control/#custom-role
|
|
|
@ -578,7 +578,7 @@ impl UserOrganization { |
|
|
|
json!({ |
|
|
|
"id": self.uuid, |
|
|
|
"userId": self.user_uuid, |
|
|
|
"name": if self.get_unrevoked_status() >= UserOrgStatus::Accepted as i32 { Some(user.name) } else { None }, |
|
|
|
"name": if self.get_unrevoked_status() >= MembershipStatus::Accepted as i32 { Some(user.name) } else { None }, |
|
|
|
"email": user.email, |
|
|
|
"externalId": self.external_id, |
|
|
|
"avatarColor": user.avatar_color, |
|
|
@ -586,7 +586,7 @@ impl UserOrganization { |
|
|
|
"collections": collections, |
|
|
|
|
|
|
|
"status": status, |
|
|
|
"type": user_org_type, |
|
|
|
"type": membership_type, |
|
|
|
"accessAll": self.access_all, |
|
|
|
"twoFactorEnabled": twofactor_enabled, |
|
|
|
"resetPasswordEnrolled": self.reset_password_key.is_some(), |
|
|
@ -630,8 +630,8 @@ impl UserOrganization { |
|
|
|
|
|
|
|
// Because BitWarden want the status to be -1 for revoked users we need to catch that here.
|
|
|
|
// We subtract/add a number so we can restore/activate the user to it's previous state again.
|
|
|
|
let status = if self.status < UserOrgStatus::Revoked as i32 { |
|
|
|
UserOrgStatus::Revoked as i32 |
|
|
|
let status = if self.status < MembershipStatus::Revoked as i32 { |
|
|
|
MembershipStatus::Revoked as i32 |
|
|
|
} else { |
|
|
|
self.status |
|
|
|
}; |
|
|
@ -654,8 +654,8 @@ impl UserOrganization { |
|
|
|
|
|
|
|
// Because Bitwarden wants the status to be -1 for revoked users we need to catch that here.
|
|
|
|
// We subtract/add a number so we can restore/activate the user to it's previous state again.
|
|
|
|
let status = if self.status < UserOrgStatus::Revoked as i32 { |
|
|
|
UserOrgStatus::Revoked as i32 |
|
|
|
let status = if self.status < MembershipStatus::Revoked as i32 { |
|
|
|
MembershipStatus::Revoked as i32 |
|
|
|
} else { |
|
|
|
self.status |
|
|
|
}; |
|
|
@ -677,7 +677,7 @@ impl UserOrganization { |
|
|
|
db_run! { conn: |
|
|
|
sqlite, mysql { |
|
|
|
match diesel::replace_into(users_organizations::table) |
|
|
|
.values(UserOrganizationDb::to_db(self)) |
|
|
|
.values(MembershipDb::to_db(self)) |
|
|
|
.execute(conn) |
|
|
|
{ |
|
|
|
Ok(_) => Ok(()), |
|
|
@ -685,7 +685,7 @@ impl UserOrganization { |
|
|
|
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => { |
|
|
|
diesel::update(users_organizations::table) |
|
|
|
.filter(users_organizations::uuid.eq(&self.uuid)) |
|
|
|
.set(UserOrganizationDb::to_db(self)) |
|
|
|
.set(MembershipDb::to_db(self)) |
|
|
|
.execute(conn) |
|
|
|
.map_res("Error adding user to organization") |
|
|
|
}, |
|
|
@ -693,7 +693,7 @@ impl UserOrganization { |
|
|
|
}.map_res("Error adding user to organization") |
|
|
|
} |
|
|
|
postgresql { |
|
|
|
let value = UserOrganizationDb::to_db(self); |
|
|
|
let value = MembershipDb::to_db(self); |
|
|
|
diesel::insert_into(users_organizations::table) |
|
|
|
.values(&value) |
|
|
|
.on_conflict(users_organizations::uuid) |
|
|
@ -709,7 +709,7 @@ impl UserOrganization { |
|
|
|
User::update_uuid_revision(&self.user_uuid, conn).await; |
|
|
|
|
|
|
|
CollectionUser::delete_all_by_user_and_org(&self.user_uuid, &self.org_uuid, conn).await?; |
|
|
|
GroupUser::delete_all_by_user(&self.uuid, conn).await?; |
|
|
|
GroupUser::delete_all_by_member(&self.uuid, conn).await?; |
|
|
|
|
|
|
|
db_run! { conn: { |
|
|
|
diesel::delete(users_organizations::table.filter(users_organizations::uuid.eq(self.uuid))) |
|
|
@ -719,46 +719,46 @@ impl UserOrganization { |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn delete_all_by_organization(org_uuid: &str, conn: &mut DbConn) -> EmptyResult { |
|
|
|
for user_org in Self::find_by_org(org_uuid, conn).await { |
|
|
|
user_org.delete(conn).await?; |
|
|
|
for member in Self::find_by_org(org_uuid, conn).await { |
|
|
|
member.delete(conn).await?; |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn delete_all_by_user(user_uuid: &str, conn: &mut DbConn) -> EmptyResult { |
|
|
|
for user_org in Self::find_any_state_by_user(user_uuid, conn).await { |
|
|
|
user_org.delete(conn).await?; |
|
|
|
for member in Self::find_any_state_by_user(user_uuid, conn).await { |
|
|
|
member.delete(conn).await?; |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn find_by_email_and_org(email: &str, org_id: &str, conn: &mut DbConn) -> Option<UserOrganization> { |
|
|
|
pub async fn find_by_email_and_org(email: &str, org_id: &str, conn: &mut DbConn) -> Option<Membership> { |
|
|
|
if let Some(user) = User::find_by_mail(email, conn).await { |
|
|
|
if let Some(user_org) = UserOrganization::find_by_user_and_org(&user.uuid, org_id, conn).await { |
|
|
|
return Some(user_org); |
|
|
|
if let Some(member) = Membership::find_by_user_and_org(&user.uuid, org_id, conn).await { |
|
|
|
return Some(member); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
None |
|
|
|
} |
|
|
|
|
|
|
|
pub fn has_status(&self, status: UserOrgStatus) -> bool { |
|
|
|
pub fn has_status(&self, status: MembershipStatus) -> bool { |
|
|
|
self.status == status as i32 |
|
|
|
} |
|
|
|
|
|
|
|
pub fn has_type(&self, user_type: UserOrgType) -> bool { |
|
|
|
pub fn has_type(&self, user_type: MembershipType) -> bool { |
|
|
|
self.atype == user_type as i32 |
|
|
|
} |
|
|
|
|
|
|
|
pub fn has_full_access(&self) -> bool { |
|
|
|
(self.access_all || self.atype >= UserOrgType::Admin) && self.has_status(UserOrgStatus::Confirmed) |
|
|
|
(self.access_all || self.atype >= MembershipType::Admin) && self.has_status(MembershipStatus::Confirmed) |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn find_by_uuid(uuid: &str, conn: &mut DbConn) -> Option<Self> { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::uuid.eq(uuid)) |
|
|
|
.first::<UserOrganizationDb>(conn) |
|
|
|
.first::<MembershipDb>(conn) |
|
|
|
.ok().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -768,7 +768,7 @@ impl UserOrganization { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::uuid.eq(uuid)) |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.first::<UserOrganizationDb>(conn) |
|
|
|
.first::<MembershipDb>(conn) |
|
|
|
.ok().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -777,8 +777,8 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Confirmed as i32)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.unwrap_or_default().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -787,8 +787,8 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Invited as i32)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Invited as i32)) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.unwrap_or_default().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -797,7 +797,7 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.unwrap_or_default().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -806,7 +806,7 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Accepted as i32).or(users_organizations::status.eq(UserOrgStatus::Confirmed as i32))) |
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Accepted as i32).or(users_organizations::status.eq(MembershipStatus::Confirmed as i32))) |
|
|
|
.count() |
|
|
|
.first::<i64>(conn) |
|
|
|
.unwrap_or(0) |
|
|
@ -817,7 +817,7 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.expect("Error loading user organizations").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -826,8 +826,8 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Confirmed as i32)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.unwrap_or_default().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -843,22 +843,22 @@ impl UserOrganization { |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn find_by_org_and_type(org_uuid: &str, atype: UserOrgType, conn: &mut DbConn) -> Vec<Self> { |
|
|
|
pub async fn find_by_org_and_type(org_uuid: &str, atype: MembershipType, conn: &mut DbConn) -> Vec<Self> { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.filter(users_organizations::atype.eq(atype as i32)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.expect("Error loading user organizations").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn count_confirmed_by_org_and_type(org_uuid: &str, atype: UserOrgType, conn: &mut DbConn) -> i64 { |
|
|
|
pub async fn count_confirmed_by_org_and_type(org_uuid: &str, atype: MembershipType, conn: &mut DbConn) -> i64 { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.filter(users_organizations::atype.eq(atype as i32)) |
|
|
|
.filter(users_organizations::status.eq(UserOrgStatus::Confirmed as i32)) |
|
|
|
.filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) |
|
|
|
.count() |
|
|
|
.first::<i64>(conn) |
|
|
|
.unwrap_or(0) |
|
|
@ -870,7 +870,7 @@ impl UserOrganization { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.first::<UserOrganizationDb>(conn) |
|
|
|
.first::<MembershipDb>(conn) |
|
|
|
.ok().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -881,9 +881,9 @@ impl UserOrganization { |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
.filter( |
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32) |
|
|
|
users_organizations::status.eq(MembershipStatus::Confirmed as i32) |
|
|
|
) |
|
|
|
.first::<UserOrganizationDb>(conn) |
|
|
|
.first::<MembershipDb>(conn) |
|
|
|
.ok().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -892,12 +892,12 @@ impl UserOrganization { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.expect("Error loading user organizations").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
|
pub async fn get_org_uuid_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<String> { |
|
|
|
pub async fn get_orgs_by_user(user_uuid: &str, conn: &mut DbConn) -> Vec<String> { |
|
|
|
db_run! { conn: { |
|
|
|
users_organizations::table |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
@ -918,10 +918,10 @@ impl UserOrganization { |
|
|
|
.and(org_policies::enabled.eq(true))) |
|
|
|
) |
|
|
|
.filter( |
|
|
|
users_organizations::status.eq(UserOrgStatus::Confirmed as i32) |
|
|
|
users_organizations::status.eq(MembershipStatus::Confirmed as i32) |
|
|
|
) |
|
|
|
.select(users_organizations::all_columns) |
|
|
|
.load::<UserOrganizationDb>(conn) |
|
|
|
.load::<MembershipDb>(conn) |
|
|
|
.unwrap_or_default().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
@ -945,7 +945,7 @@ impl UserOrganization { |
|
|
|
) |
|
|
|
.select(users_organizations::all_columns) |
|
|
|
.distinct() |
|
|
|
.load::<UserOrganizationDb>(conn).expect("Error loading user organizations").from_db() |
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
@ -971,7 +971,7 @@ impl UserOrganization { |
|
|
|
) |
|
|
|
.select(users_organizations::all_columns) |
|
|
|
.distinct() |
|
|
|
.load::<UserOrganizationDb>(conn).expect("Error loading user organizations with groups").from_db() |
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations with groups").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
@ -980,7 +980,7 @@ impl UserOrganization { |
|
|
|
users_organizations::table |
|
|
|
.inner_join(ciphers::table.on(ciphers::uuid.eq(cipher_uuid).and(ciphers::organization_uuid.eq(users_organizations::org_uuid.nullable())))) |
|
|
|
.filter(users_organizations::user_uuid.eq(user_uuid)) |
|
|
|
.filter(users_organizations::atype.eq_any(vec![UserOrgType::Owner as i32, UserOrgType::Admin as i32])) |
|
|
|
.filter(users_organizations::atype.eq_any(vec![MembershipType::Owner as i32, MembershipType::Admin as i32])) |
|
|
|
.count() |
|
|
|
.first::<i64>(conn) |
|
|
|
.ok().unwrap_or(0) != 0 |
|
|
@ -1000,7 +1000,7 @@ impl UserOrganization { |
|
|
|
) |
|
|
|
) |
|
|
|
.select(users_organizations::all_columns) |
|
|
|
.load::<UserOrganizationDb>(conn).expect("Error loading user organizations").from_db() |
|
|
|
.load::<MembershipDb>(conn).expect("Error loading user organizations").from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
|
|
|
@ -1011,7 +1011,7 @@ impl UserOrganization { |
|
|
|
users_organizations::external_id.eq(ext_id) |
|
|
|
.and(users_organizations::org_uuid.eq(org_uuid)) |
|
|
|
) |
|
|
|
.first::<UserOrganizationDb>(conn).ok().from_db() |
|
|
|
.first::<MembershipDb>(conn).ok().from_db() |
|
|
|
}} |
|
|
|
} |
|
|
|
} |
|
|
@ -1074,10 +1074,10 @@ mod tests { |
|
|
|
|
|
|
|
#[test] |
|
|
|
#[allow(non_snake_case)] |
|
|
|
fn partial_cmp_UserOrgType() { |
|
|
|
assert!(UserOrgType::Owner > UserOrgType::Admin); |
|
|
|
assert!(UserOrgType::Admin > UserOrgType::Manager); |
|
|
|
assert!(UserOrgType::Manager > UserOrgType::User); |
|
|
|
assert!(UserOrgType::Manager == UserOrgType::from_str("4").unwrap()); |
|
|
|
fn partial_cmp_MembershipType() { |
|
|
|
assert!(MembershipType::Owner > MembershipType::Admin); |
|
|
|
assert!(MembershipType::Admin > MembershipType::Manager); |
|
|
|
assert!(MembershipType::Manager > MembershipType::User); |
|
|
|
assert!(MembershipType::Manager == MembershipType::from_str("4").unwrap()); |
|
|
|
} |
|
|
|
} |
|
|
|