mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 04:07:31 +00:00
fix: refactor system api (#3500)
* fix: refactor system api * fix: search domains on get instance * fix: search domains on get instance * fix: return instance detail * fix: implement user sorting column (#3469) * fix: implement user sorting column * fix: implement user sorting column * fix: string column * isOrderByLower Co-authored-by: Livio Amstutz <livio.a@gmail.com> * fix: user converter import * Update instance.go Co-authored-by: Livio Amstutz <livio.a@gmail.com>
This commit is contained in:
@@ -75,6 +75,7 @@ type Instance struct {
|
||||
ChangeDate time.Time
|
||||
CreationDate time.Time
|
||||
Sequence uint64
|
||||
Name string
|
||||
|
||||
GlobalOrgID string
|
||||
IAMProjectID string
|
||||
@@ -83,6 +84,7 @@ type Instance struct {
|
||||
DefaultLang language.Tag
|
||||
SetupStarted domain.Step
|
||||
SetupDone domain.Step
|
||||
Domains []*InstanceDomain
|
||||
host string
|
||||
}
|
||||
|
||||
@@ -159,7 +161,7 @@ func (q *Queries) SearchInstances(ctx context.Context, queries *InstanceSearchQu
|
||||
}
|
||||
|
||||
func (q *Queries) Instance(ctx context.Context) (*Instance, error) {
|
||||
stmt, scan := prepareInstanceQuery(authz.GetInstance(ctx).RequestedDomain())
|
||||
stmt, scan := prepareInstanceDomainQuery(authz.GetInstance(ctx).RequestedDomain())
|
||||
query, args, err := stmt.Where(sq.Eq{
|
||||
InstanceColumnID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
||||
}).ToSql()
|
||||
@@ -167,7 +169,10 @@ func (q *Queries) Instance(ctx context.Context) (*Instance, error) {
|
||||
return nil, errors.ThrowInternal(err, "QUERY-d9ngs", "Errors.Query.SQLStatement")
|
||||
}
|
||||
|
||||
row := q.client.QueryRowContext(ctx, query, args...)
|
||||
row, err := q.client.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scan(row)
|
||||
}
|
||||
|
||||
@@ -181,7 +186,10 @@ func (q *Queries) InstanceByHost(ctx context.Context, host string) (authz.Instan
|
||||
return nil, errors.ThrowInternal(err, "QUERY-SAfg2", "Errors.Query.SQLStatement")
|
||||
}
|
||||
|
||||
row := q.client.QueryRowContext(ctx, query, args...)
|
||||
row, err := q.client.QueryContext(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scan(row)
|
||||
}
|
||||
|
||||
@@ -226,9 +234,9 @@ func prepareInstanceQuery(host string) (sq.SelectBuilder, func(*sql.Row) (*Insta
|
||||
)
|
||||
if err != nil {
|
||||
if errs.Is(err, sql.ErrNoRows) {
|
||||
return nil, errors.ThrowNotFound(err, "QUERY-n0wng", "Errors.IAM.NotFound")
|
||||
return nil, errors.ThrowNotFound(err, "QUERY-5m09s", "Errors.IAM.NotFound")
|
||||
}
|
||||
return nil, errors.ThrowInternal(err, "QUERY-d9nw", "Errors.Internal")
|
||||
return nil, errors.ThrowInternal(err, "QUERY-3j9sf", "Errors.Internal")
|
||||
}
|
||||
instance.DefaultLang = language.Make(lang)
|
||||
return instance, nil
|
||||
@@ -241,6 +249,7 @@ func prepareInstancesQuery() (sq.SelectBuilder, func(*sql.Rows) (*Instances, err
|
||||
InstanceColumnCreationDate.identifier(),
|
||||
InstanceColumnChangeDate.identifier(),
|
||||
InstanceColumnSequence.identifier(),
|
||||
InstanceColumnName.identifier(),
|
||||
InstanceColumnGlobalOrgID.identifier(),
|
||||
InstanceColumnProjectID.identifier(),
|
||||
InstanceColumnConsoleID.identifier(),
|
||||
@@ -262,6 +271,7 @@ func prepareInstancesQuery() (sq.SelectBuilder, func(*sql.Rows) (*Instances, err
|
||||
&instance.CreationDate,
|
||||
&instance.ChangeDate,
|
||||
&instance.Sequence,
|
||||
&instance.Name,
|
||||
&instance.GlobalOrgID,
|
||||
&instance.IAMProjectID,
|
||||
&instance.ConsoleID,
|
||||
@@ -290,12 +300,13 @@ func prepareInstancesQuery() (sq.SelectBuilder, func(*sql.Rows) (*Instances, err
|
||||
}
|
||||
}
|
||||
|
||||
func prepareInstanceDomainQuery(host string) (sq.SelectBuilder, func(*sql.Row) (*Instance, error)) {
|
||||
func prepareInstanceDomainQuery(host string) (sq.SelectBuilder, func(*sql.Rows) (*Instance, error)) {
|
||||
return sq.Select(
|
||||
InstanceColumnID.identifier(),
|
||||
InstanceColumnCreationDate.identifier(),
|
||||
InstanceColumnChangeDate.identifier(),
|
||||
InstanceColumnSequence.identifier(),
|
||||
InstanceColumnName.identifier(),
|
||||
InstanceColumnGlobalOrgID.identifier(),
|
||||
InstanceColumnProjectID.identifier(),
|
||||
InstanceColumnConsoleID.identifier(),
|
||||
@@ -303,33 +314,73 @@ func prepareInstanceDomainQuery(host string) (sq.SelectBuilder, func(*sql.Row) (
|
||||
InstanceColumnSetupStarted.identifier(),
|
||||
InstanceColumnSetupDone.identifier(),
|
||||
InstanceColumnDefaultLanguage.identifier(),
|
||||
InstanceDomainDomainCol.identifier(),
|
||||
InstanceDomainIsPrimaryCol.identifier(),
|
||||
InstanceDomainIsGeneratedCol.identifier(),
|
||||
InstanceDomainCreationDateCol.identifier(),
|
||||
InstanceDomainChangeDateCol.identifier(),
|
||||
InstanceDomainSequenceCol.identifier(),
|
||||
).
|
||||
From(instanceTable.identifier()).
|
||||
LeftJoin(join(InstanceDomainInstanceIDCol, InstanceColumnID)).
|
||||
PlaceholderFormat(sq.Dollar),
|
||||
func(row *sql.Row) (*Instance, error) {
|
||||
instance := &Instance{host: host}
|
||||
lang := ""
|
||||
err := row.Scan(
|
||||
&instance.ID,
|
||||
&instance.CreationDate,
|
||||
&instance.ChangeDate,
|
||||
&instance.Sequence,
|
||||
&instance.GlobalOrgID,
|
||||
&instance.IAMProjectID,
|
||||
&instance.ConsoleID,
|
||||
&instance.ConsoleAppID,
|
||||
&instance.SetupStarted,
|
||||
&instance.SetupDone,
|
||||
&lang,
|
||||
)
|
||||
if err != nil {
|
||||
if errs.Is(err, sql.ErrNoRows) {
|
||||
return nil, errors.ThrowNotFound(err, "QUERY-n0wng", "Errors.IAM.NotFound")
|
||||
}
|
||||
return nil, errors.ThrowInternal(err, "QUERY-d9nw", "Errors.Internal")
|
||||
func(rows *sql.Rows) (*Instance, error) {
|
||||
instance := &Instance{
|
||||
host: host,
|
||||
Domains: make([]*InstanceDomain, 0),
|
||||
}
|
||||
lang := ""
|
||||
for rows.Next() {
|
||||
var (
|
||||
domain sql.NullString
|
||||
isPrimary sql.NullBool
|
||||
isGenerated sql.NullBool
|
||||
changeDate sql.NullTime
|
||||
creationDate sql.NullTime
|
||||
sequecne sql.NullInt64
|
||||
)
|
||||
err := rows.Scan(
|
||||
&instance.ID,
|
||||
&instance.CreationDate,
|
||||
&instance.ChangeDate,
|
||||
&instance.Sequence,
|
||||
&instance.Name,
|
||||
&instance.GlobalOrgID,
|
||||
&instance.IAMProjectID,
|
||||
&instance.ConsoleID,
|
||||
&instance.ConsoleAppID,
|
||||
&instance.SetupStarted,
|
||||
&instance.SetupDone,
|
||||
&lang,
|
||||
&domain,
|
||||
&isPrimary,
|
||||
&isGenerated,
|
||||
&changeDate,
|
||||
&creationDate,
|
||||
&sequecne,
|
||||
)
|
||||
if err != nil {
|
||||
if errs.Is(err, sql.ErrNoRows) {
|
||||
return nil, errors.ThrowNotFound(err, "QUERY-n0wng", "Errors.IAM.NotFound")
|
||||
}
|
||||
return nil, errors.ThrowInternal(err, "QUERY-d9nw", "Errors.Internal")
|
||||
}
|
||||
if !domain.Valid {
|
||||
continue
|
||||
}
|
||||
instance.Domains = append(instance.Domains, &InstanceDomain{
|
||||
CreationDate: creationDate.Time,
|
||||
ChangeDate: changeDate.Time,
|
||||
Sequence: uint64(sequecne.Int64),
|
||||
Domain: domain.String,
|
||||
IsPrimary: isPrimary.Bool,
|
||||
IsGenerated: isGenerated.Bool,
|
||||
InstanceID: instance.ID,
|
||||
})
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, errors.ThrowInternal(err, "QUERY-Dfbe2", "Errors.Query.CloseRows")
|
||||
}
|
||||
instance.DefaultLang = language.Make(lang)
|
||||
return instance, nil
|
||||
}
|
||||
}
|
||||
|
@@ -31,18 +31,16 @@ func (req *SearchRequest) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
}
|
||||
|
||||
if !req.SortingColumn.isZero() {
|
||||
clause := "LOWER(" + sqlPlaceholder + ")"
|
||||
clause := req.SortingColumn.orderBy()
|
||||
if !req.Asc {
|
||||
clause += " DESC"
|
||||
}
|
||||
query = query.OrderByClause(clause, req.SortingColumn.identifier())
|
||||
query = query.OrderByClause(clause)
|
||||
}
|
||||
|
||||
return query
|
||||
}
|
||||
|
||||
const sqlPlaceholder = "?"
|
||||
|
||||
type SearchQuery interface {
|
||||
toQuery(sq.SelectBuilder) sq.SelectBuilder
|
||||
comp() sq.Sqlizer
|
||||
@@ -367,8 +365,9 @@ func (t table) isZero() bool {
|
||||
}
|
||||
|
||||
type Column struct {
|
||||
name string
|
||||
table table
|
||||
name string
|
||||
table table
|
||||
isOrderByLower bool
|
||||
}
|
||||
|
||||
func (c Column) identifier() string {
|
||||
@@ -381,6 +380,13 @@ func (c Column) identifier() string {
|
||||
return c.name
|
||||
}
|
||||
|
||||
func (c Column) orderBy() string {
|
||||
if !c.isOrderByLower {
|
||||
return c.identifier()
|
||||
}
|
||||
return "LOWER(" + c.identifier() + ")"
|
||||
}
|
||||
|
||||
func (c Column) setTable(t table) Column {
|
||||
c.table = t
|
||||
return c
|
||||
|
@@ -19,6 +19,11 @@ var (
|
||||
name: "test_col",
|
||||
table: testTable,
|
||||
}
|
||||
testLowerCol = Column{
|
||||
name: "test_lower_col",
|
||||
table: testTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
testNoCol = Column{
|
||||
name: "",
|
||||
table: testTable,
|
||||
@@ -34,7 +39,6 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
}
|
||||
type want struct {
|
||||
stmtAddition string
|
||||
args []interface{}
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -46,7 +50,6 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
fields: fields{},
|
||||
want: want{
|
||||
stmtAddition: "",
|
||||
args: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -56,7 +59,6 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "OFFSET 5",
|
||||
args: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -66,7 +68,6 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "LIMIT 5",
|
||||
args: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -76,8 +77,7 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
Asc: true,
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "ORDER BY LOWER(?)",
|
||||
args: []interface{}{"test_table.test_col"},
|
||||
stmtAddition: "ORDER BY test_table.test_col",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -86,8 +86,17 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
SortingColumn: testCol,
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "ORDER BY LOWER(?) DESC",
|
||||
args: []interface{}{"test_table.test_col"},
|
||||
stmtAddition: "ORDER BY test_table.test_col DESC",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sort lower asc",
|
||||
fields: fields{
|
||||
SortingColumn: testLowerCol,
|
||||
Asc: true,
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "ORDER BY LOWER(test_table.test_lower_col)",
|
||||
},
|
||||
},
|
||||
{
|
||||
@@ -99,8 +108,7 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
Asc: true,
|
||||
},
|
||||
want: want{
|
||||
stmtAddition: "ORDER BY LOWER(?) LIMIT 10 OFFSET 5",
|
||||
args: []interface{}{"test_table.test_col"},
|
||||
stmtAddition: "ORDER BY test_table.test_col LIMIT 10 OFFSET 5",
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -116,7 +124,7 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
query := sq.Select((testCol).identifier()).From(testTable.identifier())
|
||||
expectedQuery, _, _ := query.ToSql()
|
||||
|
||||
stmt, args, err := req.toQuery(query).ToSql()
|
||||
stmt, _, err := req.toQuery(query).ToSql()
|
||||
if len(tt.want.stmtAddition) > 0 {
|
||||
expectedQuery += " " + tt.want.stmtAddition
|
||||
}
|
||||
@@ -124,10 +132,6 @@ func TestSearchRequest_ToQuery(t *testing.T) {
|
||||
t.Errorf("stmt = %q, want %q", stmt, expectedQuery)
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(args, tt.want.args) {
|
||||
t.Errorf("args = %v, want %v", args, tt.want.stmtAddition)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("no error expected but got %v", err)
|
||||
}
|
||||
|
@@ -130,8 +130,9 @@ var (
|
||||
table: userTable,
|
||||
}
|
||||
UserUsernameCol = Column{
|
||||
name: projection.UserUsernameCol,
|
||||
table: userTable,
|
||||
name: projection.UserUsernameCol,
|
||||
table: userTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
UserTypeCol = Column{
|
||||
name: projection.UserTypeCol,
|
||||
@@ -163,20 +164,24 @@ var (
|
||||
table: humanTable,
|
||||
}
|
||||
HumanFirstNameCol = Column{
|
||||
name: projection.HumanFirstNameCol,
|
||||
table: humanTable,
|
||||
name: projection.HumanFirstNameCol,
|
||||
table: humanTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
HumanLastNameCol = Column{
|
||||
name: projection.HumanLastNameCol,
|
||||
table: humanTable,
|
||||
name: projection.HumanLastNameCol,
|
||||
table: humanTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
HumanNickNameCol = Column{
|
||||
name: projection.HumanNickNameCol,
|
||||
table: humanTable,
|
||||
name: projection.HumanNickNameCol,
|
||||
table: humanTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
HumanDisplayNameCol = Column{
|
||||
name: projection.HumanDisplayNameCol,
|
||||
table: humanTable,
|
||||
name: projection.HumanDisplayNameCol,
|
||||
table: humanTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
HumanPreferredLanguageCol = Column{
|
||||
name: projection.HumanPreferredLanguageCol,
|
||||
@@ -193,8 +198,9 @@ var (
|
||||
|
||||
// email
|
||||
HumanEmailCol = Column{
|
||||
name: projection.HumanEmailCol,
|
||||
table: humanTable,
|
||||
name: projection.HumanEmailCol,
|
||||
table: humanTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
HumanIsEmailVerifiedCol = Column{
|
||||
name: projection.HumanIsEmailVerifiedCol,
|
||||
@@ -221,8 +227,9 @@ var (
|
||||
table: machineTable,
|
||||
}
|
||||
MachineNameCol = Column{
|
||||
name: projection.MachineNameCol,
|
||||
table: machineTable,
|
||||
name: projection.MachineNameCol,
|
||||
table: machineTable,
|
||||
isOrderByLower: true,
|
||||
}
|
||||
MachineDescriptionCol = Column{
|
||||
name: projection.MachineDescriptionCol,
|
||||
@@ -397,27 +404,27 @@ func NewUserResourceOwnerSearchQuery(value string, comparison TextComparison) (S
|
||||
}
|
||||
|
||||
func NewUserUsernameSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(UserUsernameCol, value, comparison)
|
||||
return NewTextQuery(Column(UserUsernameCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserFirstNameSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(HumanFirstNameCol, value, comparison)
|
||||
return NewTextQuery(Column(HumanFirstNameCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserLastNameSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(HumanLastNameCol, value, comparison)
|
||||
return NewTextQuery(Column(HumanLastNameCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserNickNameSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(HumanNickNameCol, value, comparison)
|
||||
return NewTextQuery(Column(HumanNickNameCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserDisplayNameSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(HumanDisplayNameCol, value, comparison)
|
||||
return NewTextQuery(Column(HumanDisplayNameCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserEmailSearchQuery(value string, comparison TextComparison) (SearchQuery, error) {
|
||||
return NewTextQuery(HumanEmailCol, value, comparison)
|
||||
return NewTextQuery(Column(HumanEmailCol), value, comparison)
|
||||
}
|
||||
|
||||
func NewUserStateSearchQuery(value int32) (SearchQuery, error) {
|
||||
|
Reference in New Issue
Block a user