diff --git a/internal/api/grpc/management/org.go b/internal/api/grpc/management/org.go index 8db51dedac..8c38fb722e 100644 --- a/internal/api/grpc/management/org.go +++ b/internal/api/grpc/management/org.go @@ -12,6 +12,7 @@ import ( "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/eventstore/v1/models" org_model "github.com/caos/zitadel/internal/org/model" + "github.com/caos/zitadel/internal/query" usr_model "github.com/caos/zitadel/internal/user/model" mgmt_pb "github.com/caos/zitadel/pkg/grpc/management" ) @@ -118,14 +119,20 @@ func (s *Server) ListOrgDomains(ctx context.Context, req *mgmt_pb.ListOrgDomains if err != nil { return nil, err } - domains, err := s.org.SearchMyOrgDomains(ctx, queries) + orgIDQuery, err := query.NewOrgDomainOrgIDSearchQuery(authz.GetCtxData(ctx).OrgID) + if err != nil { + return nil, err + } + queries.Queries = append(queries.Queries, orgIDQuery) + + domains, err := s.query.SearchOrgDomains(ctx, queries) if err != nil { return nil, err } return &mgmt_pb.ListOrgDomainsResponse{ - Result: org_grpc.DomainsToPb(domains.Result), + Result: org_grpc.DomainsToPb(domains.Domains), Details: object.ToListDetails( - domains.TotalResult, + domains.Count, domains.Sequence, domains.Timestamp, ), diff --git a/internal/api/grpc/management/org_converter.go b/internal/api/grpc/management/org_converter.go index 8a4ccd2b90..a12ff4ccbf 100644 --- a/internal/api/grpc/management/org_converter.go +++ b/internal/api/grpc/management/org_converter.go @@ -8,20 +8,22 @@ import ( org_grpc "github.com/caos/zitadel/internal/api/grpc/org" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/eventstore/v1/models" - org_model "github.com/caos/zitadel/internal/org/model" + "github.com/caos/zitadel/internal/query" mgmt_pb "github.com/caos/zitadel/pkg/grpc/management" ) -func ListOrgDomainsRequestToModel(req *mgmt_pb.ListOrgDomainsRequest) (*org_model.OrgDomainSearchRequest, error) { +func ListOrgDomainsRequestToModel(req *mgmt_pb.ListOrgDomainsRequest) (*query.OrgDomainSearchQueries, error) { offset, limit, asc := object.ListQueryToModel(req.Query) queries, err := org_grpc.DomainQueriesToModel(req.Queries) if err != nil { return nil, err } - return &org_model.OrgDomainSearchRequest{ - Offset: offset, - Limit: limit, - Asc: asc, + return &query.OrgDomainSearchQueries{ + SearchRequest: query.SearchRequest{ + Offset: offset, + Limit: limit, + Asc: asc, + }, //SortingColumn: //TODO: sorting Queries: queries, }, nil diff --git a/internal/api/grpc/org/converter.go b/internal/api/grpc/org/converter.go index 354bf898df..f62eb1f845 100644 --- a/internal/api/grpc/org/converter.go +++ b/internal/api/grpc/org/converter.go @@ -4,7 +4,6 @@ import ( "github.com/caos/zitadel/internal/api/grpc/object" "github.com/caos/zitadel/internal/domain" "github.com/caos/zitadel/internal/errors" - org_model "github.com/caos/zitadel/internal/org/model" "github.com/caos/zitadel/internal/query" grant_model "github.com/caos/zitadel/internal/usergrant/model" org_pb "github.com/caos/zitadel/pkg/grpc/org" @@ -117,8 +116,8 @@ func OrgStateToPb(state domain.OrgState) org_pb.OrgState { } } -func DomainQueriesToModel(queries []*org_pb.DomainSearchQuery) (_ []*org_model.OrgDomainSearchQuery, err error) { - q := make([]*org_model.OrgDomainSearchQuery, len(queries)) +func DomainQueriesToModel(queries []*org_pb.DomainSearchQuery) (_ []query.SearchQuery, err error) { + q := make([]query.SearchQuery, len(queries)) for i, query := range queries { q[i], err = DomainQueryToModel(query) if err != nil { @@ -128,24 +127,16 @@ func DomainQueriesToModel(queries []*org_pb.DomainSearchQuery) (_ []*org_model.O return q, nil } -func DomainQueryToModel(query *org_pb.DomainSearchQuery) (*org_model.OrgDomainSearchQuery, error) { - switch q := query.Query.(type) { +func DomainQueryToModel(searchQuery *org_pb.DomainSearchQuery) (query.SearchQuery, error) { + switch q := searchQuery.Query.(type) { case *org_pb.DomainSearchQuery_DomainNameQuery: - return DomainNameQueryToModel(q.DomainNameQuery) + return query.NewOrgDomainDomainSearchQuery(object.TextMethodToQuery(q.DomainNameQuery.Method), q.DomainNameQuery.Name) default: return nil, errors.ThrowInvalidArgument(nil, "ORG-Ags42", "List.Query.Invalid") } } -func DomainNameQueryToModel(query *org_pb.DomainNameQuery) (*org_model.OrgDomainSearchQuery, error) { - return &org_model.OrgDomainSearchQuery{ - Key: org_model.OrgDomainSearchKeyDomain, - Method: object.TextMethodToModel(query.Method), - Value: query.Name, - }, nil -} - -func DomainsToPb(domains []*org_model.OrgDomainView) []*org_pb.Domain { +func DomainsToPb(domains []*query.Domain) []*org_pb.Domain { d := make([]*org_pb.Domain, len(domains)) for i, domain := range domains { d[i] = DomainToPb(domain) @@ -153,18 +144,18 @@ func DomainsToPb(domains []*org_model.OrgDomainView) []*org_pb.Domain { return d } -func DomainToPb(domain *org_model.OrgDomainView) *org_pb.Domain { +func DomainToPb(d *query.Domain) *org_pb.Domain { return &org_pb.Domain{ - OrgId: domain.OrgID, - DomainName: domain.Domain, - IsVerified: domain.Verified, - IsPrimary: domain.Primary, - ValidationType: DomainValidationTypeFromModel(domain.ValidationType), + OrgId: d.OrgID, + DomainName: d.Domain, + IsVerified: d.IsVerified, + IsPrimary: d.IsPrimary, + ValidationType: DomainValidationTypeFromModel(d.ValidationType), Details: object.ToViewDetailsPb( - 0, - domain.CreationDate, - domain.ChangeDate, - "", + d.Sequence, + d.CreationDate, + d.ChangeDate, + d.OrgID, ), } } @@ -180,11 +171,11 @@ func DomainValidationTypeToDomain(validationType org_pb.DomainValidationType) do } } -func DomainValidationTypeFromModel(validationType org_model.OrgDomainValidationType) org_pb.DomainValidationType { +func DomainValidationTypeFromModel(validationType domain.OrgDomainValidationType) org_pb.DomainValidationType { switch validationType { - case org_model.OrgDomainValidationTypeDNS: + case domain.OrgDomainValidationTypeDNS: return org_pb.DomainValidationType_DOMAIN_VALIDATION_TYPE_DNS - case org_model.OrgDomainValidationTypeHTTP: + case domain.OrgDomainValidationTypeHTTP: return org_pb.DomainValidationType_DOMAIN_VALIDATION_TYPE_HTTP default: return org_pb.DomainValidationType_DOMAIN_VALIDATION_TYPE_UNSPECIFIED diff --git a/internal/management/repository/eventsourcing/eventstore/org.go b/internal/management/repository/eventsourcing/eventstore/org.go index f9fbb58c9c..6677f670f1 100644 --- a/internal/management/repository/eventsourcing/eventstore/org.go +++ b/internal/management/repository/eventsourcing/eventstore/org.go @@ -31,7 +31,6 @@ import ( org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model" org_view "github.com/caos/zitadel/internal/org/repository/view" "github.com/caos/zitadel/internal/org/repository/view/model" - "github.com/caos/zitadel/internal/telemetry/tracing" usr_model "github.com/caos/zitadel/internal/user/model" "github.com/caos/zitadel/internal/user/repository/view" usr_es_model "github.com/caos/zitadel/internal/user/repository/view/model" @@ -79,31 +78,6 @@ func (repo *OrgRepository) GetMyOrgIamPolicy(ctx context.Context) (*iam_model.Or return iam_view_model.OrgIAMViewToModel(policy), err } -func (repo *OrgRepository) SearchMyOrgDomains(ctx context.Context, request *org_model.OrgDomainSearchRequest) (*org_model.OrgDomainSearchResponse, error) { - err := request.EnsureLimit(repo.SearchLimit) - if err != nil { - return nil, err - } - request.Queries = append(request.Queries, &org_model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyOrgID, Method: domain.SearchMethodEquals, Value: authz.GetCtxData(ctx).OrgID}) - sequence, sequenceErr := repo.View.GetLatestOrgDomainSequence() - logging.Log("EVENT-SLowp").OnError(sequenceErr).WithField("traceID", tracing.TraceIDFromCtx(ctx)).Warn("could not read latest org domain sequence") - domains, count, err := repo.View.SearchOrgDomains(request) - if err != nil { - return nil, err - } - result := &org_model.OrgDomainSearchResponse{ - Offset: request.Offset, - Limit: request.Limit, - TotalResult: uint64(count), - Result: model.OrgDomainsToModel(domains), - } - if sequenceErr == nil { - result.Sequence = sequence.CurrentSequence - result.Timestamp = sequence.LastSuccessfulSpoolerRun - } - return result, nil -} - func (repo *OrgRepository) OrgChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool, auditLogRetention time.Duration) (*org_model.OrgChanges, error) { changes, err := repo.getOrgChanges(ctx, id, lastSequence, limit, sortAscending, auditLogRetention) if err != nil { diff --git a/internal/management/repository/eventsourcing/handler/handler.go b/internal/management/repository/eventsourcing/handler/handler.go index 4b67bc0a7b..27723e6e24 100644 --- a/internal/management/repository/eventsourcing/handler/handler.go +++ b/internal/management/repository/eventsourcing/handler/handler.go @@ -42,8 +42,6 @@ func Register(configs Configs, bulkLimit, errorCount uint64, view *view.View, es newUserGrant(handler{view, bulkLimit, configs.cycleDuration("UserGrant"), errorCount, es}), newOrgMember( handler{view, bulkLimit, configs.cycleDuration("OrgMember"), errorCount, es}), - newOrgDomain( - handler{view, bulkLimit, configs.cycleDuration("OrgDomain"), errorCount, es}), newUserMembership( handler{view, bulkLimit, configs.cycleDuration("UserMembership"), errorCount, es}), newAuthNKeys( diff --git a/internal/management/repository/eventsourcing/handler/org_domain.go b/internal/management/repository/eventsourcing/handler/org_domain.go deleted file mode 100644 index 11e4355dba..0000000000 --- a/internal/management/repository/eventsourcing/handler/org_domain.go +++ /dev/null @@ -1,138 +0,0 @@ -package handler - -import ( - "github.com/caos/logging" - "github.com/caos/zitadel/internal/eventstore/v1" - es_models "github.com/caos/zitadel/internal/eventstore/v1/models" - "github.com/caos/zitadel/internal/eventstore/v1/query" - "github.com/caos/zitadel/internal/eventstore/v1/spooler" - "github.com/caos/zitadel/internal/org/repository/eventsourcing/model" - org_model "github.com/caos/zitadel/internal/org/repository/view/model" -) - -const ( - orgDomainTable = "management.org_domains" -) - -type OrgDomain struct { - handler - subscription *v1.Subscription -} - -func newOrgDomain(handler handler) *OrgDomain { - h := &OrgDomain{ - handler: handler, - } - - h.subscribe() - - return h -} - -func (m *OrgDomain) subscribe() { - m.subscription = m.es.Subscribe(m.AggregateTypes()...) - go func() { - for event := range m.subscription.Events { - query.ReduceEvent(m, event) - } - }() -} - -func (d *OrgDomain) ViewModel() string { - return orgDomainTable -} - -func (d *OrgDomain) Subscription() *v1.Subscription { - return d.subscription -} - -func (_ *OrgDomain) AggregateTypes() []es_models.AggregateType { - return []es_models.AggregateType{model.OrgAggregate} -} - -func (p *OrgDomain) CurrentSequence() (uint64, error) { - sequence, err := p.view.GetLatestOrgDomainSequence() - if err != nil { - return 0, err - } - return sequence.CurrentSequence, nil -} - -func (d *OrgDomain) EventQuery() (*es_models.SearchQuery, error) { - sequence, err := d.view.GetLatestOrgDomainSequence() - if err != nil { - return nil, err - } - return es_models.NewSearchQuery(). - AggregateTypeFilter(d.AggregateTypes()...). - LatestSequenceFilter(sequence.CurrentSequence), nil -} - -func (d *OrgDomain) Reduce(event *es_models.Event) (err error) { - switch event.AggregateType { - case model.OrgAggregate: - err = d.processOrgDomain(event) - } - return err -} - -func (d *OrgDomain) processOrgDomain(event *es_models.Event) (err error) { - domain := new(org_model.OrgDomainView) - switch event.Type { - case model.OrgDomainAdded: - err = domain.AppendEvent(event) - case model.OrgDomainVerified, - model.OrgDomainVerificationAdded: - err = domain.SetData(event) - if err != nil { - return err - } - domain, err = d.view.OrgDomainByOrgIDAndDomain(event.AggregateID, domain.Domain) - if err != nil { - return err - } - err = domain.AppendEvent(event) - case model.OrgDomainPrimarySet: - err = domain.SetData(event) - if err != nil { - return err - } - domain, err = d.view.OrgDomainByOrgIDAndDomain(event.AggregateID, domain.Domain) - if err != nil { - return err - } - existingDomains, err := d.view.OrgDomainsByOrgID(event.AggregateID) - if err != nil { - return err - } - for _, existingDomain := range existingDomains { - existingDomain.Primary = false - } - err = d.view.PutOrgDomains(existingDomains, event) - if err != nil { - return err - } - err = domain.AppendEvent(event) - case model.OrgDomainRemoved: - err = domain.SetData(event) - if err != nil { - return err - } - return d.view.DeleteOrgDomain(event.AggregateID, domain.Domain, event) - default: - return d.view.ProcessedOrgDomainSequence(event) - } - if err != nil { - return err - } - return d.view.PutOrgDomain(domain, event) -} - -func (d *OrgDomain) OnError(event *es_models.Event, err error) error { - logging.LogWithFields("SPOOL-us4sj", "id", event.AggregateID).WithError(err).Warn("something went wrong in orgdomain handler") - return spooler.HandleError(event, err, d.view.GetLatestOrgDomainFailedEvent, d.view.ProcessedOrgDomainFailedEvent, d.view.ProcessedOrgDomainSequence, d.errorCountUntilSkip) -} - -func (o *OrgDomain) OnSuccess() error { - return spooler.HandleSuccess(o.view.UpdateOrgDomainSpoolerRunTimestamp) -} diff --git a/internal/management/repository/eventsourcing/view/org_domain.go b/internal/management/repository/eventsourcing/view/org_domain.go deleted file mode 100644 index 7dde72eb96..0000000000 --- a/internal/management/repository/eventsourcing/view/org_domain.go +++ /dev/null @@ -1,74 +0,0 @@ -package view - -import ( - "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/eventstore/v1/models" - org_model "github.com/caos/zitadel/internal/org/model" - "github.com/caos/zitadel/internal/org/repository/view" - "github.com/caos/zitadel/internal/org/repository/view/model" - "github.com/caos/zitadel/internal/view/repository" -) - -const ( - orgDomainTable = "management.org_domains" -) - -func (v *View) OrgDomainByOrgIDAndDomain(orgID, domain string) (*model.OrgDomainView, error) { - return view.OrgDomainByOrgIDAndDomain(v.Db, orgDomainTable, orgID, domain) -} - -func (v *View) OrgDomainsByOrgID(domain string) ([]*model.OrgDomainView, error) { - return view.OrgDomainsByOrgID(v.Db, orgDomainTable, domain) -} - -func (v *View) VerifiedOrgDomain(domain string) (*model.OrgDomainView, error) { - return view.VerifiedOrgDomain(v.Db, orgDomainTable, domain) -} - -func (v *View) SearchOrgDomains(request *org_model.OrgDomainSearchRequest) ([]*model.OrgDomainView, uint64, error) { - return view.SearchOrgDomains(v.Db, orgDomainTable, request) -} - -func (v *View) PutOrgDomain(org *model.OrgDomainView, event *models.Event) error { - err := view.PutOrgDomain(v.Db, orgDomainTable, org) - if err != nil { - return err - } - return v.ProcessedOrgDomainSequence(event) -} - -func (v *View) PutOrgDomains(domains []*model.OrgDomainView, event *models.Event) error { - err := view.PutOrgDomains(v.Db, orgDomainTable, domains...) - if err != nil { - return err - } - return v.ProcessedUserSequence(event) -} - -func (v *View) DeleteOrgDomain(orgID, domain string, event *models.Event) error { - err := view.DeleteOrgDomain(v.Db, orgDomainTable, orgID, domain) - if err != nil && !errors.IsNotFound(err) { - return err - } - return v.ProcessedOrgDomainSequence(event) -} - -func (v *View) GetLatestOrgDomainSequence() (*repository.CurrentSequence, error) { - return v.latestSequence(orgDomainTable) -} - -func (v *View) ProcessedOrgDomainSequence(event *models.Event) error { - return v.saveCurrentSequence(orgDomainTable, event) -} - -func (v *View) UpdateOrgDomainSpoolerRunTimestamp() error { - return v.updateSpoolerRunSequence(orgDomainTable) -} - -func (v *View) GetLatestOrgDomainFailedEvent(sequence uint64) (*repository.FailedEvent, error) { - return v.latestFailedEvent(orgDomainTable, sequence) -} - -func (v *View) ProcessedOrgDomainFailedEvent(failedEvent *repository.FailedEvent) error { - return v.saveFailedEvent(failedEvent) -} diff --git a/internal/management/repository/org.go b/internal/management/repository/org.go index 7a528e2a92..e6231bf0d6 100644 --- a/internal/management/repository/org.go +++ b/internal/management/repository/org.go @@ -16,8 +16,6 @@ type OrgRepository interface { Languages(ctx context.Context) ([]language.Tag, error) OrgChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool, auditLogRetention time.Duration) (*org_model.OrgChanges, error) - SearchMyOrgDomains(ctx context.Context, request *org_model.OrgDomainSearchRequest) (*org_model.OrgDomainSearchResponse, error) - SearchMyOrgMembers(ctx context.Context, request *org_model.OrgMemberSearchRequest) (*org_model.OrgMemberSearchResponse, error) GetOrgMemberRoles() []string diff --git a/internal/org/model/domain_view.go b/internal/org/model/domain_view.go deleted file mode 100644 index 8aa2e0a928..0000000000 --- a/internal/org/model/domain_view.go +++ /dev/null @@ -1,61 +0,0 @@ -package model - -import ( - "github.com/caos/zitadel/internal/domain" - caos_errors "github.com/caos/zitadel/internal/errors" - - "time" -) - -type OrgDomainView struct { - OrgID string - CreationDate time.Time - ChangeDate time.Time - Domain string - Primary bool - Verified bool - ValidationType OrgDomainValidationType -} - -type OrgDomainSearchRequest struct { - Offset uint64 - Limit uint64 - SortingColumn OrgDomainSearchKey - Asc bool - Queries []*OrgDomainSearchQuery -} - -type OrgDomainSearchKey int32 - -const ( - OrgDomainSearchKeyUnspecified OrgDomainSearchKey = iota - OrgDomainSearchKeyDomain - OrgDomainSearchKeyOrgID - OrgDomainSearchKeyVerified - OrgDomainSearchKeyPrimary -) - -type OrgDomainSearchQuery struct { - Key OrgDomainSearchKey - Method domain.SearchMethod - Value interface{} -} - -type OrgDomainSearchResponse struct { - Offset uint64 - Limit uint64 - TotalResult uint64 - Result []*OrgDomainView - Sequence uint64 - Timestamp time.Time -} - -func (r *OrgDomainSearchRequest) EnsureLimit(limit uint64) error { - if r.Limit > limit { - return caos_errors.ThrowInvalidArgument(nil, "SEARCH-38fhs", "Errors.Limit.ExceedsDefault") - } - if r.Limit == 0 { - r.Limit = limit - } - return nil -} diff --git a/internal/org/model/org_view.go b/internal/org/model/org_view.go deleted file mode 100644 index dd8b5242b1..0000000000 --- a/internal/org/model/org_view.go +++ /dev/null @@ -1,79 +0,0 @@ -package model - -import ( - "time" - - "github.com/caos/zitadel/internal/domain" - caos_errors "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/eventstore/v1/models" -) - -type OrgView struct { - ID string - CreationDate time.Time - ChangeDate time.Time - State OrgState - ResourceOwner string - Sequence uint64 - - Name string -} - -type OrgSearchRequest struct { - Offset uint64 - Limit uint64 - SortingColumn OrgSearchKey - Asc bool - Queries []*OrgSearchQuery -} - -type OrgSearchKey int32 - -const ( - OrgSearchKeyUnspecified OrgSearchKey = iota - OrgSearchKeyOrgID - OrgSearchKeyOrgName - OrgSearchKeyOrgDomain - OrgSearchKeyState - OrgSearchKeyResourceOwner - OrgSearchKeyOrgNameIgnoreCase //used for lowercase search -) - -type OrgSearchQuery struct { - Key OrgSearchKey - Method domain.SearchMethod - Value interface{} -} - -type OrgSearchResult struct { - Offset uint64 - Limit uint64 - TotalResult uint64 - Result []*OrgView - Sequence uint64 - Timestamp time.Time -} - -func (r *OrgSearchRequest) EnsureLimit(limit uint64) error { - if r.Limit > limit { - return caos_errors.ThrowInvalidArgument(nil, "SEARCH-200ds", "Errors.Limit.ExceedsDefault") - } - if r.Limit == 0 { - r.Limit = limit - } - return nil -} - -func OrgViewToOrg(o *OrgView) *Org { - return &Org{ - ObjectRoot: models.ObjectRoot{ - AggregateID: o.ID, - ChangeDate: o.ChangeDate, - CreationDate: o.CreationDate, - ResourceOwner: o.ResourceOwner, - Sequence: o.Sequence, - }, - Name: o.Name, - State: o.State, - } -} diff --git a/internal/org/repository/view/model/org.go b/internal/org/repository/view/model/org.go deleted file mode 100644 index e44f2e1268..0000000000 --- a/internal/org/repository/view/model/org.go +++ /dev/null @@ -1,113 +0,0 @@ -package model - -import ( - "encoding/json" - "github.com/caos/zitadel/internal/domain" - "github.com/caos/zitadel/internal/org/repository/eventsourcing/model" - "time" - - "github.com/caos/logging" - "github.com/caos/zitadel/internal/errors" - es_models "github.com/caos/zitadel/internal/eventstore/v1/models" - org_model "github.com/caos/zitadel/internal/org/model" -) - -const ( - OrgKeyOrgDomain = "domain" - OrgKeyOrgID = "id" - OrgKeyOrgName = "name" - OrgKeyResourceOwner = "resource_owner" - OrgKeyState = "org_state" -) - -type OrgView struct { - ID string `json:"-" gorm:"column:id;primary_key"` - CreationDate time.Time `json:"-" gorm:"column:creation_date"` - ChangeDate time.Time `json:"-" gorm:"column:change_date"` - ResourceOwner string `json:"-" gorm:"column:resource_owner"` - State int32 `json:"-" gorm:"column:org_state"` - Sequence uint64 `json:"-" gorm:"column:sequence"` - - Name string `json:"name" gorm:"column:name"` - Domain string `json:"domain" gorm:"column:domain"` -} - -func OrgFromModel(org *org_model.OrgView) *OrgView { - return &OrgView{ - ChangeDate: org.ChangeDate, - CreationDate: org.CreationDate, - ID: org.ID, - Name: org.Name, - ResourceOwner: org.ResourceOwner, - Sequence: org.Sequence, - State: int32(org.State), - } -} - -func OrgToModel(org *OrgView) *org_model.OrgView { - return &org_model.OrgView{ - ChangeDate: org.ChangeDate, - CreationDate: org.CreationDate, - ID: org.ID, - Name: org.Name, - ResourceOwner: org.ResourceOwner, - Sequence: org.Sequence, - State: org_model.OrgState(org.State), - } -} - -func OrgToDomain(org *OrgView) *domain.Org { - return &domain.Org{ - ObjectRoot: es_models.ObjectRoot{ - AggregateID: org.ID, - ChangeDate: org.ChangeDate, - CreationDate: org.CreationDate, - Sequence: org.Sequence, - }, - Name: org.Name, - PrimaryDomain: org.Domain, - } -} - -func OrgsToModel(orgs []*OrgView) []*org_model.OrgView { - modelOrgs := make([]*org_model.OrgView, len(orgs)) - - for i, org := range orgs { - modelOrgs[i] = OrgToModel(org) - } - - return modelOrgs -} - -func (o *OrgView) AppendEvent(event *es_models.Event) (err error) { - switch event.Type { - case model.OrgAdded: - o.CreationDate = event.CreationDate - o.State = int32(org_model.OrgStateActive) - o.setRootData(event) - err = o.SetData(event) - case model.OrgChanged: - o.setRootData(event) - err = o.SetData(event) - case model.OrgDeactivated: - o.State = int32(org_model.OrgStateInactive) - case model.OrgReactivated: - o.State = int32(org_model.OrgStateActive) - } - return err -} - -func (o *OrgView) setRootData(event *es_models.Event) { - o.ChangeDate = event.CreationDate - o.Sequence = event.Sequence - o.ID = event.AggregateID - o.ResourceOwner = event.ResourceOwner -} - -func (o *OrgView) SetData(event *es_models.Event) error { - if err := json.Unmarshal(event.Data, o); err != nil { - logging.Log("VIEW-5W7Op").WithError(err).Error("could not unmarshal event data") - return errors.ThrowInternal(err, "VIEW-HZKME", "Could not unmarshal data") - } - return nil -} diff --git a/internal/org/repository/view/model/org_domain.go b/internal/org/repository/view/model/org_domain.go deleted file mode 100644 index 0ddf2e3d5c..0000000000 --- a/internal/org/repository/view/model/org_domain.go +++ /dev/null @@ -1,94 +0,0 @@ -package model - -import ( - "encoding/json" - "time" - - "github.com/caos/logging" - - caos_errs "github.com/caos/zitadel/internal/errors" - "github.com/caos/zitadel/internal/eventstore/v1/models" - "github.com/caos/zitadel/internal/org/model" - es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model" -) - -const ( - OrgDomainKeyOrgID = "org_id" - OrgDomainKeyDomain = "domain" - OrgDomainKeyVerified = "verified" - OrgDomainKeyPrimary = "primary_domain" -) - -type OrgDomainView struct { - Domain string `json:"domain" gorm:"column:domain;primary_key"` - OrgID string `json:"-" gorm:"column:org_id;primary_key"` - Verified bool `json:"-" gorm:"column:verified"` - Primary bool `json:"-" gorm:"column:primary_domain"` - ValidationType int32 `json:"validationType" gorm:"column:validation_type"` - Sequence uint64 `json:"-" gorm:"column:sequence"` - - CreationDate time.Time `json:"-" gorm:"column:creation_date"` - ChangeDate time.Time `json:"-" gorm:"column:change_date"` -} - -func OrgDomainViewFromModel(domain *model.OrgDomainView) *OrgDomainView { - return &OrgDomainView{ - OrgID: domain.OrgID, - Domain: domain.Domain, - Primary: domain.Primary, - Verified: domain.Verified, - ValidationType: int32(domain.ValidationType), - CreationDate: domain.CreationDate, - ChangeDate: domain.ChangeDate, - } -} - -func OrgDomainToModel(domain *OrgDomainView) *model.OrgDomainView { - return &model.OrgDomainView{ - OrgID: domain.OrgID, - Domain: domain.Domain, - Primary: domain.Primary, - Verified: domain.Verified, - ValidationType: model.OrgDomainValidationType(domain.ValidationType), - CreationDate: domain.CreationDate, - ChangeDate: domain.ChangeDate, - } -} - -func OrgDomainsToModel(domain []*OrgDomainView) []*model.OrgDomainView { - result := make([]*model.OrgDomainView, len(domain)) - for i, r := range domain { - result[i] = OrgDomainToModel(r) - } - return result -} - -func (d *OrgDomainView) AppendEvent(event *models.Event) (err error) { - d.Sequence = event.Sequence - d.ChangeDate = event.CreationDate - switch event.Type { - case es_model.OrgDomainAdded: - d.setRootData(event) - d.CreationDate = event.CreationDate - err = d.SetData(event) - case es_model.OrgDomainVerificationAdded: - err = d.SetData(event) - case es_model.OrgDomainVerified: - d.Verified = true - case es_model.OrgDomainPrimarySet: - d.Primary = true - } - return err -} - -func (r *OrgDomainView) setRootData(event *models.Event) { - r.OrgID = event.AggregateID -} - -func (r *OrgDomainView) SetData(event *models.Event) error { - if err := json.Unmarshal(event.Data, r); err != nil { - logging.Log("EVEN-sj4Sf").WithError(err).Error("could not unmarshal event data") - return caos_errs.ThrowInternal(err, "MODEL-lub6s", "Could not unmarshal data") - } - return nil -} diff --git a/internal/org/repository/view/model/org_domain_query.go b/internal/org/repository/view/model/org_domain_query.go deleted file mode 100644 index 79ec916608..0000000000 --- a/internal/org/repository/view/model/org_domain_query.go +++ /dev/null @@ -1,65 +0,0 @@ -package model - -import ( - "github.com/caos/zitadel/internal/domain" - org_model "github.com/caos/zitadel/internal/org/model" - "github.com/caos/zitadel/internal/view/repository" -) - -type OrgDomainSearchRequest org_model.OrgDomainSearchRequest -type OrgDomainSearchQuery org_model.OrgDomainSearchQuery -type OrgDomainSearchKey org_model.OrgDomainSearchKey - -func (req OrgDomainSearchRequest) GetLimit() uint64 { - return req.Limit -} - -func (req OrgDomainSearchRequest) GetOffset() uint64 { - return req.Offset -} - -func (req OrgDomainSearchRequest) GetSortingColumn() repository.ColumnKey { - if req.SortingColumn == org_model.OrgDomainSearchKeyUnspecified { - return nil - } - return OrgDomainSearchKey(req.SortingColumn) -} - -func (req OrgDomainSearchRequest) GetAsc() bool { - return req.Asc -} - -func (req OrgDomainSearchRequest) GetQueries() []repository.SearchQuery { - result := make([]repository.SearchQuery, len(req.Queries)) - for i, q := range req.Queries { - result[i] = OrgDomainSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method} - } - return result -} - -func (req OrgDomainSearchQuery) GetKey() repository.ColumnKey { - return OrgDomainSearchKey(req.Key) -} - -func (req OrgDomainSearchQuery) GetMethod() domain.SearchMethod { - return req.Method -} - -func (req OrgDomainSearchQuery) GetValue() interface{} { - return req.Value -} - -func (key OrgDomainSearchKey) ToColumnName() string { - switch org_model.OrgDomainSearchKey(key) { - case org_model.OrgDomainSearchKeyDomain: - return OrgDomainKeyDomain - case org_model.OrgDomainSearchKeyOrgID: - return OrgDomainKeyOrgID - case org_model.OrgDomainSearchKeyVerified: - return OrgDomainKeyVerified - case org_model.OrgDomainSearchKeyPrimary: - return OrgDomainKeyPrimary - default: - return "" - } -} diff --git a/internal/org/repository/view/model/org_query.go b/internal/org/repository/view/model/org_query.go deleted file mode 100644 index a875f1a968..0000000000 --- a/internal/org/repository/view/model/org_query.go +++ /dev/null @@ -1,69 +0,0 @@ -package model - -import ( - "github.com/caos/zitadel/internal/domain" - usr_model "github.com/caos/zitadel/internal/org/model" - "github.com/caos/zitadel/internal/view/repository" -) - -type OrgSearchRequest usr_model.OrgSearchRequest -type OrgSearchQuery usr_model.OrgSearchQuery -type OrgSearchKey usr_model.OrgSearchKey - -func (req OrgSearchRequest) GetLimit() uint64 { - return req.Limit -} - -func (req OrgSearchRequest) GetOffset() uint64 { - return req.Offset -} - -func (req OrgSearchRequest) GetSortingColumn() repository.ColumnKey { - if req.SortingColumn == usr_model.OrgSearchKeyUnspecified { - return nil - } - return OrgSearchKey(req.SortingColumn) -} - -func (req OrgSearchRequest) GetAsc() bool { - return req.Asc -} - -func (req OrgSearchRequest) GetQueries() []repository.SearchQuery { - result := make([]repository.SearchQuery, len(req.Queries)) - for i, q := range req.Queries { - result[i] = OrgSearchQuery{Key: q.Key, Value: q.Value, Method: q.Method} - } - return result -} - -func (req OrgSearchQuery) GetKey() repository.ColumnKey { - return OrgSearchKey(req.Key) -} - -func (req OrgSearchQuery) GetMethod() domain.SearchMethod { - return req.Method -} - -func (req OrgSearchQuery) GetValue() interface{} { - return req.Value -} - -func (key OrgSearchKey) ToColumnName() string { - switch usr_model.OrgSearchKey(key) { - case usr_model.OrgSearchKeyOrgDomain: - return OrgKeyOrgDomain - case usr_model.OrgSearchKeyOrgID: - return OrgKeyOrgID - case usr_model.OrgSearchKeyOrgName: - return OrgKeyOrgName - case usr_model.OrgSearchKeyOrgNameIgnoreCase: - return "LOWER(" + OrgKeyOrgName + ")" //used for lowercase search - case usr_model.OrgSearchKeyResourceOwner: - return OrgKeyResourceOwner - case usr_model.OrgSearchKeyState: - return OrgKeyState - default: - return "" - } -} diff --git a/internal/org/repository/view/org_domain_view.go b/internal/org/repository/view/org_domain_view.go deleted file mode 100644 index d4ab0972cd..0000000000 --- a/internal/org/repository/view/org_domain_view.go +++ /dev/null @@ -1,83 +0,0 @@ -package view - -import ( - domain2 "github.com/caos/zitadel/internal/domain" - caos_errs "github.com/caos/zitadel/internal/errors" - org_model "github.com/caos/zitadel/internal/org/model" - "github.com/caos/zitadel/internal/org/repository/view/model" - "github.com/caos/zitadel/internal/view/repository" - "github.com/jinzhu/gorm" -) - -func OrgDomainByOrgIDAndDomain(db *gorm.DB, table, orgID, domain string) (*model.OrgDomainView, error) { - domainView := new(model.OrgDomainView) - orgIDQuery := &model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyOrgID, Value: orgID, Method: domain2.SearchMethodEquals} - domainQuery := &model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyDomain, Value: domain, Method: domain2.SearchMethodEquals} - query := repository.PrepareGetByQuery(table, orgIDQuery, domainQuery) - err := query(db, domainView) - if caos_errs.IsNotFound(err) { - return nil, caos_errs.ThrowNotFound(nil, "VIEW-Gqwfq", "Errors.Org.DomainNotOnOrg") - } - return domainView, err -} - -func VerifiedOrgDomain(db *gorm.DB, table, domain string) (*model.OrgDomainView, error) { - domainView := new(model.OrgDomainView) - domainQuery := &model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyDomain, Value: domain, Method: domain2.SearchMethodEquals} - verifiedQuery := &model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyVerified, Value: true, Method: domain2.SearchMethodEquals} - query := repository.PrepareGetByQuery(table, domainQuery, verifiedQuery) - err := query(db, domainView) - if caos_errs.IsNotFound(err) { - return nil, caos_errs.ThrowNotFound(nil, "VIEW-Tew2q", "Errors.Org.DomainNotFound") - } - return domainView, err -} - -func SearchOrgDomains(db *gorm.DB, table string, req *org_model.OrgDomainSearchRequest) ([]*model.OrgDomainView, uint64, error) { - members := make([]*model.OrgDomainView, 0) - query := repository.PrepareSearchQuery(table, model.OrgDomainSearchRequest{Limit: req.Limit, Offset: req.Offset, Queries: req.Queries}) - count, err := query(db, &members) - if err != nil { - return nil, 0, err - } - return members, count, nil -} - -func OrgDomainsByOrgID(db *gorm.DB, table string, orgID string) ([]*model.OrgDomainView, error) { - domains := make([]*model.OrgDomainView, 0) - queries := []*org_model.OrgDomainSearchQuery{ - { - Key: org_model.OrgDomainSearchKeyOrgID, - Value: orgID, - Method: domain2.SearchMethodEquals, - }, - } - query := repository.PrepareSearchQuery(table, model.OrgDomainSearchRequest{Queries: queries}) - _, err := query(db, &domains) - if err != nil { - return nil, err - } - return domains, nil -} - -func PutOrgDomain(db *gorm.DB, table string, domain *model.OrgDomainView) error { - save := repository.PrepareSave(table) - return save(db, domain) -} - -func PutOrgDomains(db *gorm.DB, table string, domains ...*model.OrgDomainView) error { - save := repository.PrepareBulkSave(table) - d := make([]interface{}, len(domains)) - for i, domain := range domains { - d[i] = domain - } - return save(db, d...) -} - -func DeleteOrgDomain(db *gorm.DB, table, orgID, domain string) error { - delete := repository.PrepareDeleteByKeys(table, - repository.Key{Key: model.OrgDomainSearchKey(org_model.OrgDomainSearchKeyDomain), Value: domain}, - repository.Key{Key: model.OrgDomainSearchKey(org_model.OrgDomainSearchKeyOrgID), Value: orgID}, - ) - return delete(db) -} diff --git a/internal/query/current_sequence.go b/internal/query/current_sequence.go index 045747f4a0..f2247b949a 100644 --- a/internal/query/current_sequence.go +++ b/internal/query/current_sequence.go @@ -8,9 +8,8 @@ import ( sq "github.com/Masterminds/squirrel" - "github.com/caos/zitadel/internal/query/projection" - "github.com/caos/zitadel/internal/errors" + "github.com/caos/zitadel/internal/query/projection" ) type LatestSequence struct { diff --git a/internal/query/org.go b/internal/query/org.go index bcfd9129c3..d39b95023a 100644 --- a/internal/query/org.go +++ b/internal/query/org.go @@ -7,7 +7,6 @@ import ( "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" diff --git a/internal/query/org_domain.go b/internal/query/org_domain.go new file mode 100644 index 0000000000..6176bebd22 --- /dev/null +++ b/internal/query/org_domain.go @@ -0,0 +1,154 @@ +package query + +import ( + "context" + "database/sql" + "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" +) + +type Domain struct { + CreationDate time.Time + ChangeDate time.Time + Sequence uint64 + Domain string + OrgID string + IsVerified bool + IsPrimary bool + ValidationType domain.OrgDomainValidationType +} + +type Domains struct { + SearchResponse + Domains []*Domain +} + +type OrgDomainSearchQueries struct { + SearchRequest + Queries []SearchQuery +} + +func (q *OrgDomainSearchQueries) toQuery(query sq.SelectBuilder) sq.SelectBuilder { + query = q.SearchRequest.toQuery(query) + for _, q := range q.Queries { + query = q.ToQuery(query) + } + return query +} + +func NewOrgDomainDomainSearchQuery(method TextComparison, value string) (SearchQuery, error) { + return NewTextQuery(OrgDomainDomainCol, value, method) +} + +func NewOrgDomainOrgIDSearchQuery(value string) (SearchQuery, error) { + return NewTextQuery(OrgDomainOrgIDCol, value, TextEquals) +} + +func (q *Queries) SearchOrgDomains(ctx context.Context, queries *OrgDomainSearchQueries) (domains *Domains, err error) { + query, scan := prepareDomainsQuery() + stmt, args, err := queries.toQuery(query).ToSql() + if err != nil { + return nil, errors.ThrowInvalidArgument(err, "QUERY-ZRfj1", "Errors.Query.SQLStatement") + } + + rows, err := q.client.QueryContext(ctx, stmt, args...) + if err != nil { + return nil, errors.ThrowInternal(err, "QUERY-M6mYN", "Errors.Internal") + } + domains, err = scan(rows) + if err != nil { + return nil, err + } + domains.LatestSequence, err = q.latestSequence(ctx, orgDomainsTable) + return domains, err +} + +func prepareDomainsQuery() (sq.SelectBuilder, func(*sql.Rows) (*Domains, error)) { + return sq.Select( + OrgDomainCreationDateCol.identifier(), + OrgDomainChangeDateCol.identifier(), + OrgDomainSequenceCol.identifier(), + OrgDomainDomainCol.identifier(), + OrgDomainOrgIDCol.identifier(), + OrgDomainIsVerifiedCol.identifier(), + OrgDomainIsPrimaryCol.identifier(), + OrgDomainValidationTypeCol.identifier(), + countColumn.identifier(), + ).From(orgDomainsTable.identifier()).PlaceholderFormat(sq.Dollar), + func(rows *sql.Rows) (*Domains, error) { + domains := make([]*Domain, 0) + var count uint64 + for rows.Next() { + domain := new(Domain) + err := rows.Scan( + &domain.CreationDate, + &domain.ChangeDate, + &domain.Sequence, + &domain.Domain, + &domain.OrgID, + &domain.IsVerified, + &domain.IsPrimary, + &domain.ValidationType, + &count, + ) + if err != nil { + return nil, err + } + domains = append(domains, domain) + } + + if err := rows.Close(); err != nil { + return nil, errors.ThrowInternal(err, "QUERY-rKd6k", "Errors.Query.CloseRows") + } + + return &Domains{ + Domains: domains, + SearchResponse: SearchResponse{ + Count: count, + }, + }, nil + } +} + +var ( + orgDomainsTable = table{ + name: projection.OrgDomainTable, + } + + OrgDomainCreationDateCol = Column{ + name: projection.OrgDomainCreationDateCol, + table: orgDomainsTable, + } + OrgDomainChangeDateCol = Column{ + name: projection.OrgDomainChangeDateCol, + table: orgDomainsTable, + } + OrgDomainSequenceCol = Column{ + name: projection.OrgDomainSequenceCol, + table: orgDomainsTable, + } + OrgDomainDomainCol = Column{ + name: projection.OrgDomainDomainCol, + table: orgDomainsTable, + } + OrgDomainOrgIDCol = Column{ + name: projection.OrgDomainOrgIDCol, + table: orgDomainsTable, + } + OrgDomainIsVerifiedCol = Column{ + name: projection.OrgDomainIsVerifiedCol, + table: orgDomainsTable, + } + OrgDomainIsPrimaryCol = Column{ + name: projection.OrgDomainIsPrimaryCol, + table: orgDomainsTable, + } + OrgDomainValidationTypeCol = Column{ + name: projection.OrgDomainValidationTypeCol, + table: orgDomainsTable, + } +) diff --git a/internal/query/projection/org_domain.go b/internal/query/projection/org_domain.go new file mode 100644 index 0000000000..ae17380437 --- /dev/null +++ b/internal/query/projection/org_domain.go @@ -0,0 +1,180 @@ +package projection + +import ( + "context" + "fmt" + + "github.com/caos/logging" + "github.com/caos/zitadel/internal/domain" + "github.com/caos/zitadel/internal/errors" + "github.com/caos/zitadel/internal/eventstore" + "github.com/caos/zitadel/internal/eventstore/handler" + "github.com/caos/zitadel/internal/eventstore/handler/crdb" + "github.com/caos/zitadel/internal/repository/org" +) + +type OrgDomainProjection struct { + crdb.StatementHandler +} + +const ( + OrgDomainTable = "zitadel.projections.org_domains" +) + +func NewOrgDomainProjection(ctx context.Context, config crdb.StatementHandlerConfig) *OrgDomainProjection { + p := &OrgDomainProjection{} + config.ProjectionName = OrgDomainTable + config.Reducers = p.reducers() + p.StatementHandler = crdb.NewStatementHandler(ctx, config) + return p +} + +func (p *OrgDomainProjection) reducers() []handler.AggregateReducer { + return []handler.AggregateReducer{ + { + Aggregate: org.AggregateType, + EventRedusers: []handler.EventReducer{ + { + Event: org.OrgDomainAddedEventType, + Reduce: p.reduceDomainAdded, + }, + { + Event: org.OrgDomainVerificationAddedEventType, + Reduce: p.reduceDomainVerificationAdded, + }, + { + Event: org.OrgDomainVerifiedEventType, + Reduce: p.reduceDomainVerified, + }, + { + Event: org.OrgDomainPrimarySetEventType, + Reduce: p.reducePrimaryDomainSet, + }, + { + Event: org.OrgDomainRemovedEventType, + Reduce: p.reduceDomainRemoved, + }, + }, + }, + } +} + +const ( + OrgDomainCreationDateCol = "creation_date" + OrgDomainChangeDateCol = "change_date" + OrgDomainSequenceCol = "sequence" + OrgDomainDomainCol = "domain" + OrgDomainOrgIDCol = "org_id" + OrgDomainIsVerifiedCol = "is_verified" + OrgDomainIsPrimaryCol = "is_primary" + OrgDomainValidationTypeCol = "validation_type" +) + +func (p *OrgDomainProjection) reduceDomainAdded(event eventstore.EventReader) (*handler.Statement, error) { + e, ok := event.(*org.DomainAddedEvent) + if !ok { + logging.LogWithFields("PROJE-6fXKf", "seq", event.Sequence(), "expectedType", org.OrgDomainAddedEventType, "gottenType", fmt.Sprintf("%T", event)).Error("unexpected event type") + return nil, errors.ThrowInvalidArgument(nil, "PROJE-DM2DI", "reduce.wrong.event.type") + } + return crdb.NewCreateStatement( + e, + []handler.Column{ + handler.NewCol(OrgDomainCreationDateCol, e.CreationDate()), + handler.NewCol(OrgDomainChangeDateCol, e.CreationDate()), + handler.NewCol(OrgDomainSequenceCol, e.Sequence()), + handler.NewCol(OrgDomainDomainCol, e.Domain), + handler.NewCol(OrgDomainOrgIDCol, e.Aggregate().ID), + handler.NewCol(OrgDomainIsVerifiedCol, false), + handler.NewCol(OrgDomainIsPrimaryCol, false), + handler.NewCol(OrgDomainValidationTypeCol, domain.OrgDomainValidationTypeUnspecified), + }, + ), nil +} + +func (p *OrgDomainProjection) reduceDomainVerificationAdded(event eventstore.EventReader) (*handler.Statement, error) { + e, ok := event.(*org.DomainVerificationAddedEvent) + if !ok { + logging.LogWithFields("PROJE-2gGSs", "seq", event.Sequence(), "expectedType", org.OrgDomainVerificationAddedEventType, "gottenType", fmt.Sprintf("%T", event)).Error("unexpected event type") + return nil, errors.ThrowInvalidArgument(nil, "PROJE-EBzyu", "reduce.wrong.event.type") + } + return crdb.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(OrgDomainChangeDateCol, e.CreationDate()), + handler.NewCol(OrgDomainSequenceCol, e.Sequence()), + handler.NewCol(OrgDomainValidationTypeCol, e.ValidationType), + }, + []handler.Condition{ + handler.NewCond(OrgDomainDomainCol, e.Domain), + handler.NewCond(OrgDomainOrgIDCol, e.Aggregate().ID), + }, + ), nil +} + +func (p *OrgDomainProjection) reduceDomainVerified(event eventstore.EventReader) (*handler.Statement, error) { + e, ok := event.(*org.DomainVerifiedEvent) + if !ok { + logging.LogWithFields("PROJE-aeGCA", "seq", event.Sequence(), "expectedType", org.OrgDomainVerifiedEventType, "gottenType", fmt.Sprintf("%T", event)).Error("unexpected event type") + return nil, errors.ThrowInvalidArgument(nil, "PROJE-3Rvkr", "reduce.wrong.event.type") + } + return crdb.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(OrgDomainChangeDateCol, e.CreationDate()), + handler.NewCol(OrgDomainSequenceCol, e.Sequence()), + handler.NewCol(OrgDomainIsVerifiedCol, true), + }, + []handler.Condition{ + handler.NewCond(OrgDomainDomainCol, e.Domain), + handler.NewCond(OrgDomainOrgIDCol, e.Aggregate().ID), + }, + ), nil +} + +func (p *OrgDomainProjection) reducePrimaryDomainSet(event eventstore.EventReader) (*handler.Statement, error) { + e, ok := event.(*org.DomainPrimarySetEvent) + if !ok { + logging.LogWithFields("PROJE-6YjHo", "seq", event.Sequence(), "expectedType", org.OrgDomainPrimarySetEventType, "gottenType", fmt.Sprintf("%T", event)).Error("unexpected event type") + return nil, errors.ThrowInvalidArgument(nil, "PROJE-aIuei", "reduce.wrong.event.type") + } + return crdb.NewMultiStatement( + e, + crdb.AddUpdateStatement( + []handler.Column{ + handler.NewCol(OrgDomainChangeDateCol, e.CreationDate()), + handler.NewCol(OrgDomainSequenceCol, e.Sequence()), + handler.NewCol(OrgDomainIsPrimaryCol, false), + }, + []handler.Condition{ + handler.NewCond(OrgDomainOrgIDCol, e.Aggregate().ID), + handler.NewCond(OrgDomainIsPrimaryCol, true), + }, + ), + crdb.AddUpdateStatement( + []handler.Column{ + handler.NewCol(OrgDomainChangeDateCol, e.CreationDate()), + handler.NewCol(OrgDomainSequenceCol, e.Sequence()), + handler.NewCol(OrgDomainIsPrimaryCol, true), + }, + []handler.Condition{ + handler.NewCond(OrgDomainDomainCol, e.Domain), + handler.NewCond(OrgDomainOrgIDCol, e.Aggregate().ID), + }, + ), + ), nil +} + +func (p *OrgDomainProjection) reduceDomainRemoved(event eventstore.EventReader) (*handler.Statement, error) { + e, ok := event.(*org.DomainRemovedEvent) + if !ok { + logging.LogWithFields("PROJE-dDnps", "seq", event.Sequence(), "expectedType", org.OrgDomainRemovedEventType, "gottenType", fmt.Sprintf("%T", event)).Error("unexpected event type") + return nil, errors.ThrowInvalidArgument(nil, "PROJE-gh1Mx", "reduce.wrong.event.type") + } + return crdb.NewDeleteStatement( + e, + []handler.Condition{ + handler.NewCond(OrgDomainDomainCol, e.Domain), + handler.NewCond(OrgDomainOrgIDCol, e.Aggregate().ID), + }, + ), nil +} diff --git a/internal/query/projection/org_domain_test.go b/internal/query/projection/org_domain_test.go new file mode 100644 index 0000000000..74f114f4d8 --- /dev/null +++ b/internal/query/projection/org_domain_test.go @@ -0,0 +1,203 @@ +package projection + +import ( + "testing" + + "github.com/caos/zitadel/internal/domain" + "github.com/caos/zitadel/internal/errors" + "github.com/caos/zitadel/internal/eventstore" + "github.com/caos/zitadel/internal/eventstore/handler" + "github.com/caos/zitadel/internal/eventstore/repository" + "github.com/caos/zitadel/internal/repository/org" +) + +func TestOrgDomainProjection_reduces(t *testing.T) { + type args struct { + event func(t *testing.T) eventstore.EventReader + } + tests := []struct { + name string + args args + reduce func(event eventstore.EventReader) (*handler.Statement, error) + want wantReduce + }{ + { + name: "reduceDomainAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.OrgDomainAddedEventType), + org.AggregateType, + []byte(`{"domain": "domain.new"}`), + ), org.DomainAddedEventMapper), + }, + reduce: (&OrgDomainProjection{}).reduceDomainAdded, + want: wantReduce{ + projection: OrgDomainTable, + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "INSERT INTO zitadel.projections.org_domains (creation_date, change_date, sequence, domain, org_id, is_verified, is_primary, validation_type) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", + expectedArgs: []interface{}{ + anyArg{}, + anyArg{}, + uint64(15), + "domain.new", + "agg-id", + false, + false, + domain.OrgDomainValidationTypeUnspecified, + }, + }, + }, + }, + }, + }, + { + name: "reduceDomainVerificationAdded", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.OrgDomainVerificationAddedEventType), + org.AggregateType, + []byte(`{"domain": "domain.new", "validationType": 2}`), + ), org.DomainVerificationAddedEventMapper), + }, + reduce: (&OrgDomainProjection{}).reduceDomainVerificationAdded, + want: wantReduce{ + projection: OrgDomainTable, + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.org_domains SET (change_date, sequence, validation_type) = ($1, $2, $3) WHERE (domain = $4) AND (org_id = $5)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + domain.OrgDomainValidationTypeDNS, + "domain.new", + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reduceDomainVerified", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.OrgDomainVerifiedEventType), + org.AggregateType, + []byte(`{"domain": "domain.new"}`), + ), org.DomainVerifiedEventMapper), + }, + reduce: (&OrgDomainProjection{}).reduceDomainVerified, + want: wantReduce{ + projection: OrgDomainTable, + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.org_domains SET (change_date, sequence, is_verified) = ($1, $2, $3) WHERE (domain = $4) AND (org_id = $5)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + true, + "domain.new", + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reducePrimaryDomainSet", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.OrgDomainPrimarySetEventType), + org.AggregateType, + []byte(`{"domain": "domain.new"}`), + ), org.DomainPrimarySetEventMapper), + }, + reduce: (&OrgDomainProjection{}).reducePrimaryDomainSet, + want: wantReduce{ + projection: OrgDomainTable, + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "UPDATE zitadel.projections.org_domains SET (change_date, sequence, is_primary) = ($1, $2, $3) WHERE (org_id = $4) AND (is_primary = $5)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + false, + "agg-id", + true, + }, + }, + { + expectedStmt: "UPDATE zitadel.projections.org_domains SET (change_date, sequence, is_primary) = ($1, $2, $3) WHERE (domain = $4) AND (org_id = $5)", + expectedArgs: []interface{}{ + anyArg{}, + uint64(15), + true, + "domain.new", + "agg-id", + }, + }, + }, + }, + }, + }, + { + name: "reduceDomainRemoved", + args: args{ + event: getEvent(testEvent( + repository.EventType(org.OrgDomainRemovedEventType), + org.AggregateType, + []byte(`{"domain": "domain.new"}`), + ), org.DomainRemovedEventMapper), + }, + reduce: (&OrgDomainProjection{}).reduceDomainRemoved, + want: wantReduce{ + projection: OrgDomainTable, + aggregateType: eventstore.AggregateType("org"), + sequence: 15, + previousSequence: 10, + executer: &testExecuter{ + executions: []execution{ + { + expectedStmt: "DELETE FROM zitadel.projections.org_domains WHERE (domain = $1) AND (org_id = $2)", + expectedArgs: []interface{}{ + "domain.new", + "agg-id", + }, + }, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + event := baseEvent(t) + got, err := tt.reduce(event) + if _, ok := err.(errors.InvalidArgument); !ok { + t.Errorf("no wrong event mapping: %v, got: %v", err, got) + } + + event = tt.args.event(t) + got, err = tt.reduce(event) + assertReduce(t, got, err, tt.want) + }) + } +} diff --git a/internal/query/projection/projection.go b/internal/query/projection/projection.go index e567d88470..bf1f071404 100644 --- a/internal/query/projection/projection.go +++ b/internal/query/projection/projection.go @@ -39,6 +39,8 @@ func Start(ctx context.Context, sqlClient *sql.DB, es *eventstore.Eventstore, co NewProjectProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["projects"])) NewProjectGrantProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["project_grants"])) NewProjectRoleProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["project_roles"])) + // owner.NewOrgOwnerProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["org_owners"])) + NewOrgDomainProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["org_domains"])) return nil } diff --git a/migrations/cockroach/V1.76__org_domains_projection.sql b/migrations/cockroach/V1.76__org_domains_projection.sql new file mode 100644 index 0000000000..56ef51fbef --- /dev/null +++ b/migrations/cockroach/V1.76__org_domains_projection.sql @@ -0,0 +1,13 @@ +CREATE TABLE zitadel.projections.org_domains ( + creation_date TIMESTAMPTZ, + change_date TIMESTAMPTZ, + sequence BIGINT, + + domain TEXT, + org_id TEXT, + is_verified BOOLEAN, + is_primary BOOLEAN, + validation_type SMALLINT, + + PRIMARY KEY (org_id, domain) +); \ No newline at end of file