2022-02-16 15:49:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type InstanceSMTPConfigWriteModel struct {
|
2022-02-16 15:49:17 +00:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
2023-08-29 07:08:24 +00:00
|
|
|
SenderAddress string
|
|
|
|
SenderName string
|
|
|
|
ReplyToAddress string
|
|
|
|
TLS bool
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password *crypto.CryptoValue
|
|
|
|
State domain.SMTPConfigState
|
2022-05-16 14:08:47 +00:00
|
|
|
|
|
|
|
domain string
|
|
|
|
domainState domain.InstanceDomainState
|
|
|
|
smtpSenderAddressMatchesInstanceDomain bool
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 14:08:47 +00:00
|
|
|
func NewInstanceSMTPConfigWriteModel(instanceID, domain string) *InstanceSMTPConfigWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceSMTPConfigWriteModel{
|
2022-02-16 15:49:17 +00:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: instanceID,
|
|
|
|
ResourceOwner: instanceID,
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
2022-05-16 14:08:47 +00:00
|
|
|
domain: domain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceSMTPConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *instance.DomainAddedEvent:
|
|
|
|
if e.Domain != wm.domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
case *instance.DomainRemovedEvent:
|
|
|
|
if e.Domain != wm.domain {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
default:
|
|
|
|
wm.WriteModel.AppendEvents(e)
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSMTPConfigWriteModel) Reduce() error {
|
2022-02-16 15:49:17 +00:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMTPConfigAddedEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
wm.TLS = e.TLS
|
|
|
|
wm.SenderAddress = e.SenderAddress
|
|
|
|
wm.SenderName = e.SenderName
|
2023-08-29 07:08:24 +00:00
|
|
|
wm.ReplyToAddress = e.ReplyToAddress
|
2022-02-16 15:49:17 +00:00
|
|
|
wm.Host = e.Host
|
|
|
|
wm.User = e.User
|
|
|
|
wm.Password = e.Password
|
|
|
|
wm.State = domain.SMTPConfigStateActive
|
2022-03-24 16:21:34 +00:00
|
|
|
case *instance.SMTPConfigChangedEvent:
|
2022-02-16 15:49:17 +00:00
|
|
|
if e.TLS != nil {
|
|
|
|
wm.TLS = *e.TLS
|
|
|
|
}
|
|
|
|
if e.FromAddress != nil {
|
|
|
|
wm.SenderAddress = *e.FromAddress
|
|
|
|
}
|
|
|
|
if e.FromName != nil {
|
|
|
|
wm.SenderName = *e.FromName
|
|
|
|
}
|
2023-08-29 07:08:24 +00:00
|
|
|
if e.ReplyToAddress != nil {
|
|
|
|
wm.ReplyToAddress = *e.ReplyToAddress
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
if e.Host != nil {
|
|
|
|
wm.Host = *e.Host
|
|
|
|
}
|
|
|
|
if e.User != nil {
|
|
|
|
wm.User = *e.User
|
|
|
|
}
|
2022-06-10 10:39:38 +00:00
|
|
|
case *instance.SMTPConfigRemovedEvent:
|
|
|
|
wm.State = domain.SMTPConfigStateRemoved
|
|
|
|
wm.TLS = false
|
|
|
|
wm.SenderName = ""
|
|
|
|
wm.SenderAddress = ""
|
2023-08-29 07:08:24 +00:00
|
|
|
wm.ReplyToAddress = ""
|
2022-06-10 10:39:38 +00:00
|
|
|
wm.Host = ""
|
|
|
|
wm.User = ""
|
|
|
|
wm.Password = nil
|
2022-05-16 14:08:47 +00:00
|
|
|
case *instance.DomainAddedEvent:
|
|
|
|
wm.domainState = domain.InstanceDomainStateActive
|
|
|
|
case *instance.DomainRemovedEvent:
|
|
|
|
wm.domainState = domain.InstanceDomainStateRemoved
|
|
|
|
case *instance.DomainPolicyAddedEvent:
|
|
|
|
wm.smtpSenderAddressMatchesInstanceDomain = e.SMTPSenderAddressMatchesInstanceDomain
|
|
|
|
case *instance.DomainPolicyChangedEvent:
|
|
|
|
if e.SMTPSenderAddressMatchesInstanceDomain != nil {
|
|
|
|
wm.smtpSenderAddressMatchesInstanceDomain = *e.SMTPSenderAddressMatchesInstanceDomain
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSMTPConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
2022-02-16 15:49:17 +00:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
2022-03-24 16:21:34 +00:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2022-02-16 15:49:17 +00:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.SMTPConfigAddedEventType,
|
2023-10-10 12:45:45 +00:00
|
|
|
instance.SMTPConfigRemovedEventType,
|
2022-03-24 16:21:34 +00:00
|
|
|
instance.SMTPConfigChangedEventType,
|
2022-05-16 14:08:47 +00:00
|
|
|
instance.SMTPConfigPasswordChangedEventType,
|
|
|
|
instance.InstanceDomainAddedEventType,
|
|
|
|
instance.InstanceDomainRemovedEventType,
|
|
|
|
instance.DomainPolicyAddedEventType,
|
|
|
|
instance.DomainPolicyChangedEventType).
|
2022-02-16 15:49:17 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2023-08-29 07:08:24 +00:00
|
|
|
func (wm *InstanceSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, tls bool, fromAddress, fromName, replyToAddress, smtpHost, smtpUser string) (*instance.SMTPConfigChangedEvent, bool, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes := make([]instance.SMTPConfigChanges, 0)
|
2022-02-16 15:49:17 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if wm.TLS != tls {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigTLS(tls))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.SenderAddress != fromAddress {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigFromAddress(fromAddress))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.SenderName != fromName {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigFromName(fromName))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-08-29 07:08:24 +00:00
|
|
|
if wm.ReplyToAddress != replyToAddress {
|
|
|
|
changes = append(changes, instance.ChangeSMTPConfigReplyToAddress(replyToAddress))
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
if wm.Host != smtpHost {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigSMTPHost(smtpHost))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
if wm.User != smtpUser {
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigSMTPUser(smtpUser))
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent, err := instance.NewSMTPConfigChangeEvent(ctx, aggregate, changes)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|