2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-07-06 13:55:57 +02:00
|
|
|
|
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/repository/org"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type ORGOrgIAMPolicyWriteModel struct {
|
|
|
|
PolicyOrgIAMWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewORGOrgIAMPolicyWriteModel(orgID string) *ORGOrgIAMPolicyWriteModel {
|
|
|
|
return &ORGOrgIAMPolicyWriteModel{
|
|
|
|
PolicyOrgIAMWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 12:59:51 +01:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-04 14:52:13 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 14:52:13 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.OrgIAMPolicyAddedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyAddedEvent)
|
|
|
|
case *org.OrgIAMPolicyChangedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyChangedEvent)
|
2021-02-18 14:48:27 +01:00
|
|
|
case *org.OrgIAMPolicyRemovedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyRemovedEvent)
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyOrgIAMWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2021-07-06 13:55:57 +02:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
2021-02-18 14:48:27 +01:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
2021-07-06 13:55:57 +02:00
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyOrgIAMWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(org.OrgIAMPolicyAddedEventType,
|
|
|
|
org.OrgIAMPolicyChangedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
org.OrgIAMPolicyRemovedEventType).
|
|
|
|
Builder()
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userLoginMustBeDomain bool) (*org.OrgIAMPolicyChangedEvent, bool) {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes := make([]policy.OrgIAMPolicyChanges, 0)
|
2021-01-04 14:52:13 +01:00
|
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
2021-01-18 11:24:15 +01:00
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
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 := org.NewOrgIAMPolicyChangedEvent(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
|
|
|
}
|