2021-01-12 11:59:51 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-12 11:59:51 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueProjectnameType = "project_names"
|
2021-01-12 11:59:51 +00:00
|
|
|
projectEventTypePrefix = eventstore.EventType("project.")
|
2021-01-22 12:31:52 +00:00
|
|
|
ProjectAddedType = projectEventTypePrefix + "added"
|
2021-01-28 05:35:26 +00:00
|
|
|
ProjectChangedType = projectEventTypePrefix + "changed"
|
|
|
|
ProjectDeactivatedType = projectEventTypePrefix + "deactivated"
|
|
|
|
ProjectReactivatedType = projectEventTypePrefix + "reactivated"
|
|
|
|
ProjectRemovedType = projectEventTypePrefix + "removed"
|
2021-01-12 11:59:51 +00:00
|
|
|
)
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func NewAddProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueProjectnameType,
|
2021-01-21 09:49:38 +00:00
|
|
|
projectName+resourceOwner,
|
|
|
|
"Errors.Project.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.EventUniqueConstraint {
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueProjectnameType,
|
2021-01-21 09:49:38 +00:00
|
|
|
projectName+resourceOwner)
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:59:51 +00:00
|
|
|
type ProjectAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ProjectRoleAssertion bool `json:"projectRoleAssertion,omitempty"`
|
|
|
|
ProjectRoleCheck bool `json:"projectRoleCheck,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectAddedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *ProjectAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 13:48:27 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddProjectNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewProjectAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
name string,
|
|
|
|
) *ProjectAddedEvent {
|
2021-01-12 11:59:51 +00:00
|
|
|
return &ProjectAddedEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2021-01-12 11:59:51 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-22 12:31:52 +00:00
|
|
|
ProjectAddedType,
|
2021-01-12 11:59:51 +00:00
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ProjectAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-Bfg2f", "unable to unmarshal project")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
type ProjectChangeEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
ProjectRoleAssertion *bool `json:"projectRoleAssertion,omitempty"`
|
|
|
|
ProjectRoleCheck *bool `json:"projectRoleCheck,omitempty"`
|
2021-02-15 12:31:24 +00:00
|
|
|
oldName string
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectChangeEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectChangeEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 12:31:24 +00:00
|
|
|
if e.oldName != "" {
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-02-18 13:48:27 +00:00
|
|
|
NewRemoveProjectNameUniqueConstraint(e.oldName, e.Aggregate().ResourceOwner),
|
|
|
|
NewAddProjectNameUniqueConstraint(*e.Name, e.Aggregate().ResourceOwner),
|
2021-02-15 12:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectChangeEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
oldName string,
|
|
|
|
changes []ProjectChanges,
|
|
|
|
) (*ProjectChangeEvent, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "PROJECT-mV9xc", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &ProjectChangeEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2021-01-28 05:35:26 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ProjectChangedType,
|
|
|
|
),
|
2021-02-18 13:48:27 +00:00
|
|
|
oldName: oldName,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
for _, change := range changes {
|
|
|
|
change(changeEvent)
|
|
|
|
}
|
|
|
|
return changeEvent, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectChanges func(event *ProjectChangeEvent)
|
|
|
|
|
|
|
|
func ChangeName(name string) func(event *ProjectChangeEvent) {
|
|
|
|
return func(e *ProjectChangeEvent) {
|
|
|
|
e.Name = &name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeProjectRoleAssertion(projectRoleAssertion bool) func(event *ProjectChangeEvent) {
|
|
|
|
return func(e *ProjectChangeEvent) {
|
|
|
|
e.ProjectRoleAssertion = &projectRoleAssertion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeProjectRoleCheck(projectRoleCheck bool) func(event *ProjectChangeEvent) {
|
|
|
|
return func(e *ProjectChangeEvent) {
|
|
|
|
e.ProjectRoleCheck = &projectRoleCheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectChangeEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &ProjectChangeEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
|
|
|
err := json.Unmarshal(event.Data, e)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "PROJECT-M9osd", "unable to unmarshal project")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectDeactivatedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewProjectDeactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *ProjectDeactivatedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ProjectDeactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &ProjectDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectReactivatedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewProjectReactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *ProjectReactivatedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ProjectReactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &ProjectReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectRemovedEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ProjectRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 13:48:27 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveProjectNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewProjectRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
name string,
|
|
|
|
) *ProjectRemovedEvent {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectRemovedEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2021-01-28 05:35:26 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2021-01-28 05:35:26 +00:00
|
|
|
ProjectRemovedType,
|
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &ProjectRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|