2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 12:59:51 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMLabelPolicyWriteModel struct {
|
|
|
|
LabelPolicyWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func NewIAMLabelPolicyWriteModel() *IAMLabelPolicyWriteModel {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &IAMLabelPolicyWriteModel{
|
|
|
|
LabelPolicyWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: domain.IAMID,
|
|
|
|
ResourceOwner: domain.IAMID,
|
2021-01-04 14:52:13 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMLabelPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.LabelPolicyAddedEvent:
|
|
|
|
wm.LabelPolicyWriteModel.AppendEvents(&e.LabelPolicyAddedEvent)
|
|
|
|
case *iam.LabelPolicyChangedEvent:
|
|
|
|
wm.LabelPolicyWriteModel.AppendEvents(&e.LabelPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMLabelPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.LabelPolicyWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMLabelPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, iam.AggregateType).
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateIDs(wm.LabelPolicyWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(
|
|
|
|
iam.LabelPolicyAddedEventType,
|
|
|
|
iam.LabelPolicyChangedEventType)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMLabelPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-04 14:52:13 +01:00
|
|
|
primaryColor,
|
|
|
|
secondaryColor string,
|
2021-03-25 14:41:07 +01:00
|
|
|
hideLoginNameSuffix bool,
|
2021-01-04 14:52:13 +01:00
|
|
|
) (*iam.LabelPolicyChangedEvent, bool) {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes := make([]policy.LabelPolicyChanges, 0)
|
2021-01-04 14:52:13 +01:00
|
|
|
if wm.PrimaryColor != primaryColor {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes = append(changes, policy.ChangePrimaryColor(primaryColor))
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
if wm.SecondaryColor != secondaryColor {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes = append(changes, policy.ChangeSecondaryColor(secondaryColor))
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
2021-03-25 14:41:07 +01:00
|
|
|
if wm.HideLoginNameSuffix != hideLoginNameSuffix {
|
|
|
|
changes = append(changes, policy.ChangeHideLoginNameSuffix(hideLoginNameSuffix))
|
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
changedEvent, err := iam.NewLabelPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 11:24:15 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|