Skip to content
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

Block registration based on email domain #5157

Merged
Prev Previous commit
Next Next commit
update function naming and use strings#ToLower
  • Loading branch information
adelowo committed Nov 2, 2018
commit 512c2f2ded30161b79b538c97b944ec0f91b2ea5
8 changes: 4 additions & 4 deletions modules/auth/user_form.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ func (f *RegisterForm) Validate(ctx *macaron.Context, errs binding.Errors) bindi
return validate(errs, ctx.Data, f, ctx.Locale)
}

// IsEmaildomainwhitelisted validates that the email address
// IsEmailDomainWhitelisted validates that the email address
// provided by the user matches what has been configured .
// If the domain whitelist from the config is empty, it marks the
// email as whitelisted
func (f RegisterForm) IsEmaildomainwhitelisted() bool {
func (f RegisterForm) IsEmailDomainWhitelisted() bool {
adelowo marked this conversation as resolved.
Show resolved Hide resolved
if len(setting.Service.EmailDomainWhitelist) == 0 {
return true
}
Expand All @@ -101,10 +101,10 @@ func (f RegisterForm) IsEmaildomainwhitelisted() bool {
return false
}

domain := f.Email[n+1:]
domain := strings.ToLower(f.Email[n+1:])

for _, v := range setting.Service.EmailDomainWhitelist {
if v == domain {
if strings.ToLower(v) == domain {
return true
}
}
Expand Down
7 changes: 4 additions & 3 deletions modules/auth/user_form_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRegisterForm_IsDomainWhiteList_Empty(t *testing.T) {

form := RegisterForm{}

assert.True(t, form.IsEmaildomainwhitelisted())
assert.True(t, form.IsEmailDomainWhitelisted())
}

func TestRegisterForm_IsDomainWhiteList_InvalidEmail(t *testing.T) {
Expand All @@ -37,7 +37,7 @@ func TestRegisterForm_IsDomainWhiteList_InvalidEmail(t *testing.T) {
for _, v := range tt {
form := RegisterForm{Email: v.email}

assert.False(t, form.IsEmaildomainwhitelisted())
assert.False(t, form.IsEmailDomainWhitelisted())
}
}

Expand All @@ -51,13 +51,14 @@ func TestRegisterForm_IsDomainWhiteList_ValidEmail(t *testing.T) {
valid bool
}{
{"[email protected]", true},
{"[email protected]", true},
{"hdudhdd", false},
{"[email protected]", false},
}

for _, v := range tt {
form := RegisterForm{Email: v.email}

assert.Equal(t, v.valid, form.IsEmaildomainwhitelisted())
assert.Equal(t, v.valid, form.IsEmailDomainWhitelisted())
}
}
2 changes: 1 addition & 1 deletion routers/user/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ func SignUpPost(ctx *context.Context, cpt *captcha.Captcha, form auth.RegisterFo
}
}

if !form.IsEmaildomainwhitelisted() {
if !form.IsEmailDomainWhitelisted() {
ctx.RenderWithErr(ctx.Tr("auth.email_domain_blacklisted"), tplSignUp, &form)
return
}
Expand Down