zitadel/internal/v2/repository/user/user.go
Livio Amstutz 21ffe1b0cb
new pkg structure (#1150)
* fix: split command query side

* fix: split command query side

* fix: members in correct pkg structure

* fix: label policy in correct pkg structure

* fix: structure

* fix: structure of login policy

* fix: identityprovider structure

* fix: org iam policy structure

* fix: password age policy structure

* fix: password complexity policy structure

* fix: password lockout policy structure

* fix: idp structure

* fix: user events structure

* fix: user write model

* fix: profile email changed command

* fix: address changed command

* fix: user states

* fix: user

* fix: org structure and add human

* begin iam setup command side

* setup

* step2

* step2

* fix: add user

* step2

* isvalid

* fix: folder structure v2 business

Co-authored-by: Fabiennne <fabienne.gerschwiler@gmail.com>
2021-01-04 14:52:13 +01:00

286 lines
6.6 KiB
Go

package user
import (
"context"
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
"time"
)
const (
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"
)
type UserLockedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *UserLockedEvent) Data() interface{} {
return nil
}
func NewUserLockedEvent(ctx context.Context) *UserLockedEvent {
return &UserLockedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserLockedType,
),
}
}
func UserLockedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &UserLockedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UserUnlockedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *UserUnlockedEvent) Data() interface{} {
return nil
}
func NewUserUnlockedEvent(ctx context.Context) *UserUnlockedEvent {
return &UserUnlockedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserUnlockedType,
),
}
}
func UserUnlockedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &UserUnlockedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UserDeactivatedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *UserDeactivatedEvent) Data() interface{} {
return nil
}
func NewUserDeactivatedEvent(ctx context.Context) *UserDeactivatedEvent {
return &UserDeactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserDeactivatedType,
),
}
}
func UserDeactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &UserDeactivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UserReactivatedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *UserReactivatedEvent) Data() interface{} {
return nil
}
func NewUserReactivatedEvent(ctx context.Context) *UserReactivatedEvent {
return &UserReactivatedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserReactivatedType,
),
}
}
func UserReactivatedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &UserReactivatedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UserRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *UserRemovedEvent) Data() interface{} {
return nil
}
func NewUserRemovedEvent(ctx context.Context) *UserRemovedEvent {
return &UserRemovedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserRemovedType,
),
}
}
func UserRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &UserRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UserTokenAddedEvent struct {
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"`
}
func (e *UserTokenAddedEvent) Data() interface{} {
return e
}
func NewUserTokenAddedEvent(
ctx context.Context,
tokenID,
applicationID,
userAgentID,
preferredLanguage string,
audience,
scopes []string,
expiration time.Time,
) *UserTokenAddedEvent {
return &UserTokenAddedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserTokenAddedType,
),
TokenID: tokenID,
ApplicationID: applicationID,
UserAgentID: userAgentID,
Audience: audience,
Scopes: scopes,
Expiration: expiration,
}
}
func UserTokenAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
tokenAdded := &UserTokenAddedEvent{
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:"-"`
UserName string `json:"userName"`
}
func (e *DomainClaimedEvent) Data() interface{} {
return e
}
func NewDomainClaimedEvent(
ctx context.Context,
userName string,
) *DomainClaimedEvent {
return &DomainClaimedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserDomainClaimedType,
),
UserName: userName,
}
}
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
}
func NewDomainClaimedSentEvent(
ctx context.Context,
) *DomainClaimedSentEvent {
return &DomainClaimedSentEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserDomainClaimedSentType,
),
}
}
func DomainClaimedSentEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &DomainClaimedSentEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}
type UsernameChangedEvent struct {
eventstore.BaseEvent `json:"-"`
UserName string `json:"userName"`
}
func (e *UsernameChangedEvent) Data() interface{} {
return e
}
func NewUsernameChangedEvent(
ctx context.Context,
userName string,
) *UsernameChangedEvent {
return &UsernameChangedEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
UserUserNameChangedType,
),
UserName: userName,
}
}
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
}