Add cleanup cron for old tokens

This commit is contained in:
kolaente 2021-07-13 22:19:25 +02:00
parent 96f68e595a
commit 4712a95909
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 28 additions and 0 deletions

View File

@ -94,6 +94,7 @@ func FullInit() {
cron.Init()
models.RegisterReminderCron()
models.RegisterOverdueReminderCron()
user.RegisterTokenCleanupCron()
// Start processing events
go func() {

View File

@ -97,6 +97,7 @@ func (n *ResetPasswordNotification) ToMail() *notifications.Mail {
Greeting("Hi "+n.User.GetName()+",").
Line("To reset your password, click the link below:").
Action("Reset your password", config.ServiceFrontendurl.GetString()+"?userPasswordReset="+n.Token.Token).
Line("This link will be valid for 24 hours.").
Line("Have a nice day!")
}

View File

@ -17,6 +17,9 @@
package user
import (
"code.vikunja.io/api/pkg/cron"
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/utils"
"time"
"xorm.io/xorm"
@ -71,3 +74,26 @@ func removeTokens(s *xorm.Session, u *User, kind TokenKind) (err error) {
Delete(&Token{})
return
}
func RegisterTokenCleanupCron() {
const logPrefix = "[User Token Cleanup Cron] "
err := cron.Schedule("0 * * * *", func() {
s := db.NewSession()
defer s.Close()
deleted, err := s.
Where("created > ? AND kind = ?", time.Now().Add(time.Hour*24*-1), TokenPasswordReset).
Delete(&Token{})
if err != nil {
log.Errorf(logPrefix+"Error removing old password reset tokens: %s", err)
return
}
if deleted > 0 {
log.Debugf(logPrefix+"Deleted %d old password reset tokens", deleted)
}
})
if err != nil {
log.Fatalf("Could not register token cleanup cron: %s", err)
}
}