2021-01-12 11:59:51 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2/repository"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return []*eventstore.EventUniqueConstraint{NewAddProjectNameUniqueConstraint(e.Name, e.ResourceOwner())}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectAddedEvent(ctx context.Context, name, resourceOwner string) *ProjectAddedEvent {
|
2021-01-12 11:59:51 +00:00
|
|
|
return &ProjectAddedEvent{
|
2021-01-21 09:49:38 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
|
2021-01-12 11:59:51 +00:00
|
|
|
ctx,
|
2021-01-22 12:31:52 +00:00
|
|
|
ProjectAddedType,
|
2021-01-21 09:49:38 +00:00
|
|
|
resourceOwner,
|
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{
|
|
|
|
NewRemoveProjectNameUniqueConstraint(e.oldName, e.ResourceOwner()),
|
|
|
|
NewAddProjectNameUniqueConstraint(*e.Name, e.ResourceOwner()),
|
|
|
|
}
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectChangeEvent(
|
|
|
|
ctx context.Context,
|
2021-02-15 12:31:24 +00:00
|
|
|
resourceOwner, oldName string,
|
2021-01-28 05:35:26 +00:00
|
|
|
changes []ProjectChanges) (*ProjectChangeEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "PROJECT-mV9xc", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &ProjectChangeEvent{
|
2021-02-15 12:31:24 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
|
2021-01-28 05:35:26 +00:00
|
|
|
ctx,
|
|
|
|
ProjectChangedType,
|
2021-02-15 12:31:24 +00:00
|
|
|
resourceOwner,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectDeactivatedEvent(ctx context.Context) *ProjectDeactivatedEvent {
|
|
|
|
return &ProjectDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectReactivatedEvent(ctx context.Context) *ProjectReactivatedEvent {
|
|
|
|
return &ProjectReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
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 {
|
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveProjectNameUniqueConstraint(e.Name, e.ResourceOwner())}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProjectRemovedEvent(ctx context.Context, name, resourceOwner string) *ProjectRemovedEvent {
|
|
|
|
return &ProjectRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
|
|
|
|
ctx,
|
|
|
|
ProjectRemovedType,
|
|
|
|
resourceOwner,
|
|
|
|
),
|
|
|
|
Name: name,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ProjectRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &ProjectRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|