2021-02-28 15:41:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2024-08-01 19:17:05 +00:00
|
|
|
lm "github.com/hrfee/jfa-go/logmessages"
|
2021-03-29 20:49:46 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2023-10-19 17:56:35 +00:00
|
|
|
"github.com/lithammer/shortuuid/v3"
|
2021-02-28 15:41:06 +00:00
|
|
|
)
|
|
|
|
|
2024-07-31 13:24:02 +00:00
|
|
|
func newUserDaemon(interval time.Duration, app *appContext) *GenericDaemon {
|
|
|
|
d := NewGenericDaemon(interval, app,
|
|
|
|
func(app *appContext) {
|
|
|
|
app.checkUsers()
|
|
|
|
},
|
|
|
|
)
|
|
|
|
d.Name("User daemon")
|
|
|
|
return d
|
2021-04-06 17:12:06 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (app *appContext) checkUsers() {
|
2023-06-24 18:13:05 +00:00
|
|
|
if len(app.storage.GetUserExpiries()) == 0 {
|
2021-02-28 15:41:06 +00:00
|
|
|
return
|
|
|
|
}
|
2024-08-05 12:56:34 +00:00
|
|
|
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Println(lm.CheckUserExpiries)
|
2024-08-06 13:48:31 +00:00
|
|
|
users, err := app.jf.GetUsers(false)
|
|
|
|
if err != nil {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.err.Printf(lm.FailedGetUsers, lm.Jellyfin, err)
|
2021-02-28 15:41:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mode := "disable"
|
|
|
|
if app.config.Section("user_expiry").Key("behaviour").MustString("disable_user") == "delete_user" {
|
|
|
|
mode = "delete"
|
|
|
|
}
|
2024-08-05 12:56:34 +00:00
|
|
|
|
|
|
|
deleteAfterPeriod := app.config.Section("user_expiry").Key("delete_expired_after_days").MustInt(0)
|
|
|
|
if mode == "delete" {
|
|
|
|
deleteAfterPeriod = 0
|
|
|
|
}
|
|
|
|
|
2021-05-07 15:06:47 +00:00
|
|
|
contact := false
|
2021-05-07 20:53:29 +00:00
|
|
|
if messagesEnabled && app.config.Section("user_expiry").Key("send_email").MustBool(true) {
|
2021-05-07 15:06:47 +00:00
|
|
|
contact = true
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
2021-04-06 12:31:42 +00:00
|
|
|
// Use a map to speed up checking for deleted users later
|
2024-08-05 12:56:34 +00:00
|
|
|
userExists := map[string]mediabrowser.User{}
|
2021-04-06 12:31:42 +00:00
|
|
|
for _, user := range users {
|
2024-08-05 12:56:34 +00:00
|
|
|
userExists[user.ID] = user
|
2021-04-06 12:31:42 +00:00
|
|
|
}
|
2023-06-24 18:13:05 +00:00
|
|
|
for _, expiry := range app.storage.GetUserExpiries() {
|
|
|
|
id := expiry.JellyfinID
|
2024-08-05 12:56:34 +00:00
|
|
|
user, ok := userExists[id]
|
|
|
|
if !ok {
|
2024-08-01 19:17:05 +00:00
|
|
|
app.info.Printf(lm.DeleteExpiryForOldUser, id)
|
2023-06-24 18:13:05 +00:00
|
|
|
app.storage.DeleteUserExpiryKey(expiry.JellyfinID)
|
2024-08-05 12:56:34 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if !time.Now().After(expiry.Expiry) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deleteUserLater := deleteAfterPeriod != 0 && expiry.DeleteAfterPeriod
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-05 12:56:34 +00:00
|
|
|
// Record activity
|
|
|
|
activity := Activity{
|
|
|
|
UserID: id,
|
|
|
|
SourceType: ActivityDaemon,
|
|
|
|
Time: time.Now(),
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-05 12:56:34 +00:00
|
|
|
deleteUserNow := deleteUserLater && time.Now().After(expiry.Expiry.AddDate(0, 0, deleteAfterPeriod))
|
|
|
|
|
|
|
|
if mode == "delete" || deleteUserNow {
|
|
|
|
app.info.Printf(lm.DeleteExpiredUser, user.Name)
|
|
|
|
deleted := false
|
|
|
|
err, deleted = app.DeleteUser(user)
|
|
|
|
// Silence unimportant errors
|
|
|
|
if deleted {
|
|
|
|
err = nil
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
2024-08-05 12:56:34 +00:00
|
|
|
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" && !deleteUserLater {
|
|
|
|
app.info.Printf(lm.DisableExpiredUser, user.Name)
|
|
|
|
// Admins can't be disabled
|
|
|
|
// so they're not an admin anymore, sorry
|
|
|
|
user.Policy.IsAdministrator = false
|
|
|
|
err, _, _ = app.SetUserDisabled(user, true)
|
|
|
|
activity.Type = ActivityDisabled
|
|
|
|
}
|
2024-08-06 13:48:31 +00:00
|
|
|
if err != nil {
|
2024-08-05 12:56:34 +00:00
|
|
|
app.err.Printf(lm.FailedDeleteOrDisableExpiredUser, user.ID, err)
|
|
|
|
continue
|
|
|
|
}
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-05 12:56:34 +00:00
|
|
|
app.storage.SetActivityKey(shortuuid.New(), activity, nil, false)
|
2023-10-19 17:56:35 +00:00
|
|
|
|
2024-08-05 12:56:34 +00:00
|
|
|
// If the we're not gonna be deleting the user later, we don't need this.
|
|
|
|
// also, if the admin re-enabled the user, we should get rid of the countdown.
|
|
|
|
if deleteAfterPeriod <= 0 || mode == "delete" || deleteUserNow || (deleteUserLater && !user.Policy.IsDisabled) {
|
|
|
|
app.storage.DeleteUserExpiryKey(user.ID)
|
|
|
|
} else if deleteAfterPeriod > 0 && !deleteUserLater {
|
|
|
|
expiry.DeleteAfterPeriod = true
|
|
|
|
app.storage.SetUserExpiryKey(user.ID, expiry)
|
|
|
|
}
|
|
|
|
app.jf.CacheExpiry = time.Now()
|
|
|
|
if contact {
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
name := app.getAddressOrName(user.ID)
|
|
|
|
msg, err := app.email.constructUserExpired(app, false)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf(lm.FailedConstructExpiryMessage, user.ID, err)
|
|
|
|
} else if err := app.sendByID(msg, user.ID); err != nil {
|
|
|
|
app.err.Printf(lm.FailedSendExpiryMessage, user.ID, name, err)
|
|
|
|
} else {
|
|
|
|
app.err.Printf(lm.SentExpiryMessage, user.ID, name)
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|