mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 03:37:34 +00:00
feat: iam query (#3085)
* fix: only show factors with state ready * fix: get iam by id and clean up code * fix: get iam by id and clean up code * fix: remove unused code
This commit is contained in:
116
internal/query/iam.go
Normal file
116
internal/query/iam.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
errs "errors"
|
||||
"time"
|
||||
|
||||
sq "github.com/Masterminds/squirrel"
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/query/projection"
|
||||
)
|
||||
|
||||
var (
|
||||
iamTable = table{
|
||||
name: projection.IAMProjectionTable,
|
||||
}
|
||||
IAMColumnID = Column{
|
||||
name: projection.IAMColumnID,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnChangeDate = Column{
|
||||
name: projection.IAMColumnChangeDate,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnSequence = Column{
|
||||
name: projection.IAMColumnSequence,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnGlobalOrgID = Column{
|
||||
name: projection.IAMColumnGlobalOrgID,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnProjectID = Column{
|
||||
name: projection.IAMColumnProjectID,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnSetupStarted = Column{
|
||||
name: projection.IAMColumnSetUpStarted,
|
||||
table: iamTable,
|
||||
}
|
||||
IAMColumnSetupDone = Column{
|
||||
name: projection.IAMColumnSetUpDone,
|
||||
table: iamTable,
|
||||
}
|
||||
)
|
||||
|
||||
type IAM struct {
|
||||
ID string
|
||||
ChangeDate time.Time
|
||||
Sequence uint64
|
||||
|
||||
GlobalOrgID string
|
||||
IAMProjectID string
|
||||
SetupStarted domain.Step
|
||||
SetupDone domain.Step
|
||||
}
|
||||
|
||||
type IAMSearchQueries struct {
|
||||
SearchRequest
|
||||
Queries []SearchQuery
|
||||
}
|
||||
|
||||
func (q *IAMSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder {
|
||||
query = q.SearchRequest.toQuery(query)
|
||||
for _, q := range q.Queries {
|
||||
query = q.toQuery(query)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (q *Queries) IAMByID(ctx context.Context, id string) (*IAM, error) {
|
||||
stmt, scan := prepareIAMQuery()
|
||||
query, args, err := stmt.Where(sq.Eq{
|
||||
IAMColumnID.identifier(): id,
|
||||
}).ToSql()
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "QUERY-d9ngs", "Errors.Query.SQLStatement")
|
||||
}
|
||||
|
||||
row := q.client.QueryRowContext(ctx, query, args...)
|
||||
return scan(row)
|
||||
}
|
||||
|
||||
func prepareIAMQuery() (sq.SelectBuilder, func(*sql.Row) (*IAM, error)) {
|
||||
return sq.Select(
|
||||
IAMColumnID.identifier(),
|
||||
IAMColumnChangeDate.identifier(),
|
||||
IAMColumnSequence.identifier(),
|
||||
IAMColumnGlobalOrgID.identifier(),
|
||||
IAMColumnProjectID.identifier(),
|
||||
IAMColumnSetupStarted.identifier(),
|
||||
IAMColumnSetupDone.identifier(),
|
||||
).
|
||||
From(iamTable.identifier()).PlaceholderFormat(sq.Dollar),
|
||||
func(row *sql.Row) (*IAM, error) {
|
||||
o := new(IAM)
|
||||
err := row.Scan(
|
||||
&o.ID,
|
||||
&o.ChangeDate,
|
||||
&o.Sequence,
|
||||
&o.GlobalOrgID,
|
||||
&o.IAMProjectID,
|
||||
&o.SetupStarted,
|
||||
&o.SetupDone,
|
||||
)
|
||||
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")
|
||||
}
|
||||
return o, nil
|
||||
}
|
||||
}
|
124
internal/query/iam_test.go
Normal file
124
internal/query/iam_test.go
Normal file
@@ -0,0 +1,124 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"database/sql/driver"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"testing"
|
||||
|
||||
"github.com/caos/zitadel/internal/domain"
|
||||
errs "github.com/caos/zitadel/internal/errors"
|
||||
)
|
||||
|
||||
func Test_IAMPrepares(t *testing.T) {
|
||||
type want struct {
|
||||
sqlExpectations sqlExpectation
|
||||
err checkErr
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
prepare interface{}
|
||||
want want
|
||||
object interface{}
|
||||
}{
|
||||
{
|
||||
name: "prepareIAMQuery no result",
|
||||
prepare: prepareIAMQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueries(
|
||||
regexp.QuoteMeta(`SELECT zitadel.projections.iam.id,`+
|
||||
` zitadel.projections.iam.change_date,`+
|
||||
` zitadel.projections.iam.sequence,`+
|
||||
` zitadel.projections.iam.global_org_id,`+
|
||||
` zitadel.projections.iam.iam_project_id,`+
|
||||
` zitadel.projections.iam.setup_started,`+
|
||||
` zitadel.projections.iam.setup_done`+
|
||||
` FROM zitadel.projections.iam`),
|
||||
nil,
|
||||
nil,
|
||||
),
|
||||
err: func(err error) (error, bool) {
|
||||
if !errs.IsNotFound(err) {
|
||||
return fmt.Errorf("err should be zitadel.NotFoundError got: %w", err), false
|
||||
}
|
||||
return nil, true
|
||||
},
|
||||
},
|
||||
object: (*IAM)(nil),
|
||||
},
|
||||
{
|
||||
name: "prepareIAMQuery found",
|
||||
prepare: prepareIAMQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQuery(
|
||||
regexp.QuoteMeta(`SELECT zitadel.projections.iam.id,`+
|
||||
` zitadel.projections.iam.change_date,`+
|
||||
` zitadel.projections.iam.sequence,`+
|
||||
` zitadel.projections.iam.global_org_id,`+
|
||||
` zitadel.projections.iam.iam_project_id,`+
|
||||
` zitadel.projections.iam.setup_started,`+
|
||||
` zitadel.projections.iam.setup_done`+
|
||||
` FROM zitadel.projections.iam`),
|
||||
[]string{
|
||||
"id",
|
||||
"change_date",
|
||||
"sequence",
|
||||
"global_org_id",
|
||||
"iam_project_id",
|
||||
"setup_started",
|
||||
"setup_done",
|
||||
},
|
||||
[]driver.Value{
|
||||
"id",
|
||||
testNow,
|
||||
uint64(20211108),
|
||||
"global-org-id",
|
||||
"project-id",
|
||||
domain.Step2,
|
||||
domain.Step1,
|
||||
},
|
||||
),
|
||||
},
|
||||
object: &IAM{
|
||||
ID: "id",
|
||||
ChangeDate: testNow,
|
||||
Sequence: 20211108,
|
||||
GlobalOrgID: "global-org-id",
|
||||
IAMProjectID: "project-id",
|
||||
SetupStarted: domain.Step2,
|
||||
SetupDone: domain.Step1,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "prepareIAMQuery sql err",
|
||||
prepare: prepareIAMQuery,
|
||||
want: want{
|
||||
sqlExpectations: mockQueryErr(
|
||||
regexp.QuoteMeta(`SELECT zitadel.projections.iam.id,`+
|
||||
` zitadel.projections.iam.change_date,`+
|
||||
` zitadel.projections.iam.sequence,`+
|
||||
` zitadel.projections.iam.global_org_id,`+
|
||||
` zitadel.projections.iam.iam_project_id,`+
|
||||
` zitadel.projections.iam.setup_started,`+
|
||||
` zitadel.projections.iam.setup_done`+
|
||||
` FROM zitadel.projections.iam`),
|
||||
sql.ErrConnDone,
|
||||
),
|
||||
err: func(err error) (error, bool) {
|
||||
if !errors.Is(err, sql.ErrConnDone) {
|
||||
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
||||
}
|
||||
return nil, true
|
||||
},
|
||||
},
|
||||
object: nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err)
|
||||
})
|
||||
}
|
||||
}
|
21
internal/query/languages.go
Normal file
21
internal/query/languages.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package query
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/i18n"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
func (q *Queries) Languages(ctx context.Context) ([]language.Tag, error) {
|
||||
if len(q.supportedLangs) == 0 {
|
||||
langs, err := i18n.SupportedLanguages(q.LoginDir)
|
||||
if err != nil {
|
||||
logging.Log("ADMIN-tiMWs").WithError(err).Debug("unable to parse language")
|
||||
return nil, err
|
||||
}
|
||||
q.supportedLangs = langs
|
||||
}
|
||||
return q.supportedLangs, nil
|
||||
}
|
@@ -10,7 +10,6 @@ import (
|
||||
sd "github.com/caos/zitadel/internal/config/systemdefaults"
|
||||
"github.com/caos/zitadel/internal/config/types"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
iam_model "github.com/caos/zitadel/internal/iam/model"
|
||||
"github.com/caos/zitadel/internal/query/projection"
|
||||
"github.com/caos/zitadel/internal/repository/action"
|
||||
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
||||
@@ -19,7 +18,6 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/project"
|
||||
usr_repo "github.com/caos/zitadel/internal/repository/user"
|
||||
"github.com/caos/zitadel/internal/repository/usergrant"
|
||||
"github.com/caos/zitadel/internal/telemetry/tracing"
|
||||
"github.com/rakyll/statik/fs"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
@@ -35,6 +33,7 @@ type Queries struct {
|
||||
mutex sync.Mutex
|
||||
LoginTranslationFileContents map[string][]byte
|
||||
NotificationTranslationFileContents map[string][]byte
|
||||
supportedLangs []language.Tag
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -78,25 +77,3 @@ func StartQueries(ctx context.Context, es *eventstore.Eventstore, projections pr
|
||||
|
||||
return repo, nil
|
||||
}
|
||||
|
||||
func (r *Queries) IAMByID(ctx context.Context, id string) (_ *iam_model.IAM, err error) {
|
||||
readModel, err := r.iamByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return readModelToIAM(readModel), nil
|
||||
}
|
||||
|
||||
func (r *Queries) iamByID(ctx context.Context, id string) (_ *ReadModel, err error) {
|
||||
ctx, span := tracing.NewSpan(ctx)
|
||||
defer func() { span.EndWithError(err) }()
|
||||
|
||||
readModel := NewReadModel(id)
|
||||
err = r.eventstore.FilterToQueryReducer(ctx, readModel)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return readModel, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user