Skip to content

Commit 40e5b1f

Browse files
committed
refactor allow_existing_users
1 parent 61565ef commit 40e5b1f

File tree

1 file changed

+22
-23
lines changed
  • crates/handlers/src/upstream_oauth2

1 file changed

+22
-23
lines changed

crates/handlers/src/upstream_oauth2/link.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -839,32 +839,29 @@ pub(crate) async fn post(
839839
)
840840
.into_response());
841841
}
842-
let mut is_new_user = true;
843-
let user = if provider.allow_existing_users {
844-
// If the provider allows existing users, we can use the existing user
845-
let existing_user = repo.user().find_by_username(&username).await?;
846-
if existing_user.is_some() {
847-
is_new_user = false;
848-
existing_user.unwrap()
849-
} else {
850-
REGISTRATION_COUNTER
851-
.add(1, &[KeyValue::new(PROVIDER, provider.id.to_string())]);
852-
repo.user().add(&mut rng, &clock, username).await?
853-
}
842+
843+
let mut existing_user: Option<mas_data_model::User> = None;
844+
845+
//search and use existing users if allowed
846+
if provider.allow_existing_users {
847+
existing_user = repo.user().find_by_username(&username).await?;
848+
}
849+
850+
851+
let user = if existing_user.is_some(){
852+
existing_user.unwrap()
854853
} else {
855854
REGISTRATION_COUNTER.add(1, &[KeyValue::new(PROVIDER, provider.id.to_string())]);
855+
856856
// Now we can create the user
857-
repo.user().add(&mut rng, &clock, username).await?
858-
};
859-
860-
if let Some(terms_url) = &site_config.tos_uri {
861-
repo.user_terms()
862-
.accept_terms(&mut rng, &clock, &user, terms_url.clone())
863-
.await?;
864-
}
857+
let user = repo.user().add(&mut rng, &clock, username).await?;
858+
859+
if let Some(terms_url) = &site_config.tos_uri {
860+
repo.user_terms()
861+
.accept_terms(&mut rng, &clock, &user, terms_url.clone())
862+
.await?;
863+
}
865864

866-
//create user in synapse only if needed
867-
if is_new_user {
868865
// And schedule the job to provision it
869866
let mut job = ProvisionUserJob::new(&user);
870867

@@ -874,7 +871,9 @@ pub(crate) async fn post(
874871
}
875872

876873
repo.queue_job().schedule_job(&mut rng, &clock, job).await?;
877-
}
874+
875+
user
876+
};
878877

879878
// If we have an email, add it to the user
880879
if let Some(email) = email {

0 commit comments

Comments
 (0)