@@ -2,9 +2,7 @@ package projection
22
33import (
44 "context"
5- "strings"
6-
7- sq "github.com/Masterminds/squirrel"
5+ _ "embed"
86
97 "github.com/zitadel/zitadel/internal/eventstore"
108 old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
@@ -58,105 +56,8 @@ const (
5856 LoginNamePoliciesInstanceIDCol = "instance_id"
5957)
6058
61- var (
62- policyUsers = sq .Select (
63- alias (
64- col (usersAlias , LoginNameUserIDCol ),
65- LoginNameUserCol ,
66- ),
67- col (usersAlias , LoginNameUserUserNameCol ),
68- col (usersAlias , LoginNameUserInstanceIDCol ),
69- col (usersAlias , LoginNameUserResourceOwnerCol ),
70- alias (
71- coalesce (col (policyCustomAlias , LoginNamePoliciesMustBeDomainCol ), col (policyDefaultAlias , LoginNamePoliciesMustBeDomainCol )),
72- LoginNamePoliciesMustBeDomainCol ,
73- ),
74- ).From (alias (LoginNameUserProjectionTable , usersAlias )).
75- LeftJoin (
76- leftJoin (LoginNamePolicyProjectionTable , policyCustomAlias ,
77- eq (col (policyCustomAlias , LoginNamePoliciesResourceOwnerCol ), col (usersAlias , LoginNameUserResourceOwnerCol )),
78- eq (col (policyCustomAlias , LoginNamePoliciesInstanceIDCol ), col (usersAlias , LoginNameUserInstanceIDCol )),
79- ),
80- ).
81- LeftJoin (
82- leftJoin (LoginNamePolicyProjectionTable , policyDefaultAlias ,
83- eq (col (policyDefaultAlias , LoginNamePoliciesIsDefaultCol ), "true" ),
84- eq (col (policyDefaultAlias , LoginNamePoliciesInstanceIDCol ), col (usersAlias , LoginNameUserInstanceIDCol )),
85- ),
86- )
87-
88- loginNamesTable = sq .Select (
89- col (policyUsersAlias , LoginNameUserCol ),
90- col (policyUsersAlias , LoginNameUserUserNameCol ),
91- col (policyUsersAlias , LoginNameUserResourceOwnerCol ),
92- alias (col (policyUsersAlias , LoginNameUserInstanceIDCol ),
93- LoginNameInstanceIDCol ),
94- col (policyUsersAlias , LoginNamePoliciesMustBeDomainCol ),
95- alias (col (domainsAlias , LoginNameDomainNameCol ),
96- domainAlias ),
97- col (domainsAlias , LoginNameDomainIsPrimaryCol ),
98- ).FromSelect (policyUsers , policyUsersAlias ).
99- LeftJoin (
100- leftJoin (LoginNameDomainProjectionTable , domainsAlias ,
101- col (policyUsersAlias , LoginNamePoliciesMustBeDomainCol ),
102- eq (col (policyUsersAlias , LoginNameUserResourceOwnerCol ), col (domainsAlias , LoginNameDomainResourceOwnerCol )),
103- eq (col (policyUsersAlias , LoginNamePoliciesInstanceIDCol ), col (domainsAlias , LoginNameDomainInstanceIDCol )),
104- ),
105- )
106-
107- viewStmt , _ = sq .Select (
108- LoginNameUserCol ,
109- alias (
110- whenThenElse (
111- LoginNamePoliciesMustBeDomainCol ,
112- concat (LoginNameUserUserNameCol , "'@'" , domainAlias ),
113- LoginNameUserUserNameCol ),
114- LoginNameCol ),
115- alias (coalesce (LoginNameDomainIsPrimaryCol , "true" ),
116- LoginNameIsPrimaryCol ),
117- LoginNameInstanceIDCol ,
118- ).FromSelect (loginNamesTable , LoginNameTableAlias ).MustSql ()
119- )
120-
121- func col (table , name string ) string {
122- return table + "." + name
123- }
124-
125- func alias (col , alias string ) string {
126- return col + " AS " + alias
127- }
128-
129- func coalesce (values ... string ) string {
130- str := "COALESCE("
131- for i , value := range values {
132- if i > 0 {
133- str += ", "
134- }
135- str += value
136- }
137- str += ")"
138- return str
139- }
140-
141- func eq (first , second string ) string {
142- return first + " = " + second
143- }
144-
145- func leftJoin (table , alias , on string , and ... string ) string {
146- st := table + " " + alias + " ON " + on
147- for _ , a := range and {
148- st += " AND " + a
149- }
150- return st
151- }
152-
153- func concat (strs ... string ) string {
154- return "CONCAT(" + strings .Join (strs , ", " ) + ")"
155- }
156-
157- func whenThenElse (when , then , el string ) string {
158- return "(CASE WHEN " + when + " THEN " + then + " ELSE " + el + " END)"
159- }
59+ //go:embed login_name_query.sql
60+ var loginNameViewStmt string
16061
16162type loginNameProjection struct {}
16263
@@ -170,7 +71,7 @@ func (*loginNameProjection) Name() string {
17071
17172func (* loginNameProjection ) Init () * old_handler.Check {
17273 return handler .NewViewCheck (
173- viewStmt ,
74+ loginNameViewStmt ,
17475 handler .NewSuffixedTable (
17576 []* handler.InitColumn {
17677 handler .NewColumn (LoginNameUserIDCol , handler .ColumnTypeText ),
@@ -229,7 +130,9 @@ func (*loginNameProjection) Init() *old_handler.Check {
229130 },
230131 handler .NewPrimaryKey (LoginNamePoliciesInstanceIDCol , LoginNamePoliciesResourceOwnerCol ),
231132 loginNamePolicySuffix ,
232- handler .WithIndex (handler .NewIndex ("is_default" , []string {LoginNamePoliciesResourceOwnerCol , LoginNamePoliciesIsDefaultCol })),
133+ // this index is not used anymore, but kept for understanding why the default exists on existing systems, TODO: remove in login_names4
134+ // handler.WithIndex(handler.NewIndex("is_default", []string{LoginNamePoliciesResourceOwnerCol, LoginNamePoliciesIsDefaultCol})),
135+ handler .WithIndex (handler .NewIndex ("is_default_owner" , []string {LoginNamePoliciesInstanceIDCol , LoginNamePoliciesIsDefaultCol , LoginNamePoliciesResourceOwnerCol }, handler .WithInclude (LoginNamePoliciesMustBeDomainCol ))),
233136 ),
234137 )
235138}
0 commit comments