Fix sso_user dropped on User::save (#6262)

* Admin delete SSO association prompt

* User.save don't use replace_into

* User.save use upsert with sqlite

* User.save use upsert with mysql
This commit is contained in:
Timshel
2025-10-13 21:25:53 +02:00
committed by GitHub
parent a79cd40ea9
commit e83faad8d2
2 changed files with 11 additions and 18 deletions

View File

@@ -283,24 +283,17 @@ impl User {
self.updated_at = Utc::now().naive_utc(); self.updated_at = Utc::now().naive_utc();
db_run! {conn: db_run! {conn:
sqlite, mysql { mysql {
match diesel::replace_into(users::table) let value = UserDb::to_db(self);
.values(UserDb::to_db(self)) diesel::insert_into(users::table)
.execute(conn) .values(&value)
{ .on_conflict(diesel::dsl::DuplicatedKeys)
Ok(_) => Ok(()), .do_update()
// Record already exists and causes a Foreign Key Violation because replace_into() wants to delete the record first. .set(&value)
Err(diesel::result::Error::DatabaseError(diesel::result::DatabaseErrorKind::ForeignKeyViolation, _)) => {
diesel::update(users::table)
.filter(users::uuid.eq(&self.uuid))
.set(UserDb::to_db(self))
.execute(conn) .execute(conn)
.map_res("Error saving user") .map_res("Error saving user")
} }
Err(e) => Err(e.into()), postgresql, sqlite {
}.map_res("Error saving user")
}
postgresql {
let value = UserDb::to_db(self); let value = UserDb::to_db(self);
diesel::insert_into(users::table) // Insert or update diesel::insert_into(users::table) // Insert or update
.values(&value) .values(&value)

View File

@@ -33,11 +33,11 @@ function deleteSSOUser(event) {
alert("Required parameters not found!"); alert("Required parameters not found!");
return false; return false;
} }
const input_email = prompt(`To delete user "${email}", please type the email below`); const input_email = prompt(`To delete user "${email}" SSO association, please type the email below`);
if (input_email != null) { if (input_email != null) {
if (input_email == email) { if (input_email == email) {
_delete(`${BASE_URL}/admin/users/${id}/sso`, _delete(`${BASE_URL}/admin/users/${id}/sso`,
"User SSO Associtation deleted correctly", "User SSO association deleted correctly",
"Error deleting user SSO association" "Error deleting user SSO association"
); );
} else { } else {