2021-01-08 10:33:45 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-08 10:33:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgDomainWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Domain string
|
|
|
|
ValidationType domain.OrgDomainValidationType
|
|
|
|
ValidationCode *crypto.CryptoValue
|
|
|
|
Primary bool
|
|
|
|
Verified bool
|
|
|
|
|
|
|
|
State domain.OrgDomainState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgDomainWriteModel(orgID string, domain string) *OrgDomainWriteModel {
|
|
|
|
return &OrgDomainWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-08 10:33:45 +00:00
|
|
|
},
|
|
|
|
Domain: domain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.DomainAddedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainVerificationAddedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainVerificationFailedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainVerifiedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainRemovedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.DomainAddedEvent:
|
|
|
|
wm.Domain = e.Domain
|
|
|
|
wm.State = domain.OrgDomainStateActive
|
|
|
|
case *org.DomainVerificationAddedEvent:
|
|
|
|
wm.ValidationType = e.ValidationType
|
|
|
|
wm.ValidationCode = e.ValidationCode
|
|
|
|
case *org.DomainVerifiedEvent:
|
|
|
|
wm.Verified = true
|
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.Primary = e.Domain == wm.Domain
|
|
|
|
case *org.DomainRemovedEvent:
|
|
|
|
wm.State = domain.OrgDomainStateRemoved
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
EventTypes(
|
|
|
|
org.OrgDomainAddedEventType,
|
|
|
|
org.OrgDomainVerifiedEventType,
|
|
|
|
org.OrgDomainVerificationAddedEventType,
|
|
|
|
org.OrgDomainVerifiedEventType,
|
|
|
|
org.OrgDomainPrimarySetEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.OrgDomainRemovedEventType).
|
|
|
|
Builder()
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
2021-04-19 14:43:36 +00:00
|
|
|
|
|
|
|
type OrgDomainsWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Domains []*Domain
|
|
|
|
PrimaryDomain string
|
|
|
|
OrgName string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Domain struct {
|
|
|
|
Domain string
|
|
|
|
Verified bool
|
|
|
|
State domain.OrgDomainState
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgDomainsWriteModel(orgID string) *OrgDomainsWriteModel {
|
|
|
|
return &OrgDomainsWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
Domains: make([]*Domain, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainsWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.OrgAddedEvent:
|
|
|
|
wm.OrgName = e.Name
|
|
|
|
case *org.OrgChangedEvent:
|
|
|
|
wm.OrgName = e.Name
|
|
|
|
case *org.DomainAddedEvent:
|
|
|
|
wm.Domains = append(wm.Domains, &Domain{Domain: e.Domain, State: domain.OrgDomainStateActive})
|
|
|
|
case *org.DomainVerifiedEvent:
|
|
|
|
for _, d := range wm.Domains {
|
|
|
|
if d.Domain == e.Domain {
|
|
|
|
d.Verified = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *org.DomainPrimarySetEvent:
|
|
|
|
wm.PrimaryDomain = e.Domain
|
|
|
|
case *org.DomainRemovedEvent:
|
|
|
|
for _, d := range wm.Domains {
|
|
|
|
if d.Domain == e.Domain {
|
|
|
|
d.State = domain.OrgDomainStateRemoved
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainsWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 11:55:57 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-04-19 14:43:36 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 11:55:57 +00:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.AggregateID).
|
2021-04-19 14:43:36 +00:00
|
|
|
EventTypes(
|
|
|
|
org.OrgAddedEventType,
|
|
|
|
org.OrgChangedEventType,
|
|
|
|
org.OrgDomainAddedEventType,
|
|
|
|
org.OrgDomainVerifiedEventType,
|
|
|
|
org.OrgDomainVerificationAddedEventType,
|
|
|
|
org.OrgDomainVerifiedEventType,
|
|
|
|
org.OrgDomainPrimarySetEventType,
|
2021-07-06 11:55:57 +00:00
|
|
|
org.OrgDomainRemovedEventType).
|
|
|
|
Builder()
|
2021-04-19 14:43:36 +00:00
|
|
|
}
|
2021-08-25 09:12:24 +00:00
|
|
|
|
|
|
|
type OrgDomainVerifiedWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
Domain string
|
|
|
|
Verified bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgDomainVerifiedWriteModel(domain string) *OrgDomainVerifiedWriteModel {
|
|
|
|
return &OrgDomainVerifiedWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{},
|
|
|
|
Domain: domain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainVerifiedWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.DomainVerifiedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *org.DomainRemovedEvent:
|
|
|
|
if e.Domain != wm.Domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainVerifiedWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.DomainVerifiedEvent:
|
|
|
|
wm.Verified = true
|
|
|
|
wm.ResourceOwner = e.Aggregate().ResourceOwner
|
|
|
|
case *org.DomainRemovedEvent:
|
|
|
|
wm.Verified = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainVerifiedWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
org.OrgDomainVerifiedEventType,
|
|
|
|
org.OrgDomainRemovedEventType).
|
|
|
|
Builder()
|
|
|
|
}
|