mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 23:07:32 +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>
136 lines
3.5 KiB
Go
136 lines
3.5 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
const (
|
|
machineEventPrefix = userEventTypePrefix + "machine."
|
|
MachineAddedEventType = machineEventPrefix + "added"
|
|
MachineChangedEventType = machineEventPrefix + "changed"
|
|
)
|
|
|
|
type MachineAddedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
UserName string `json:"userName"`
|
|
orgScopedUsername bool
|
|
|
|
Name string `json:"name,omitempty"`
|
|
Description string `json:"description,omitempty"`
|
|
AccessTokenType domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
|
}
|
|
|
|
func (e *MachineAddedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return []*eventstore.UniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.orgScopedUsername)}
|
|
}
|
|
|
|
func NewMachineAddedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
userName,
|
|
name,
|
|
description string,
|
|
orgScopedUsername bool,
|
|
accessTokenType domain.OIDCTokenType,
|
|
) *MachineAddedEvent {
|
|
return &MachineAddedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineAddedEventType,
|
|
),
|
|
UserName: userName,
|
|
Name: name,
|
|
Description: description,
|
|
orgScopedUsername: orgScopedUsername,
|
|
AccessTokenType: accessTokenType,
|
|
}
|
|
}
|
|
|
|
func MachineAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineAdded := &MachineAddedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineAdded)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "USER-tMv9s", "unable to unmarshal machine added")
|
|
}
|
|
|
|
return machineAdded, nil
|
|
}
|
|
|
|
type MachineChangedEvent struct {
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
Name *string `json:"name,omitempty"`
|
|
Description *string `json:"description,omitempty"`
|
|
AccessTokenType *domain.OIDCTokenType `json:"accessTokenType,omitempty"`
|
|
}
|
|
|
|
func (e *MachineChangedEvent) Payload() interface{} {
|
|
return e
|
|
}
|
|
|
|
func (e *MachineChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
|
return nil
|
|
}
|
|
|
|
func NewMachineChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
changes []MachineChanges,
|
|
) *MachineChangedEvent {
|
|
changeEvent := &MachineChangedEvent{
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
ctx,
|
|
aggregate,
|
|
MachineChangedEventType,
|
|
),
|
|
}
|
|
for _, change := range changes {
|
|
change(changeEvent)
|
|
}
|
|
return changeEvent
|
|
}
|
|
|
|
type MachineChanges func(event *MachineChangedEvent)
|
|
|
|
func ChangeName(name string) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.Name = &name
|
|
}
|
|
}
|
|
|
|
func ChangeDescription(description string) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.Description = &description
|
|
}
|
|
}
|
|
|
|
func ChangeAccessTokenType(accessTokenType domain.OIDCTokenType) func(event *MachineChangedEvent) {
|
|
return func(e *MachineChangedEvent) {
|
|
e.AccessTokenType = &accessTokenType
|
|
}
|
|
}
|
|
|
|
func MachineChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
|
machineChanged := &MachineChangedEvent{
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
}
|
|
err := event.Unmarshal(machineChanged)
|
|
if err != nil {
|
|
return nil, zerrors.ThrowInternal(err, "USER-4M9ds", "unable to unmarshal machine changed")
|
|
}
|
|
|
|
return machineChanged, nil
|
|
}
|