mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
8203f2dad3
* start org * refactor(eventstore): filter in sql for querier * feat(eventstore): Aggregate precondition preconditions are checked right before insert. Insert is still transaction save * feat(eventstore): check preconditions in repository * test(eventstore): test precondition in models * test(eventstore): precondition-tests * start org * refactor(eventstore): filter in sql for querier * feat(eventstore): Aggregate precondition preconditions are checked right before insert. Insert is still transaction save * feat(admin): start implement org * feat(eventstore): check preconditions in repository * fix(eventstore): data as NULL if empty refactor(eventstore): naming in sequence methods * feat(admin): org command side * feat(management): start org-repo * feat(org): member * fix: replace ObjectRoot.ID with ObjectRoot.AggregateID * aggregateID * add remove,change member * refactor(org): namings * refactor(eventstore): querier as type * fix(precondition): rename validation from precondition to validation * test(eventstore): isErr func instead of wantErr bool * fix(tests): Data * fix(eventstore): correct check for existing events in push, simplify insert statement * fix(eventstore): aggregate id public * test(org): eventsourcing * test(org): eventstore * test(org): deactivate, reactivate, orgbyid * test(org): getMemberByIDs * tests * running tests * add config * add user repo to admin * thorw not found if no org found * iam setup * eventstore tests done * setup iam * lauft * iam eventstore * validate if user is already member of org * modules * delete unused file * iam member * add member validation test * iam member * return error if unable to validat member * generate org id once, set resourceowner of org * start iam repo * set resourceowner on unique aggregates * setup user const * better code * generate files * fix tests * Update internal/admin/repository/eventsourcing/repository.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * set ctx data Co-authored-by: adlerhurst <silvan.reusser@gmail.com> Co-authored-by: Livio Amstutz <livio.a@gmail.com>
202 lines
6.6 KiB
Go
202 lines
6.6 KiB
Go
package eventsourcing
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
"github.com/sony/sonyflake"
|
|
)
|
|
|
|
var idGenerator = sonyflake.NewSonyflake(sonyflake.Settings{})
|
|
|
|
func OrgByIDQuery(id string, latestSequence uint64) (*es_models.SearchQuery, error) {
|
|
if id == "" {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dke74", "id should be filled")
|
|
}
|
|
return OrgQuery(latestSequence).
|
|
AggregateIDFilter(id), nil
|
|
}
|
|
|
|
func OrgDomainUniqueQuery(domain string) *es_models.SearchQuery {
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(org_model.OrgDomainAggregate).
|
|
AggregateIDFilter(domain).
|
|
OrderDesc().
|
|
SetLimit(1)
|
|
}
|
|
|
|
func OrgNameUniqueQuery(name string) *es_models.SearchQuery {
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(org_model.OrgNameAggregate).
|
|
AggregateIDFilter(name).
|
|
OrderDesc().
|
|
SetLimit(1)
|
|
}
|
|
|
|
func OrgQuery(latestSequence uint64) *es_models.SearchQuery {
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(org_model.OrgAggregate).
|
|
LatestSequenceFilter(latestSequence)
|
|
}
|
|
|
|
func OrgAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, id string, sequence uint64) (*es_models.Aggregate, error) {
|
|
return aggCreator.NewAggregate(ctx, id, org_model.OrgAggregate, orgVersion, sequence)
|
|
}
|
|
|
|
func orgCreatedAggregates(ctx context.Context, aggCreator *es_models.AggregateCreator, org *Org) (_ []*es_models.Aggregate, err error) {
|
|
if org == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-kdie7", "org should not be nil")
|
|
}
|
|
|
|
domainAgrregate, err := uniqueDomainAggregate(ctx, aggCreator, org.AggregateID, org.Domain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
nameAggregate, err := uniqueNameAggregate(ctx, aggCreator, org.AggregateID, org.Name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
agg, err := aggCreator.NewAggregate(ctx, org.AggregateID, org_model.OrgAggregate, orgVersion, org.Sequence, es_models.OverwriteResourceOwner(org.AggregateID))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
agg, err = agg.AppendEvent(org_model.OrgAdded, org)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return []*es_models.Aggregate{
|
|
agg,
|
|
domainAgrregate,
|
|
nameAggregate,
|
|
}, nil
|
|
}
|
|
|
|
func OrgUpdateAggregates(ctx context.Context, aggCreator *es_models.AggregateCreator, existing *Org, updated *Org) ([]*es_models.Aggregate, error) {
|
|
if existing == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dk83d", "existing org must not be nil")
|
|
}
|
|
if updated == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-dhr74", "updated org must not be nil")
|
|
}
|
|
changes := existing.Changes(updated)
|
|
if len(changes) == 0 {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-E0hc5", "no changes")
|
|
}
|
|
|
|
aggregates := make([]*es_models.Aggregate, 0, 3)
|
|
|
|
if name, ok := changes["name"]; ok {
|
|
nameAggregate, err := uniqueNameAggregate(ctx, aggCreator, "", name.(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aggregates = append(aggregates, nameAggregate)
|
|
}
|
|
|
|
if name, ok := changes["domain"]; ok {
|
|
domainAggregate, err := uniqueDomainAggregate(ctx, aggCreator, "", name.(string))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aggregates = append(aggregates, domainAggregate)
|
|
}
|
|
|
|
orgAggregate, err := OrgAggregate(ctx, aggCreator, existing.AggregateID, existing.Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
orgAggregate, err = orgAggregate.AppendEvent(org_model.OrgChanged, changes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aggregates = append(aggregates, orgAggregate)
|
|
|
|
return aggregates, nil
|
|
}
|
|
|
|
func orgDeactivateAggregate(aggCreator *es_models.AggregateCreator, org *Org) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if org == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-R03z8", "existing org must not be nil")
|
|
}
|
|
if org.State == int32(org_model.ORGSTATE_INACTIVE) {
|
|
return nil, errors.ThrowInvalidArgument(nil, "EVENT-mcPH0", "org already inactive")
|
|
}
|
|
agg, err := OrgAggregate(ctx, aggCreator, org.AggregateID, org.Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return agg.AppendEvent(org_model.OrgDeactivated, nil)
|
|
}
|
|
}
|
|
|
|
func orgReactivateAggregate(aggCreator *es_models.AggregateCreator, org *Org) func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
return func(ctx context.Context) (*es_models.Aggregate, error) {
|
|
if org == nil {
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-cTHLd", "existing org must not be nil")
|
|
}
|
|
if org.State == int32(org_model.ORGSTATE_ACTIVE) {
|
|
return nil, errors.ThrowInvalidArgument(nil, "EVENT-pUSMs", "org already active")
|
|
}
|
|
agg, err := OrgAggregate(ctx, aggCreator, org.AggregateID, org.Sequence)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return agg.AppendEvent(org_model.OrgReactivated, nil)
|
|
}
|
|
}
|
|
|
|
func uniqueDomainAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, resourceOwner, domain string) (*es_models.Aggregate, error) {
|
|
aggregate, err := aggCreator.NewAggregate(ctx, domain, org_model.OrgDomainAggregate, orgVersion, 0)
|
|
if resourceOwner != "" {
|
|
aggregate, err = aggCreator.NewAggregate(ctx, domain, org_model.OrgDomainAggregate, orgVersion, 0, es_models.OverwriteResourceOwner(resourceOwner))
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aggregate, err = aggregate.AppendEvent(org_model.OrgDomainReserved, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return aggregate.SetPrecondition(OrgDomainUniqueQuery(domain), isReservedValidation(aggregate, org_model.OrgDomainReserved)), nil
|
|
}
|
|
|
|
func uniqueNameAggregate(ctx context.Context, aggCreator *es_models.AggregateCreator, resourceOwner, name string) (aggregate *es_models.Aggregate, err error) {
|
|
aggregate, err = aggCreator.NewAggregate(ctx, name, org_model.OrgNameAggregate, orgVersion, 0)
|
|
if resourceOwner != "" {
|
|
aggregate, err = aggCreator.NewAggregate(ctx, name, org_model.OrgNameAggregate, orgVersion, 0, es_models.OverwriteResourceOwner(resourceOwner))
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
aggregate, err = aggregate.AppendEvent(org_model.OrgNameReserved, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return aggregate.SetPrecondition(OrgNameUniqueQuery(name), isReservedValidation(aggregate, org_model.OrgNameReserved)), nil
|
|
}
|
|
|
|
func isReservedValidation(aggregate *es_models.Aggregate, resevedEventType es_models.EventType) func(...*es_models.Event) error {
|
|
return func(events ...*es_models.Event) error {
|
|
if len(events) == 0 {
|
|
aggregate.PreviousSequence = 0
|
|
return nil
|
|
}
|
|
if events[0].Type == resevedEventType {
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-eJQqe", "org already reseved")
|
|
}
|
|
aggregate.PreviousSequence = events[0].Sequence
|
|
return nil
|
|
}
|
|
}
|