mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
e3528ff0b2
* feat: add default language to eventstore * feat: add secret generator configs events * feat: tests * feat: secret generators in eventstore * feat: secret generators in eventstore * feat: smtp config in eventstore * feat: smtp config in eventstore * feat: smtp config in eventstore * feat: smtp config in eventstore * feat: smtp config in eventstore * fix: migrations * fix migration version * fix test * feat: change secret generator type to enum * feat: change smtp attribute names * feat: change smtp attribute names * feat: remove engryption algorithms from command side * feat: remove engryption algorithms from command side * feat: smtp config * feat: smtp config * format smtp from header Co-authored-by: Livio Amstutz <livio.a@gmail.com>
120 lines
3.4 KiB
Go
120 lines
3.4 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/crypto"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
"github.com/caos/zitadel/internal/notification/channels/smtp"
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
|
)
|
|
|
|
func (c *Commands) AddSMTPConfig(ctx context.Context, config *smtp.EmailConfig) (*domain.ObjectDetails, error) {
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx)
|
|
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 != "" {
|
|
smtpPassword, err = crypto.Encrypt([]byte(config.SMTP.Password), c.smtpPasswordCrypto)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
|
pushedEvents, err := c.eventstore.Push(ctx, iam.NewSMTPConfigAddedEvent(
|
|
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
|
|
}
|
|
|
|
func (c *Commands) ChangeSMTPConfig(ctx context.Context, config *smtp.EmailConfig) (*domain.ObjectDetails, error) {
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if smtpConfigWriteModel.State == domain.SMTPConfigStateUnspecified {
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-3n9ls", "Errors.SMTPConfig.NotFound")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
|
|
|
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
|
|
}
|
|
|
|
func (c *Commands) ChangeSMTPConfigPassword(ctx context.Context, password string) (*domain.ObjectDetails, error) {
|
|
smtpConfigWriteModel, err := c.getSMTPConfig(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if smtpConfigWriteModel.State == domain.SMTPConfigStateUnspecified {
|
|
return nil, caos_errs.ThrowNotFound(nil, "COMMAND-3n9ls", "Errors.SMTPConfig.NotFound")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&smtpConfigWriteModel.WriteModel)
|
|
newPW, err := crypto.Encrypt([]byte(password), c.smtpPasswordCrypto)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
pushedEvents, err := c.eventstore.Push(ctx, iam.NewSMTPConfigPasswordChangedEvent(
|
|
ctx,
|
|
iamAgg,
|
|
newPW))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = AppendAndReduce(smtpConfigWriteModel, pushedEvents...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModelToObjectDetails(&smtpConfigWriteModel.WriteModel), nil
|
|
}
|
|
|
|
func (c *Commands) getSMTPConfig(ctx context.Context) (_ *IAMSMTPConfigWriteModel, err error) {
|
|
writeModel := NewIAMSMTPConfigWriteModel()
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return writeModel, nil
|
|
}
|