2020-05-18 10:06:36 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-06-03 12:37:24 +00:00
|
|
|
"context"
|
2024-01-30 15:17:54 +00:00
|
|
|
"time"
|
2022-06-03 12:37:24 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
auth_view "github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
2022-06-03 12:37:24 +00:00
|
|
|
query2 "github.com/zitadel/zitadel/internal/query"
|
2022-10-26 13:06:48 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-06-03 12:37:24 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2024-05-22 15:26:02 +00:00
|
|
|
es_model "github.com/zitadel/zitadel/internal/user/repository/eventsourcing/model"
|
2022-04-26 23:01:45 +00:00
|
|
|
view_model "github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
userSessionTable = "auth.user_sessions"
|
|
|
|
)
|
|
|
|
|
2020-05-18 10:06:36 +00:00
|
|
|
type UserSession struct {
|
2023-10-19 10:19:10 +00:00
|
|
|
queries *query2.Queries
|
|
|
|
view *auth_view.View
|
|
|
|
es handler.EventStore
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
var _ handler.Projection = (*UserSession)(nil)
|
2020-12-18 15:47:45 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func newUserSession(
|
|
|
|
ctx context.Context,
|
|
|
|
config handler.Config,
|
|
|
|
view *auth_view.View,
|
|
|
|
queries *query2.Queries,
|
|
|
|
) *handler.Handler {
|
|
|
|
return handler.NewHandler(
|
|
|
|
ctx,
|
|
|
|
&config,
|
|
|
|
&UserSession{
|
|
|
|
queries: queries,
|
|
|
|
view: view,
|
|
|
|
es: config.Eventstore,
|
|
|
|
},
|
|
|
|
)
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
2020-05-18 10:06:36 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Name implements [handler.Projection]
|
|
|
|
func (*UserSession) Name() string {
|
2020-05-18 10:06:36 +00:00
|
|
|
return userSessionTable
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
// Reducers implements [handler.Projection]
|
|
|
|
func (s *UserSession) Reducers() []handler.AggregateReducer {
|
|
|
|
return []handler.AggregateReducer{
|
|
|
|
{
|
|
|
|
Aggregate: user.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: user.UserV1PasswordCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1PasswordCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1MFAOTPCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1MFAOTPCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1SignedOutType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLoginCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenCheckSucceededType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenCheckFailedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanSignedOutType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1PasswordChangedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserV1MFAOTPRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserLockedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserDeactivatedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordChangedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanMFAOTPRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLinkRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserIDPLinkCascadeRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanPasswordlessTokenRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.HumanU2FTokenRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Event: user.UserRemovedType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
2024-04-24 15:50:58 +00:00
|
|
|
{
|
|
|
|
Event: user.HumanRegisteredType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
2023-10-19 10:19:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: org.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: org.OrgRemovedEventType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Aggregate: instance.AggregateType,
|
|
|
|
EventReducers: []handler.EventReducer{
|
|
|
|
{
|
|
|
|
Event: instance.InstanceRemovedEventType,
|
|
|
|
Reduce: s.Reduce,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-12-18 15:47:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 15:26:02 +00:00
|
|
|
func sessionColumns(event eventstore.Event, columns ...handler.Column) ([]handler.Column, error) {
|
|
|
|
userAgent, err := agentIDFromSession(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return append([]handler.Column{
|
|
|
|
handler.NewCol(view_model.UserSessionKeyUserAgentID, userAgent),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyCreationDate, handler.OnlySetValueOnInsert(userSessionTable, event.CreatedAt())),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyResourceOwner, event.Aggregate().ResourceOwner),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
|
|
|
}, columns...), nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (u *UserSession) Reduce(event eventstore.Event) (_ *handler.Statement, err error) {
|
2024-05-22 15:26:02 +00:00
|
|
|
// in case anything needs to be change here check if appendEvent function needs the change as well
|
2024-01-30 15:17:54 +00:00
|
|
|
switch event.Type() {
|
|
|
|
case user.UserV1PasswordCheckSucceededType,
|
2024-05-22 15:26:02 +00:00
|
|
|
user.HumanPasswordCheckSucceededType:
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.UserV1PasswordCheckFailedType,
|
|
|
|
user.HumanPasswordCheckFailedType:
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, time.Time{}),
|
2024-05-28 08:59:49 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
2024-05-22 15:26:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.UserV1MFAOTPCheckSucceededType,
|
|
|
|
user.HumanMFAOTPCheckSucceededType:
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerificationType, domain.MFATypeTOTP),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.UserV1MFAOTPCheckFailedType,
|
2024-01-30 15:17:54 +00:00
|
|
|
user.HumanMFAOTPCheckFailedType,
|
2024-05-22 15:26:02 +00:00
|
|
|
user.HumanU2FTokenCheckFailedType:
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, time.Time{}),
|
2024-05-28 08:59:49 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
2024-05-22 15:26:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.UserV1SignedOutType,
|
2024-01-30 15:17:54 +00:00
|
|
|
user.HumanSignedOutType:
|
2024-05-22 15:26:02 +00:00
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordlessVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerificationType, domain.MFALevelNotSetUp),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerificationType, domain.MFALevelNotSetUp),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyExternalLoginVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateTerminated),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.UserIDPLoginCheckSucceededType:
|
|
|
|
data := new(es_model.AuthRequest)
|
|
|
|
err := data.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyExternalLoginVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySelectedIDPConfigID, data.SelectedIDPConfigID),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.HumanU2FTokenCheckSucceededType:
|
|
|
|
data := new(es_model.AuthRequest)
|
|
|
|
err := data.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerificationType, domain.MFATypeU2F),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.HumanPasswordlessTokenCheckSucceededType:
|
|
|
|
data := new(es_model.AuthRequest)
|
|
|
|
err := data.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordlessVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerificationType, domain.MFATypeU2FUserVerification),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
|
|
|
case user.HumanPasswordlessTokenCheckFailedType:
|
|
|
|
data := new(es_model.AuthRequest)
|
|
|
|
err := data.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordlessVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerification, time.Time{}),
|
2024-05-28 08:59:49 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
2024-05-22 15:26:02 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewUpsertStatement(event, columns[0:3], columns), nil
|
2024-01-30 15:17:54 +00:00
|
|
|
case user.UserLockedType,
|
|
|
|
user.UserDeactivatedType:
|
|
|
|
return handler.NewUpdateStatement(event,
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordlessVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerificationType, domain.MFALevelNotSetUp),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerificationType, domain.MFALevelNotSetUp),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyExternalLoginVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateTerminated),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeyState, domain.UserSessionStateTerminated)),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
case user.UserV1PasswordChangedType,
|
|
|
|
user.HumanPasswordChangedType:
|
|
|
|
userAgent, err := agentIDFromSession(event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-24 15:50:58 +00:00
|
|
|
return handler.NewMultiStatement(event,
|
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-04-24 15:50:58 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserAgentID, userAgent),
|
2024-04-24 15:50:58 +00:00
|
|
|
}),
|
|
|
|
handler.AddUpdateStatement(
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-04-24 15:50:58 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeyUserAgentID, userAgent)),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeyState, domain.UserSessionStateTerminated)),
|
2024-04-24 15:50:58 +00:00
|
|
|
}),
|
2024-01-30 15:17:54 +00:00
|
|
|
), nil
|
|
|
|
case user.UserV1MFAOTPRemovedType,
|
|
|
|
user.HumanMFAOTPRemovedType,
|
|
|
|
user.HumanU2FTokenRemovedType:
|
|
|
|
return handler.NewUpdateStatement(event,
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeySecondFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeyState, domain.UserSessionStateTerminated)),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
case user.UserIDPLinkRemovedType,
|
|
|
|
user.UserIDPLinkCascadeRemovedType:
|
|
|
|
return handler.NewUpdateStatement(event,
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyExternalLoginVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySelectedIDPConfigID, ""),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeySelectedIDPConfigID, "")),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
case user.HumanPasswordlessTokenRemovedType:
|
|
|
|
return handler.NewUpdateStatement(event,
|
|
|
|
[]handler.Column{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordlessVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyMultiFactorVerification, time.Time{}),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyChangeDate, event.CreatedAt()),
|
|
|
|
handler.NewCol(view_model.UserSessionKeySequence, event.Sequence()),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
[]handler.Condition{
|
2024-05-22 15:26:02 +00:00
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
handler.Not(handler.NewCond(view_model.UserSessionKeyState, domain.UserSessionStateTerminated)),
|
2024-01-30 15:17:54 +00:00
|
|
|
},
|
|
|
|
), nil
|
|
|
|
case user.UserRemovedType:
|
2024-05-22 15:26:02 +00:00
|
|
|
return handler.NewDeleteStatement(event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyUserID, event.Aggregate().ID),
|
|
|
|
},
|
|
|
|
), nil
|
2024-04-24 15:50:58 +00:00
|
|
|
case user.HumanRegisteredType:
|
2024-05-22 15:26:02 +00:00
|
|
|
columns, err := sessionColumns(event,
|
|
|
|
handler.NewCol(view_model.UserSessionKeyState, domain.UserSessionStateActive),
|
|
|
|
handler.NewCol(view_model.UserSessionKeyPasswordVerification, event.CreatedAt()),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return handler.NewCreateStatement(event,
|
|
|
|
columns,
|
|
|
|
), nil
|
2024-01-30 15:17:54 +00:00
|
|
|
case instance.InstanceRemovedEventType:
|
2024-05-22 15:26:02 +00:00
|
|
|
return handler.NewDeleteStatement(event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
},
|
|
|
|
), nil
|
2024-01-30 15:17:54 +00:00
|
|
|
case org.OrgRemovedEventType:
|
2024-05-22 15:26:02 +00:00
|
|
|
return handler.NewDeleteStatement(event,
|
|
|
|
[]handler.Condition{
|
|
|
|
handler.NewCond(view_model.UserSessionKeyInstanceID, event.Aggregate().InstanceID),
|
|
|
|
handler.NewCond(view_model.UserSessionKeyResourceOwner, event.Aggregate().ResourceOwner),
|
|
|
|
},
|
|
|
|
), nil
|
2024-01-30 15:17:54 +00:00
|
|
|
default:
|
2024-05-22 15:26:02 +00:00
|
|
|
return handler.NewNoOpStatement(event), nil
|
2020-12-07 11:09:10 +00:00
|
|
|
}
|
2022-06-03 12:37:24 +00:00
|
|
|
}
|