mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 17:10:10 +00:00
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:
parent
f289680d98
commit
44311162a6
79
api-users.go
79
api-users.go
@ -381,30 +381,29 @@ func (app *appContext) EnableDisableUsers(gc *gin.Context) {
|
|||||||
for _, userID := range req.Users {
|
for _, userID := range req.Users {
|
||||||
user, status, err := app.jf.UserByID(userID, false)
|
user, status, err := app.jf.UserByID(userID, false)
|
||||||
if status != 200 || err != nil {
|
if status != 200 || err != nil {
|
||||||
errors["GetUser"][userID] = fmt.Sprintf("%d %v", status, err)
|
errors["GetUser"][user.ID] = fmt.Sprintf("%d %v", status, err)
|
||||||
app.err.Printf(lm.FailedGetUser, userID, lm.Jellyfin, err)
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
user.Policy.IsDisabled = !req.Enabled
|
err, _, _ = app.SetUserDisabled(user, !req.Enabled)
|
||||||
status, err = app.jf.SetPolicy(userID, user.Policy)
|
if err != nil {
|
||||||
if !(status == 200 || status == 204) || err != nil {
|
errors["SetPolicy"][user.ID] = err.Error()
|
||||||
errors["SetPolicy"][userID] = fmt.Sprintf("%d %v", status, err)
|
app.err.Printf(lm.FailedApplyTemplate, "policy", lm.Jellyfin, user.ID, err)
|
||||||
app.err.Printf(lm.FailedApplyTemplate, "policy", lm.Jellyfin, userID, err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record activity
|
// Record activity
|
||||||
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
||||||
Type: activityType,
|
Type: activityType,
|
||||||
UserID: userID,
|
UserID: user.ID,
|
||||||
SourceType: ActivityAdmin,
|
SourceType: ActivityAdmin,
|
||||||
Source: gc.GetString("jfId"),
|
Source: gc.GetString("jfId"),
|
||||||
Time: time.Now(),
|
Time: time.Now(),
|
||||||
}, gc, false)
|
}, gc, false)
|
||||||
|
|
||||||
if sendMail && req.Notify {
|
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.FailedSendEnableDisableMessage, userID, "?", err)
|
app.err.Printf(lm.FailedSendEnableDisableMessage, user.ID, "?", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -430,7 +429,6 @@ func (app *appContext) DeleteUsers(gc *gin.Context) {
|
|||||||
var req deleteUserDTO
|
var req deleteUserDTO
|
||||||
gc.BindJSON(&req)
|
gc.BindJSON(&req)
|
||||||
errors := map[string]string{}
|
errors := map[string]string{}
|
||||||
ombiEnabled := app.config.Section("ombi").Key("enabled").MustBool(false)
|
|
||||||
sendMail := messagesEnabled
|
sendMail := messagesEnabled
|
||||||
var msg *Message
|
var msg *Message
|
||||||
var err error
|
var err error
|
||||||
@ -442,46 +440,41 @@ func (app *appContext) DeleteUsers(gc *gin.Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, userID := range req.Users {
|
for _, userID := range req.Users {
|
||||||
if ombiEnabled {
|
user, status, err := app.jf.UserByID(userID, false)
|
||||||
ombiUser, code, err := app.getOmbiUser(userID)
|
if status != 200 && err == nil {
|
||||||
if code == 200 && err == nil {
|
err = fmt.Errorf("failed (code %d)", status)
|
||||||
if id, ok := ombiUser["id"]; ok {
|
}
|
||||||
status, err := app.ombi.DeleteUser(id.(string))
|
if err != nil {
|
||||||
if err != nil || status != 200 {
|
app.err.Printf(lm.FailedGetUser, user.ID, lm.Jellyfin, err)
|
||||||
app.err.Printf(lm.FailedDeleteUser, lm.Ombi, userID, err)
|
errors[userID] = err.Error()
|
||||||
errors[userID] = fmt.Sprintf("Ombi: %d %v, ", status, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
username := ""
|
deleted := false
|
||||||
if user, status, err := app.jf.UserByID(userID, false); status == 200 && err == nil {
|
err, deleted = app.DeleteUser(user)
|
||||||
username = user.Name
|
if err != nil {
|
||||||
}
|
if _, ok := errors[user.ID]; !ok {
|
||||||
|
errors[user.ID] = 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
|
|
||||||
} else {
|
} else {
|
||||||
errors[userID] += msg
|
errors[user.ID] += err.Error() + " "
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record activity
|
if deleted {
|
||||||
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
// Record activity
|
||||||
Type: ActivityDeletion,
|
app.storage.SetActivityKey(shortuuid.New(), Activity{
|
||||||
UserID: userID,
|
Type: ActivityDeletion,
|
||||||
SourceType: ActivityAdmin,
|
UserID: userID,
|
||||||
Source: gc.GetString("jfId"),
|
SourceType: ActivityAdmin,
|
||||||
Value: username,
|
Source: gc.GetString("jfId"),
|
||||||
Time: time.Now(),
|
Value: user.Name,
|
||||||
}, gc, false)
|
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 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)
|
app.err.Printf(lm.FailedSendDeletionMessage, userID, "?", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
user-d.go
12
user-d.go
@ -72,14 +72,20 @@ func (app *appContext) checkUsers() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if mode == "delete" {
|
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
|
activity.Type = ActivityDeletion
|
||||||
|
// Store the user name, since there's no longer a user ID to reference back to
|
||||||
activity.Value = user.Name
|
activity.Value = user.Name
|
||||||
} else if mode == "disable" {
|
} else if mode == "disable" {
|
||||||
user.Policy.IsDisabled = true
|
|
||||||
// Admins can't be disabled
|
// Admins can't be disabled
|
||||||
|
// so they're not an admin anymore, sorry
|
||||||
user.Policy.IsAdministrator = false
|
user.Policy.IsAdministrator = false
|
||||||
status, err = app.jf.SetPolicy(id, user.Policy)
|
err, _, _ = app.SetUserDisabled(user, true)
|
||||||
activity.Type = ActivityDisabled
|
activity.Type = ActivityDisabled
|
||||||
}
|
}
|
||||||
if !(status == 200 || status == 204) || err != nil {
|
if !(status == 200 || status == 204) || err != nil {
|
||||||
|
57
users.go
57
users.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -154,3 +155,59 @@ func (app *appContext) WelcomeNewUser(user mediabrowser.User, expiry time.Time)
|
|||||||
}
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user