aboutsummaryrefslogtreecommitdiff
path: root/internal/db/email_addresses.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/db/email_addresses.go')
-rw-r--r--internal/db/email_addresses.go20
1 files changed, 14 insertions, 6 deletions
diff --git a/internal/db/email_addresses.go b/internal/db/email_addresses.go
index 2929ffd0..8cef705e 100644
--- a/internal/db/email_addresses.go
+++ b/internal/db/email_addresses.go
@@ -8,6 +8,7 @@ import (
"context"
"fmt"
+ "github.com/pkg/errors"
"gorm.io/gorm"
"gogs.io/gogs/internal/errutil"
@@ -17,9 +18,11 @@ import (
//
// NOTE: All methods are sorted in alphabetical order.
type EmailAddressesStore interface {
- // GetByEmail returns the email address with given email. It may return
- // unverified email addresses and returns ErrEmailNotExist when not found.
- GetByEmail(ctx context.Context, email string) (*EmailAddress, error)
+ // GetByEmail returns the email address with given email. If `needsActivated` is
+ // true, only activated email will be returned, otherwise, it may return
+ // inactivated email addresses. It returns ErrEmailNotExist when no qualified
+ // email is not found.
+ GetByEmail(ctx context.Context, email string, needsActivated bool) (*EmailAddress, error)
}
var EmailAddresses EmailAddressesStore
@@ -43,7 +46,7 @@ type ErrEmailNotExist struct {
}
func IsErrEmailAddressNotExist(err error) bool {
- _, ok := err.(ErrEmailNotExist)
+ _, ok := errors.Cause(err).(ErrEmailNotExist)
return ok
}
@@ -55,9 +58,14 @@ func (ErrEmailNotExist) NotFound() bool {
return true
}
-func (db *emailAddresses) GetByEmail(ctx context.Context, email string) (*EmailAddress, error) {
+func (db *emailAddresses) GetByEmail(ctx context.Context, email string, needsActivated bool) (*EmailAddress, error) {
+ tx := db.WithContext(ctx).Where("email = ?", email)
+ if needsActivated {
+ tx = tx.Where("is_activated = ?", true)
+ }
+
emailAddress := new(EmailAddress)
- err := db.WithContext(ctx).Where("email = ?", email).First(emailAddress).Error
+ err := tx.First(emailAddress).Error
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, ErrEmailNotExist{