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:
parent
d697cf66de
commit
b1706e279f
|
@ -19,6 +19,7 @@ import (
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/util"
|
"code.gitea.io/gitea/modules/util"
|
||||||
"code.gitea.io/gitea/modules/validation"
|
"code.gitea.io/gitea/modules/validation"
|
||||||
|
"github.com/gobwas/glob"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
@ -494,11 +495,16 @@ func validateEmailDomain(email string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsEmailDomainAllowed(email string) bool {
|
func IsEmailDomainAllowed(email string) bool {
|
||||||
if len(setting.Service.EmailDomainAllowList) == 0 {
|
return IsEmailDomainAllowedInternal(email, setting.Service.EmailDomainAllowList, setting.Service.EmailDomainBlockList, setting.Federation.Enabled)
|
||||||
return !validation.IsEmailDomainListed(setting.Service.EmailDomainBlockList, email)
|
}
|
||||||
}
|
|
||||||
if setting.Federation.Enabled {
|
func IsEmailDomainAllowedInternal(email string, emailDomainAllowList []glob.Glob,
|
||||||
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email) || validation.IsLocalEmailDomain(email)
|
emailDomainBlockList []glob.Glob, isFederation bool) bool {
|
||||||
}
|
if len(emailDomainAllowList) == 0 {
|
||||||
return validation.IsEmailDomainListed(setting.Service.EmailDomainAllowList, email)
|
return !validation.IsEmailDomainListed(emailDomainBlockList, email)
|
||||||
|
}
|
||||||
|
if isFederation {
|
||||||
|
return validation.IsEmailDomainListed(emailDomainAllowList, email) || validation.IsLocalEmailDomain(email)
|
||||||
|
}
|
||||||
|
return validation.IsEmailDomainListed(emailDomainAllowList, email)
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,6 @@ import (
|
||||||
user_model "code.gitea.io/gitea/models/user"
|
user_model "code.gitea.io/gitea/models/user"
|
||||||
"code.gitea.io/gitea/modules/optional"
|
"code.gitea.io/gitea/modules/optional"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/test"
|
|
||||||
|
|
||||||
"github.com/gobwas/glob"
|
"github.com/gobwas/glob"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -23,30 +22,25 @@ import (
|
||||||
func TestEmailDomainAllowList(t *testing.T) {
|
func TestEmailDomainAllowList(t *testing.T) {
|
||||||
res := user_model.IsEmailDomainAllowed("someuser@localhost.localdomain")
|
res := user_model.IsEmailDomainAllowed("someuser@localhost.localdomain")
|
||||||
assert.True(t, res)
|
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) {
|
func TestEmailDomainAllowListInternal(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
|
|
||||||
}()
|
|
||||||
localFQDN, _ := url.ParseRequestURI(setting.AppURL)
|
localFQDN, _ := url.ParseRequestURI(setting.AppURL)
|
||||||
localDomain := localFQDN.Hostname()
|
localDomain := localFQDN.Hostname()
|
||||||
|
email := "someuser@" + localDomain
|
||||||
|
|
||||||
res := user_model.IsEmailDomainAllowed("someuser@" + localDomain)
|
domain, _ := glob.Compile("domain.de", ',')
|
||||||
assert.True(t, res)
|
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) {
|
func TestGetEmailAddresses(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue