2022-02-16 16:49:17 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02: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 16:49:17 +01:00
|
|
|
)
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
type IAMSMTPConfigWriteModel struct {
|
2022-02-16 16:49:17 +01:00
|
|
|
eventstore.WriteModel
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
ID string
|
|
|
|
Description string
|
2023-08-29 09:08:24 +02:00
|
|
|
TLS bool
|
|
|
|
Host string
|
|
|
|
User string
|
|
|
|
Password *crypto.CryptoValue
|
2024-04-11 09:16:10 +02:00
|
|
|
SenderAddress string
|
|
|
|
SenderName string
|
|
|
|
ReplyToAddress string
|
2023-08-29 09:08:24 +02:00
|
|
|
State domain.SMTPConfigState
|
2022-05-16 16:08:47 +02:00
|
|
|
|
|
|
|
domain string
|
|
|
|
domainState domain.InstanceDomainState
|
|
|
|
smtpSenderAddressMatchesInstanceDomain bool
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
func NewIAMSMTPConfigWriteModel(instanceID, id, domain string) *IAMSMTPConfigWriteModel {
|
|
|
|
return &IAMSMTPConfigWriteModel{
|
2022-02-16 16:49:17 +01:00
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 07:58:09 +02:00
|
|
|
AggregateID: instanceID,
|
|
|
|
ResourceOwner: instanceID,
|
2024-04-11 09:16:10 +02:00
|
|
|
InstanceID: instanceID,
|
2022-02-16 16:49:17 +01:00
|
|
|
},
|
2024-04-11 09:16:10 +02:00
|
|
|
ID: id,
|
2022-05-16 16:08:47 +02:00
|
|
|
domain: domain,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
func (wm *IAMSMTPConfigWriteModel) AppendEvents(events ...eventstore.Event) {
|
2022-05-16 16:08:47 +02:00
|
|
|
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 16:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
func (wm *IAMSMTPConfigWriteModel) Reduce() error {
|
2022-02-16 16:49:17 +01:00
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
2022-03-24 17:21:34 +01:00
|
|
|
case *instance.SMTPConfigAddedEvent:
|
2024-04-11 09:16:10 +02:00
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
wm.reduceSMTPConfigAddedEvent(e)
|
|
|
|
case *instance.SMTPConfigChangedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
wm.reduceSMTPConfigChangedEvent(e)
|
|
|
|
case *instance.SMTPConfigRemovedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
2023-08-29 09:08:24 +02:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
wm.reduceSMTPConfigRemovedEvent(e)
|
|
|
|
case *instance.SMTPConfigActivatedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
wm.State = domain.SMTPConfigStateActive
|
|
|
|
case *instance.SMTPConfigDeactivatedEvent:
|
|
|
|
if wm.ID != e.ID {
|
|
|
|
continue
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
wm.State = domain.SMTPConfigStateInactive
|
2022-05-16 16:08:47 +02: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 16:49:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
func (wm *IAMSMTPConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
// If ID equals ResourceOwner we're dealing with the old and unique smtp settings
|
|
|
|
// Let's set the empty ID for the query
|
|
|
|
if wm.ID == wm.ResourceOwner {
|
|
|
|
wm.ID = ""
|
|
|
|
}
|
|
|
|
|
2022-02-16 16:49:17 +01:00
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
2022-03-24 17:21:34 +01:00
|
|
|
AggregateTypes(instance.AggregateType).
|
2022-02-16 16:49:17 +01:00
|
|
|
AggregateIDs(wm.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-24 17:21:34 +01:00
|
|
|
instance.SMTPConfigAddedEventType,
|
2023-10-10 14:45:45 +02:00
|
|
|
instance.SMTPConfigRemovedEventType,
|
2022-03-24 17:21:34 +01:00
|
|
|
instance.SMTPConfigChangedEventType,
|
2022-05-16 16:08:47 +02:00
|
|
|
instance.SMTPConfigPasswordChangedEventType,
|
2024-04-11 09:16:10 +02:00
|
|
|
instance.SMTPConfigActivatedEventType,
|
|
|
|
instance.SMTPConfigDeactivatedEventType,
|
|
|
|
instance.SMTPConfigRemovedEventType,
|
2022-05-16 16:08:47 +02:00
|
|
|
instance.InstanceDomainAddedEventType,
|
|
|
|
instance.InstanceDomainRemovedEventType,
|
|
|
|
instance.DomainPolicyAddedEventType,
|
|
|
|
instance.DomainPolicyChangedEventType).
|
2022-02-16 16:49:17 +01:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
func (wm *IAMSMTPConfigWriteModel) NewChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, id, description string, tls bool, fromAddress, fromName, replyToAddress, smtpHost, smtpUser string, smtpPassword *crypto.CryptoValue) (*instance.SMTPConfigChangedEvent, bool, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes := make([]instance.SMTPConfigChanges, 0)
|
2022-02-16 16:49:17 +01:00
|
|
|
var err error
|
|
|
|
|
2024-04-11 09:16:10 +02:00
|
|
|
if wm.ID != id {
|
|
|
|
changes = append(changes, instance.ChangeSMTPConfigID(id))
|
|
|
|
}
|
|
|
|
if wm.Description != description {
|
|
|
|
changes = append(changes, instance.ChangeSMTPConfigDescription(description))
|
|
|
|
}
|
2022-02-16 16:49:17 +01:00
|
|
|
if wm.TLS != tls {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigTLS(tls))
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
if wm.SenderAddress != fromAddress {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigFromAddress(fromAddress))
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
if wm.SenderName != fromName {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigFromName(fromName))
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2023-08-29 09:08:24 +02:00
|
|
|
if wm.ReplyToAddress != replyToAddress {
|
|
|
|
changes = append(changes, instance.ChangeSMTPConfigReplyToAddress(replyToAddress))
|
|
|
|
}
|
2022-02-16 16:49:17 +01:00
|
|
|
if wm.Host != smtpHost {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigSMTPHost(smtpHost))
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
|
|
|
if wm.User != smtpUser {
|
2022-03-24 17:21:34 +01:00
|
|
|
changes = append(changes, instance.ChangeSMTPConfigSMTPUser(smtpUser))
|
2022-02-16 16:49:17 +01:00
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
if smtpPassword != nil {
|
|
|
|
changes = append(changes, instance.ChangeSMTPConfigSMTPPassword(smtpPassword))
|
|
|
|
}
|
2022-02-16 16:49:17 +01:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, false, nil
|
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
changeEvent, err := instance.NewSMTPConfigChangeEvent(ctx, aggregate, id, changes)
|
2022-02-16 16:49:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
return changeEvent, true, nil
|
|
|
|
}
|
2024-04-11 09:16:10 +02:00
|
|
|
|
|
|
|
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigAddedEvent(e *instance.SMTPConfigAddedEvent) {
|
|
|
|
wm.Description = e.Description
|
|
|
|
wm.TLS = e.TLS
|
|
|
|
wm.Host = e.Host
|
|
|
|
wm.User = e.User
|
|
|
|
wm.Password = e.Password
|
|
|
|
wm.SenderAddress = e.SenderAddress
|
|
|
|
wm.SenderName = e.SenderName
|
|
|
|
wm.ReplyToAddress = e.ReplyToAddress
|
|
|
|
wm.State = domain.SMTPConfigStateInactive
|
|
|
|
// If ID has empty value we're dealing with the old and unique smtp settings
|
|
|
|
// These would be the default values for ID and State
|
|
|
|
if e.ID == "" {
|
|
|
|
wm.Description = "generic"
|
|
|
|
wm.ID = e.Aggregate().ResourceOwner
|
|
|
|
wm.State = domain.SMTPConfigStateActive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigChangedEvent(e *instance.SMTPConfigChangedEvent) {
|
|
|
|
if e.Description != nil {
|
|
|
|
wm.Description = *e.Description
|
|
|
|
}
|
|
|
|
if e.TLS != nil {
|
|
|
|
wm.TLS = *e.TLS
|
|
|
|
}
|
|
|
|
if e.Host != nil {
|
|
|
|
wm.Host = *e.Host
|
|
|
|
}
|
|
|
|
if e.User != nil {
|
|
|
|
wm.User = *e.User
|
|
|
|
}
|
|
|
|
if e.Password != nil {
|
|
|
|
wm.Password = e.Password
|
|
|
|
}
|
|
|
|
if e.FromAddress != nil {
|
|
|
|
wm.SenderAddress = *e.FromAddress
|
|
|
|
}
|
|
|
|
if e.FromName != nil {
|
|
|
|
wm.SenderName = *e.FromName
|
|
|
|
}
|
|
|
|
if e.ReplyToAddress != nil {
|
|
|
|
wm.ReplyToAddress = *e.ReplyToAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// If ID has empty value we're dealing with the old and unique smtp settings
|
|
|
|
// These would be the default values for ID and State
|
|
|
|
if e.ID == "" {
|
|
|
|
wm.Description = "generic"
|
|
|
|
wm.ID = e.Aggregate().ResourceOwner
|
|
|
|
wm.State = domain.SMTPConfigStateActive
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *IAMSMTPConfigWriteModel) reduceSMTPConfigRemovedEvent(e *instance.SMTPConfigRemovedEvent) {
|
|
|
|
wm.Description = ""
|
|
|
|
wm.TLS = false
|
|
|
|
wm.SenderName = ""
|
|
|
|
wm.SenderAddress = ""
|
|
|
|
wm.ReplyToAddress = ""
|
|
|
|
wm.Host = ""
|
|
|
|
wm.User = ""
|
|
|
|
wm.Password = nil
|
|
|
|
wm.State = domain.SMTPConfigStateRemoved
|
|
|
|
|
|
|
|
// If ID has empty value we're dealing with the old and unique smtp settings
|
|
|
|
// These would be the default values for ID and State
|
|
|
|
if e.ID == "" {
|
|
|
|
wm.ID = e.Aggregate().ResourceOwner
|
|
|
|
}
|
|
|
|
}
|