Skip to content

Skip email domain check when admin users adds user manually #29522

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 5, 2024
22 changes: 12 additions & 10 deletions models/user/email_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func UpdateEmailAddress(ctx context.Context, email *EmailAddress) error {
var emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]*@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")

// ValidateEmail check if email is a allowed address
func ValidateEmail(email string) error {
func ValidateEmail(email string, skipDomainCheck bool) error {
if len(email) == 0 {
return ErrEmailInvalid{email}
}
Expand All @@ -172,16 +172,18 @@ func ValidateEmail(email string) error {
return ErrEmailInvalid{email}
}

// if there is no allow list, then check email against block list
if len(setting.Service.EmailDomainAllowList) == 0 &&
validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email) {
return ErrEmailInvalid{email}
}
if !skipDomainCheck {
// if there is no allow list, then check email against block list
if len(setting.Service.EmailDomainAllowList) == 0 &&
validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email) {
return ErrEmailInvalid{email}
}

// if there is an allow list, then check email against allow list
if len(setting.Service.EmailDomainAllowList) > 0 &&
!validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) {
return ErrEmailInvalid{email}
// if there is an allow list, then check email against allow list
if len(setting.Service.EmailDomainAllowList) > 0 &&
!validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) {
return ErrEmailInvalid{email}
}
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion models/user/email_address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func TestEmailAddressValidate(t *testing.T) {
}
for kase, err := range kases {
t.Run(kase, func(t *testing.T) {
assert.EqualValues(t, err, user_model.ValidateEmail(kase))
assert.EqualValues(t, err, user_model.ValidateEmail(kase, false))
})
}
}
2 changes: 1 addition & 1 deletion models/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ func CreateUser(ctx context.Context, u *User, overwriteDefault ...*CreateUserOve
return err
}

if err := ValidateEmail(u.Email); err != nil {
if err := ValidateEmail(u.Email, false); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion routers/web/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ func ActivatePost(ctx *context.Context) {
if code == "" {
newEmail := strings.TrimSpace(ctx.FormString("change_email"))
if ctx.Doer != nil && newEmail != "" && !strings.EqualFold(ctx.Doer.Email, newEmail) {
if user_model.ValidateEmail(newEmail) != nil {
if user_model.ValidateEmail(newEmail, false) != nil {
ctx.Flash.Error(ctx.Locale.Tr("form.email_invalid"), true)
renderActivationChangeEmail(ctx)
return
Expand Down
2 changes: 1 addition & 1 deletion routers/web/org/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func TeamsAction(ctx *context.Context) {
u, err = user_model.GetUserByName(ctx, uname)
if err != nil {
if user_model.IsErrUserNotExist(err) {
if setting.MailService != nil && user_model.ValidateEmail(uname) == nil {
if setting.MailService != nil && user_model.ValidateEmail(uname, false) == nil {
if err := org_service.CreateTeamInvite(ctx, ctx.Doer, ctx.Org.Team, uname); err != nil {
if org_model.IsErrTeamInviteAlreadyExist(err) {
ctx.Flash.Error(ctx.Tr("form.duplicate_invite_to_team"))
Expand Down
4 changes: 2 additions & 2 deletions services/auth/source/pam/source_authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ func (source *Source) Authenticate(ctx context.Context, user *user_model.User, u
if idx > -1 {
username = pamLogin[:idx]
}
if user_model.ValidateEmail(email) != nil {
if user_model.ValidateEmail(email, false) != nil {
if source.EmailDomain != "" {
email = fmt.Sprintf("%s@%s", username, source.EmailDomain)
} else {
email = fmt.Sprintf("%s@%s", username, setting.Service.NoReplyAddress)
}
if user_model.ValidateEmail(email) != nil {
if user_model.ValidateEmail(email, false) != nil {
email = uuid.New().String() + "@localhost"
}
}
Expand Down
2 changes: 1 addition & 1 deletion services/doctor/breaking.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func checkUserEmail(ctx context.Context, logger log.Logger, _ bool) error {
return nil
}

if err := user.ValidateEmail(u.Email); err != nil {
if err := user.ValidateEmail(u.Email, false); err != nil {
invalidUserCount++
logger.Warn("User[id=%d name=%q] have not a valid e-mail: %v", u.ID, u.Name, err)
}
Expand Down
6 changes: 3 additions & 3 deletions services/user/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func AddOrSetPrimaryEmailAddress(ctx context.Context, u *user_model.User, emailS
return nil
}

if err := user_model.ValidateEmail(emailStr); err != nil {
if err := user_model.ValidateEmail(emailStr, false); err != nil {
return err
}

Expand Down Expand Up @@ -72,7 +72,7 @@ func ReplacePrimaryEmailAddress(ctx context.Context, u *user_model.User, emailSt
return nil
}

if err := user_model.ValidateEmail(emailStr); err != nil {
if err := user_model.ValidateEmail(emailStr, false); err != nil {
return err
}

Expand Down Expand Up @@ -117,7 +117,7 @@ func ReplacePrimaryEmailAddress(ctx context.Context, u *user_model.User, emailSt

func AddEmailAddresses(ctx context.Context, u *user_model.User, emails []string) error {
for _, emailStr := range emails {
if err := user_model.ValidateEmail(emailStr); err != nil {
if err := user_model.ValidateEmail(emailStr, false); err != nil {
return err
}

Expand Down