2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type ORGOrgIAMPolicyWriteModel struct {
|
|
|
|
PolicyOrgIAMWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewORGOrgIAMPolicyWriteModel(orgID string) *ORGOrgIAMPolicyWriteModel {
|
|
|
|
return &ORGOrgIAMPolicyWriteModel{
|
|
|
|
PolicyOrgIAMWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
2021-01-04 13:52:13 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) AppendEvents(events ...eventstore.EventReader) {
|
|
|
|
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 13:48:27 +00:00
|
|
|
case *org.OrgIAMPolicyRemovedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyRemovedEvent)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyOrgIAMWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent, org.AggregateType).
|
2021-01-12 11:59:51 +00:00
|
|
|
AggregateIDs(wm.PolicyOrgIAMWriteModel.AggregateID).
|
2021-02-18 13:48:27 +00:00
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
EventTypes(org.OrgIAMPolicyAddedEventType,
|
|
|
|
org.OrgIAMPolicyChangedEventType,
|
|
|
|
org.OrgIAMPolicyRemovedEventType)
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func (wm *ORGOrgIAMPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userLoginMustBeDomain bool) (*org.OrgIAMPolicyChangedEvent, bool) {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes := make([]policy.OrgIAMPolicyChanges, 0)
|
2021-01-04 13:52:13 +00:00
|
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
2021-01-18 10:24:15 +00:00
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
changedEvent, err := org.NewOrgIAMPolicyChangedEvent(ctx, aggregate, changes)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|