@@ -154,37 +154,18 @@ func UpdateEmailAddress(ctx context.Context, email *EmailAddress) error {
154154
155155var 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])?)*$" )
156156
157- // ValidateEmail check if email is a allowed address
157+ // ValidateEmail check if email is a valid & allowed address
158158func ValidateEmail (email string ) error {
159- if len (email ) == 0 {
160- return ErrEmailInvalid {email }
161- }
162-
163- if ! emailRegexp .MatchString (email ) {
164- return ErrEmailCharIsNotSupported {email }
165- }
166-
167- if email [0 ] == '-' {
168- return ErrEmailInvalid {email }
169- }
170-
171- if _ , err := mail .ParseAddress (email ); err != nil {
172- return ErrEmailInvalid {email }
173- }
174-
175- // if there is no allow list, then check email against block list
176- if len (setting .Service .EmailDomainAllowList ) == 0 &&
177- validation .IsEmailDomainListed (setting .Service .EmailDomainBlockList , email ) {
178- return ErrEmailInvalid {email }
179- }
180-
181- // if there is an allow list, then check email against allow list
182- if len (setting .Service .EmailDomainAllowList ) > 0 &&
183- ! validation .IsEmailDomainListed (setting .Service .EmailDomainAllowList , email ) {
184- return ErrEmailInvalid {email }
159+ if err := validateEmailBasic (email ); err != nil {
160+ return err
185161 }
162+ return validateEmailDomain (email )
163+ }
186164
187- return nil
165+ // ValidateEmailForAdmin check if email is a valid address when admins manually add users
166+ func ValidateEmailForAdmin (email string ) error {
167+ return validateEmailBasic (email )
168+ // In this case we do not need to check the email domain
188169}
189170
190171func GetEmailAddressByEmail (ctx context.Context , email string ) (* EmailAddress , error ) {
@@ -534,3 +515,41 @@ func ActivateUserEmail(ctx context.Context, userID int64, email string, activate
534515
535516 return committer .Commit ()
536517}
518+
519+ // validateEmailBasic checks whether the email complies with the rules
520+ func validateEmailBasic (email string ) error {
521+ if len (email ) == 0 {
522+ return ErrEmailInvalid {email }
523+ }
524+
525+ if ! emailRegexp .MatchString (email ) {
526+ return ErrEmailCharIsNotSupported {email }
527+ }
528+
529+ if email [0 ] == '-' {
530+ return ErrEmailInvalid {email }
531+ }
532+
533+ if _ , err := mail .ParseAddress (email ); err != nil {
534+ return ErrEmailInvalid {email }
535+ }
536+
537+ return nil
538+ }
539+
540+ // validateEmailDomain checks whether the email domain is allowed or blocked
541+ func validateEmailDomain (email string ) error {
542+ // if there is no allow list, then check email against block list
543+ if len (setting .Service .EmailDomainAllowList ) == 0 &&
544+ validation .IsEmailDomainListed (setting .Service .EmailDomainBlockList , email ) {
545+ return ErrEmailInvalid {email }
546+ }
547+
548+ // if there is an allow list, then check email against allow list
549+ if len (setting .Service .EmailDomainAllowList ) > 0 &&
550+ ! validation .IsEmailDomainListed (setting .Service .EmailDomainAllowList , email ) {
551+ return ErrEmailInvalid {email }
552+ }
553+
554+ return nil
555+ }
0 commit comments