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"
|
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/notification/channels/smtp"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) AddSMTPConfig(ctx context.Context, instanceID string, config *smtp.EmailConfig) (*domain.ObjectDetails, error) {
|
|
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if smtpConfigWriteModel.State == domain.SMTPConfigStateActive {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "COMMAND-en9lw", "Errors.SMTPConfig.AlreadyExists")
|
|
|
|
}
|
|
|
|
var smtpPassword *crypto.CryptoValue
|
|
|
|
if config.SMTP.Password != "" {
|
2022-04-20 14:59:37 +00:00
|
|
|
smtpPassword, err = crypto.Encrypt([]byte(config.SMTP.Password), c.smtpEncryption)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigAddedEvent(
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
config.Tls,
|
|
|
|
config.From,
|
|
|
|
config.FromName,
|
|
|
|
config.SMTP.Host,
|
|
|
|
config.SMTP.User,
|
|
|
|
smtpPassword))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) ChangeSMTPConfig(ctx context.Context, instanceID string, config *smtp.EmailConfig) (*domain.ObjectDetails, error) {
|
|
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if smtpConfigWriteModel.State == domain.SMTPConfigStateUnspecified {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-3n9ls", "Errors.SMTPConfig.NotFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
2022-02-16 15:49:17 +00:00
|
|
|
|
|
|
|
changedEvent, hasChanged, err := smtpConfigWriteModel.NewChangedEvent(
|
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
config.Tls,
|
|
|
|
config.From,
|
|
|
|
config.FromName,
|
|
|
|
config.SMTP.Host,
|
|
|
|
config.SMTP.User)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-m0o3f", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) ChangeSMTPConfigPassword(ctx context.Context, instanceID, password string) (*domain.ObjectDetails, error) {
|
|
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx, instanceID)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if smtpConfigWriteModel.State == domain.SMTPConfigStateUnspecified {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-3n9ls", "Errors.SMTPConfig.NotFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
iamAgg := InstanceAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
2022-04-20 14:59:37 +00:00
|
|
|
newPW, err := crypto.Encrypt([]byte(password), c.smtpEncryption)
|
2022-02-16 15:49:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, instance.NewSMTPConfigPasswordChangedEvent(
|
2022-02-16 15:49:17 +00:00
|
|
|
ctx,
|
|
|
|
iamAgg,
|
|
|
|
newPW))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func (c *Commands) getSMTPConfig(ctx context.Context, instanceID string) (_ *InstanceSMTPConfigWriteModel, err error) {
|
|
|
|
writeModel := NewInstanceSMTPConfigWriteModel(instanceID)
|
2022-02-16 15:49:17 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|