2020-12-10 16:18:52 +01:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-18 14:48:27 +01:00
|
|
|
"time"
|
|
|
|
|
2021-08-24 09:22:21 +02:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-05-20 13:33:35 +02:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-10 16:18:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueUsername = "usernames"
|
2020-12-10 16:18:52 +01:00
|
|
|
userEventTypePrefix = eventstore.EventType("user.")
|
|
|
|
UserLockedType = userEventTypePrefix + "locked"
|
|
|
|
UserUnlockedType = userEventTypePrefix + "unlocked"
|
|
|
|
UserDeactivatedType = userEventTypePrefix + "deactivated"
|
|
|
|
UserReactivatedType = userEventTypePrefix + "reactivated"
|
|
|
|
UserRemovedType = userEventTypePrefix + "removed"
|
|
|
|
UserTokenAddedType = userEventTypePrefix + "token.added"
|
2021-11-03 08:35:24 +01:00
|
|
|
UserTokenRemovedType = userEventTypePrefix + "token.removed"
|
2020-12-10 16:18:52 +01:00
|
|
|
UserDomainClaimedType = userEventTypePrefix + "domain.claimed"
|
|
|
|
UserDomainClaimedSentType = userEventTypePrefix + "domain.claimed.sent"
|
|
|
|
UserUserNameChangedType = userEventTypePrefix + "username.changed"
|
|
|
|
)
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func NewAddUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.EventUniqueConstraint {
|
|
|
|
uniqueUserName := userName
|
|
|
|
if userLoginMustBeDomain {
|
|
|
|
uniqueUserName = userName + resourceOwner
|
|
|
|
}
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueUsername,
|
2021-01-21 10:49:38 +01:00
|
|
|
uniqueUserName,
|
|
|
|
"Errors.User.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewRemoveUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.EventUniqueConstraint {
|
|
|
|
uniqueUserName := userName
|
|
|
|
if userLoginMustBeDomain {
|
|
|
|
uniqueUserName = userName + resourceOwner
|
|
|
|
}
|
|
|
|
return eventstore.NewRemoveEventUniqueConstraint(
|
2021-02-15 13:31:24 +01:00
|
|
|
UniqueUsername,
|
2021-01-21 10:49:38 +01:00
|
|
|
uniqueUserName)
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserLockedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserLockedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserLockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserLockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserLockedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserLockedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserLockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserLockedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserLockedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserUnlockedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserUnlockedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserUnlockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserUnlockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserUnlockedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserUnlockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserUnlockedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserDeactivatedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserDeactivatedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserDeactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserDeactivatedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserDeactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserDeactivatedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserReactivatedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserReactivatedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserReactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserReactivatedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserReactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserReactivatedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserRemovedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-01-21 10:49:38 +01:00
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
userName string
|
2021-11-02 10:08:47 +01:00
|
|
|
externalIDPs []*domain.UserIDPLink
|
2021-02-18 14:48:27 +01:00
|
|
|
loginMustBeDomain bool
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserRemovedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-08-24 09:22:21 +02:00
|
|
|
events := make([]*eventstore.EventUniqueConstraint, 0)
|
|
|
|
if e.userName != "" {
|
|
|
|
events = append(events, NewRemoveUsernameUniqueConstraint(e.userName, e.Aggregate().ResourceOwner, e.loginMustBeDomain))
|
|
|
|
}
|
|
|
|
for _, idp := range e.externalIDPs {
|
2021-11-02 10:08:47 +01:00
|
|
|
events = append(events, NewRemoveUserIDPLinkUniqueConstraint(idp.IDPConfigID, idp.ExternalUserID))
|
2021-08-24 09:22:21 +02:00
|
|
|
}
|
|
|
|
return events
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
func NewUserRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userName string,
|
2021-11-02 10:08:47 +01:00
|
|
|
externalIDPs []*domain.UserIDPLink,
|
2021-02-18 14:48:27 +01:00
|
|
|
userLoginMustBeDomain bool,
|
|
|
|
) *UserRemovedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserRemovedEvent{
|
2021-02-18 14:48:27 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2020-12-10 16:18:52 +01:00
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserRemovedType,
|
|
|
|
),
|
2021-02-18 14:48:27 +01:00
|
|
|
userName: userName,
|
2021-08-24 09:22:21 +02:00
|
|
|
externalIDPs: externalIDPs,
|
2021-02-18 14:48:27 +01:00
|
|
|
loginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &UserRemovedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type UserTokenAddedEvent struct {
|
2020-12-10 16:18:52 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
ApplicationID string `json:"applicationId"`
|
|
|
|
UserAgentID string `json:"userAgentId"`
|
2021-11-03 08:35:24 +01:00
|
|
|
RefreshTokenID string `json:"refreshTokenID,omitempty"`
|
2020-12-10 16:18:52 +01:00
|
|
|
Audience []string `json:"audience"`
|
2021-05-20 13:33:35 +02:00
|
|
|
Scopes []string `json:"scopes"`
|
2020-12-10 16:18:52 +01:00
|
|
|
Expiration time.Time `json:"expiration"`
|
|
|
|
PreferredLanguage string `json:"preferredLanguage"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *UserTokenAddedEvent) Data() interface{} {
|
2020-12-10 16:18:52 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UserTokenAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewUserTokenAddedEvent(
|
2020-12-10 16:18:52 +01:00
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
tokenID,
|
|
|
|
applicationID,
|
|
|
|
userAgentID,
|
2021-11-03 08:35:24 +01:00
|
|
|
preferredLanguage,
|
|
|
|
refreshTokenID string,
|
2020-12-10 16:18:52 +01:00
|
|
|
audience,
|
|
|
|
scopes []string,
|
|
|
|
expiration time.Time,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *UserTokenAddedEvent {
|
|
|
|
return &UserTokenAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserTokenAddedType,
|
|
|
|
),
|
2021-01-21 10:49:38 +01:00
|
|
|
TokenID: tokenID,
|
|
|
|
ApplicationID: applicationID,
|
|
|
|
UserAgentID: userAgentID,
|
2021-11-03 08:35:24 +01:00
|
|
|
RefreshTokenID: refreshTokenID,
|
2021-01-21 10:49:38 +01:00
|
|
|
Audience: audience,
|
|
|
|
Scopes: scopes,
|
|
|
|
Expiration: expiration,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserTokenAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
tokenAdded := &UserTokenAddedEvent{
|
2020-12-10 16:18:52 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, tokenAdded)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-7M9sd", "unable to unmarshal token added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenAdded, nil
|
|
|
|
}
|
|
|
|
|
2021-11-03 08:35:24 +01:00
|
|
|
type UserTokenRemovedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserTokenRemovedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UserTokenRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewUserTokenRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
tokenID string,
|
|
|
|
) *UserTokenRemovedEvent {
|
|
|
|
return &UserTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
|
|
|
aggregate,
|
|
|
|
UserTokenRemovedType,
|
|
|
|
),
|
|
|
|
TokenID: tokenID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UserTokenRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2021-11-03 08:35:24 +01:00
|
|
|
tokenRemoved := &UserTokenRemovedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, tokenRemoved)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-7M9sd", "unable to unmarshal token added")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tokenRemoved, nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
type DomainClaimedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 13:31:24 +01:00
|
|
|
UserName string `json:"userName"`
|
|
|
|
oldUserName string `json:"-"`
|
|
|
|
userLoginMustBeDomain bool `json:"-"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DomainClaimedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *DomainClaimedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 13:31:24 +01:00
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-02-18 14:48:27 +01:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-02-15 13:31:24 +01:00
|
|
|
}
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
func NewDomainClaimedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-02-15 13:31:24 +01:00
|
|
|
userName,
|
|
|
|
oldUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2020-12-10 16:18:52 +01:00
|
|
|
) *DomainClaimedEvent {
|
|
|
|
return &DomainClaimedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserDomainClaimedType,
|
|
|
|
),
|
2021-02-15 13:31:24 +01:00
|
|
|
UserName: userName,
|
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func DomainClaimedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2020-12-10 16:18:52 +01:00
|
|
|
domainClaimed := &DomainClaimedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, domainClaimed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-aR8jc", "unable to unmarshal domain claimed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return domainClaimed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainClaimedSentEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DomainClaimedSentEvent) Data() interface{} {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *DomainClaimedSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
func NewDomainClaimedSentEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
) *DomainClaimedSentEvent {
|
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserDomainClaimedSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func DomainClaimedSentEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2020-12-10 16:18:52 +01:00
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsernameChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
UserName string `json:"userName"`
|
2021-02-15 13:31:24 +01:00
|
|
|
oldUserName string `json:"-"`
|
|
|
|
userLoginMustBeDomain bool `json:"-"`
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UsernameChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *UsernameChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-02-18 14:48:27 +01:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-01-21 10:49:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:18:52 +01:00
|
|
|
func NewUsernameChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-21 10:49:38 +01:00
|
|
|
oldUserName,
|
|
|
|
newUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2020-12-10 16:18:52 +01:00
|
|
|
) *UsernameChangedEvent {
|
|
|
|
return &UsernameChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 14:48:27 +01:00
|
|
|
aggregate,
|
2020-12-10 16:18:52 +01:00
|
|
|
UserUserNameChangedType,
|
|
|
|
),
|
2021-01-21 10:49:38 +01:00
|
|
|
UserName: newUserName,
|
2021-02-15 13:31:24 +01:00
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 16:18:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func UsernameChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
2020-12-10 16:18:52 +01:00
|
|
|
domainClaimed := &UsernameChangedEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(event.Data, domainClaimed)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "USER-4Bm9s", "unable to unmarshal username changed")
|
|
|
|
}
|
|
|
|
|
|
|
|
return domainClaimed, nil
|
|
|
|
}
|