zitadel/internal/query/smtp.go
Fabi e3528ff0b2
feat: Config to eventstore (#3158)
* 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>
2022-02-16 15:49:17 +00:00

140 lines
3.6 KiB
Go

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/crypto"
"github.com/caos/zitadel/internal/query/projection"
"github.com/caos/zitadel/internal/errors"
)
var (
smtpConfigsTable = table{
name: projection.SMTPConfigProjectionTable,
}
SMTPConfigColumnAggregateID = Column{
name: projection.SMTPConfigColumnAggregateID,
table: smtpConfigsTable,
}
SMTPConfigColumnCreationDate = Column{
name: projection.SMTPConfigColumnCreationDate,
table: smtpConfigsTable,
}
SMTPConfigColumnChangeDate = Column{
name: projection.SMTPConfigColumnChangeDate,
table: smtpConfigsTable,
}
SMTPConfigColumnResourceOwner = Column{
name: projection.SMTPConfigColumnResourceOwner,
table: smtpConfigsTable,
}
SMTPConfigColumnSequence = Column{
name: projection.SMTPConfigColumnSequence,
table: smtpConfigsTable,
}
SMTPConfigColumnTLS = Column{
name: projection.SMTPConfigColumnTLS,
table: smtpConfigsTable,
}
SMTPConfigColumnFromAddress = Column{
name: projection.SMTPConfigColumnFromAddress,
table: smtpConfigsTable,
}
SMTPConfigColumnFromName = Column{
name: projection.SMTPConfigColumnFromName,
table: smtpConfigsTable,
}
SMTPConfigColumnSMTPHost = Column{
name: projection.SMTPConfigColumnSMTPHost,
table: smtpConfigsTable,
}
SMTPConfigColumnSMTPUser = Column{
name: projection.SMTPConfigColumnSMTPUser,
table: smtpConfigsTable,
}
SMTPConfigColumnSMTPPassword = Column{
name: projection.SMTPConfigColumnSMTPPassword,
table: smtpConfigsTable,
}
)
type SMTPConfigs struct {
SearchResponse
SMTPConfigs []*SMTPConfig
}
type SMTPConfig struct {
AggregateID string
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
Sequence uint64
TLS bool
SenderAddress string
SenderName string
Host string
User string
Password *crypto.CryptoValue
}
func (q *Queries) SMTPConfigByAggregateID(ctx context.Context, aggregateID string) (*SMTPConfig, error) {
stmt, scan := prepareSMTPConfigQuery()
query, args, err := stmt.Where(sq.Eq{
SMTPConfigColumnAggregateID.identifier(): aggregateID,
}).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-3m9sl", "Errors.Query.SQLStatment")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func prepareSMTPConfigQuery() (sq.SelectBuilder, func(*sql.Row) (*SMTPConfig, error)) {
password := new(crypto.CryptoValue)
return sq.Select(
SMTPConfigColumnAggregateID.identifier(),
SMTPConfigColumnCreationDate.identifier(),
SMTPConfigColumnChangeDate.identifier(),
SMTPConfigColumnResourceOwner.identifier(),
SMTPConfigColumnSequence.identifier(),
SMTPConfigColumnTLS.identifier(),
SMTPConfigColumnFromAddress.identifier(),
SMTPConfigColumnFromName.identifier(),
SMTPConfigColumnSMTPHost.identifier(),
SMTPConfigColumnSMTPUser.identifier(),
SMTPConfigColumnSMTPPassword.identifier()).
From(smtpConfigsTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*SMTPConfig, error) {
config := new(SMTPConfig)
err := row.Scan(
&config.AggregateID,
&config.CreationDate,
&config.ChangeDate,
&config.ResourceOwner,
&config.Sequence,
&config.TLS,
&config.SenderAddress,
&config.SenderName,
&config.Host,
&config.User,
&password,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-fwofw", "Errors.SMTPConfig.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-9k87F", "Errors.Internal")
}
config.Password = password
return config, nil
}
}