2020-12-10 15:18:52 +00:00
|
|
|
package user
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-02-18 13:48:27 +00:00
|
|
|
"time"
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-10 15:18:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueUsername = "usernames"
|
2020-12-10 15:18:52 +00:00
|
|
|
userEventTypePrefix = eventstore.EventType("user.")
|
|
|
|
UserLockedType = userEventTypePrefix + "locked"
|
|
|
|
UserUnlockedType = userEventTypePrefix + "unlocked"
|
|
|
|
UserDeactivatedType = userEventTypePrefix + "deactivated"
|
|
|
|
UserReactivatedType = userEventTypePrefix + "reactivated"
|
|
|
|
UserRemovedType = userEventTypePrefix + "removed"
|
|
|
|
UserTokenAddedType = userEventTypePrefix + "token.added"
|
|
|
|
UserDomainClaimedType = userEventTypePrefix + "domain.claimed"
|
|
|
|
UserDomainClaimedSentType = userEventTypePrefix + "domain.claimed.sent"
|
|
|
|
UserUserNameChangedType = userEventTypePrefix + "username.changed"
|
|
|
|
)
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func NewAddUsernameUniqueConstraint(userName, resourceOwner string, userLoginMustBeDomain bool) *eventstore.EventUniqueConstraint {
|
|
|
|
uniqueUserName := userName
|
|
|
|
if userLoginMustBeDomain {
|
|
|
|
uniqueUserName = userName + resourceOwner
|
|
|
|
}
|
|
|
|
return eventstore.NewAddEventUniqueConstraint(
|
2021-02-15 12:31:24 +00:00
|
|
|
UniqueUsername,
|
2021-01-21 09:49:38 +00: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 12:31:24 +00:00
|
|
|
UniqueUsername,
|
2021-01-21 09:49:38 +00:00
|
|
|
uniqueUserName)
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserLockedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserLockedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserLockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserLockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserLockedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserLockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserLockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserLockedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &UserLockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserUnlockedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserUnlockedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserUnlockedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserUnlockedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserUnlockedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserUnlockedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserUnlockedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &UserUnlockedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserDeactivatedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserDeactivatedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserDeactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserDeactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserDeactivatedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDeactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &UserDeactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserReactivatedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserReactivatedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserReactivatedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserReactivatedEvent(ctx context.Context, aggregate *eventstore.Aggregate) *UserReactivatedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserReactivatedType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &UserReactivatedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserRemovedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
2021-01-21 09:49:38 +00:00
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
userName string
|
|
|
|
loginMustBeDomain bool
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserRemovedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-18 13:48:27 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{NewRemoveUsernameUniqueConstraint(e.userName, e.Aggregate().ResourceOwner, e.loginMustBeDomain)}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func NewUserRemovedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
|
|
|
userName string,
|
|
|
|
userLoginMustBeDomain bool,
|
|
|
|
) *UserRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &UserRemovedEvent{
|
2021-02-18 13:48:27 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserRemovedType,
|
|
|
|
),
|
2021-02-18 13:48:27 +00:00
|
|
|
userName: userName,
|
|
|
|
loginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &UserRemovedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type UserTokenAddedEvent struct {
|
2020-12-10 15:18:52 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
TokenID string `json:"tokenId"`
|
|
|
|
ApplicationID string `json:"applicationId"`
|
|
|
|
UserAgentID string `json:"userAgentId"`
|
|
|
|
Audience []string `json:"audience"`
|
|
|
|
Scopes []string `json:"scopes""`
|
|
|
|
Expiration time.Time `json:"expiration"`
|
|
|
|
PreferredLanguage string `json:"preferredLanguage"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *UserTokenAddedEvent) Data() interface{} {
|
2020-12-10 15:18:52 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UserTokenAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewUserTokenAddedEvent(
|
2020-12-10 15:18:52 +00:00
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
tokenID,
|
|
|
|
applicationID,
|
|
|
|
userAgentID,
|
|
|
|
preferredLanguage string,
|
|
|
|
audience,
|
|
|
|
scopes []string,
|
|
|
|
expiration time.Time,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *UserTokenAddedEvent {
|
|
|
|
return &UserTokenAddedEvent{
|
2020-12-10 15:18:52 +00:00
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserTokenAddedType,
|
|
|
|
),
|
2021-01-21 09:49:38 +00:00
|
|
|
TokenID: tokenID,
|
|
|
|
ApplicationID: applicationID,
|
|
|
|
UserAgentID: userAgentID,
|
|
|
|
Audience: audience,
|
|
|
|
Scopes: scopes,
|
|
|
|
Expiration: expiration,
|
|
|
|
PreferredLanguage: preferredLanguage,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func UserTokenAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
tokenAdded := &UserTokenAddedEvent{
|
2020-12-10 15:18:52 +00: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
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainClaimedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-02-15 12:31:24 +00:00
|
|
|
UserName string `json:"userName"`
|
|
|
|
oldUserName string `json:"-"`
|
|
|
|
userLoginMustBeDomain bool `json:"-"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *DomainClaimedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *DomainClaimedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-02-15 12:31:24 +00:00
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-02-18 13:48:27 +00:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-02-15 12:31:24 +00:00
|
|
|
}
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewDomainClaimedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-02-15 12:31:24 +00:00
|
|
|
userName,
|
|
|
|
oldUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *DomainClaimedEvent {
|
|
|
|
return &DomainClaimedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDomainClaimedType,
|
|
|
|
),
|
2021-02-15 12:31:24 +00:00
|
|
|
UserName: userName,
|
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DomainClaimedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
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 09:49:38 +00:00
|
|
|
func (e *DomainClaimedSentEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewDomainClaimedSentEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *DomainClaimedSentEvent {
|
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserDomainClaimedSentType,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DomainClaimedSentEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
return &DomainClaimedSentEvent{
|
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type UsernameChangedEvent struct {
|
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
UserName string `json:"userName"`
|
2021-02-15 12:31:24 +00:00
|
|
|
oldUserName string `json:"-"`
|
|
|
|
userLoginMustBeDomain bool `json:"-"`
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *UsernameChangedEvent) Data() interface{} {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *UsernameChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return []*eventstore.EventUniqueConstraint{
|
2021-02-18 13:48:27 +00:00
|
|
|
NewRemoveUsernameUniqueConstraint(e.oldUserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
|
|
|
NewAddUsernameUniqueConstraint(e.UserName, e.Aggregate().ResourceOwner, e.userLoginMustBeDomain),
|
2021-01-21 09:49:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 15:18:52 +00:00
|
|
|
func NewUsernameChangedEvent(
|
|
|
|
ctx context.Context,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate *eventstore.Aggregate,
|
2021-01-21 09:49:38 +00:00
|
|
|
oldUserName,
|
|
|
|
newUserName string,
|
|
|
|
userLoginMustBeDomain bool,
|
2020-12-10 15:18:52 +00:00
|
|
|
) *UsernameChangedEvent {
|
|
|
|
return &UsernameChangedEvent{
|
|
|
|
BaseEvent: *eventstore.NewBaseEventForPush(
|
|
|
|
ctx,
|
2021-02-18 13:48:27 +00:00
|
|
|
aggregate,
|
2020-12-10 15:18:52 +00:00
|
|
|
UserUserNameChangedType,
|
|
|
|
),
|
2021-01-21 09:49:38 +00:00
|
|
|
UserName: newUserName,
|
2021-02-15 12:31:24 +00:00
|
|
|
oldUserName: oldUserName,
|
|
|
|
userLoginMustBeDomain: userLoginMustBeDomain,
|
2020-12-10 15:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func UsernameChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
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
|
|
|
|
}
|