2020-04-06 06:42:21 +02:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-07-08 13:56:37 +02:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-04-06 06:42:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AggregateCreator struct {
|
|
|
|
serviceName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAggregateCreator(serviceName string) *AggregateCreator {
|
|
|
|
return &AggregateCreator{serviceName: serviceName}
|
|
|
|
}
|
|
|
|
|
|
|
|
type option func(*Aggregate)
|
|
|
|
|
2020-04-28 16:01:00 +02:00
|
|
|
func (c *AggregateCreator) NewAggregate(ctx context.Context, id string, typ AggregateType, version Version, previousSequence uint64, opts ...option) (*Aggregate, error) {
|
2020-07-08 13:56:37 +02:00
|
|
|
ctxData := authz.GetCtxData(ctx)
|
2020-04-06 06:42:21 +02:00
|
|
|
editorUser := ctxData.UserID
|
|
|
|
resourceOwner := ctxData.OrgID
|
|
|
|
|
|
|
|
aggregate := &Aggregate{
|
2020-05-13 14:22:29 +02:00
|
|
|
ID: id,
|
2020-04-28 16:01:00 +02:00
|
|
|
typ: typ,
|
|
|
|
PreviousSequence: previousSequence,
|
|
|
|
version: version,
|
|
|
|
Events: make([]*Event, 0, 2),
|
|
|
|
editorService: c.serviceName,
|
|
|
|
editorUser: editorUser,
|
|
|
|
resourceOwner: resourceOwner,
|
2020-04-06 06:42:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(aggregate)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := aggregate.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return aggregate, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func OverwriteEditorUser(userID string) func(*Aggregate) {
|
|
|
|
return func(a *Aggregate) {
|
|
|
|
a.editorUser = userID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func OverwriteResourceOwner(resourceOwner string) func(*Aggregate) {
|
|
|
|
return func(a *Aggregate) {
|
|
|
|
a.resourceOwner = resourceOwner
|
|
|
|
}
|
|
|
|
}
|