2022-03-24 16:21:34 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrgDomainPolicyWriteModel struct {
|
|
|
|
PolicyDomainWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgDomainPolicyWriteModel(orgID string) *OrgDomainPolicyWriteModel {
|
|
|
|
return &OrgDomainPolicyWriteModel{
|
|
|
|
PolicyDomainWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyAddedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyAddedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyChangedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyChangedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyRemovedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyRemovedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyDomainWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyDomainWriteModel.AggregateID).
|
2022-03-28 08:05:09 +00:00
|
|
|
EventTypes(org.DomainPolicyAddedEventType,
|
|
|
|
org.DomainPolicyChangedEventType,
|
|
|
|
org.DomainPolicyRemovedEventType).
|
2022-03-24 16:21:34 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2022-03-28 08:05:09 +00:00
|
|
|
userLoginMustBeDomain bool) (*org.DomainPolicyChangedEvent, bool) {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes := make([]policy.OrgPolicyChanges, 0)
|
|
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
|
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
changedEvent, err := org.NewDomainPolicyChangedEvent(ctx, aggregate, changes)
|
2022-03-24 16:21:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|