Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions services/headless-lms/models/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub async fn find_by_upstream_id(
) -> ModelResult<Option<User>> {
let user = sqlx::query_as!(
User,
"SELECT * FROM users WHERE upstream_id = $1",
"SELECT * FROM users WHERE upstream_id = $1 AND deleted_at IS NULL",
upstream_id
)
.fetch_optional(conn)
Expand Down Expand Up @@ -209,7 +209,7 @@ pub async fn update_email_for_user(

let user = sqlx::query_as!(
User,
"SELECT * FROM users WHERE upstream_id = $1",
"SELECT * FROM users WHERE upstream_id = $1 AND deleted_at IS NULL",
upstream_id
)
.fetch_one(&mut *tx)
Expand Down
49 changes: 31 additions & 18 deletions services/headless-lms/server/src/controllers/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,24 +218,37 @@ pub async fn signup(
.await;
let user = match user {
Ok(user) => user,
Err(error) => match error.error_type() {
ModelErrorType::DatabaseConstraint { constraint, .. }
if constraint == "users_email"
|| constraint == "users_upstream_id_active_uniq_idx" =>
{
// Either the email or the upstream_id already belongs to an existing account,
// so the caller already has a courses.mooc.fi user.
let token = skip_authorize();
return token.authorized_ok(web::Json(SignupResponse::EmailAlreadyExists));
}
_ => {
return Err(controller_err!(
InternalServerError,
"Failed to insert user.".to_string(),
anyhow!(error)
));
}
},
Err(error)
if matches!(
error.error_type(),
ModelErrorType::DatabaseConstraint { constraint, .. }
if constraint == "users_email"
) =>
{
let token = skip_authorize();
return token.authorized_ok(web::Json(SignupResponse::EmailAlreadyExists));
}
// TMC synchronously posts the new user back to /api/v0/tmc-server/users/create
// while post_new_user_to_tmc is still in flight, so that callback has usually
// already created the user; continue with the existing row.
Err(error)
if matches!(
error.error_type(),
ModelErrorType::DatabaseConstraint { constraint, .. }
if constraint == "users_upstream_id_active_uniq_idx"
) =>
{
models::users::find_by_upstream_id(&mut conn, upstream_id)
.await?
.ok_or(error)?
}
Err(error) => {
return Err(controller_err!(
InternalServerError,
"Failed to insert user.".to_string(),
anyhow!(error)
));
}
};

let country = user_details.country.clone();
Expand Down
Loading