From 66cca48b6275ae865bf3e9ac7f67bfc06e6c56a5 Mon Sep 17 00:00:00 2001 From: Livio Amstutz Date: Mon, 22 Jun 2020 10:38:29 +0200 Subject: [PATCH] fix: login names (and query) (#253) --- .../auth/repository/eventsourcing/handler/user.go | 4 ++-- .../repository/eventsourcing/handler/user.go | 4 ++-- internal/user/repository/view/model/user.go | 14 ++++++++------ internal/user/repository/view/user_view.go | 6 +++--- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/internal/auth/repository/eventsourcing/handler/user.go b/internal/auth/repository/eventsourcing/handler/user.go index 1da477bc02..3344bf5be9 100644 --- a/internal/auth/repository/eventsourcing/handler/user.go +++ b/internal/auth/repository/eventsourcing/handler/user.go @@ -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 diff --git a/internal/management/repository/eventsourcing/handler/user.go b/internal/management/repository/eventsourcing/handler/user.go index 8612611545..21a978b783 100644 --- a/internal/management/repository/eventsourcing/handler/user.go +++ b/internal/management/repository/eventsourcing/handler/user.go @@ -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 } diff --git a/internal/user/repository/view/model/user.go b/internal/user/repository/view/model/user.go index 4284f6deb9..8f8202028e 100644 --- a/internal/user/repository/view/model/user.go +++ b/internal/user/repository/view/model/user.go @@ -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 } diff --git a/internal/user/repository/view/user_view.go b/internal/user/repository/view/user_view.go index 8a6b6b86ee..0892250ef9 100644 --- a/internal/user/repository/view/user_view.go +++ b/internal/user/repository/view/user_view.go @@ -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)