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/domain"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
|
|
"github.com/caos/zitadel/internal/repository/policy"
|
2021-01-04 14:52:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type IAMOrgIAMPolicyWriteModel struct {
|
|
|
|
PolicyOrgIAMWriteModel
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
func NewIAMOrgIAMPolicyWriteModel() *IAMOrgIAMPolicyWriteModel {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &IAMOrgIAMPolicyWriteModel{
|
|
|
|
PolicyOrgIAMWriteModel{
|
|
|
|
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
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (wm *IAMOrgIAMPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
2021-01-04 14:52:13 +01:00
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *iam.OrgIAMPolicyAddedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyAddedEvent)
|
|
|
|
case *iam.OrgIAMPolicyChangedEvent:
|
|
|
|
wm.PolicyOrgIAMWriteModel.AppendEvents(&e.OrgIAMPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMOrgIAMPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyOrgIAMWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMOrgIAMPolicyWriteModel) 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(iam.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyOrgIAMWriteModel.AggregateID).
|
2021-02-18 14:48:27 +01:00
|
|
|
EventTypes(
|
|
|
|
iam.OrgIAMPolicyAddedEventType,
|
2021-07-06 13:55:57 +02:00
|
|
|
iam.OrgIAMPolicyChangedEventType).
|
|
|
|
Builder()
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func (wm *IAMOrgIAMPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userLoginMustBeDomain bool) (*iam.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 := iam.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
|
|
|
}
|