2022-03-24 16:21:34 +00:00
|
|
|
package instance
|
2022-02-16 15:49:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-05-13 12:13:07 +00:00
|
|
|
smtpConfigPrefix = "smtp.config."
|
2022-03-24 16:21:34 +00:00
|
|
|
SMTPConfigAddedEventType = instanceEventTypePrefix + smtpConfigPrefix + "added"
|
|
|
|
SMTPConfigChangedEventType = instanceEventTypePrefix + smtpConfigPrefix + "changed"
|
|
|
|
SMTPConfigPasswordChangedEventType = instanceEventTypePrefix + smtpConfigPrefix + "password.changed"
|
2022-06-10 10:39:38 +00:00
|
|
|
SMTPConfigRemovedEventType = instanceEventTypePrefix + smtpConfigPrefix + "removed"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SMTPConfigAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-08-29 07:08:24 +00:00
|
|
|
SenderAddress string `json:"senderAddress,omitempty"`
|
|
|
|
SenderName string `json:"senderName,omitempty"`
|
|
|
|
ReplyToAddress string `json:"replyToAddress,omitempty"`
|
|
|
|
TLS bool `json:"tls,omitempty"`
|
|
|
|
Host string `json:"host,omitempty"`
|
|
|
|
User string `json:"user,omitempty"`
|
|
|
|
Password *crypto.CryptoValue `json:"password,omitempty"`
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMTPConfigAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tls bool,
|
|
|
|
senderAddress,
|
|
|
|
senderName,
|
2023-08-29 07:08:24 +00:00
|
|
|
replyToAddress,
|
2022-02-16 15:49:17 +00:00
|
|
|
host,
|
|
|
|
user string,
|
|
|
|
password *crypto.CryptoValue,
|
|
|
|
) *SMTPConfigAddedEvent {
|
|
|
|
return &SMTPConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMTPConfigAddedEventType,
|
|
|
|
),
|
2023-08-29 07:08:24 +00:00
|
|
|
TLS: tls,
|
|
|
|
SenderAddress: senderAddress,
|
|
|
|
SenderName: senderName,
|
|
|
|
ReplyToAddress: replyToAddress,
|
|
|
|
Host: host,
|
|
|
|
User: user,
|
|
|
|
Password: password,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigAddedEvent) Payload() interface{} {
|
2022-02-16 15:49:17 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-16 15:49:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMTPConfigAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-16 15:49:17 +00:00
|
|
|
smtpConfigAdded := &SMTPConfigAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smtpConfigAdded)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "IAM-39fks", "unable to unmarshal smtp config added")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return smtpConfigAdded, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMTPConfigChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2023-08-29 07:08:24 +00:00
|
|
|
FromAddress *string `json:"senderAddress,omitempty"`
|
|
|
|
FromName *string `json:"senderName,omitempty"`
|
|
|
|
ReplyToAddress *string `json:"replyToAddress,omitempty"`
|
|
|
|
TLS *bool `json:"tls,omitempty"`
|
|
|
|
Host *string `json:"host,omitempty"`
|
|
|
|
User *string `json:"user,omitempty"`
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigChangedEvent) Payload() interface{} {
|
2022-02-16 15:49:17 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-16 15:49:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMTPConfigChangeEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
changes []SMTPConfigChanges,
|
|
|
|
) (*SMTPConfigChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "IAM-o0pWf", "Errors.NoChangesFound")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
changeEvent := &SMTPConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMTPConfigChangedEventType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMTPConfigChanges func(event *SMTPConfigChangedEvent)
|
|
|
|
|
|
|
|
func ChangeSMTPConfigTLS(tls bool) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.TLS = &tls
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeSMTPConfigFromAddress(senderAddress string) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.FromAddress = &senderAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeSMTPConfigFromName(senderName string) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.FromName = &senderName
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 07:08:24 +00:00
|
|
|
func ChangeSMTPConfigReplyToAddress(replyToAddress string) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.ReplyToAddress = &replyToAddress
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-16 15:49:17 +00:00
|
|
|
func ChangeSMTPConfigSMTPHost(smtpHost string) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.Host = &smtpHost
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeSMTPConfigSMTPUser(smtpUser string) func(event *SMTPConfigChangedEvent) {
|
|
|
|
return func(e *SMTPConfigChangedEvent) {
|
|
|
|
e.User = &smtpUser
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMTPConfigChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-16 15:49:17 +00:00
|
|
|
e := &SMTPConfigChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "IAM-m09oo", "unable to unmarshal smtp changed")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type SMTPConfigPasswordChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Password *crypto.CryptoValue `json:"password,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMTPConfigPasswordChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
password *crypto.CryptoValue,
|
|
|
|
) *SMTPConfigPasswordChangedEvent {
|
|
|
|
return &SMTPConfigPasswordChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMTPConfigPasswordChangedEventType,
|
|
|
|
),
|
|
|
|
Password: password,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigPasswordChangedEvent) Payload() interface{} {
|
2022-02-16 15:49:17 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigPasswordChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-02-16 15:49:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMTPConfigPasswordChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-02-16 15:49:17 +00:00
|
|
|
smtpConfigPasswordChagned := &SMTPConfigPasswordChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smtpConfigPasswordChagned)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "IAM-99iNF", "unable to unmarshal smtp config password changed")
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return smtpConfigPasswordChagned, nil
|
|
|
|
}
|
2022-06-10 10:39:38 +00:00
|
|
|
|
|
|
|
type SMTPConfigRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSMTPConfigRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
) *SMTPConfigRemovedEvent {
|
|
|
|
return &SMTPConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
SMTPConfigRemovedEventType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigRemovedEvent) Payload() interface{} {
|
2022-06-10 10:39:38 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *SMTPConfigRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2022-06-10 10:39:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func SMTPConfigRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-06-10 10:39:38 +00:00
|
|
|
smtpConfigRemoved := &SMTPConfigRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(smtpConfigRemoved)
|
2022-06-10 10:39:38 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "IAM-DVw1s", "unable to unmarshal smtp config removed")
|
2022-06-10 10:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return smtpConfigRemoved, nil
|
|
|
|
}
|