2021-02-28 15:41:06 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2021-03-29 20:49:46 +00:00
|
|
|
"github.com/hrfee/mediabrowser"
|
2021-02-28 15:41:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type userDaemon struct {
|
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
Interval time.Duration
|
|
|
|
period time.Duration
|
|
|
|
app *appContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUserDaemon(interval time.Duration, app *appContext) *userDaemon {
|
|
|
|
return &userDaemon{
|
|
|
|
Stopped: false,
|
|
|
|
ShutdownChannel: make(chan string),
|
|
|
|
Interval: interval,
|
|
|
|
period: interval,
|
|
|
|
app: app,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *userDaemon) run() {
|
|
|
|
rt.app.info.Println("User daemon started")
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-rt.ShutdownChannel:
|
|
|
|
rt.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
case <-time.After(rt.period):
|
|
|
|
break
|
|
|
|
}
|
|
|
|
started := time.Now()
|
|
|
|
rt.app.checkUsers()
|
|
|
|
finished := time.Now()
|
|
|
|
duration := finished.Sub(started)
|
|
|
|
rt.period = rt.Interval - duration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 17:12:06 +00:00
|
|
|
func (rt *userDaemon) shutdown() {
|
|
|
|
rt.Stopped = true
|
|
|
|
rt.ShutdownChannel <- "Down"
|
|
|
|
<-rt.ShutdownChannel
|
|
|
|
close(rt.ShutdownChannel)
|
|
|
|
}
|
|
|
|
|
2021-02-28 15:41:06 +00:00
|
|
|
func (app *appContext) checkUsers() {
|
2021-04-06 12:44:52 +00:00
|
|
|
if err := app.storage.loadUsers(); err != nil {
|
|
|
|
app.err.Printf("Failed to load user expiries: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
app.storage.usersLock.Lock()
|
|
|
|
defer app.storage.usersLock.Unlock()
|
2021-02-28 15:41:06 +00:00
|
|
|
if len(app.storage.users) == 0 {
|
|
|
|
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"
|
|
|
|
termPlural := "Disabling"
|
|
|
|
if app.config.Section("user_expiry").Key("behaviour").MustString("disable_user") == "delete_user" {
|
|
|
|
mode = "delete"
|
|
|
|
termPlural = "Deleting"
|
|
|
|
}
|
|
|
|
email := false
|
|
|
|
if emailEnabled && app.config.Section("user_expiry").Key("send_email").MustBool(true) {
|
|
|
|
email = true
|
|
|
|
}
|
2021-04-06 12:31:42 +00:00
|
|
|
// Use a map to speed up checking for deleted users later
|
|
|
|
userExists := map[string]bool{}
|
|
|
|
for _, user := range users {
|
|
|
|
userExists[user.ID] = true
|
|
|
|
}
|
2021-02-28 15:41:06 +00:00
|
|
|
for id, expiry := range app.storage.users {
|
2021-04-06 12:31:42 +00:00
|
|
|
if _, ok := userExists[id]; !ok {
|
2021-04-06 17:02:15 +00:00
|
|
|
app.info.Printf("Deleting expiry for non-existent user \"%s\"", id)
|
2021-04-06 12:31:42 +00:00
|
|
|
delete(app.storage.users, id)
|
|
|
|
} else if time.Now().After(expiry) {
|
2021-02-28 15:41:06 +00:00
|
|
|
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.")
|
2021-03-21 22:50:33 +00:00
|
|
|
delete(app.storage.users, id)
|
2021-02-28 15:41:06 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
app.info.Printf("%s expired user \"%s\"", termPlural, user.Name)
|
|
|
|
if mode == "delete" {
|
|
|
|
status, err = app.jf.DeleteUser(id)
|
|
|
|
} else if mode == "disable" {
|
|
|
|
user.Policy.IsDisabled = true
|
2021-04-06 12:31:42 +00:00
|
|
|
// Admins can't be disabled
|
|
|
|
user.Policy.IsAdministrator = false
|
2021-02-28 15:41:06 +00:00
|
|
|
status, err = app.jf.SetPolicy(id, user.Policy)
|
|
|
|
}
|
|
|
|
if !(status == 200 || status == 204) || err != nil {
|
|
|
|
app.err.Printf("Failed to %s \"%s\" (%d): %s", mode, user.Name, status, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
delete(app.storage.users, id)
|
2021-04-06 17:39:12 +00:00
|
|
|
app.jf.CacheExpiry = time.Now()
|
2021-02-28 15:41:06 +00:00
|
|
|
if email {
|
|
|
|
address, ok := app.storage.emails[id]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
msg, err := app.email.constructUserExpired(app, false)
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to construct expiry email for \"%s\": %s", user.Name, err)
|
|
|
|
} else if err := app.email.send(msg, address.(string)); err != nil {
|
|
|
|
app.err.Printf("Failed to send expiry email to \"%s\": %s", user.Name, err)
|
|
|
|
} else {
|
|
|
|
app.info.Printf("Sent expiry notification to \"%s\"", address.(string))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-06 17:02:15 +00:00
|
|
|
err = app.storage.storeUsers()
|
|
|
|
if err != nil {
|
|
|
|
app.err.Printf("Failed to store user expiries: %s", err)
|
|
|
|
}
|
2021-02-28 15:41:06 +00:00
|
|
|
}
|