2022-02-16 15:49:17 +00:00
|
|
|
package projection
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-02-16 15:49:17 +00:00
|
|
|
)
|
|
|
|
|
2022-03-23 08:02:39 +00:00
|
|
|
const (
|
2022-08-31 07:52:43 +00:00
|
|
|
SecretGeneratorProjectionTable = "projections.secret_generators2"
|
2022-03-23 08:02:39 +00:00
|
|
|
|
|
|
|
SecretGeneratorColumnGeneratorType = "generator_type"
|
|
|
|
SecretGeneratorColumnAggregateID = "aggregate_id"
|
|
|
|
SecretGeneratorColumnCreationDate = "creation_date"
|
|
|
|
SecretGeneratorColumnChangeDate = "change_date"
|
|
|
|
SecretGeneratorColumnSequence = "sequence"
|
|
|
|
SecretGeneratorColumnResourceOwner = "resource_owner"
|
|
|
|
SecretGeneratorColumnInstanceID = "instance_id"
|
|
|
|
SecretGeneratorColumnLength = "length"
|
|
|
|
SecretGeneratorColumnExpiry = "expiry"
|
|
|
|
SecretGeneratorColumnIncludeLowerLetters = "include_lower_letters"
|
|
|
|
SecretGeneratorColumnIncludeUpperLetters = "include_upper_letters"
|
|
|
|
SecretGeneratorColumnIncludeDigits = "include_digits"
|
|
|
|
SecretGeneratorColumnIncludeSymbols = "include_symbols"
|
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
type secretGeneratorProjection struct{}
|
|
|
|
|
|
|
|
func newSecretGeneratorProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
|
|
return handler.NewHandler(ctx, &config, new(secretGeneratorProjection))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (*secretGeneratorProjection) Name() string {
|
|
|
|
return SecretGeneratorProjectionTable
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (*secretGeneratorProjection) Init() *old_handler.Check {
|
|
|
|
return handler.NewTableCheck(
|
|
|
|
handler.NewTable([]*handler.InitColumn{
|
|
|
|
handler.NewColumn(SecretGeneratorColumnGeneratorType, handler.ColumnTypeEnum),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnAggregateID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnSequence, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnResourceOwner, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnInstanceID, handler.ColumnTypeText),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnLength, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnExpiry, handler.ColumnTypeInt64),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnIncludeLowerLetters, handler.ColumnTypeBool),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnIncludeUpperLetters, handler.ColumnTypeBool),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnIncludeDigits, handler.ColumnTypeBool),
|
|
|
|
handler.NewColumn(SecretGeneratorColumnIncludeSymbols, handler.ColumnTypeBool),
|
2022-03-23 08:02:39 +00:00
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
handler.NewPrimaryKey(SecretGeneratorColumnInstanceID, SecretGeneratorColumnGeneratorType, SecretGeneratorColumnAggregateID),
|
2022-03-23 08:02:39 +00:00
|
|
|
),
|
|
|
|
)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (p *secretGeneratorProjection) Reducers() []handler.AggregateReducer {
|
2022-02-16 15:49:17 +00:00
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
2022-04-12 14:20:17 +00:00
|
|
|
Aggregate: instance.AggregateType,
|
2023-10-19 10:19:10 +00:00
|
|
|
EventReducers: []handler.EventReducer{
|
2022-02-16 15:49:17 +00:00
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorAddedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorAdded,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorChangedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorChanged,
|
|
|
|
},
|
|
|
|
{
|
2022-03-24 16:21:34 +00:00
|
|
|
Event: instance.SecretGeneratorRemovedEventType,
|
2022-02-16 15:49:17 +00:00
|
|
|
Reduce: p.reduceSecretGeneratorRemoved,
|
|
|
|
},
|
2022-10-26 13:06:48 +00:00
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: reduceInstanceRemovedHelper(SecretGeneratorColumnInstanceID),
|
|
|
|
},
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *secretGeneratorProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorAddedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-sk99F", "reduce.wrong.event.type %s", instance.SecretGeneratorAddedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewCreateStatement(
|
2022-02-16 15:49:17 +00:00
|
|
|
e,
|
|
|
|
[]handler.Column{
|
|
|
|
handler.NewCol(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCol(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
|
|
|
handler.NewCol(SecretGeneratorColumnCreationDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnResourceOwner, e.Aggregate().ResourceOwner),
|
2022-03-23 08:02:39 +00:00
|
|
|
handler.NewCol(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnLength, e.Length),
|
|
|
|
handler.NewCol(SecretGeneratorColumnExpiry, e.Expiry),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, e.IncludeLowerLetters),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, e.IncludeUpperLetters),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeDigits, e.IncludeDigits),
|
|
|
|
handler.NewCol(SecretGeneratorColumnIncludeSymbols, e.IncludeSymbols),
|
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *secretGeneratorProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorChangedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-s00Fs", "reduce.wrong.event.type %s", instance.SecretGeneratorChangedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
columns := make([]handler.Column, 0, 7)
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnChangeDate, e.CreationDate()),
|
|
|
|
handler.NewCol(SecretGeneratorColumnSequence, e.Sequence()))
|
|
|
|
if e.Length != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnLength, *e.Length))
|
|
|
|
}
|
|
|
|
if e.Expiry != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnExpiry, *e.Expiry))
|
|
|
|
}
|
|
|
|
if e.IncludeLowerLetters != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeLowerLetters, *e.IncludeLowerLetters))
|
|
|
|
}
|
|
|
|
if e.IncludeUpperLetters != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeUpperLetters, *e.IncludeUpperLetters))
|
|
|
|
}
|
|
|
|
if e.IncludeDigits != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeDigits, *e.IncludeDigits))
|
|
|
|
}
|
|
|
|
if e.IncludeSymbols != nil {
|
|
|
|
columns = append(columns, handler.NewCol(SecretGeneratorColumnIncludeSymbols, *e.IncludeSymbols))
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewUpdateStatement(
|
2022-02-16 15:49:17 +00:00
|
|
|
e,
|
|
|
|
columns,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2022-06-14 05:51:00 +00:00
|
|
|
func (p *secretGeneratorProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e, ok := event.(*instance.SecretGeneratorRemovedEvent)
|
2022-02-16 15:49:17 +00:00
|
|
|
if !ok {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-fmiIf", "reduce.wrong.event.type %s", instance.SecretGeneratorRemovedEventType)
|
2022-02-16 15:49:17 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return handler.NewDeleteStatement(
|
2022-02-16 15:49:17 +00:00
|
|
|
e,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(SecretGeneratorColumnAggregateID, e.Aggregate().ID),
|
|
|
|
handler.NewCond(SecretGeneratorColumnGeneratorType, e.GeneratorType),
|
2022-11-10 10:59:33 +00:00
|
|
|
handler.NewCond(SecretGeneratorColumnInstanceID, e.Aggregate().InstanceID),
|
2022-02-16 15:49:17 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
}
|