fix: user search with login_name (e.g. password reset) (#3149)

This commit is contained in:
Livio Amstutz 2022-02-02 08:21:54 +01:00 committed by GitHub
parent f96f62a1ab
commit 585ebf9a81
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 18 deletions

View File

@ -131,9 +131,14 @@ var (
name: projection.UserTypeCol,
table: userTable,
}
userLoginNamesTable = loginNameTable.setAlias("login_names")
userLoginNamesUserIDCol = LoginNameUserIDCol.setTable(userLoginNamesTable)
userLoginNamesCol = LoginNameNameCol.setTable(userLoginNamesTable)
userLoginNamesNameCol = LoginNameNameCol.setTable(userLoginNamesTable)
userLoginNamesListCol = Column{
name: "loginnames",
table: userLoginNamesTable,
}
userPreferredLoginNameTable = loginNameTable.setAlias("preferred_login_name")
userPreferredLoginNameUserIDCol = LoginNameUserIDCol.setTable(userPreferredLoginNameTable)
userPreferredLoginNameCol = LoginNameNameCol.setTable(userPreferredLoginNameTable)
@ -411,13 +416,13 @@ func NewUserPreferredLoginNameSearchQuery(value string, comparison TextCompariso
}
func NewUserLoginNamesSearchQuery(value string) (SearchQuery, error) {
return NewTextQuery(userLoginNamesCol, value, TextListContains)
return NewTextQuery(userLoginNamesListCol, value, TextListContains)
}
func prepareUserQuery() (sq.SelectBuilder, func(*sql.Row) (*User, error)) {
loginNamesQuery, _, err := sq.Select(
userLoginNamesUserIDCol.identifier(),
"ARRAY_AGG("+userLoginNamesCol.identifier()+") as login_names").
"ARRAY_AGG("+userLoginNamesNameCol.identifier()+") as "+userLoginNamesListCol.name).
From(userLoginNamesTable.identifier()).
GroupBy(userLoginNamesUserIDCol.identifier()).
ToSql()
@ -444,7 +449,7 @@ func prepareUserQuery() (sq.SelectBuilder, func(*sql.Row) (*User, error)) {
UserStateCol.identifier(),
UserTypeCol.identifier(),
UserUsernameCol.identifier(),
"login_names.login_names",
userLoginNamesListCol.identifier(),
userPreferredLoginNameCol.identifier(),
HumanUserIDCol.identifier(),
HumanFirstNameCol.identifier(),
@ -465,8 +470,8 @@ func prepareUserQuery() (sq.SelectBuilder, func(*sql.Row) (*User, error)) {
From(userTable.identifier()).
LeftJoin(join(HumanUserIDCol, UserIDCol)).
LeftJoin(join(MachineUserIDCol, UserIDCol)).
LeftJoin("("+loginNamesQuery+") as login_names on "+userLoginNamesUserIDCol.identifier()+" = "+UserIDCol.identifier()).
LeftJoin("("+preferredLoginNameQuery+") as preferred_login_name on "+userPreferredLoginNameUserIDCol.identifier()+" = "+UserIDCol.identifier(), preferredLoginNameArgs...).
LeftJoin("("+loginNamesQuery+") as "+userLoginNamesTable.alias+" on "+userLoginNamesUserIDCol.identifier()+" = "+UserIDCol.identifier()).
LeftJoin("("+preferredLoginNameQuery+") as "+userPreferredLoginNameTable.alias+" on "+userPreferredLoginNameUserIDCol.identifier()+" = "+UserIDCol.identifier(), preferredLoginNameArgs...).
PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*User, error) {
u := new(User)
@ -753,7 +758,7 @@ func prepareUserUniqueQuery() (sq.SelectBuilder, func(*sql.Row) (bool, error)) {
func prepareUsersQuery() (sq.SelectBuilder, func(*sql.Rows) (*Users, error)) {
loginNamesQuery, _, err := sq.Select(
userLoginNamesUserIDCol.identifier(),
"ARRAY_AGG("+userLoginNamesCol.identifier()+") as login_names").
"ARRAY_AGG("+userLoginNamesNameCol.identifier()+") as "+userLoginNamesListCol.name).
From(userLoginNamesTable.identifier()).
GroupBy(userLoginNamesUserIDCol.identifier()).
ToSql()
@ -780,7 +785,7 @@ func prepareUsersQuery() (sq.SelectBuilder, func(*sql.Rows) (*Users, error)) {
UserStateCol.identifier(),
UserTypeCol.identifier(),
UserUsernameCol.identifier(),
"login_names.login_names",
userLoginNamesListCol.identifier(),
userPreferredLoginNameCol.identifier(),
HumanUserIDCol.identifier(),
HumanFirstNameCol.identifier(),
@ -801,8 +806,8 @@ func prepareUsersQuery() (sq.SelectBuilder, func(*sql.Rows) (*Users, error)) {
From(userTable.identifier()).
LeftJoin(join(HumanUserIDCol, UserIDCol)).
LeftJoin(join(MachineUserIDCol, UserIDCol)).
LeftJoin("("+loginNamesQuery+") as login_names on "+userLoginNamesUserIDCol.identifier()+" = "+UserIDCol.identifier()).
LeftJoin("("+preferredLoginNameQuery+") as preferred_login_name on "+userPreferredLoginNameUserIDCol.identifier()+" = "+UserIDCol.identifier(), preferredLoginNameArgs...).
LeftJoin("("+loginNamesQuery+") as "+userLoginNamesTable.alias+" on "+userLoginNamesUserIDCol.identifier()+" = "+UserIDCol.identifier()).
LeftJoin("("+preferredLoginNameQuery+") as "+userPreferredLoginNameTable.alias+" on "+userPreferredLoginNameUserIDCol.identifier()+" = "+UserIDCol.identifier(), preferredLoginNameArgs...).
PlaceholderFormat(sq.Dollar),
func(rows *sql.Rows) (*Users, error) {
users := make([]*User, 0)

View File

@ -24,7 +24,7 @@ var (
` zitadel.projections.users.state,` +
` zitadel.projections.users.type,` +
` zitadel.projections.users.username,` +
` login_names.login_names,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` zitadel.projections.users_humans.user_id,` +
` zitadel.projections.users_humans.first_name,` +
@ -45,7 +45,7 @@ var (
` LEFT JOIN zitadel.projections.users_humans ON zitadel.projections.users.id = zitadel.projections.users_humans.user_id` +
` LEFT JOIN zitadel.projections.users_machines ON zitadel.projections.users.id = zitadel.projections.users_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as login_names` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as loginnames` +
` FROM zitadel.projections.login_names as login_names` +
` GROUP BY login_names.user_id) as login_names` +
` on login_names.user_id = zitadel.projections.users.id` +
@ -61,7 +61,7 @@ var (
"state",
"type",
"username",
"login_names",
"loginnames",
"login_name",
//human
"user_id",
@ -195,7 +195,7 @@ var (
` zitadel.projections.users.state,` +
` zitadel.projections.users.type,` +
` zitadel.projections.users.username,` +
` login_names.login_names,` +
` login_names.loginnames,` +
` preferred_login_name.login_name,` +
` zitadel.projections.users_humans.user_id,` +
` zitadel.projections.users_humans.first_name,` +
@ -217,7 +217,7 @@ var (
` LEFT JOIN zitadel.projections.users_humans ON zitadel.projections.users.id = zitadel.projections.users_humans.user_id` +
` LEFT JOIN zitadel.projections.users_machines ON zitadel.projections.users.id = zitadel.projections.users_machines.user_id` +
` LEFT JOIN` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as login_names` +
` (SELECT login_names.user_id, ARRAY_AGG(login_names.login_name) as loginnames` +
` FROM zitadel.projections.login_names as login_names` +
` GROUP BY login_names.user_id) as login_names` +
` on login_names.user_id = zitadel.projections.users.id` +
@ -233,7 +233,7 @@ var (
"state",
"type",
"username",
"login_names",
"loginnames",
"login_name",
//human
"user_id",