2021-01-12 11:59:51 +00:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-08-24 06:34:10 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
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
|
|
|
)
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewAddProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.UniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func NewRemoveProjectNameUniqueConstraint(projectName, resourceOwner string) *eventstore.UniqueConstraint {
|
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
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:"-"`
|
|
|
|
|
2021-08-24 06:34:10 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
ProjectRoleAssertion bool `json:"projectRoleAssertion,omitempty"`
|
|
|
|
ProjectRoleCheck bool `json:"projectRoleCheck,omitempty"`
|
|
|
|
HasProjectCheck bool `json:"hasProjectCheck,omitempty"`
|
|
|
|
PrivateLabelingSetting domain.PrivateLabelingSetting `json:"privateLabelingSetting,omitempty"`
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectAddedEvent) Payload() interface{} {
|
2021-01-12 11:59:51 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{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,
|
2021-08-18 08:49:04 +00:00
|
|
|
projectRoleAssertion,
|
|
|
|
projectRoleCheck,
|
|
|
|
hasProjectCheck bool,
|
2021-08-24 06:34:10 +00:00
|
|
|
privateLabelingSetting domain.PrivateLabelingSetting,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *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
|
|
|
),
|
2021-08-24 06:34:10 +00:00
|
|
|
Name: name,
|
|
|
|
ProjectRoleAssertion: projectRoleAssertion,
|
|
|
|
ProjectRoleCheck: projectRoleCheck,
|
|
|
|
HasProjectCheck: hasProjectCheck,
|
|
|
|
PrivateLabelingSetting: privateLabelingSetting,
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func ProjectAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
e := &ProjectAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-Bfg2f", "unable to unmarshal project")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-28 05:35:26 +00:00
|
|
|
|
|
|
|
type ProjectChangeEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-08-24 06:34:10 +00:00
|
|
|
Name *string `json:"name,omitempty"`
|
|
|
|
ProjectRoleAssertion *bool `json:"projectRoleAssertion,omitempty"`
|
|
|
|
ProjectRoleCheck *bool `json:"projectRoleCheck,omitempty"`
|
|
|
|
HasProjectCheck *bool `json:"hasProjectCheck,omitempty"`
|
|
|
|
PrivateLabelingSetting *domain.PrivateLabelingSetting `json:"privateLabelingSetting,omitempty"`
|
|
|
|
oldName string
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectChangeEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectChangeEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
if e.Name != nil {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "PROJECT-mV9xc", "Errors.NoChangesFound")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 08:49:04 +00:00
|
|
|
func ChangeHasProjectCheck(ChangeHasProjectCheck bool) func(event *ProjectChangeEvent) {
|
|
|
|
return func(e *ProjectChangeEvent) {
|
|
|
|
e.HasProjectCheck = &ChangeHasProjectCheck
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:34:10 +00:00
|
|
|
func ChangePrivateLabelingSetting(ChangePrivateLabelingSetting domain.PrivateLabelingSetting) func(event *ProjectChangeEvent) {
|
|
|
|
return func(e *ProjectChangeEvent) {
|
|
|
|
e.PrivateLabelingSetting = &ChangePrivateLabelingSetting
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func ProjectChangeEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
e := &ProjectChangeEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 05:35:26 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "PROJECT-M9osd", "unable to unmarshal project")
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectDeactivatedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-28 05:35:26 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func ProjectDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectReactivatedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectReactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-28 05:35:26 +00:00
|
|
|
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,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func ProjectReactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProjectRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2022-09-12 16:18:08 +00:00
|
|
|
Name string
|
2023-10-19 10:19:10 +00:00
|
|
|
entityIDUniqueContraints []*eventstore.UniqueConstraint
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectRemovedEvent) Payload() interface{} {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *ProjectRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
constraints := []*eventstore.UniqueConstraint{NewRemoveProjectNameUniqueConstraint(e.Name, e.Aggregate().ResourceOwner)}
|
2022-09-12 16:18:08 +00:00
|
|
|
if e.entityIDUniqueContraints != nil {
|
|
|
|
for _, constraint := range e.entityIDUniqueContraints {
|
|
|
|
constraints = append(constraints, constraint)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return constraints
|
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,
|
2023-10-19 10:19:10 +00:00
|
|
|
entityIDUniqueContraints []*eventstore.UniqueConstraint,
|
2021-02-18 13:48:27 +00:00
|
|
|
) *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,
|
|
|
|
),
|
2022-09-12 16:18:08 +00:00
|
|
|
Name: name,
|
|
|
|
entityIDUniqueContraints: entityIDUniqueContraints,
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func ProjectRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 05:35:26 +00:00
|
|
|
return &ProjectRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|