2022-03-24 16:21:34 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-03-24 16:21:34 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2022-03-24 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceDomainPolicyWriteModel struct {
|
|
|
|
PolicyDomainWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceDomainPolicyWriteModel(ctx context.Context) *InstanceDomainPolicyWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceDomainPolicyWriteModel{
|
|
|
|
PolicyDomainWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2022-03-24 16:21:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-28 08:05:09 +00:00
|
|
|
case *instance.DomainPolicyAddedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyAddedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *instance.DomainPolicyChangedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyDomainWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(instance.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyDomainWriteModel.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.DomainPolicyAddedEventType,
|
|
|
|
instance.DomainPolicyChangedEventType).
|
2022-03-24 16:21:34 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2022-04-13 09:24:03 +00:00
|
|
|
userLoginMustBeDomain,
|
2022-05-16 14:08:47 +00:00
|
|
|
validateOrgDomain,
|
|
|
|
smtpSenderAddresssMatchesInstanceDomain bool) (*instance.DomainPolicyChangedEvent, bool) {
|
2022-04-13 09:24:03 +00:00
|
|
|
changes := make([]policy.DomainPolicyChanges, 0)
|
2022-03-24 16:21:34 +00:00
|
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
|
|
|
}
|
2022-04-13 09:24:03 +00:00
|
|
|
if wm.ValidateOrgDomains != validateOrgDomain {
|
|
|
|
changes = append(changes, policy.ChangeValidateOrgDomains(validateOrgDomain))
|
|
|
|
}
|
2022-05-16 14:08:47 +00:00
|
|
|
if wm.SMTPSenderAddressMatchesInstanceDomain != smtpSenderAddresssMatchesInstanceDomain {
|
|
|
|
changes = append(changes, policy.ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddresssMatchesInstanceDomain))
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
changedEvent, err := instance.NewDomainPolicyChangedEvent(ctx, aggregate, changes)
|
2022-03-24 16:21:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return changedEvent, true
|
|
|
|
}
|