2022-01-21 13:01:25 +00:00
|
|
|
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"
|
2022-02-16 15:49:17 +00:00
|
|
|
"golang.org/x/text/language"
|
2022-01-21 13:01:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
IAMColumnDefaultLanguage = Column{
|
|
|
|
name: projection.IAMColumnDefaultLanguage,
|
|
|
|
table: iamTable,
|
|
|
|
}
|
2022-01-21 13:01:25 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAM struct {
|
|
|
|
ID string
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
GlobalOrgID string
|
|
|
|
IAMProjectID string
|
|
|
|
DefaultLanguage language.Tag
|
|
|
|
SetupStarted domain.Step
|
|
|
|
SetupDone domain.Step
|
2022-01-21 13:01:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
func (q *Queries) GetDefaultLanguage(ctx context.Context) language.Tag {
|
|
|
|
iam, err := q.IAMByID(ctx, domain.IAMID)
|
|
|
|
if err != nil {
|
|
|
|
return language.Und
|
|
|
|
}
|
|
|
|
return iam.DefaultLanguage
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:01:25 +00:00
|
|
|
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(),
|
2022-02-16 15:49:17 +00:00
|
|
|
IAMColumnDefaultLanguage.identifier(),
|
2022-01-21 13:01:25 +00:00
|
|
|
).
|
|
|
|
From(iamTable.identifier()).PlaceholderFormat(sq.Dollar),
|
|
|
|
func(row *sql.Row) (*IAM, error) {
|
2022-02-16 15:49:17 +00:00
|
|
|
iam := new(IAM)
|
|
|
|
lang := ""
|
2022-01-21 13:01:25 +00:00
|
|
|
err := row.Scan(
|
2022-02-16 15:49:17 +00:00
|
|
|
&iam.ID,
|
|
|
|
&iam.ChangeDate,
|
|
|
|
&iam.Sequence,
|
|
|
|
&iam.GlobalOrgID,
|
|
|
|
&iam.IAMProjectID,
|
|
|
|
&iam.SetupStarted,
|
|
|
|
&iam.SetupDone,
|
|
|
|
&lang,
|
2022-01-21 13:01:25 +00:00
|
|
|
)
|
|
|
|
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")
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
iam.DefaultLanguage = language.Make(lang)
|
|
|
|
return iam, nil
|
2022-01-21 13:01:25 +00:00
|
|
|
}
|
|
|
|
}
|