diff --git a/api-users.go b/api-users.go index f5c1fc4..6e2aaef 100644 --- a/api-users.go +++ b/api-users.go @@ -381,30 +381,29 @@ func (app *appContext) EnableDisableUsers(gc *gin.Context) { for _, userID := range req.Users { user, status, err := app.jf.UserByID(userID, false) if status != 200 || err != nil { - errors["GetUser"][userID] = fmt.Sprintf("%d %v", status, err) - app.err.Printf(lm.FailedGetUser, userID, lm.Jellyfin, err) + errors["GetUser"][user.ID] = fmt.Sprintf("%d %v", status, err) + app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err) continue } - user.Policy.IsDisabled = !req.Enabled - status, err = app.jf.SetPolicy(userID, user.Policy) - if !(status == 200 || status == 204) || err != nil { - errors["SetPolicy"][userID] = fmt.Sprintf("%d %v", status, err) - app.err.Printf(lm.FailedApplyTemplate, "policy", lm.Jellyfin, userID, err) + err, _, _ = app.SetUserDisabled(user, !req.Enabled) + if err != nil { + errors["SetPolicy"][user.ID] = err.Error() + app.err.Printf(lm.FailedApplyTemplate, "policy", lm.Jellyfin, user.ID, err) continue } // Record activity app.storage.SetActivityKey(shortuuid.New(), Activity{ Type: activityType, - UserID: userID, + UserID: user.ID, SourceType: ActivityAdmin, Source: gc.GetString("jfId"), Time: time.Now(), }, gc, false) if sendMail && req.Notify { - if err := app.sendByID(msg, userID); err != nil { - app.err.Printf(lm.FailedSendEnableDisableMessage, userID, "?", err) + if err := app.sendByID(msg, user.ID); err != nil { + app.err.Printf(lm.FailedSendEnableDisableMessage, user.ID, "?", err) continue } } @@ -430,7 +429,6 @@ func (app *appContext) DeleteUsers(gc *gin.Context) { var req deleteUserDTO gc.BindJSON(&req) errors := map[string]string{} - ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false) sendMail := messagesEnabled var msg *Message var err error @@ -442,46 +440,41 @@ func (app *appContext) DeleteUsers(gc *gin.Context) { } } for _, userID := range req.Users { - if ombiEnabled { - ombiUser, code, err := app.getOmbiUser(userID) - if code == 200 && err == nil { - if id, ok := ombiUser["id"]; ok { - status, err := app.ombi.DeleteUser(id.(string)) - if err != nil || status != 200 { - app.err.Printf(lm.FailedDeleteUser, lm.Ombi, userID, err) - errors[userID] = fmt.Sprintf("Ombi: %d %v, ", status, err) - } - } - } + user, status, err := app.jf.UserByID(userID, false) + if status != 200 && err == nil { + err = fmt.Errorf("failed (code %d)", status) } - - username := "" - if user, status, err := app.jf.UserByID(userID, false); status == 200 && err == nil { - username = user.Name + if err != nil { + app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err) + errors[userID] = err.Error() } - status, err := app.jf.DeleteUser(userID) - if !(status == 200 || status == 204) || err != nil { - msg := fmt.Sprintf("%d: %v", status, err) - if _, ok := errors[userID]; !ok { - errors[userID] = msg + deleted := false + err, deleted = app.DeleteUser(user) + if err != nil { + if _, ok := errors[user.ID]; !ok { + errors[user.ID] = err.Error() + " " } else { - errors[userID] += msg + errors[user.ID] += err.Error() + " " } } - // Record activity - app.storage.SetActivityKey(shortuuid.New(), Activity{ - Type: ActivityDeletion, - UserID: userID, - SourceType: ActivityAdmin, - Source: gc.GetString("jfId"), - Value: username, - Time: time.Now(), - }, gc, false) + if deleted { + // Record activity + app.storage.SetActivityKey(shortuuid.New(), Activity{ + Type: ActivityDeletion, + UserID: userID, + SourceType: ActivityAdmin, + Source: gc.GetString("jfId"), + Value: user.Name, + Time: time.Now(), + }, gc, false) + } + // Contact details are stored separately and periodically removed, + // putting this here is hoping the daemon doesn't beat us. if sendMail && req.Notify { - if err := app.sendByID(msg, userID); err != nil { + if err := app.sendByID(msg, user.ID); err != nil { app.err.Printf(lm.FailedSendDeletionMessage, userID, "?", err) } } diff --git a/user-d.go b/user-d.go index ad83e8e..11a4012 100644 --- a/user-d.go +++ b/user-d.go @@ -72,14 +72,20 @@ func (app *appContext) checkUsers() { } if mode == "delete" { - status, err = app.jf.DeleteUser(id) + deleted := false + err, deleted = app.DeleteUser(user) + // Silence unimportant errors + if deleted { + err = nil + } activity.Type = ActivityDeletion + // Store the user name, since there's no longer a user ID to reference back to activity.Value = user.Name } else if mode == "disable" { - user.Policy.IsDisabled = true // Admins can't be disabled + // so they're not an admin anymore, sorry user.Policy.IsAdministrator = false - status, err = app.jf.SetPolicy(id, user.Policy) + err, _, _ = app.SetUserDisabled(user, true) activity.Type = ActivityDisabled } if !(status == 200 || status == 204) || err != nil { diff --git a/users.go b/users.go index 106a854..7febe89 100644 --- a/users.go +++ b/users.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "time" "github.com/gin-gonic/gin" @@ -154,3 +155,59 @@ func (app *appContext) WelcomeNewUser(user mediabrowser.User, expiry time.Time) } return } + +func (app *appContext) SetUserDisabled(user mediabrowser.User, disabled bool) (err error, change bool, activityType ActivityType) { + activityType = ActivityEnabled + if disabled { + activityType = ActivityDisabled + } + change = user.Policy.IsDisabled != disabled + user.Policy.IsDisabled = disabled + + var status int + status, err = app.jf.SetPolicy(user.ID, user.Policy) + if !(status == 200 || status == 204) && err == nil { + err = fmt.Errorf("failed (code %d)", status) + } + if err != nil { + return + } + + if app.discord != nil && app.config.Section("discord").Key("disable_enable_role").MustBool(false) { + // FIXME: Un-apply role + } + return +} + +func (app *appContext) DeleteUser(user mediabrowser.User) (err error, deleted bool) { + var status int + if app.ombi != nil { + var tpUser map[string]any + tpUser, status, err = app.getOmbiUser(user.ID) + if status == 200 && err == nil { + if id, ok := tpUser["id"]; ok { + status, err = app.ombi.DeleteUser(id.(string)) + if status != 200 && err == nil { + err = fmt.Errorf("failed (code %d)", status) + } + if err != nil { + app.err.Printf(lm.FailedDeleteUser, lm.Ombi, user.ID, err) + } + } + } + } + + if app.discord != nil && app.config.Section("discord").Key("disable_enable_role").MustBool(false) { + // FIXME: Un-apply role + } + + status, err = app.jf.DeleteUser(user.ID) + if status != 200 && status != 204 && err == nil { + err = fmt.Errorf("failed (code %d)", status) + } + if err != nil { + return + } + deleted = true + return +}