Skip to content

Commit

Permalink
Merge pull request dani-garcia#3690 from BlackDex/fix-issue-3685
Browse files Browse the repository at this point in the history
Fix some external_id issues
  • Loading branch information
dani-garcia authored Jul 14, 2023
2 parents 8707728 + 631d022 commit ab65d79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 38 deletions.
46 changes: 21 additions & 25 deletions src/api/core/organizations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use serde_json::Value;
use crate::{
api::{
core::{log_event, CipherSyncData, CipherSyncType},
ApiResult, EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, JsonVec, Notify, NumberOrString, PasswordData,
UpdateType,
EmptyResult, JsonResult, JsonUpcase, JsonUpcaseVec, JsonVec, Notify, NumberOrString, PasswordData, UpdateType,
},
auth::{decode_invite, AdminHeaders, Headers, ManagerHeaders, ManagerHeadersLoose, OwnerHeaders},
db::{models::*, DbConn},
Expand Down Expand Up @@ -468,7 +467,11 @@ async fn post_organization_collection_update(
}

collection.name = data.Name;
collection.external_id = data.ExternalId;
collection.external_id = match data.ExternalId {
Some(external_id) if !external_id.trim().is_empty() => Some(external_id),
_ => None,
};

collection.save(&mut conn).await?;

log_event(
Expand Down Expand Up @@ -2222,29 +2225,22 @@ struct GroupRequest {
}

impl GroupRequest {
pub fn to_group(&self, organizations_uuid: &str) -> ApiResult<Group> {
match self.AccessAll {
Some(access_all_value) => Ok(Group::new(
organizations_uuid.to_owned(),
self.Name.clone(),
access_all_value,
self.ExternalId.clone(),
)),
_ => err!("Could not convert GroupRequest to Group, because AccessAll has no value!"),
}
pub fn to_group(&self, organizations_uuid: &str) -> Group {
Group::new(
String::from(organizations_uuid),
self.Name.clone(),
self.AccessAll.unwrap_or(false),
self.ExternalId.clone(),
)
}

pub fn update_group(&self, mut group: Group) -> ApiResult<Group> {
match self.AccessAll {
Some(access_all_value) => {
group.name = self.Name.clone();
group.access_all = access_all_value;
group.set_external_id(self.ExternalId.clone());
pub fn update_group(&self, mut group: Group) -> Group {
group.name = self.Name.clone();
group.access_all = self.AccessAll.unwrap_or(false);
// Group Updates do not support changing the external_id
// These input fields are in a disabled state, and can only be updated/added via ldap_import

Ok(group)
}
_ => err!("Could not update group, because AccessAll has no value!"),
}
group
}
}

Expand Down Expand Up @@ -2305,7 +2301,7 @@ async fn post_groups(
}

let group_request = data.into_inner().data;
let group = group_request.to_group(org_id)?;
let group = group_request.to_group(org_id);

log_event(
EventType::GroupCreated as i32,
Expand Down Expand Up @@ -2339,7 +2335,7 @@ async fn put_group(
};

let group_request = data.into_inner().data;
let updated_group = group_request.update_group(group)?;
let updated_group = group_request.update_group(group);

CollectionGroup::delete_all_by_group(group_id, &mut conn).await?;
GroupUser::delete_all_by_group(group_id, &mut conn).await?;
Expand Down
17 changes: 5 additions & 12 deletions src/db/models/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,11 @@ impl Group {
}

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,
}
// Check if external_id is empty. We do not want to have empty strings in the database
self.external_id = match external_id {
Some(external_id) if !external_id.trim().is_empty() => Some(external_id),
_ => None,
};
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/db/models/organization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,14 +434,15 @@ impl UserOrganization {
"UserId": self.user_uuid,
"Name": user.name,
"Email": user.email,
"ExternalId": user.external_id,
"Groups": groups,
"Collections": collections,

"Status": status,
"Type": self.atype,
"AccessAll": self.access_all,
"TwoFactorEnabled": twofactor_enabled,
"ResetPasswordEnrolled":self.reset_password_key.is_some(),
"ResetPasswordEnrolled": self.reset_password_key.is_some(),

"Object": "organizationUserUserDetails",
})
Expand Down

0 comments on commit ab65d79

Please sign in to comment.