2016-07-16 04:08:04 +02:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
2020-03-02 19:25:36 +01:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2016-07-16 04:08:04 +02:00
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-11-14 17:53:43 +01:00
|
|
|
"net/mail"
|
2016-07-16 04:08:04 +02:00
|
|
|
"strings"
|
2020-03-02 19:25:36 +01:00
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2020-03-02 19:25:36 +01:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
|
|
|
"xorm.io/builder"
|
2016-07-16 04:08:04 +02:00
|
|
|
)
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
// EmailAddress is the list of all email addresses of a user. It also contains the
|
|
|
|
// primary email address which is saved in user table.
|
2016-07-16 04:08:04 +02:00
|
|
|
type EmailAddress struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
UID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
Email string `xorm:"UNIQUE NOT NULL"`
|
2021-06-08 05:52:51 +02:00
|
|
|
LowerEmail string `xorm:"UNIQUE NOT NULL"`
|
2016-07-16 04:08:04 +02:00
|
|
|
IsActivated bool
|
2021-06-08 05:52:51 +02:00
|
|
|
IsPrimary bool `xorm:"DEFAULT(false) NOT NULL"`
|
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(EmailAddress))
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
// BeforeInsert will be invoked by XORM before inserting a record
|
|
|
|
func (email *EmailAddress) BeforeInsert() {
|
|
|
|
if email.LowerEmail == "" {
|
|
|
|
email.LowerEmail = strings.ToLower(email.Email)
|
|
|
|
}
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2020-11-20 22:45:55 +01:00
|
|
|
// ValidateEmail check if email is a allowed address
|
|
|
|
func ValidateEmail(email string) error {
|
|
|
|
if len(email) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := mail.ParseAddress(email); err != nil {
|
|
|
|
return ErrEmailInvalid{email}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add an email allow/block list
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-16 04:08:04 +02:00
|
|
|
// GetEmailAddresses returns all email addresses belongs to given user.
|
|
|
|
func GetEmailAddresses(uid int64) ([]*EmailAddress, error) {
|
|
|
|
emails := make([]*EmailAddress, 0, 5)
|
2021-09-19 13:49:59 +02:00
|
|
|
if err := db.DefaultContext().Engine().
|
2016-11-10 16:16:32 +01:00
|
|
|
Where("uid=?", uid).
|
2021-06-08 05:52:51 +02:00
|
|
|
Asc("id").
|
2016-11-10 16:16:32 +01:00
|
|
|
Find(&emails); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return emails, nil
|
|
|
|
}
|
|
|
|
|
2020-03-02 19:25:36 +01:00
|
|
|
// GetEmailAddressByID gets a user's email address by ID
|
|
|
|
func GetEmailAddressByID(uid, id int64) (*EmailAddress, error) {
|
|
|
|
// User ID is required for security reasons
|
2020-06-17 19:50:11 +02:00
|
|
|
email := &EmailAddress{UID: uid}
|
2021-09-19 13:49:59 +02:00
|
|
|
if has, err := db.DefaultContext().Engine().ID(id).Get(email); err != nil {
|
2020-03-02 19:25:36 +01:00
|
|
|
return nil, err
|
|
|
|
} else if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return email, nil
|
|
|
|
}
|
|
|
|
|
2021-07-13 22:59:27 +02:00
|
|
|
// isEmailActive check if email is activated with a different emailID
|
2021-09-19 13:49:59 +02:00
|
|
|
func isEmailActive(e db.Engine, email string, excludeEmailID int64) (bool, error) {
|
2020-03-02 19:25:36 +01:00
|
|
|
if len(email) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Can't filter by boolean field unless it's explicit
|
|
|
|
cond := builder.NewCond()
|
2021-07-13 22:59:27 +02:00
|
|
|
cond = cond.And(builder.Eq{"lower_email": strings.ToLower(email)}, builder.Neq{"id": excludeEmailID})
|
2020-03-02 19:25:36 +01:00
|
|
|
if setting.Service.RegisterEmailConfirm {
|
|
|
|
// Inactive (unvalidated) addresses don't count as active if email validation is required
|
|
|
|
cond = cond.And(builder.Eq{"is_activated": true})
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
var em EmailAddress
|
2020-03-02 19:25:36 +01:00
|
|
|
if has, err := e.Where(cond).Get(&em); has || err != nil {
|
|
|
|
if has {
|
2021-07-13 22:59:27 +02:00
|
|
|
log.Info("isEmailActive(%q, %d) found duplicate in email ID %d", email, excludeEmailID, em.ID)
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
return has, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func isEmailUsed(e db.Engine, email string) (bool, error) {
|
2016-07-16 04:08:04 +02:00
|
|
|
if len(email) == 0 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
return e.Where("lower_email=?", strings.ToLower(email)).Get(&EmailAddress{})
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmailUsed returns true if the email has been used.
|
|
|
|
func IsEmailUsed(email string) (bool, error) {
|
2021-09-19 13:49:59 +02:00
|
|
|
return isEmailUsed(db.DefaultContext().Engine(), email)
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func addEmailAddress(e db.Engine, email *EmailAddress) error {
|
2021-06-08 05:52:51 +02:00
|
|
|
email.Email = strings.TrimSpace(email.Email)
|
2016-07-16 04:08:04 +02:00
|
|
|
used, err := isEmailUsed(e, email.Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if used {
|
|
|
|
return ErrEmailAlreadyUsed{email.Email}
|
|
|
|
}
|
|
|
|
|
2020-11-20 22:45:55 +01:00
|
|
|
if err = ValidateEmail(email.Email); err != nil {
|
|
|
|
return err
|
2020-11-14 17:53:43 +01:00
|
|
|
}
|
|
|
|
|
2016-07-16 04:08:04 +02:00
|
|
|
_, err = e.Insert(email)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// AddEmailAddress adds an email address to given user.
|
2016-07-16 04:08:04 +02:00
|
|
|
func AddEmailAddress(email *EmailAddress) error {
|
2021-09-19 13:49:59 +02:00
|
|
|
return addEmailAddress(db.DefaultContext().Engine(), email)
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// AddEmailAddresses adds an email address to given user.
|
2016-07-16 04:08:04 +02:00
|
|
|
func AddEmailAddresses(emails []*EmailAddress) error {
|
|
|
|
if len(emails) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if any of them has been used
|
|
|
|
for i := range emails {
|
2021-06-08 05:52:51 +02:00
|
|
|
emails[i].Email = strings.TrimSpace(emails[i].Email)
|
2016-07-16 04:08:04 +02:00
|
|
|
used, err := IsEmailUsed(emails[i].Email)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if used {
|
|
|
|
return ErrEmailAlreadyUsed{emails[i].Email}
|
|
|
|
}
|
2020-11-20 22:45:55 +01:00
|
|
|
if err = ValidateEmail(emails[i].Email); err != nil {
|
|
|
|
return err
|
2020-11-14 17:53:43 +01:00
|
|
|
}
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
if _, err := db.DefaultContext().Engine().Insert(emails); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return fmt.Errorf("Insert: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// Activate activates the email address to given user.
|
2016-07-16 04:08:04 +02:00
|
|
|
func (email *EmailAddress) Activate() error {
|
2021-09-19 13:49:59 +02:00
|
|
|
sess := db.DefaultContext().NewSession()
|
2020-03-02 19:25:36 +01:00
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
if err := email.updateActivation(sess, true); err != nil {
|
2016-12-20 13:32:02 +01:00
|
|
|
return err
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
return sess.Commit()
|
|
|
|
}
|
2016-07-16 04:08:04 +02:00
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
func (email *EmailAddress) updateActivation(e db.Engine, activate bool) error {
|
2020-03-02 19:25:36 +01:00
|
|
|
user, err := getUserByID(e, email.UID)
|
|
|
|
if err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
if user.Rands, err = GetUserSalt(); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return err
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
email.IsActivated = activate
|
|
|
|
if _, err := e.ID(email.ID).Cols("is_activated").Update(email); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
return updateUserCols(e, user, "rands")
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// DeleteEmailAddress deletes an email address of given user.
|
2016-07-16 04:08:04 +02:00
|
|
|
func DeleteEmailAddress(email *EmailAddress) (err error) {
|
2021-06-08 05:52:51 +02:00
|
|
|
if email.IsPrimary {
|
|
|
|
return ErrPrimaryEmailCannotDelete{Email: email.Email}
|
|
|
|
}
|
|
|
|
|
2016-12-15 09:49:06 +01:00
|
|
|
var deleted int64
|
|
|
|
// ask to check UID
|
2021-03-14 19:52:12 +01:00
|
|
|
address := EmailAddress{
|
2016-12-15 09:49:06 +01:00
|
|
|
UID: email.UID,
|
|
|
|
}
|
2016-07-16 04:08:04 +02:00
|
|
|
if email.ID > 0 {
|
2021-09-19 13:49:59 +02:00
|
|
|
deleted, err = db.DefaultContext().Engine().ID(email.ID).Delete(&address)
|
2016-07-16 04:08:04 +02:00
|
|
|
} else {
|
2021-06-08 05:52:51 +02:00
|
|
|
if email.Email != "" && email.LowerEmail == "" {
|
|
|
|
email.LowerEmail = strings.ToLower(email.Email)
|
|
|
|
}
|
2021-09-19 13:49:59 +02:00
|
|
|
deleted, err = db.DefaultContext().Engine().
|
2021-06-08 05:52:51 +02:00
|
|
|
Where("lower_email=?", email.LowerEmail).
|
2016-12-15 09:49:06 +01:00
|
|
|
Delete(&address)
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
2016-12-15 09:49:06 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if deleted != 1 {
|
2021-04-10 08:12:38 +02:00
|
|
|
return ErrEmailAddressNotExist{Email: email.Email}
|
2016-12-15 09:49:06 +01:00
|
|
|
}
|
|
|
|
return nil
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// DeleteEmailAddresses deletes multiple email addresses
|
2016-07-16 04:08:04 +02:00
|
|
|
func DeleteEmailAddresses(emails []*EmailAddress) (err error) {
|
|
|
|
for i := range emails {
|
|
|
|
if err = DeleteEmailAddress(emails[i]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-27 12:59:12 +01:00
|
|
|
// MakeEmailPrimary sets primary email address of given user.
|
2016-07-16 04:08:04 +02:00
|
|
|
func MakeEmailPrimary(email *EmailAddress) error {
|
2021-09-19 13:49:59 +02:00
|
|
|
has, err := db.DefaultContext().Engine().Get(email)
|
2016-07-16 04:08:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2021-06-08 05:52:51 +02:00
|
|
|
return ErrEmailAddressNotExist{Email: email.Email}
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !email.IsActivated {
|
|
|
|
return ErrEmailNotActivated
|
|
|
|
}
|
|
|
|
|
2020-06-17 19:50:11 +02:00
|
|
|
user := &User{}
|
2021-09-19 13:49:59 +02:00
|
|
|
has, err = db.DefaultContext().Engine().ID(email.UID).Get(user)
|
2016-07-16 04:08:04 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2016-11-12 00:01:09 +01:00
|
|
|
return ErrUserNotExist{email.UID, "", 0}
|
2016-07-16 04:08:04 +02:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
sess := db.DefaultContext().NewSession()
|
2017-06-21 02:57:05 +02:00
|
|
|
defer sess.Close()
|
2016-07-16 04:08:04 +02:00
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
// 1. Update user table
|
2016-07-16 04:08:04 +02:00
|
|
|
user.Email = email.Email
|
2017-10-05 06:43:04 +02:00
|
|
|
if _, err = sess.ID(user.ID).Cols("email").Update(user); err != nil {
|
2016-07-16 04:08:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-08 05:52:51 +02:00
|
|
|
// 2. Update old primary email
|
|
|
|
if _, err = sess.Where("uid=? AND is_primary=?", email.UID, true).Cols("is_primary").Update(&EmailAddress{
|
|
|
|
IsPrimary: false,
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-08 13:38:13 +02:00
|
|
|
// 3. update new primary email
|
2021-06-08 05:52:51 +02:00
|
|
|
email.IsPrimary = true
|
|
|
|
if _, err = sess.ID(email.ID).Cols("is_primary").Update(email); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-16 04:08:04 +02:00
|
|
|
return sess.Commit()
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
|
|
|
|
// SearchEmailOrderBy is used to sort the results from SearchEmails()
|
|
|
|
type SearchEmailOrderBy string
|
|
|
|
|
|
|
|
func (s SearchEmailOrderBy) String() string {
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strings for sorting result
|
|
|
|
const (
|
2021-06-08 05:52:51 +02:00
|
|
|
SearchEmailOrderByEmail SearchEmailOrderBy = "email_address.lower_email ASC, email_address.is_primary DESC, email_address.id ASC"
|
|
|
|
SearchEmailOrderByEmailReverse SearchEmailOrderBy = "email_address.lower_email DESC, email_address.is_primary ASC, email_address.id DESC"
|
|
|
|
SearchEmailOrderByName SearchEmailOrderBy = "`user`.lower_name ASC, email_address.is_primary DESC, email_address.id ASC"
|
|
|
|
SearchEmailOrderByNameReverse SearchEmailOrderBy = "`user`.lower_name DESC, email_address.is_primary ASC, email_address.id DESC"
|
2020-03-02 19:25:36 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// SearchEmailOptions are options to search e-mail addresses for the admin panel
|
|
|
|
type SearchEmailOptions struct {
|
|
|
|
ListOptions
|
|
|
|
Keyword string
|
|
|
|
SortType SearchEmailOrderBy
|
|
|
|
IsPrimary util.OptionalBool
|
|
|
|
IsActivated util.OptionalBool
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchEmailResult is an e-mail address found in the user or email_address table
|
|
|
|
type SearchEmailResult struct {
|
|
|
|
UID int64
|
|
|
|
Email string
|
|
|
|
IsActivated bool
|
|
|
|
IsPrimary bool
|
|
|
|
// From User
|
|
|
|
Name string
|
|
|
|
FullName string
|
|
|
|
}
|
|
|
|
|
|
|
|
// SearchEmails takes options i.e. keyword and part of email name to search,
|
|
|
|
// it returns results in given range and number of total results.
|
|
|
|
func SearchEmails(opts *SearchEmailOptions) ([]*SearchEmailResult, int64, error) {
|
2021-06-30 18:37:20 +02:00
|
|
|
var cond builder.Cond = builder.Eq{"`user`.`type`": UserTypeIndividual}
|
2020-03-02 19:25:36 +01:00
|
|
|
if len(opts.Keyword) > 0 {
|
|
|
|
likeStr := "%" + strings.ToLower(opts.Keyword) + "%"
|
2021-06-08 05:52:51 +02:00
|
|
|
cond = cond.And(builder.Or(
|
|
|
|
builder.Like{"lower(`user`.full_name)", likeStr},
|
|
|
|
builder.Like{"`user`.lower_name", likeStr},
|
|
|
|
builder.Like{"email_address.lower_email", likeStr},
|
|
|
|
))
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case opts.IsPrimary.IsTrue():
|
2021-06-08 05:52:51 +02:00
|
|
|
cond = cond.And(builder.Eq{"email_address.is_primary": true})
|
2020-03-02 19:25:36 +01:00
|
|
|
case opts.IsPrimary.IsFalse():
|
2021-06-08 05:52:51 +02:00
|
|
|
cond = cond.And(builder.Eq{"email_address.is_primary": false})
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case opts.IsActivated.IsTrue():
|
2021-06-08 05:52:51 +02:00
|
|
|
cond = cond.And(builder.Eq{"email_address.is_activated": true})
|
2020-03-02 19:25:36 +01:00
|
|
|
case opts.IsActivated.IsFalse():
|
2021-06-08 05:52:51 +02:00
|
|
|
cond = cond.And(builder.Eq{"email_address.is_activated": false})
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
|
2021-09-19 13:49:59 +02:00
|
|
|
count, err := db.DefaultContext().Engine().Join("INNER", "`user`", "`user`.ID = email_address.uid").
|
2021-06-08 05:52:51 +02:00
|
|
|
Where(cond).Count(new(EmailAddress))
|
2020-03-02 19:25:36 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, fmt.Errorf("Count: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
orderby := opts.SortType.String()
|
|
|
|
if orderby == "" {
|
|
|
|
orderby = SearchEmailOrderByEmail.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.setDefaultValues()
|
|
|
|
|
|
|
|
emails := make([]*SearchEmailResult, 0, opts.PageSize)
|
2021-09-19 13:49:59 +02:00
|
|
|
err = db.DefaultContext().Engine().Table("email_address").
|
2021-06-08 05:52:51 +02:00
|
|
|
Select("email_address.*, `user`.name, `user`.full_name").
|
|
|
|
Join("INNER", "`user`", "`user`.ID = email_address.uid").
|
|
|
|
Where(cond).
|
|
|
|
OrderBy(orderby).
|
|
|
|
Limit(opts.PageSize, (opts.Page-1)*opts.PageSize).
|
|
|
|
Find(&emails)
|
2020-03-02 19:25:36 +01:00
|
|
|
|
|
|
|
return emails, count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActivateUserEmail will change the activated state of an email address,
|
2021-07-13 22:59:27 +02:00
|
|
|
// either primary or secondary (all in the email_address table)
|
|
|
|
func ActivateUserEmail(userID int64, email string, activate bool) (err error) {
|
2021-09-19 13:49:59 +02:00
|
|
|
sess := db.DefaultContext().NewSession()
|
2020-03-02 19:25:36 +01:00
|
|
|
defer sess.Close()
|
|
|
|
if err = sess.Begin(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-08 05:52:51 +02:00
|
|
|
|
|
|
|
// Activate/deactivate a user's secondary email address
|
|
|
|
// First check if there's another user active with the same address
|
|
|
|
addr := EmailAddress{UID: userID, LowerEmail: strings.ToLower(email)}
|
|
|
|
if has, err := sess.Get(&addr); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
|
|
|
return fmt.Errorf("no such email: %d (%s)", userID, email)
|
|
|
|
}
|
|
|
|
if addr.IsActivated == activate {
|
|
|
|
// Already in the desired state; no action
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if activate {
|
2021-07-13 22:59:27 +02:00
|
|
|
if used, err := isEmailActive(sess, email, addr.ID); err != nil {
|
|
|
|
return fmt.Errorf("unable to check isEmailActive() for %s: %v", email, err)
|
2021-06-08 05:52:51 +02:00
|
|
|
} else if used {
|
|
|
|
return ErrEmailAlreadyUsed{Email: email}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err = addr.updateActivation(sess, activate); err != nil {
|
2021-07-13 22:59:27 +02:00
|
|
|
return fmt.Errorf("unable to updateActivation() for %d:%s: %w", addr.ID, addr.Email, err)
|
2021-06-08 05:52:51 +02:00
|
|
|
}
|
|
|
|
|
2021-07-13 22:59:27 +02:00
|
|
|
// Activate/deactivate a user's primary email address and account
|
|
|
|
if addr.IsPrimary {
|
2020-03-02 19:25:36 +01:00
|
|
|
user := User{ID: userID, Email: email}
|
|
|
|
if has, err := sess.Get(&user); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !has {
|
2021-07-13 22:59:27 +02:00
|
|
|
return fmt.Errorf("no user with ID: %d and Email: %s", userID, email)
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
2021-07-13 22:59:27 +02:00
|
|
|
// The user's activation state should be synchronized with the primary email
|
|
|
|
if user.IsActive != activate {
|
|
|
|
user.IsActive = activate
|
|
|
|
if user.Rands, err = GetUserSalt(); err != nil {
|
|
|
|
return fmt.Errorf("unable to generate salt: %v", err)
|
|
|
|
}
|
|
|
|
if err = updateUserCols(sess, &user, "is_active", "rands"); err != nil {
|
|
|
|
return fmt.Errorf("unable to updateUserCols() for user ID: %d: %v", userID, err)
|
|
|
|
}
|
2020-03-02 19:25:36 +01:00
|
|
|
}
|
|
|
|
}
|
2021-06-08 05:52:51 +02:00
|
|
|
|
2020-03-02 19:25:36 +01:00
|
|
|
return sess.Commit()
|
|
|
|
}
|