fix: login names (and query) (#253)

This commit is contained in:
Livio Amstutz 2020-06-22 10:38:29 +02:00 committed by GitHub
parent d89087cbe0
commit 66cca48b62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View File

@ -102,7 +102,7 @@ func (u *User) fillLoginNames(user *view_model.UserView) (err error) {
return err
}
user.SetLoginNames(policy, org.Domains)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
return nil
}
@ -165,7 +165,7 @@ func (u *User) fillPreferredLoginNamesOnOrgUsers(event *models.Event) error {
return err
}
for _, user := range users {
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
err := u.view.PutUser(user, 0)
if err != nil {
return err

View File

@ -149,7 +149,7 @@ func (u *User) fillPreferredLoginNamesOnOrgUsers(event *models.Event) error {
return err
}
for _, user := range users {
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
err := u.view.PutUser(user, 0)
if err != nil {
return err
@ -168,7 +168,7 @@ func (u *User) fillLoginNames(user *view_model.UserView) (err error) {
return err
}
user.SetLoginNames(policy, org.Domains)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain)
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
return nil
}

View File

@ -145,21 +145,23 @@ func UsersToModel(users []*UserView) []*model.UserView {
return result
}
func (u *UserView) GenerateLoginName(domain string) string {
func (u *UserView) GenerateLoginName(domain string, appendDomain bool) string {
if !appendDomain {
return u.UserName
}
return u.UserName + "@" + domain
}
func (u *UserView) SetLoginNames(policy *org_model.OrgIamPolicy, domains []*org_model.OrgDomain) {
loginNames := make([]string, 0)
if !policy.UserLoginMustBeDomain {
u.LoginNames = []string{u.UserName}
return
}
for _, d := range domains {
if d.Verified {
loginNames = append(loginNames, u.GenerateLoginName(d.Domain))
loginNames = append(loginNames, u.GenerateLoginName(d.Domain, true))
}
}
if !policy.UserLoginMustBeDomain {
loginNames = append(loginNames, u.UserName)
}
u.LoginNames = loginNames
}

View File

@ -1,13 +1,13 @@
package view
import (
"github.com/jinzhu/gorm"
caos_errs "github.com/caos/zitadel/internal/errors"
global_model "github.com/caos/zitadel/internal/model"
usr_model "github.com/caos/zitadel/internal/user/model"
"github.com/caos/zitadel/internal/user/repository/view/model"
"github.com/caos/zitadel/internal/view"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
)
func UserByID(db *gorm.DB, table, userID string) (*model.UserView, error) {
@ -35,7 +35,7 @@ func UserByLoginName(db *gorm.DB, table, loginName string) (*model.UserView, err
loginNameQuery := &model.UserSearchQuery{
Key: usr_model.USERSEARCHKEY_LOGIN_NAMES,
Method: global_model.SEARCHMETHOD_LIST_CONTAINS,
Value: pq.Array([]string{loginName}),
Value: loginName,
}
query := view.PrepareGetByQuery(table, loginNameQuery)
err := query(db, user)