Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-30 22:06:11 +01:00

introduce internal function without global var reference

This commit is contained in:
Michael Jerger 2024-09-27 16:46:16 +02:00
parent d697cf66de
commit b1706e279f
2 changed files with 27 additions and 27 deletions

View file

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/validation"
"github.com/gobwas/glob"
"xorm.io/builder"
)
@ -494,11 +495,16 @@ func validateEmailDomain(email string) error {
}
func IsEmailDomainAllowed(email string) bool {
if len(setting.Service.EmailDomainAllowList) == 0 {
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email)
}
if setting.Federation.Enabled {
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) || validation.IsLocalEmailDomain(email)
}
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email)
return IsEmailDomainAllowedInternal(email, setting.Service.EmailDomainAllowList, setting.Service.EmailDomainBlockList, setting.Federation.Enabled)
}
func IsEmailDomainAllowedInternal(email string, emailDomainAllowList []glob.Glob,
emailDomainBlockList []glob.Glob, isFederation bool) bool {
if len(emailDomainAllowList) == 0 {
return !validation.IsEmailDomainListed(emailDomainBlockList, email)
}
if isFederation {
return validation.IsEmailDomainListed(emailDomainAllowList, email) || validation.IsLocalEmailDomain(email)
}
return validation.IsEmailDomainListed(emailDomainAllowList, email)
}

View file

@ -13,7 +13,6 @@ import (
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/test"
"github.com/gobwas/glob"
"github.com/stretchr/testify/assert"
@ -23,30 +22,25 @@ import (
func TestEmailDomainAllowList(t *testing.T) {
res := user_model.IsEmailDomainAllowed("someuser@localhost.localdomain")
assert.True(t, res)
domain, _ := glob.Compile("domain.de", ',')
test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{domain})
defer func() {
test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{})
}()
res = user_model.IsEmailDomainAllowed("someuser@repo.domain.de")
assert.False(t, res)
}
func TestLocalFQDNIsValidEmailDomain(t *testing.T) {
setting.Federation.Enabled = true
remoteDomain, _ := glob.Compile("domain.de", ',')
test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{remoteDomain})
defer func() {
test.MockVariableValue(&setting.Service.EmailDomainAllowList, []glob.Glob{})
setting.Federation.Enabled = false
}()
func TestEmailDomainAllowListInternal(t *testing.T) {
localFQDN, _ := url.ParseRequestURI(setting.AppURL)
localDomain := localFQDN.Hostname()
email := "someuser@" + localDomain
res := user_model.IsEmailDomainAllowed("someuser@" + localDomain)
assert.True(t, res)
domain, _ := glob.Compile("domain.de", ',')
emailDomainAllowList := []glob.Glob{domain}
emailDomainBlockList := []glob.Glob{}
res := user_model.IsEmailDomainAllowedInternal(email, emailDomainAllowList,
emailDomainBlockList, false)
assert.False(t, res)
res = user_model.IsEmailDomainAllowedInternal(email, emailDomainAllowList,
emailDomainBlockList, true)
assert.False(t, res)
}
func TestGetEmailAddresses(t *testing.T) {