2021-01-12 12:59:51 +01:00
|
|
|
package project
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-28 06:35:26 +01:00
|
|
|
"fmt"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2021-01-12 12:59:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueAppNameType = "appname"
|
2021-01-12 12:59:51 +01:00
|
|
|
applicationEventTypePrefix = projectEventTypePrefix + "application."
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationAddedType = applicationEventTypePrefix + "added"
|
|
|
|
ApplicationChangedType = applicationEventTypePrefix + "changed"
|
|
|
|
ApplicationDeactivatedType = applicationEventTypePrefix + "deactivated"
|
|
|
|
ApplicationReactivatedType = applicationEventTypePrefix + "reactivated"
|
|
|
|
ApplicationRemovedType = applicationEventTypePrefix + "removed"
|
2021-01-12 12:59:51 +01:00
|
|
|
)
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewAddApplicationUniqueConstraint(name, projectID string) *eventstore.UniqueConstraint {
|
2021-01-28 06:35:26 +01:00
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueAppNameType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s", name, projectID),
|
|
|
|
"Errors.Project.App.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func NewRemoveApplicationUniqueConstraint(name, projectID string) *eventstore.UniqueConstraint {
|
|
|
|
return eventstore.NewRemoveUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueAppNameType,
|
2021-01-28 06:35:26 +01:00
|
|
|
fmt.Sprintf("%s:%s", name, projectID))
|
|
|
|
}
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
type ApplicationAddedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 12:51:15 +01:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
2021-01-12 12:59:51 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationAddedEvent) Payload() interface{} {
|
2021-01-12 12:59:51 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{NewAddApplicationUniqueConstraint(e.Name, e.Aggregate().ID)}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewApplicationAddedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
2021-03-15 12:51:15 +01:00
|
|
|
name string,
|
2021-02-18 14:48:27 +01:00
|
|
|
) *ApplicationAddedEvent {
|
2021-01-12 12:59:51 +01:00
|
|
|
return &ApplicationAddedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationAddedType,
|
2021-01-12 12:59:51 +01:00
|
|
|
),
|
2021-03-15 12:51:15 +01:00
|
|
|
AppID: appID,
|
|
|
|
Name: name,
|
2021-01-12 12:59:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func ApplicationAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
e := &ApplicationAddedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-12 12:59:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-Nffg2", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-28 06:35:26 +01:00
|
|
|
|
|
|
|
type ApplicationChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-15 12:51:15 +01:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
oldName string
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationChangedEvent) Payload() interface{} {
|
2021-01-28 06:35:26 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
return []*eventstore.UniqueConstraint{
|
2021-03-15 12:51:15 +01:00
|
|
|
NewRemoveApplicationUniqueConstraint(e.oldName, e.Aggregate().ID),
|
|
|
|
NewAddApplicationUniqueConstraint(e.Name, e.Aggregate().ID),
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewApplicationChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
|
|
|
oldName,
|
2021-03-15 12:51:15 +01:00
|
|
|
newName string,
|
2021-02-18 14:48:27 +01:00
|
|
|
) *ApplicationChangedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &ApplicationChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationChangedType,
|
|
|
|
),
|
2021-03-15 12:51:15 +01:00
|
|
|
AppID: appID,
|
|
|
|
Name: newName,
|
|
|
|
oldName: oldName,
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func ApplicationChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &ApplicationChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-9l0cs", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationDeactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationDeactivatedEvent) Payload() interface{} {
|
2021-01-28 06:35:26 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-28 06:35:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewApplicationDeactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID string,
|
|
|
|
) *ApplicationDeactivatedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &ApplicationDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationDeactivatedType,
|
|
|
|
),
|
|
|
|
AppID: appID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func ApplicationDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &ApplicationDeactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-0p9fB", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationReactivatedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationReactivatedEvent) Payload() interface{} {
|
2021-01-28 06:35:26 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationReactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-28 06:35:26 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewApplicationReactivatedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID string,
|
|
|
|
) *ApplicationReactivatedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &ApplicationReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationReactivatedType,
|
|
|
|
),
|
|
|
|
AppID: appID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func ApplicationReactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &ApplicationReactivatedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-1m9e3", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ApplicationRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2022-09-12 17:18:08 +01:00
|
|
|
AppID string `json:"appId,omitempty"`
|
|
|
|
name string
|
|
|
|
entityID string
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationRemovedEvent) Payload() interface{} {
|
2021-01-28 06:35:26 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *ApplicationRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
|
|
remove := []*eventstore.UniqueConstraint{NewRemoveApplicationUniqueConstraint(e.name, e.Aggregate().ID)}
|
2022-09-12 17:18:08 +01:00
|
|
|
if e.entityID != "" {
|
|
|
|
remove = append(remove, NewRemoveSAMLConfigEntityIDUniqueConstraint(e.entityID))
|
|
|
|
}
|
|
|
|
return remove
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewApplicationRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
appID,
|
2021-03-15 12:51:15 +01:00
|
|
|
name string,
|
2022-09-12 17:18:08 +01:00
|
|
|
entityID string,
|
2021-02-18 14:48:27 +01:00
|
|
|
) *ApplicationRemovedEvent {
|
2021-01-28 06:35:26 +01:00
|
|
|
return &ApplicationRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2021-01-28 06:35:26 +01:00
|
|
|
ApplicationRemovedType,
|
|
|
|
),
|
2022-09-12 17:18:08 +01:00
|
|
|
AppID: appID,
|
|
|
|
name: name,
|
|
|
|
entityID: entityID,
|
2021-01-28 06:35:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func ApplicationRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-28 06:35:26 +01:00
|
|
|
e := &ApplicationRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2021-01-28 06:35:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "APPLICATION-1m9e3", "unable to unmarshal application")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|