2020-04-17 16:01:45 +02:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-04-17 16:01:45 +02:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-04-17 16:01:45 +02:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-04-17 16:01:45 +02:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-04-17 16:01:45 +02:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
2021-02-07 22:05:09 +01:00
|
|
|
"code.vikunja.io/api/pkg/notifications"
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm"
|
2020-04-17 16:01:45 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// EmailUpdate is the data structure to update a user's email address
|
|
|
|
type EmailUpdate struct {
|
|
|
|
User *User `json:"-"`
|
|
|
|
// The new email address. Needs to be a valid email address.
|
|
|
|
NewEmail string `json:"new_email" valid:"email,length(0|250),required"`
|
|
|
|
// The password of the user for confirmation.
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateEmail lets a user update their email address
|
2020-12-23 16:32:28 +01:00
|
|
|
func UpdateEmail(s *xorm.Session, update *EmailUpdate) (err error) {
|
2020-04-17 16:01:45 +02:00
|
|
|
|
|
|
|
// Check the email is not already used
|
|
|
|
user := &User{}
|
2020-12-23 16:32:28 +01:00
|
|
|
has, err := s.Where("email = ?", update.NewEmail).Get(user)
|
2020-04-17 16:01:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if has {
|
|
|
|
return ErrUserEmailExists{UserID: user.ID, Email: update.NewEmail}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the user as unconfirmed and the new email address
|
2020-12-23 16:32:28 +01:00
|
|
|
update.User, err = GetUserWithEmail(s, &User{ID: update.User.ID})
|
2020-04-17 16:01:45 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
update.User.Email = update.NewEmail
|
2021-07-13 22:56:02 +02:00
|
|
|
|
|
|
|
// Send the confirmation mail
|
|
|
|
if !config.MailerEnabled.GetBool() {
|
|
|
|
_, err = s.
|
|
|
|
Where("id = ?", update.User.ID).
|
|
|
|
Cols("email").
|
|
|
|
Update(update.User)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
update.User.Status = StatusEmailConfirmationRequired
|
2022-03-30 20:25:56 +02:00
|
|
|
token, err := generateToken(s, update.User, TokenEmailConfirm)
|
2021-07-13 22:56:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
_, err = s.
|
2020-04-17 16:01:45 +02:00
|
|
|
Where("id = ?", update.User.ID).
|
2021-07-13 22:56:02 +02:00
|
|
|
Cols("email", "is_active"). // TODO: Status change
|
2020-04-17 16:01:45 +02:00
|
|
|
Update(update.User)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the user a mail with a link to confirm the mail
|
2021-02-07 22:05:09 +01:00
|
|
|
n := &EmailConfirmNotification{
|
2021-07-13 22:56:02 +02:00
|
|
|
User: update.User,
|
|
|
|
IsNew: false,
|
|
|
|
ConfirmToken: token.Token,
|
2020-04-17 16:01:45 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 22:05:09 +01:00
|
|
|
err = notifications.Notify(update.User, n)
|
2020-04-17 16:01:45 +02:00
|
|
|
return
|
|
|
|
}
|