@ -22,6 +22,7 @@ func (app *appContext) checkUsers() {
if len ( app . storage . GetUserExpiries ( ) ) == 0 {
return
}
app . info . Println ( lm . CheckUserExpiries )
users , status , err := app . jf . GetUsers ( false )
if err != nil || status != 200 {
@ -29,40 +30,36 @@ func (app *appContext) checkUsers() {
return
}
mode := "disable"
phrase := lm . DisableExpiredUser
if app . config . Section ( "user_expiry" ) . Key ( "behaviour" ) . MustString ( "disable_user" ) == "delete_user" {
mode = "delete"
phrase = lm . DeleteExpiredUser
}
deleteAfterPeriod := app . config . Section ( "user_expiry" ) . Key ( "delete_expired_after_days" ) . MustInt ( 0 )
if mode == "delete" {
deleteAfterPeriod = 0
}
contact := false
if messagesEnabled && app . config . Section ( "user_expiry" ) . Key ( "send_email" ) . MustBool ( true ) {
contact = true
}
// Use a map to speed up checking for deleted users later
userExists := map [ string ] bool { }
userExists := map [ string ] mediabrowser . User { }
for _ , user := range users {
userExists [ user . ID ] = true
userExists [ user . ID ] = user
}
for _ , expiry := range app . storage . GetUserExpiries ( ) {
id := expiry . JellyfinID
if _ , ok := userExists [ id ] ; ! ok {
user , ok := userExists [ id ]
if ! ok {
app . info . Printf ( lm . DeleteExpiryForOldUser , id )
app . storage . DeleteUserExpiryKey ( expiry . JellyfinID )
} else if time . Now ( ) . After ( expiry . Expiry ) {
found := false
var user mediabrowser . User
for _ , u := range users {
if u . ID == id {
found = true
user = u
break
}
continue
}
if ! found {
app . storage . DeleteUserExpiryKey ( expiry . JellyfinID )
if ! time . Now ( ) . After ( expiry . Expiry ) {
continue
}
app . info . Printf ( phrase , user . Name )
deleteUserLater := deleteAfterPeriod != 0 && expiry . DeleteAfterPeriod
// Record activity
activity := Activity {
@ -71,7 +68,10 @@ func (app *appContext) checkUsers() {
Time : time . Now ( ) ,
}
if mode == "delete" {
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
@ -81,7 +81,8 @@ func (app *appContext) checkUsers() {
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" {
} 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
@ -95,7 +96,14 @@ func (app *appContext) checkUsers() {
app . storage . SetActivityKey ( shortuuid . New ( ) , activity , nil , false )
app . storage . DeleteUserExpiryKey ( expiry . JellyfinID )
// 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 {
@ -106,11 +114,10 @@ func (app *appContext) checkUsers() {
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 . Failed Construct ExpiryMessage, user . ID , name , err )
app . err . Printf ( lm . Failed Send ExpiryMessage, user . ID , name , err )
} else {
app . err . Printf ( lm . SentExpiryMessage , user . ID , name )
}
}
}
}
}