feat: uniqueness (#1190)

* 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
This commit is contained in:
Fabi
2021-01-21 10:49:38 +01:00
committed by GitHub
parent c2e6e782a8
commit 28bfe72930
62 changed files with 1046 additions and 313 deletions
+58 -20
View File
@@ -26,7 +26,8 @@ const (
type HumanAddedEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string `json:"userName"`
UserName string `json:"userName"`
userLoginMustBeDomain bool
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
@@ -53,6 +54,10 @@ func (e *HumanAddedEvent) Data() interface{} {
return e
}
func (e *HumanAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.ResourceOwner(), e.userLoginMustBeDomain)}
}
func (e *HumanAddedEvent) AddAddressData(
country,
locality,
@@ -83,6 +88,7 @@ func (e *HumanAddedEvent) AddPasswordData(
func NewHumanAddedEvent(
ctx context.Context,
resourceOwner,
userName,
firstName,
lastName,
@@ -91,20 +97,23 @@ func NewHumanAddedEvent(
preferredLanguage language.Tag,
gender domain.Gender,
emailAddress string,
userLoginMustBeDomain bool,
) *HumanAddedEvent {
return &HumanAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
ctx,
HumanAddedType,
resourceOwner,
),
UserName: userName,
FirstName: firstName,
LastName: lastName,
NickName: nickName,
DisplayName: displayName,
PreferredLanguage: preferredLanguage,
Gender: gender,
EmailAddress: emailAddress,
UserName: userName,
FirstName: firstName,
LastName: lastName,
NickName: nickName,
DisplayName: displayName,
PreferredLanguage: preferredLanguage,
Gender: gender,
EmailAddress: emailAddress,
userLoginMustBeDomain: userLoginMustBeDomain,
}
}
@@ -123,7 +132,8 @@ func HumanAddedEventMapper(event *repository.Event) (eventstore.EventReader, err
type HumanRegisteredEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string `json:"userName"`
UserName string `json:"userName"`
userLoginMustBeDomain bool
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
@@ -150,6 +160,10 @@ func (e *HumanRegisteredEvent) Data() interface{} {
return e
}
func (e *HumanRegisteredEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.ResourceOwner(), e.userLoginMustBeDomain)}
}
func (e *HumanRegisteredEvent) AddAddressData(
country,
locality,
@@ -180,6 +194,7 @@ func (e *HumanRegisteredEvent) AddPasswordData(
func NewHumanRegisteredEvent(
ctx context.Context,
resourceOwner,
userName,
firstName,
lastName,
@@ -188,20 +203,23 @@ func NewHumanRegisteredEvent(
preferredLanguage language.Tag,
gender domain.Gender,
emailAddress string,
userLoginMustBeDomain bool,
) *HumanRegisteredEvent {
return &HumanRegisteredEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
ctx,
HumanRegisteredType,
resourceOwner,
),
UserName: userName,
FirstName: firstName,
LastName: lastName,
NickName: nickName,
DisplayName: displayName,
PreferredLanguage: preferredLanguage,
Gender: gender,
EmailAddress: emailAddress,
UserName: userName,
FirstName: firstName,
LastName: lastName,
NickName: nickName,
DisplayName: displayName,
PreferredLanguage: preferredLanguage,
Gender: gender,
EmailAddress: emailAddress,
userLoginMustBeDomain: userLoginMustBeDomain,
}
}
@@ -227,6 +245,10 @@ func (e *HumanInitialCodeAddedEvent) Data() interface{} {
return e
}
func (e *HumanInitialCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanInitialCodeAddedEvent(
ctx context.Context,
code *crypto.CryptoValue,
@@ -262,6 +284,10 @@ func (e *HumanInitialCodeSentEvent) Data() interface{} {
return nil
}
func (e *HumanInitialCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanInitialCodeSentEvent(ctx context.Context) *HumanInitialCodeSentEvent {
return &HumanInitialCodeSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -285,6 +311,10 @@ func (e *HumanInitializedCheckSucceededEvent) Data() interface{} {
return nil
}
func (e *HumanInitializedCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanInitializedCheckSucceededEvent(ctx context.Context) *HumanInitializedCheckSucceededEvent {
return &HumanInitializedCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -308,6 +338,10 @@ func (e *HumanInitializedCheckFailedEvent) Data() interface{} {
return nil
}
func (e *HumanInitializedCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanInitializedCheckFailedEvent(ctx context.Context) *HumanInitializedCheckFailedEvent {
return &HumanInitializedCheckFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -331,6 +365,10 @@ func (e *HumanSignedOutEvent) Data() interface{} {
return nil
}
func (e *HumanSignedOutEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanSignedOutEvent(ctx context.Context) *HumanSignedOutEvent {
return &HumanSignedOutEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -27,6 +27,10 @@ func (e *HumanAddressChangedEvent) Data() interface{} {
return e
}
func (e *HumanAddressChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanAddressChangedEvent(ctx context.Context) *HumanAddressChangedEvent {
return &HumanAddressChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -29,6 +29,10 @@ func (e *HumanEmailChangedEvent) Data() interface{} {
return e
}
func (e *HumanEmailChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanEmailChangedEvent(ctx context.Context) *HumanEmailChangedEvent {
return &HumanEmailChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -60,6 +64,10 @@ func (e *HumanEmailVerifiedEvent) Data() interface{} {
return nil
}
func (e *HumanEmailVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanEmailVerifiedEvent(ctx context.Context) *HumanEmailVerifiedEvent {
return &HumanEmailVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -85,6 +93,10 @@ func (e *HumanEmailVerificationFailedEvent) Data() interface{} {
return nil
}
func (e *HumanEmailVerificationFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanEmailVerificationFailedEvent(ctx context.Context) *HumanEmailVerificationFailedEvent {
return &HumanEmailVerificationFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -111,6 +123,10 @@ func (e *HumanEmailCodeAddedEvent) Data() interface{} {
return e
}
func (e *HumanEmailCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanEmailCodeAddedEvent(
ctx context.Context,
code *crypto.CryptoValue,
@@ -145,6 +161,10 @@ func (e *HumanEmailCodeSentEvent) Data() interface{} {
return nil
}
func (e *HumanEmailCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanEmailCodeSentEvent(ctx context.Context) *HumanEmailCodeSentEvent {
return &HumanEmailCodeSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -9,6 +9,7 @@ import (
)
const (
uniqueExternalIDPType = "external_idps"
externalIDPEventPrefix = humanEventPrefix + "externalidp."
externalLoginEventPrefix = humanEventPrefix + "externallogin."
@@ -23,38 +24,17 @@ const (
HumanExternalLoginCheckSucceededType = externalLoginEventPrefix + "check.succeeded"
)
type HumanExternalIDPReservedEvent struct {
eventstore.BaseEvent `json:"-"`
func NewAddExternalIDPUniqueConstraint(idpConfigID, externalUserID string) *eventstore.EventUniqueConstraint {
return eventstore.NewAddEventUniqueConstraint(
uniqueExternalIDPType,
idpConfigID+externalUserID,
"Errors.User.ExternalIDP.AlreadyExists")
}
func (e *HumanExternalIDPReservedEvent) Data() interface{} {
return nil
}
func NewHumanExternalIDPReservedEvent(ctx context.Context) *HumanExternalIDPReservedEvent {
return &HumanExternalIDPReservedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanExternalIDPReservedType,
),
}
}
type HumanExternalIDPReleasedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *HumanExternalIDPReleasedEvent) Data() interface{} {
return nil
}
func NewHumanExternalIDPReleasedEvent(ctx context.Context) *HumanExternalIDPReleasedEvent {
return &HumanExternalIDPReleasedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
HumanExternalIDPReleasedType,
),
}
func NewRemoveExternalIDPUniqueConstraint(idpConfigID, externalUserID string) *eventstore.EventUniqueConstraint {
return eventstore.NewRemoveEventUniqueConstraint(
uniqueExternalIDPType,
idpConfigID+externalUserID)
}
type HumanExternalIDPAddedEvent struct {
@@ -69,6 +49,10 @@ func (e *HumanExternalIDPAddedEvent) Data() interface{} {
return e
}
func (e *HumanExternalIDPAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewAddExternalIDPUniqueConstraint(e.IDPConfigID, e.UserID)}
}
func NewHumanExternalIDPAddedEvent(ctx context.Context, idpConfigID, displayName string) *HumanExternalIDPAddedEvent {
return &HumanExternalIDPAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -104,6 +88,10 @@ func (e *HumanExternalIDPRemovedEvent) Data() interface{} {
return e
}
func (e *HumanExternalIDPRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewRemoveExternalIDPUniqueConstraint(e.IDPConfigID, e.UserID)}
}
func NewHumanExternalIDPRemovedEvent(ctx context.Context, idpConfigID, externalUserID string) *HumanExternalIDPRemovedEvent {
return &HumanExternalIDPRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -139,6 +127,10 @@ func (e *HumanExternalIDPCascadeRemovedEvent) Data() interface{} {
return e
}
func (e *HumanExternalIDPCascadeRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewRemoveExternalIDPUniqueConstraint(e.IDPConfigID, e.UserID)}
}
func NewHumanExternalIDPCascadeRemovedEvent(ctx context.Context, idpConfigID, externalUserID string) *HumanExternalIDPCascadeRemovedEvent {
return &HumanExternalIDPCascadeRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -171,6 +163,10 @@ func (e *HumanExternalIDPCheckSucceededEvent) Data() interface{} {
return nil
}
func (e *HumanExternalIDPCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanExternalIDPCheckSucceededEvent(ctx context.Context) *HumanExternalIDPCheckSucceededEvent {
return &HumanExternalIDPCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -19,6 +19,10 @@ func (e *HumanMFAInitSkippedEvent) Data() interface{} {
return e
}
func (e *HumanMFAInitSkippedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanMFAInitSkippedEvent(ctx context.Context) *HumanMFAInitSkippedEvent {
return &HumanMFAInitSkippedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -28,6 +28,10 @@ func (e *HumanOTPAddedEvent) Data() interface{} {
return e
}
func (e *HumanOTPAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanOTPAddedEvent(ctx context.Context,
secret *crypto.CryptoValue) *HumanOTPAddedEvent {
return &HumanOTPAddedEvent{
@@ -59,6 +63,10 @@ func (e *HumanOTPVerifiedEvent) Data() interface{} {
return nil
}
func (e *HumanOTPVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanOTPVerifiedEvent(ctx context.Context, userAgentID string) *HumanOTPVerifiedEvent {
return &HumanOTPVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -83,6 +91,10 @@ func (e *HumanOTPRemovedEvent) Data() interface{} {
return nil
}
func (e *HumanOTPRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanOTPRemovedEvent(ctx context.Context) *HumanOTPRemovedEvent {
return &HumanOTPRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -106,6 +118,10 @@ func (e *HumanOTPCheckSucceededEvent) Data() interface{} {
return nil
}
func (e *HumanOTPCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanOTPCheckSucceededEvent(ctx context.Context) *HumanOTPCheckSucceededEvent {
return &HumanOTPCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -129,6 +145,10 @@ func (e *HumanOTPCheckFailedEvent) Data() interface{} {
return nil
}
func (e *HumanOTPCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanOTPCheckFailedEvent(ctx context.Context) *HumanOTPCheckFailedEvent {
return &HumanOTPCheckFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -40,6 +40,10 @@ func (e *HumanWebAuthNAddedEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FAddedEvent(
ctx context.Context,
webAuthNTokenID,
@@ -97,6 +101,10 @@ func (e *HumanWebAuthNVerifiedEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FVerifiedEvent(
ctx context.Context,
webAuthNTokenID,
@@ -169,6 +177,10 @@ func (e *HumanWebAuthNSignCountChangedEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNSignCountChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FSignCountChangedEvent(
ctx context.Context,
webAuthNTokenID string,
@@ -221,6 +233,10 @@ func (e *HumanWebAuthNRemovedEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FRemovedEvent(
ctx context.Context,
webAuthNTokenID string,
@@ -271,6 +287,10 @@ func (e *HumanWebAuthNBeginLoginEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNBeginLoginEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FBeginLoginEvent(
ctx context.Context,
webAuthNTokenID,
@@ -323,6 +343,10 @@ func (e *HumanWebAuthNCheckSucceededEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FCheckSucceededEvent(ctx context.Context) *HumanWebAuthNCheckSucceededEvent {
return &HumanWebAuthNCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -363,6 +387,10 @@ func (e *HumanWebAuthNCheckFailedEvent) Data() interface{} {
return e
}
func (e *HumanWebAuthNCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanU2FCheckFailedEvent(ctx context.Context) *HumanWebAuthNCheckFailedEvent {
return &HumanWebAuthNCheckFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -32,6 +32,10 @@ func (e *HumanPasswordChangedEvent) Data() interface{} {
return e
}
func (e *HumanPasswordChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPasswordChangedEvent(
ctx context.Context,
secret *crypto.CryptoValue,
@@ -73,6 +77,10 @@ func (e *HumanPasswordCodeAddedEvent) Data() interface{} {
return e
}
func (e *HumanPasswordCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPasswordCodeAddedEvent(
ctx context.Context,
code *crypto.CryptoValue,
@@ -110,6 +118,10 @@ func (e *HumanPasswordCodeSentEvent) Data() interface{} {
return nil
}
func (e *HumanPasswordCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPasswordCodeSentEvent(ctx context.Context) *HumanPasswordCodeSentEvent {
return &HumanPasswordCodeSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -133,6 +145,10 @@ func (e *HumanPasswordCheckSucceededEvent) Data() interface{} {
return nil
}
func (e *HumanPasswordCheckSucceededEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPasswordCheckSucceededEvent(ctx context.Context) *HumanPasswordCheckSucceededEvent {
return &HumanPasswordCheckSucceededEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -156,6 +172,10 @@ func (e *HumanPasswordCheckFailedEvent) Data() interface{} {
return nil
}
func (e *HumanPasswordCheckFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPasswordCheckFailedEvent(ctx context.Context) *HumanPasswordCheckFailedEvent {
return &HumanPasswordCheckFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -30,6 +30,10 @@ func (e *HumanPhoneChangedEvent) Data() interface{} {
return e
}
func (e *HumanPhoneChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneChangedEvent(ctx context.Context) *HumanPhoneChangedEvent {
return &HumanPhoneChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -59,6 +63,10 @@ func (e *HumanPhoneRemovedEvent) Data() interface{} {
return nil
}
func (e *HumanPhoneRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneRemovedEvent(ctx context.Context) *HumanPhoneRemovedEvent {
return &HumanPhoneRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -84,6 +92,10 @@ func (e *HumanPhoneVerifiedEvent) Data() interface{} {
return nil
}
func (e *HumanPhoneVerifiedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneVerifiedEvent(ctx context.Context) *HumanPhoneVerifiedEvent {
return &HumanPhoneVerifiedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -108,6 +120,10 @@ func (e *HumanPhoneVerificationFailedEvent) Data() interface{} {
return nil
}
func (e *HumanPhoneVerificationFailedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneVerificationFailedEvent(ctx context.Context) *HumanPhoneVerificationFailedEvent {
return &HumanPhoneVerificationFailedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -134,6 +150,10 @@ func (e *HumanPhoneCodeAddedEvent) Data() interface{} {
return e
}
func (e *HumanPhoneCodeAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneCodeAddedEvent(
ctx context.Context,
code *crypto.CryptoValue,
@@ -169,6 +189,10 @@ func (e *HumanPhoneCodeSentEvent) Data() interface{} {
return e
}
func (e *HumanPhoneCodeSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanPhoneCodeSentEvent(ctx context.Context) *HumanPhoneCodeSentEvent {
return &HumanPhoneCodeSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -30,6 +30,10 @@ func (e *HumanProfileChangedEvent) Data() interface{} {
return e
}
func (e *HumanProfileChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewHumanProfileChangedEvent(
ctx context.Context) *HumanProfileChangedEvent {
return &HumanProfileChangedEvent{
+15 -4
View File
@@ -17,7 +17,8 @@ const (
type MachineAddedEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string `json:"userName"`
UserName string `json:"userName"`
UserLoginMustBeDomain bool
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
@@ -27,20 +28,26 @@ func (e *MachineAddedEvent) Data() interface{} {
return e
}
func (e *MachineAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewAddUsernameUniqueConstraint(e.UserName, e.ResourceOwner(), e.UserLoginMustBeDomain)}
}
func NewMachineAddedEvent(
ctx context.Context,
userName,
name,
description string,
userLoginMustBeDomain bool,
) *MachineAddedEvent {
return &MachineAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
MachineAddedEventType,
),
UserName: userName,
Name: name,
Description: description,
UserName: userName,
Name: name,
Description: description,
UserLoginMustBeDomain: userLoginMustBeDomain,
}
}
@@ -69,6 +76,10 @@ func (e *MachineChangedEvent) Data() interface{} {
return e
}
func (e *MachineChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewMachineChangedEvent(
ctx context.Context,
) *MachineChangedEvent {
@@ -29,6 +29,10 @@ func (e *MachineKeyAddedEvent) Data() interface{} {
return e
}
func (e *MachineKeyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewMachineKeyAddedEvent(
ctx context.Context,
keyID string,
@@ -70,6 +74,10 @@ func (e *MachineKeyRemovedEvent) Data() interface{} {
return e
}
func (e *MachineKeyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewMachineKeyRemovedEvent(
ctx context.Context,
keyID string,
+85 -11
View File
@@ -10,6 +10,7 @@ import (
)
const (
uniqueUsername = "usernames"
userEventTypePrefix = eventstore.EventType("user.")
UserLockedType = userEventTypePrefix + "locked"
UserUnlockedType = userEventTypePrefix + "unlocked"
@@ -22,6 +23,27 @@ const (
UserUserNameChangedType = userEventTypePrefix + "username.changed"
)
func NewAddUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.EventUniqueConstraint {
uniqueUserName := userName
if userLoginMustBeDomain {
uniqueUserName = userName + resourceOwner
}
return eventstore.NewAddEventUniqueConstraint(
uniqueUsername,
uniqueUserName,
"Errors.User.AlreadyExists")
}
func NewRemoveUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.EventUniqueConstraint {
uniqueUserName := userName
if userLoginMustBeDomain {
uniqueUserName = userName + resourceOwner
}
return eventstore.NewRemoveEventUniqueConstraint(
uniqueUsername,
uniqueUserName)
}
type UserLockedEvent struct {
eventstore.BaseEvent `json:"-"`
}
@@ -30,6 +52,10 @@ func (e *UserLockedEvent) Data() interface{} {
return nil
}
func (e *UserLockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewUserLockedEvent(ctx context.Context) *UserLockedEvent {
return &UserLockedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -53,6 +79,10 @@ func (e *UserUnlockedEvent) Data() interface{} {
return nil
}
func (e *UserUnlockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewUserUnlockedEvent(ctx context.Context) *UserUnlockedEvent {
return &UserUnlockedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -76,6 +106,10 @@ func (e *UserDeactivatedEvent) Data() interface{} {
return nil
}
func (e *UserDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewUserDeactivatedEvent(ctx context.Context) *UserDeactivatedEvent {
return &UserDeactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -99,6 +133,10 @@ func (e *UserReactivatedEvent) Data() interface{} {
return nil
}
func (e *UserReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewUserReactivatedEvent(ctx context.Context) *UserReactivatedEvent {
return &UserReactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
@@ -116,18 +154,28 @@ func UserReactivatedEventMapper(event *repository.Event) (eventstore.EventReader
type UserRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string
UserLoginMustBeDomain bool
}
func (e *UserRemovedEvent) Data() interface{} {
return nil
}
func NewUserRemovedEvent(ctx context.Context) *UserRemovedEvent {
func (e *UserRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{NewRemoveUsernameUniqueConstraint(e.UserName, e.ResourceOwner(), e.UserLoginMustBeDomain)}
}
func NewUserRemovedEvent(ctx context.Context, resourceOwner, userName string, userLoginMustBeDomain bool) *UserRemovedEvent {
return &UserRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
BaseEvent: *eventstore.NewBaseEventForPushWithResourceOwner(
ctx,
UserRemovedType,
resourceOwner,
),
UserName: userName,
UserLoginMustBeDomain: userLoginMustBeDomain,
}
}
@@ -153,6 +201,10 @@ func (e *UserTokenAddedEvent) Data() interface{} {
return e
}
func (e *UserTokenAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewUserTokenAddedEvent(
ctx context.Context,
tokenID,
@@ -168,12 +220,13 @@ func NewUserTokenAddedEvent(
ctx,
UserTokenAddedType,
),
TokenID: tokenID,
ApplicationID: applicationID,
UserAgentID: userAgentID,
Audience: audience,
Scopes: scopes,
Expiration: expiration,
TokenID: tokenID,
ApplicationID: applicationID,
UserAgentID: userAgentID,
Audience: audience,
Scopes: scopes,
Expiration: expiration,
PreferredLanguage: preferredLanguage,
}
}
@@ -199,6 +252,10 @@ func (e *DomainClaimedEvent) Data() interface{} {
return e
}
func (e *DomainClaimedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewDomainClaimedEvent(
ctx context.Context,
userName string,
@@ -232,6 +289,10 @@ func (e *DomainClaimedSentEvent) Data() interface{} {
return nil
}
func (e *DomainClaimedSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return nil
}
func NewDomainClaimedSentEvent(
ctx context.Context,
) *DomainClaimedSentEvent {
@@ -252,23 +313,36 @@ func DomainClaimedSentEventMapper(event *repository.Event) (eventstore.EventRead
type UsernameChangedEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string `json:"userName"`
UserName string `json:"userName"`
OldUserName string
UserLoginMustBeDomain bool
}
func (e *UsernameChangedEvent) Data() interface{} {
return e
}
func (e *UsernameChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
return []*eventstore.EventUniqueConstraint{
NewRemoveUsernameUniqueConstraint(e.OldUserName, e.ResourceOwner(), e.UserLoginMustBeDomain),
NewAddUsernameUniqueConstraint(e.UserName, e.ResourceOwner(), e.UserLoginMustBeDomain),
}
}
func NewUsernameChangedEvent(
ctx context.Context,
userName string,
oldUserName,
newUserName string,
userLoginMustBeDomain bool,
) *UsernameChangedEvent {
return &UsernameChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserUserNameChangedType,
),
UserName: userName,
UserName: newUserName,
OldUserName: oldUserName,
UserLoginMustBeDomain: userLoginMustBeDomain,
}
}