2022-02-16 15:49:17 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/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
|
|
|
|
|
|
|
|
SenderAddress string
|
|
|
|
SenderName string
|
|
|
|
TLS bool
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password *crypto.CryptoValue
|
|
|
|
State domain.SMTPConfigState
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceSMTPConfigWriteModel(instanceID 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-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
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if e.Host != nil {
|
|
|
|
wm.Host = *e.Host
|
|
|
|
}
|
|
|
|
if e.User != nil {
|
|
|
|
wm.User = *e.User
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
instance.SMTPConfigChangedEventType,
|
|
|
|
instance.SMTPConfigPasswordChangedEventType).
|
2022-02-16 15:49:17 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (wm *InstanceSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, tls bool, fromAddress, fromName, smtpHost, smtpUser string) (*instance.SMTPConfigChangedEvent, bool, error) {
|
|
|
|
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
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|