mirror of
https://github.com/foxcpp/maddy.git
synced 2025-04-04 13:37:41 +03:00
Merge pull request #566 from arisudesu-forks/loop-extract
Extract mapping lookup from loop
This commit is contained in:
commit
ca44d0e036
1 changed files with 18 additions and 16 deletions
|
@ -9,28 +9,30 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func AuthorizeEmailUse(ctx context.Context, username string, addrs []string, mapping module.Table) (bool, error) {
|
func AuthorizeEmailUse(ctx context.Context, username string, addrs []string, mapping module.Table) (bool, error) {
|
||||||
|
var validEmails []string
|
||||||
|
|
||||||
|
if multi, ok := mapping.(module.MultiTable); ok {
|
||||||
|
var err error
|
||||||
|
validEmails, err = multi.LookupMulti(ctx, username)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("authz: %w", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
validEmail, ok, err := mapping.Lookup(ctx, username)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("authz: %w", err)
|
||||||
|
}
|
||||||
|
if ok {
|
||||||
|
validEmails = []string{validEmail}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, addr := range addrs {
|
for _, addr := range addrs {
|
||||||
_, domain, err := address.Split(addr)
|
_, domain, err := address.Split(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("authz: %w", err)
|
return false, fmt.Errorf("authz: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var validEmails []string
|
|
||||||
if multi, ok := mapping.(module.MultiTable); ok {
|
|
||||||
validEmails, err = multi.LookupMulti(ctx, username)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("authz: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
validEmail, ok, err := mapping.Lookup(ctx, username)
|
|
||||||
if err != nil {
|
|
||||||
return false, fmt.Errorf("authz: %w", err)
|
|
||||||
}
|
|
||||||
if ok {
|
|
||||||
validEmails = []string{validEmail}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, ent := range validEmails {
|
for _, ent := range validEmails {
|
||||||
if ent == domain || ent == "*" || ent == addr {
|
if ent == domain || ent == "*" || ent == addr {
|
||||||
return true, nil
|
return true, nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue