2022-02-16 15:49:17 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
errs "errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2023-02-27 21:36:43 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/call"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-12-01 08:18:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query/projection"
|
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
smtpConfigsTable = table{
|
2022-10-27 06:08:36 +00:00
|
|
|
name: projection.SMTPConfigProjectionTable,
|
|
|
|
instanceIDCol: projection.SMTPConfigColumnInstanceID,
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
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,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
SMTPConfigColumnInstanceID = Column{
|
|
|
|
name: projection.SMTPConfigColumnInstanceID,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
SMTPConfigColumnSequence = Column{
|
|
|
|
name: projection.SMTPConfigColumnSequence,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
|
|
|
SMTPConfigColumnTLS = Column{
|
|
|
|
name: projection.SMTPConfigColumnTLS,
|
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
SMTPConfigColumnSenderAddress = Column{
|
|
|
|
name: projection.SMTPConfigColumnSenderAddress,
|
2022-02-16 15:49:17 +00:00
|
|
|
table: smtpConfigsTable,
|
|
|
|
}
|
2022-03-23 08:02:39 +00:00
|
|
|
SMTPConfigColumnSenderName = Column{
|
|
|
|
name: projection.SMTPConfigColumnSenderName,
|
2022-02-16 15:49:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-12-01 08:18:53 +00:00
|
|
|
func (q *Queries) SMTPConfigByAggregateID(ctx context.Context, aggregateID string) (_ *SMTPConfig, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
stmt, scan := prepareSMTPConfigQuery(ctx, q.client)
|
2022-02-16 15:49:17 +00:00
|
|
|
query, args, err := stmt.Where(sq.Eq{
|
|
|
|
SMTPConfigColumnAggregateID.identifier(): aggregateID,
|
2022-03-29 09:53:19 +00:00
|
|
|
SMTPConfigColumnInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
|
2022-02-16 15:49:17 +00:00
|
|
|
}).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-3m9sl", "Errors.Query.SQLStatment")
|
|
|
|
}
|
|
|
|
|
|
|
|
row := q.client.QueryRowContext(ctx, query, args...)
|
|
|
|
return scan(row)
|
|
|
|
}
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
func prepareSMTPConfigQuery(ctx context.Context, db prepareDatabase) (sq.SelectBuilder, func(*sql.Row) (*SMTPConfig, error)) {
|
2022-02-16 15:49:17 +00:00
|
|
|
password := new(crypto.CryptoValue)
|
|
|
|
|
|
|
|
return sq.Select(
|
|
|
|
SMTPConfigColumnAggregateID.identifier(),
|
|
|
|
SMTPConfigColumnCreationDate.identifier(),
|
|
|
|
SMTPConfigColumnChangeDate.identifier(),
|
|
|
|
SMTPConfigColumnResourceOwner.identifier(),
|
|
|
|
SMTPConfigColumnSequence.identifier(),
|
|
|
|
SMTPConfigColumnTLS.identifier(),
|
2022-03-23 08:02:39 +00:00
|
|
|
SMTPConfigColumnSenderAddress.identifier(),
|
|
|
|
SMTPConfigColumnSenderName.identifier(),
|
2022-02-16 15:49:17 +00:00
|
|
|
SMTPConfigColumnSMTPHost.identifier(),
|
|
|
|
SMTPConfigColumnSMTPUser.identifier(),
|
|
|
|
SMTPConfigColumnSMTPPassword.identifier()).
|
2023-02-27 21:36:43 +00:00
|
|
|
From(smtpConfigsTable.identifier() + db.Timetravel(call.Took(ctx))).
|
|
|
|
PlaceholderFormat(sq.Dollar),
|
2022-02-16 15:49:17 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|