2022-03-24 16:21:34 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-12-06 08:01:31 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2022-04-26 23:01:45 +00:00
|
|
|
"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/org"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2022-03-24 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgDomainPolicyWriteModel struct {
|
|
|
|
PolicyDomainWriteModel
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewOrgDomainPolicyWriteModel(orgID string) *OrgDomainPolicyWriteModel {
|
|
|
|
return &OrgDomainPolicyWriteModel{
|
|
|
|
PolicyDomainWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
AggregateID: orgID,
|
|
|
|
ResourceOwner: orgID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyAddedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyAddedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyChangedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyChangedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *org.DomainPolicyRemovedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyRemovedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyDomainWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyDomainWriteModel.AggregateID).
|
2022-03-28 08:05:09 +00:00
|
|
|
EventTypes(org.DomainPolicyAddedEventType,
|
|
|
|
org.DomainPolicyChangedEventType,
|
|
|
|
org.DomainPolicyRemovedEventType).
|
2022-03-24 16:21:34 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *OrgDomainPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2022-04-13 09:24:03 +00:00
|
|
|
userLoginMustBeDomain,
|
2022-05-16 14:08:47 +00:00
|
|
|
validateOrgDomains,
|
2022-12-06 08:01:31 +00:00
|
|
|
smtpSenderAddressMatchesInstanceDomain bool) (changedEvent *org.DomainPolicyChangedEvent, usernameChange bool, err error) {
|
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 {
|
2022-12-06 08:01:31 +00:00
|
|
|
usernameChange = true
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
|
|
|
}
|
2022-04-13 09:24:03 +00:00
|
|
|
if wm.ValidateOrgDomains != validateOrgDomains {
|
|
|
|
changes = append(changes, policy.ChangeValidateOrgDomains(validateOrgDomains))
|
|
|
|
}
|
2022-05-16 14:08:47 +00:00
|
|
|
if wm.SMTPSenderAddressMatchesInstanceDomain != smtpSenderAddressMatchesInstanceDomain {
|
|
|
|
changes = append(changes, policy.ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddressMatchesInstanceDomain))
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
if len(changes) == 0 {
|
2022-12-06 08:01:31 +00:00
|
|
|
return nil, false, caos_errs.ThrowPreconditionFailed(nil, "ORG-3M9ds", "Errors.Org.LabelPolicy.NotChanged")
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
2022-12-06 08:01:31 +00:00
|
|
|
changedEvent, err = org.NewDomainPolicyChangedEvent(ctx, aggregate, changes)
|
|
|
|
return changedEvent, usernameChange, err
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|