zitadel/internal/auth/repository/eventsourcing/view/user_session.go
Silvan aa407c3c3e
fix(auth): optimise user sessions (#7199)
* fix(auth): start optimise user sessions

* reduce and query user sessions directly without gorm statements

* cleanup

* cleanup

* fix requested changes

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-01-30 15:17:54 +00:00

71 lines
2.1 KiB
Go

package view
import (
"context"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/query"
"github.com/zitadel/zitadel/internal/user/repository/view"
"github.com/zitadel/zitadel/internal/user/repository/view/model"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
userSessionTable = "auth.user_sessions"
)
func (v *View) UserSessionByIDs(agentID, userID, instanceID string) (*model.UserSessionView, error) {
return view.UserSessionByIDs(v.client, agentID, userID, instanceID)
}
func (v *View) UserSessionsByAgentID(agentID, instanceID string) ([]*model.UserSessionView, error) {
return view.UserSessionsByAgentID(v.client, agentID, instanceID)
}
func (v *View) PutUserSession(userSession *model.UserSessionView) error {
return view.PutUserSession(v.Db, userSessionTable, userSession)
}
func (v *View) DeleteUserSessions(userID, instanceID string) error {
err := view.DeleteUserSessions(v.Db, userSessionTable, userID, instanceID)
if err != nil && !zerrors.IsNotFound(err) {
return err
}
return nil
}
func (v *View) DeleteInstanceUserSessions(instanceID string) error {
err := view.DeleteInstanceUserSessions(v.Db, userSessionTable, instanceID)
if err != nil && !zerrors.IsNotFound(err) {
return err
}
return nil
}
func (v *View) DeleteOrgUserSessions(event eventstore.Event) error {
err := view.DeleteOrgUserSessions(v.Db, userSessionTable, event.Aggregate().InstanceID, event.Aggregate().ResourceOwner)
if err != nil && !zerrors.IsNotFound(err) {
return err
}
return nil
}
func (v *View) GetLatestUserSessionSequence(ctx context.Context, instanceID string) (_ *query.CurrentState, err error) {
q := &query.CurrentStateSearchQueries{
Queries: make([]query.SearchQuery, 2),
}
q.Queries[0], err = query.NewCurrentStatesInstanceIDSearchQuery(instanceID)
if err != nil {
return nil, err
}
q.Queries[1], err = query.NewCurrentStatesProjectionSearchQuery(userSessionTable)
if err != nil {
return nil, err
}
states, err := v.query.SearchCurrentStates(ctx, q)
if err != nil || states.SearchResponse.Count == 0 {
return nil, err
}
return states.CurrentStates[0], nil
}