mirror of https://github.com/hrfee/jfa-go
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.0 KiB
111 lines
3.0 KiB
4 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"time"
|
||
|
|
||
4 years ago
|
"github.com/hrfee/mediabrowser"
|
||
1 year ago
|
"github.com/lithammer/shortuuid/v3"
|
||
4 years ago
|
)
|
||
|
|
||
4 months ago
|
func newUserDaemon(interval time.Duration, app *appContext) *GenericDaemon {
|
||
|
d := NewGenericDaemon(interval, app,
|
||
|
func(app *appContext) {
|
||
|
app.checkUsers()
|
||
|
},
|
||
|
)
|
||
|
d.Name("User daemon")
|
||
|
return d
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
func (app *appContext) checkUsers() {
|
||
1 year ago
|
if len(app.storage.GetUserExpiries()) == 0 {
|
||
4 years ago
|
return
|
||
|
}
|
||
|
app.info.Println("Daemon: Checking for user expiry")
|
||
|
users, status, err := app.jf.GetUsers(false)
|
||
|
if err != nil || status != 200 {
|
||
|
app.err.Printf("Failed to get users (%d): %s", status, err)
|
||
|
return
|
||
|
}
|
||
|
mode := "disable"
|
||
1 year ago
|
term := "Disabling"
|
||
4 years ago
|
if app.config.Section("user_expiry").Key("behaviour").MustString("disable_user") == "delete_user" {
|
||
|
mode = "delete"
|
||
1 year ago
|
term = "Deleting"
|
||
4 years ago
|
}
|
||
4 years ago
|
contact := false
|
||
4 years ago
|
if messagesEnabled && app.config.Section("user_expiry").Key("send_email").MustBool(true) {
|
||
4 years ago
|
contact = true
|
||
4 years ago
|
}
|
||
4 years ago
|
// Use a map to speed up checking for deleted users later
|
||
|
userExists := map[string]bool{}
|
||
|
for _, user := range users {
|
||
|
userExists[user.ID] = true
|
||
|
}
|
||
1 year ago
|
for _, expiry := range app.storage.GetUserExpiries() {
|
||
|
id := expiry.JellyfinID
|
||
4 years ago
|
if _, ok := userExists[id]; !ok {
|
||
4 years ago
|
app.info.Printf("Deleting expiry for non-existent user \"%s\"", id)
|
||
1 year ago
|
app.storage.DeleteUserExpiryKey(expiry.JellyfinID)
|
||
|
} else if time.Now().After(expiry.Expiry) {
|
||
4 years ago
|
found := false
|
||
|
var user mediabrowser.User
|
||
|
for _, u := range users {
|
||
|
if u.ID == id {
|
||
|
found = true
|
||
|
user = u
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
if !found {
|
||
|
app.info.Printf("Expired user already deleted, ignoring.")
|
||
1 year ago
|
app.storage.DeleteUserExpiryKey(expiry.JellyfinID)
|
||
4 years ago
|
continue
|
||
|
}
|
||
1 year ago
|
app.info.Printf("%s expired user \"%s\"", term, user.Name)
|
||
1 year ago
|
|
||
|
// Record activity
|
||
|
activity := Activity{
|
||
|
UserID: id,
|
||
|
SourceType: ActivityDaemon,
|
||
|
Time: time.Now(),
|
||
|
}
|
||
|
|
||
4 years ago
|
if mode == "delete" {
|
||
|
status, err = app.jf.DeleteUser(id)
|
||
1 year ago
|
activity.Type = ActivityDeletion
|
||
1 year ago
|
activity.Value = user.Name
|
||
4 years ago
|
} else if mode == "disable" {
|
||
|
user.Policy.IsDisabled = true
|
||
4 years ago
|
// Admins can't be disabled
|
||
|
user.Policy.IsAdministrator = false
|
||
4 years ago
|
status, err = app.jf.SetPolicy(id, user.Policy)
|
||
1 year ago
|
activity.Type = ActivityDisabled
|
||
4 years ago
|
}
|
||
|
if !(status == 200 || status == 204) || err != nil {
|
||
|
app.err.Printf("Failed to %s \"%s\" (%d): %s", mode, user.Name, status, err)
|
||
|
continue
|
||
|
}
|
||
1 year ago
|
|
||
11 months ago
|
app.storage.SetActivityKey(shortuuid.New(), activity, nil, false)
|
||
1 year ago
|
|
||
1 year ago
|
app.storage.DeleteUserExpiryKey(expiry.JellyfinID)
|
||
4 years ago
|
app.jf.CacheExpiry = time.Now()
|
||
4 years ago
|
if contact {
|
||
4 years ago
|
if !ok {
|
||
|
continue
|
||
|
}
|
||
4 years ago
|
name := app.getAddressOrName(user.ID)
|
||
4 years ago
|
msg, err := app.email.constructUserExpired(app, false)
|
||
|
if err != nil {
|
||
4 years ago
|
app.err.Printf("Failed to construct expiry message for \"%s\": %s", user.Name, err)
|
||
|
} else if err := app.sendByID(msg, user.ID); err != nil {
|
||
|
app.err.Printf("Failed to send expiry message to \"%s\": %s", name, err)
|
||
4 years ago
|
} else {
|
||
4 years ago
|
app.info.Printf("Sent expiry notification to \"%s\"", name)
|
||
4 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|