mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 11:04:25 +00:00
c740ee5d81
* fix: add events for domain * fix: add/remove domain command side * fix: add/remove domain command side * fix: add/remove domain query side * fix: create instance * fix: merge v2 * fix: instance domain * fix: instance domain * fix: instance domain * fix: instance domain * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from writemodels * fix: remove domain.IAMID from api * fix: remove domain.IAMID * fix: remove domain.IAMID * fix: add instance domain queries * fix: fix after merge * Update auth_request.go * fix keypair * remove unused code * feat: read instance id from context * feat: remove unused code * feat: use instance id from context * some fixes Co-authored-by: Livio Amstutz <livio.a@gmail.com>
142 lines
4.2 KiB
Go
142 lines
4.2 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
|
"github.com/caos/zitadel/internal/domain"
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
|
)
|
|
|
|
type InstanceSecretGeneratorConfigWriteModel struct {
|
|
eventstore.WriteModel
|
|
|
|
GeneratorType domain.SecretGeneratorType
|
|
Length uint
|
|
Expiry time.Duration
|
|
IncludeLowerLetters bool
|
|
IncludeUpperLetters bool
|
|
IncludeDigits bool
|
|
IncludeSymbols bool
|
|
State domain.SecretGeneratorState
|
|
}
|
|
|
|
func NewInstanceSecretGeneratorConfigWriteModel(ctx context.Context, GeneratorType domain.SecretGeneratorType) *InstanceSecretGeneratorConfigWriteModel {
|
|
return &InstanceSecretGeneratorConfigWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
|
},
|
|
GeneratorType: GeneratorType,
|
|
}
|
|
}
|
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) Reduce() error {
|
|
for _, event := range wm.Events {
|
|
switch e := event.(type) {
|
|
case *instance.SecretGeneratorAddedEvent:
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
continue
|
|
}
|
|
wm.Length = e.Length
|
|
wm.Expiry = e.Expiry
|
|
wm.IncludeLowerLetters = e.IncludeLowerLetters
|
|
wm.IncludeUpperLetters = e.IncludeUpperLetters
|
|
wm.IncludeDigits = e.IncludeDigits
|
|
wm.IncludeSymbols = e.IncludeDigits
|
|
wm.State = domain.SecretGeneratorStateActive
|
|
case *instance.SecretGeneratorChangedEvent:
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
continue
|
|
}
|
|
if e.Length != nil {
|
|
wm.Length = *e.Length
|
|
}
|
|
if e.Expiry != nil {
|
|
wm.Expiry = *e.Expiry
|
|
}
|
|
if e.IncludeUpperLetters != nil {
|
|
wm.IncludeUpperLetters = *e.IncludeUpperLetters
|
|
}
|
|
if e.IncludeLowerLetters != nil {
|
|
wm.IncludeLowerLetters = *e.IncludeLowerLetters
|
|
}
|
|
if e.IncludeDigits != nil {
|
|
wm.IncludeDigits = *e.IncludeDigits
|
|
}
|
|
if e.IncludeSymbols != nil {
|
|
wm.IncludeSymbols = *e.IncludeSymbols
|
|
}
|
|
case *instance.SecretGeneratorRemovedEvent:
|
|
if wm.GeneratorType != e.GeneratorType {
|
|
continue
|
|
}
|
|
wm.State = domain.SecretGeneratorStateRemoved
|
|
wm.Length = 0
|
|
wm.Expiry = 0
|
|
wm.IncludeLowerLetters = false
|
|
wm.IncludeUpperLetters = false
|
|
wm.IncludeDigits = false
|
|
wm.IncludeSymbols = false
|
|
}
|
|
}
|
|
return wm.WriteModel.Reduce()
|
|
}
|
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
ResourceOwner(wm.ResourceOwner).
|
|
AddQuery().
|
|
AggregateTypes(instance.AggregateType).
|
|
AggregateIDs(wm.AggregateID).
|
|
EventTypes(
|
|
instance.SecretGeneratorAddedEventType,
|
|
instance.SecretGeneratorChangedEventType,
|
|
instance.SecretGeneratorRemovedEventType).
|
|
Builder()
|
|
}
|
|
|
|
func (wm *InstanceSecretGeneratorConfigWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
generatorType domain.SecretGeneratorType,
|
|
length uint,
|
|
expiry time.Duration,
|
|
includeLowerLetters,
|
|
includeUpperLetters,
|
|
includeDigits,
|
|
includeSymbols bool,
|
|
) (*instance.SecretGeneratorChangedEvent, bool, error) {
|
|
changes := make([]instance.SecretGeneratorChanges, 0)
|
|
var err error
|
|
|
|
if wm.Length != length {
|
|
changes = append(changes, instance.ChangeSecretGeneratorLength(length))
|
|
}
|
|
if wm.Expiry != expiry {
|
|
changes = append(changes, instance.ChangeSecretGeneratorExpiry(expiry))
|
|
}
|
|
if wm.IncludeLowerLetters != includeLowerLetters {
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeLowerLetters(includeLowerLetters))
|
|
}
|
|
if wm.IncludeUpperLetters != includeUpperLetters {
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeUpperLetters(includeUpperLetters))
|
|
}
|
|
if wm.IncludeDigits != includeDigits {
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeDigits(includeDigits))
|
|
}
|
|
if wm.IncludeSymbols != includeSymbols {
|
|
changes = append(changes, instance.ChangeSecretGeneratorIncludeSymbols(includeSymbols))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil, false, nil
|
|
}
|
|
changeEvent, err := instance.NewSecretGeneratorChangeEvent(ctx, aggregate, generatorType, changes)
|
|
if err != nil {
|
|
return nil, false, err
|
|
}
|
|
return changeEvent, true, nil
|
|
}
|