users: consolidate methods for disable/enable and deleting users

now both in users.go and shared between the admin UI and daemon. Will be
used for discord role auto-add/deletion.
This commit is contained in:
Harvey Tindall
2024-08-04 15:14:51 +01:00
parent f289680d98
commit 44311162a6
3 changed files with 102 additions and 46 deletions
+57
View File
@@ -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
}