From d0aedc7223ceaddf0966009f98f3e3d8a2e604db Mon Sep 17 00:00:00 2001 From: kolaente Date: Mon, 2 Aug 2021 08:16:59 +0200 Subject: [PATCH] Add sending account deletion notifications --- pkg/user/delete.go | 58 +++++++++++++++++++++++++++++++++++++++ pkg/user/notifications.go | 1 + pkg/user/token.go | 3 +- 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 pkg/user/delete.go diff --git a/pkg/user/delete.go b/pkg/user/delete.go new file mode 100644 index 000000000..b47e0c46b --- /dev/null +++ b/pkg/user/delete.go @@ -0,0 +1,58 @@ +// Vikunja is a to-do list application to facilitate your life. +// Copyright 2018-2021 Vikunja and contributors. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public Licensee as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public Licensee for more details. +// +// You should have received a copy of the GNU Affero General Public Licensee +// along with this program. If not, see . + +package user + +import ( + "code.vikunja.io/api/pkg/notifications" + + "xorm.io/xorm" +) + +// RequestDeletion creates a user deletion confirm token and sends a notification to the user +func RequestDeletion(s *xorm.Session, user *User) (err error) { + token, err := generateNewToken(s, user, TokenAccountDeletion) + if err != nil { + return err + } + + return notifications.Notify(user, &AccountDeletionConfirmNotification{ + User: user, + ConfirmToken: token.Token, + }) +} + +// ConfirmDeletion ConformDeletion checks a token and schedules the user for deletion +func ConfirmDeletion(s *xorm.Session, user *User, token string) (err error) { + tk, err := getToken(s, token, TokenAccountDeletion) + if err != nil { + return err + } + + if tk == nil { + // TODO: return invalid token error + return + } + + err = removeTokens(s, user, TokenAccountDeletion) + if err != nil { + return err + } + + // TODO: Schedule deletion + + return nil +} diff --git a/pkg/user/notifications.go b/pkg/user/notifications.go index 968bdf973..07d646887 100644 --- a/pkg/user/notifications.go +++ b/pkg/user/notifications.go @@ -201,6 +201,7 @@ func (n *AccountDeletionConfirmNotification) ToMail() *notifications.Mail { Greeting("Hi "+n.User.GetName()+","). Line("You have requested the deletion of your account. To confirm this, please click the link below:"). Action("Confirm the deletion of my account", config.ServiceFrontendurl.GetString()+"?accountDeletionConfirm="+n.ConfirmToken). + Line("This link will be valid for 24 hours."). Line("Once you confirm the deletion we will schedule the deletion of your account in three days and send you another email until then."). Line("If you did not requested the deletion or changed your mind, you can simply ignore this email."). Line("Have a nice day!") diff --git a/pkg/user/token.go b/pkg/user/token.go index 1af05efeb..076df9532 100644 --- a/pkg/user/token.go +++ b/pkg/user/token.go @@ -33,6 +33,7 @@ const ( TokenUnknown TokenKind = iota TokenPasswordReset TokenEmailConfirm + TokenAccountDeletion tokenSize = 64 ) @@ -88,7 +89,7 @@ func RegisterTokenCleanupCron() { defer s.Close() deleted, err := s. - Where("created > ? AND kind = ?", time.Now().Add(time.Hour*24*-1), TokenPasswordReset). + Where("created > ? AND (kind = ? OR kind = ?)", time.Now().Add(time.Hour*24*-1), TokenPasswordReset, TokenAccountDeletion). Delete(&Token{}) if err != nil { log.Errorf(logPrefix+"Error removing old password reset tokens: %s", err)