Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

224 lines
5.9 KiB
Go

package org
import (
"context"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/repository/project"
"github.com/zitadel/zitadel/internal/repository/user"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
uniqueOrgname = "org_name"
OrgAddedEventType = orgEventTypePrefix + "added"
OrgChangedEventType = orgEventTypePrefix + "changed"
OrgDeactivatedEventType = orgEventTypePrefix + "deactivated"
OrgReactivatedEventType = orgEventTypePrefix + "reactivated"
OrgRemovedEventType = orgEventTypePrefix + "removed"
)
func NewAddOrgNameUniqueConstraint(orgName string) *eventstore.UniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
uniqueOrgname,
orgName,
"Errors.Org.AlreadyExists")
}
func NewRemoveOrgNameUniqueConstraint(orgName string) *eventstore.UniqueConstraint {
return eventstore.NewRemoveUniqueConstraint(
uniqueOrgname,
orgName)
}
type OrgAddedEvent struct {
eventstore.BaseEvent `json:"-"`
Name string `json:"name,omitempty"`
}
func (e *OrgAddedEvent) Payload() interface{} {
return e
}
func (e *OrgAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return []*eventstore.UniqueConstraint{NewAddOrgNameUniqueConstraint(e.Name)}
}
func NewOrgAddedEvent(ctx context.Context, aggregate *eventstore.Aggregate, name string) *OrgAddedEvent {
return &OrgAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
OrgAddedEventType,
),
Name: name,
}
}
func OrgAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
orgAdded := &OrgAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(orgAdded)
if err != nil {
return nil, zerrors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
}
return orgAdded, nil
}
type OrgChangedEvent struct {
eventstore.BaseEvent `json:"-"`
Name string `json:"name,omitempty"`
oldName string `json:"-"`
}
func (e *OrgChangedEvent) Payload() interface{} {
return e
}
func (e *OrgChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return []*eventstore.UniqueConstraint{
NewRemoveOrgNameUniqueConstraint(e.oldName),
NewAddOrgNameUniqueConstraint(e.Name),
}
}
func NewOrgChangedEvent(ctx context.Context, aggregate *eventstore.Aggregate, oldName, newName string) *OrgChangedEvent {
return &OrgChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
OrgChangedEventType,
),
Name: newName,
oldName: oldName,
}
}
func OrgChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
orgChanged := &OrgChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(orgChanged)
if err != nil {
return nil, zerrors.ThrowInternal(err, "ORG-Bren2", "unable to unmarshal org added")
}
return orgChanged, nil
}
type OrgDeactivatedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *OrgDeactivatedEvent) Payload() interface{} {
return e
}
func (e *OrgDeactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewOrgDeactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *OrgDeactivatedEvent {
return &OrgDeactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
OrgDeactivatedEventType,
),
}
}
func OrgDeactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &OrgDeactivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type OrgReactivatedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *OrgReactivatedEvent) Payload() interface{} {
return e
}
func (e *OrgReactivatedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func NewOrgReactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *OrgReactivatedEvent {
return &OrgReactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
OrgReactivatedEventType,
),
}
}
func OrgReactivatedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &OrgReactivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type OrgRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
name string
usernames []string
loginMustBeDomain bool
domains []string
externalIDPs []*domain.UserIDPLink
samlEntityIDs []string
}
func (e *OrgRemovedEvent) Payload() interface{} {
return nil
}
func (e *OrgRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
constraints := []*eventstore.UniqueConstraint{
NewRemoveOrgNameUniqueConstraint(e.name),
}
for _, name := range e.usernames {
constraints = append(constraints, user.NewRemoveUsernameUniqueConstraint(name, e.Aggregate().ID, e.loginMustBeDomain))
}
for _, domain := range e.domains {
constraints = append(constraints, NewRemoveOrgDomainUniqueConstraint(domain))
}
for _, idp := range e.externalIDPs {
constraints = append(constraints, user.NewRemoveUserIDPLinkUniqueConstraint(idp.IDPConfigID, idp.ExternalUserID))
}
for _, entityID := range e.samlEntityIDs {
constraints = append(constraints, project.NewRemoveSAMLConfigEntityIDUniqueConstraint(entityID))
}
return constraints
}
func NewOrgRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate, name string, usernames []string, loginMustBeDomain bool, domains []string, externalIDPs []*domain.UserIDPLink, samlEntityIDs []string) *OrgRemovedEvent {
return &OrgRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
OrgRemovedEventType,
),
name: name,
usernames: usernames,
domains: domains,
externalIDPs: externalIDPs,
samlEntityIDs: samlEntityIDs,
loginMustBeDomain: loginMustBeDomain,
}
}
func OrgRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
return &OrgRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}