From cf94fdb2f049c3ac81a3c1e23f8aa3acb3b731ff Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Mon, 9 Oct 2023 10:39:22 +0100 Subject: [PATCH] ombi: fix password reset on default route the ombi password wasn't being changed w/ password resets initiated through the admin page (and probably by other routes, too), as the code was considering a HTTP 204 from Jellyfin as a failure, causing it to skip anything with Ombi. Also added a little check for Ombi-imported accounts that probably won't help with anything, but whatever. --- api-ombi.go | 15 +++++++++++ api-users.go | 73 ++++++++++++++++++++++++++++------------------------ api.go | 2 +- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/api-ombi.go b/api-ombi.go index 13610f4..ca148aa 100644 --- a/api-ombi.go +++ b/api-ombi.go @@ -132,3 +132,18 @@ func (app *appContext) DeleteOmbiProfile(gc *gin.Context) { app.storage.SetProfileKey(profileName, profile) respondBool(204, true, gc) } + +func (app *appContext) applyOmbiProfile(user map[string]interface{}, profile map[string]interface{}) (status int, err error) { + for k, v := range profile { + switch v.(type) { + case map[string]interface{}, []interface{}: + user[k] = v + default: + if v != user[k] { + user[k] = v + } + } + } + status, err = app.ombi.ModifyUser(user) + return +} diff --git a/api-users.go b/api-users.go index f30930d..ff2a0bd 100644 --- a/api-users.go +++ b/api-users.go @@ -377,30 +377,47 @@ func (app *appContext) newUser(req newUserDTO, confirmed bool) (f errorFunc, suc if profile.Ombi != nil && len(profile.Ombi) != 0 { template := profile.Ombi errors, code, err := app.ombi.NewUser(req.Username, req.Password, req.Email, template) + accountExists := false + var ombiUser map[string]interface{} if err != nil || code != 200 { - app.info.Printf("Failed to create Ombi user (%d): %s", code, err) - app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", ")) - } else { - app.info.Println("Created Ombi user") - if discordVerified || telegramVerified { - ombiUser, status, err := app.getOmbiUser(id) + // Check if on the off chance, Ombi's user importer has already added the account. + ombiUser, status, err = app.getOmbiImportedUser(req.Username) + if status == 200 && err == nil { + app.info.Println("Found existing Ombi user, applying changes") + accountExists = true + template["password"] = req.Password + status, err = app.applyOmbiProfile(ombiUser, template) if status != 200 || err != nil { - app.err.Printf("Failed to get Ombi user (%d): %v", status, err) - } else { - dID := "" - tUser := "" - if discordVerified { - dID = discordUser.ID - } - if telegramVerified { - u, _ := app.storage.GetTelegramKey(user.ID) - tUser = u.Username - } - resp, status, err := app.ombi.SetNotificationPrefs(ombiUser, dID, tUser) - if !(status == 200 || status == 204) || err != nil { - app.err.Printf("Failed to link Telegram/Discord to Ombi (%d): %v", status, err) - app.debug.Printf("Response: %v", resp) - } + app.err.Printf("Failed to modify existing Ombi user (%d): %v\n", status, err) + } + } else { + app.info.Printf("Failed to create Ombi user (%d): %s", code, err) + app.debug.Printf("Errors reported by Ombi: %s", strings.Join(errors, ", ")) + } + } else { + ombiUser, status, err = app.getOmbiUser(id) + if status != 200 || err != nil { + app.err.Printf("Failed to get Ombi user (%d): %v", status, err) + } else { + app.info.Println("Created Ombi user") + accountExists = true + } + } + if accountExists { + if discordVerified || telegramVerified { + dID := "" + tUser := "" + if discordVerified { + dID = discordUser.ID + } + if telegramVerified { + u, _ := app.storage.GetTelegramKey(user.ID) + tUser = u.Username + } + resp, status, err := app.ombi.SetNotificationPrefs(ombiUser, dID, tUser) + if !(status == 200 || status == 204) || err != nil { + app.err.Printf("Failed to link Telegram/Discord to Ombi (%d): %v", status, err) + app.debug.Printf("Response: %v", resp) } } } @@ -1214,17 +1231,7 @@ func (app *appContext) ApplySettings(gc *gin.Context) { // newUser["userName"] = user["userName"] // newUser["alias"] = user["alias"] // newUser["emailAddress"] = user["emailAddress"] - for k, v := range ombi { - switch v.(type) { - case map[string]interface{}, []interface{}: - user[k] = v - default: - if v != user[k] { - user[k] = v - } - } - } - status, err = app.ombi.ModifyUser(user) + status, err = app.applyOmbiProfile(user, ombi) if status != 200 || err != nil { errorString += fmt.Sprintf("Apply %d: %v ", status, err) } diff --git a/api.go b/api.go index 53344eb..d9d44e4 100644 --- a/api.go +++ b/api.go @@ -182,7 +182,7 @@ func (app *appContext) ResetSetPassword(gc *gin.Context) { } if app.config.Section("ombi").Key("enabled").MustBool(false) { // Silently fail for changing ombi passwords - if status != 200 || err != nil { + if (status != 200 && status != 204) || err != nil { app.err.Printf("Failed to get user \"%s\" from jellyfin/emby (%d): %v", username, status, err) respondBool(200, true, gc) return