mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 19:44:21 +00:00
28bfe72930
* feat: uniqueness on events * fix: some tests * fix: add unique username * fix: nice error message * fix: add unique test * fix: add unique test * fix: add unique constraint to events * fix: correct unique constraint on user events * fix: remove user constraint * fix: add unique constraints * fix: add unique constraints * fix: add unique constraints * fix: unnique constraints without interface * fix: unique idp config * fix: unique constraint comments * fix: unique constraints in one table * fix: unique constraints error * fix: fix unique constraint on create user * fix: fix unique constraint on create project * fix: fix unique constraint on idp configs
177 lines
4.2 KiB
Go
177 lines
4.2 KiB
Go
package org
|
|
|
|
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 (
|
|
uniqueOrgname = "org_name"
|
|
OrgAddedEventType = orgEventTypePrefix + "added"
|
|
OrgChangedEventType = orgEventTypePrefix + "changed"
|
|
OrgDeactivatedEventType = orgEventTypePrefix + "deactivated"
|
|
OrgReactivatedEventType = orgEventTypePrefix + "reactivated"
|
|
OrgRemovedEventType = orgEventTypePrefix + "removed"
|
|
)
|
|
|
|
type OrgnameUniqueConstraint struct {
|
|
uniqueType string
|
|
orgName string
|
|
action eventstore.UniqueConstraintAction
|
|
}
|
|
|
|
func NewAddOrgnameUniqueConstraint(orgName string) *eventstore.EventUniqueConstraint {
|
|
return eventstore.NewAddEventUniqueConstraint(
|
|
uniqueOrgname,
|
|
orgName,
|
|
"Errors.Org.AlreadyExists")
|
|
}
|
|
|
|
func NewRemoveUsernameUniqueConstraint(orgName string) *eventstore.EventUniqueConstraint {
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
|
uniqueOrgname,
|
|
orgName)
|
|
}
|
|
|
|
type OrgAddedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
}
|
|
|
|
func (e *OrgAddedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *OrgAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return []*eventstore.EventUniqueConstraint{NewAddOrgnameUniqueConstraint(e.Name)}
|
|
}
|
|
|
|
func NewOrgAddedEvent(ctx context.Context, name string) *OrgAddedEvent {
|
|
return &OrgAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
OrgAddedEventType,
|
|
),
|
|
Name: name,
|
|
}
|
|
}
|
|
|
|
func OrgAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
orgAdded := &OrgAddedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := json.Unmarshal(event.Data, orgAdded)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
|
|
}
|
|
|
|
return orgAdded, nil
|
|
}
|
|
|
|
type OrgChangedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
}
|
|
|
|
func (e *OrgChangedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *OrgChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewOrgChangedEvent(ctx context.Context, name string) *OrgChangedEvent {
|
|
return &OrgChangedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
OrgChangedEventType,
|
|
),
|
|
Name: name,
|
|
}
|
|
}
|
|
|
|
func OrgChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
orgChanged := &OrgChangedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := json.Unmarshal(event.Data, orgChanged)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
|
|
}
|
|
|
|
return orgChanged, nil
|
|
}
|
|
|
|
type OrgDeactivatedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
}
|
|
|
|
func (e *OrgDeactivatedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *OrgDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewOrgDeactivatedEvent(ctx context.Context) *OrgDeactivatedEvent {
|
|
return &OrgDeactivatedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
OrgDeactivatedEventType,
|
|
),
|
|
}
|
|
}
|
|
|
|
func OrgDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
orgChanged := &OrgDeactivatedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := json.Unmarshal(event.Data, orgChanged)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "ORG-DAfbs", "unable to unmarshal org deactivated")
|
|
}
|
|
|
|
return orgChanged, nil
|
|
}
|
|
|
|
type OrgReactivatedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
}
|
|
|
|
func (e *OrgReactivatedEvent) Data() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *OrgReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewOrgReactivatedEvent(ctx context.Context) *OrgReactivatedEvent {
|
|
return &OrgReactivatedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
OrgReactivatedEventType,
|
|
),
|
|
}
|
|
}
|
|
|
|
func OrgReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
orgChanged := &OrgReactivatedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := json.Unmarshal(event.Data, orgChanged)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "ORG-DAfbs", "unable to unmarshal org deactivated")
|
|
}
|
|
|
|
return orgChanged, nil
|
|
}
|