mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 20:37:30 +00:00

# Which Problems Are Solved Currently the username uniqueness is on instance level, we want to achieve a way to set it at organization level. # How the Problems Are Solved Addition of endpoints and a resource on organization level, where this setting can be managed. If nothing it set, the uniqueness is expected to be at instance level, where only users with instance permissions should be able to change this setting. # Additional Changes None # Additional Context Includes #10086 Closes #9964 --------- Co-authored-by: Marco A. <marco@zitadel.com>
346 lines
9.2 KiB
Go
346 lines
9.2 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"
|
|
|
|
OrgSearchType = "org"
|
|
OrgNameSearchField = "name"
|
|
OrgStateSearchField = "state"
|
|
)
|
|
|
|
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 (e *OrgAddedEvent) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
orgSearchObject(e.Aggregate().ID),
|
|
OrgNameSearchField,
|
|
&eventstore.Value{
|
|
Value: e.Name,
|
|
ShouldIndex: true,
|
|
},
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
orgSearchObject(e.Aggregate().ID),
|
|
OrgStateSearchField,
|
|
&eventstore.Value{
|
|
Value: domain.OrgStateActive,
|
|
ShouldIndex: true,
|
|
},
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
}
|
|
}
|
|
|
|
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 (e *OrgChangedEvent) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
orgSearchObject(e.Aggregate().ID),
|
|
OrgNameSearchField,
|
|
&eventstore.Value{
|
|
Value: e.Name,
|
|
ShouldIndex: true,
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
}
|
|
}
|
|
|
|
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 (e *OrgDeactivatedEvent) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
orgSearchObject(e.Aggregate().ID),
|
|
OrgStateSearchField,
|
|
&eventstore.Value{
|
|
Value: domain.OrgStateInactive,
|
|
ShouldIndex: true,
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
}
|
|
}
|
|
|
|
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) Fields() []*eventstore.FieldOperation {
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.SetField(
|
|
e.Aggregate(),
|
|
orgSearchObject(e.Aggregate().ID),
|
|
OrgStateSearchField,
|
|
&eventstore.Value{
|
|
Value: domain.OrgStateActive,
|
|
ShouldIndex: true,
|
|
},
|
|
|
|
eventstore.FieldTypeInstanceID,
|
|
eventstore.FieldTypeResourceOwner,
|
|
eventstore.FieldTypeAggregateType,
|
|
eventstore.FieldTypeAggregateID,
|
|
eventstore.FieldTypeObjectType,
|
|
eventstore.FieldTypeObjectID,
|
|
eventstore.FieldTypeFieldName,
|
|
),
|
|
}
|
|
}
|
|
|
|
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
|
|
organizationScopedUsernames 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.organizationScopedUsernames))
|
|
}
|
|
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 (e *OrgRemovedEvent) Fields() []*eventstore.FieldOperation {
|
|
// TODO: project grants are currently not removed because we don't have the relationship between the granted org and the grant
|
|
return []*eventstore.FieldOperation{
|
|
eventstore.RemoveSearchFieldsByAggregate(e.Aggregate()),
|
|
}
|
|
}
|
|
|
|
func NewOrgRemovedEvent(ctx context.Context, aggregate *eventstore.Aggregate, name string, usernames []string, organizationScopedUsernames 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,
|
|
organizationScopedUsernames: organizationScopedUsernames,
|
|
}
|
|
}
|
|
|
|
func OrgRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
return &OrgRemovedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}, nil
|
|
}
|
|
|
|
func orgSearchObject(id string) eventstore.Object {
|
|
return eventstore.Object{
|
|
Type: OrgSearchType,
|
|
Revision: 1,
|
|
ID: id,
|
|
}
|
|
}
|