2020-05-18 12:06:36 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2023-04-28 16:28:13 +02:00
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
userSessionTable = "auth.user_sessions"
|
|
|
|
)
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (v *View) UserSessionByIDs(agentID, userID, instanceID string) (*model.UserSessionView, error) {
|
|
|
|
return view.UserSessionByIDs(v.Db, userSessionTable, agentID, userID, instanceID)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (v *View) UserSessionsByUserID(userID, instanceID string) ([]*model.UserSessionView, error) {
|
|
|
|
return view.UserSessionsByUserID(v.Db, userSessionTable, userID, instanceID)
|
2020-06-05 07:50:04 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (v *View) UserSessionsByAgentID(agentID, instanceID string) ([]*model.UserSessionView, error) {
|
|
|
|
return view.UserSessionsByAgentID(v.Db, userSessionTable, agentID, instanceID)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-06-03 14:37:24 +02:00
|
|
|
func (v *View) UserSessionsByOrgID(orgID, instanceID string) ([]*model.UserSessionView, error) {
|
|
|
|
return view.UserSessionsByOrgID(v.Db, userSessionTable, orgID, instanceID)
|
|
|
|
}
|
|
|
|
|
2021-12-06 16:28:49 +01:00
|
|
|
func (v *View) ActiveUserSessionsCount() (uint64, error) {
|
2020-12-02 08:50:59 +01:00
|
|
|
return view.ActiveUserSessions(v.Db, userSessionTable)
|
|
|
|
}
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
func (v *View) PutUserSession(userSession *model.UserSessionView, event *models.Event) error {
|
2020-05-18 12:06:36 +02:00
|
|
|
err := view.PutUserSession(v.Db, userSessionTable, userSession)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 16:47:45 +01:00
|
|
|
return v.ProcessedUserSessionSequence(event)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
func (v *View) PutUserSessions(userSession []*model.UserSessionView, event *models.Event) error {
|
2020-08-26 13:01:37 +02:00
|
|
|
err := view.PutUserSessions(v.Db, userSessionTable, userSession...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 16:47:45 +01:00
|
|
|
return v.ProcessedUserSessionSequence(event)
|
2020-08-26 13:01:37 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (v *View) DeleteUserSessions(userID, instanceID string, event *models.Event) error {
|
|
|
|
err := view.DeleteUserSessions(v.Db, userSessionTable, userID, instanceID)
|
2020-12-21 18:42:34 +01:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
2020-12-18 16:47:45 +01:00
|
|
|
return v.ProcessedUserSessionSequence(event)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-10-26 15:06:48 +02:00
|
|
|
func (v *View) DeleteInstanceUserSessions(event *models.Event) error {
|
|
|
|
err := view.DeleteInstanceUserSessions(v.Db, userSessionTable, event.InstanceID)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
2022-11-30 17:01:17 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.ProcessedUserSessionSequence(event)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) DeleteOrgUserSessions(event *models.Event) error {
|
|
|
|
err := view.DeleteOrgUserSessions(v.Db, userSessionTable, event.InstanceID, event.ResourceOwner)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
2022-10-26 15:06:48 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.ProcessedUserSessionSequence(event)
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:28:13 +02:00
|
|
|
func (v *View) GetLatestUserSessionSequence(ctx context.Context, instanceID string) (*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequence(ctx, userSessionTable, instanceID)
|
2022-04-19 08:26:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:28:13 +02:00
|
|
|
func (v *View) GetLatestUserSessionSequences(ctx context.Context, instanceIDs []string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequences(ctx, userSessionTable, instanceIDs)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
func (v *View) ProcessedUserSessionSequence(event *models.Event) error {
|
|
|
|
return v.saveCurrentSequence(userSessionTable, event)
|
2020-12-02 08:50:59 +01:00
|
|
|
}
|
|
|
|
|
2022-11-22 07:36:48 +01:00
|
|
|
func (v *View) UpdateUserSessionSpoolerRunTimestamp(instanceIDs []string) error {
|
|
|
|
return v.updateSpoolerRunSequence(userSessionTable, instanceIDs)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2022-04-19 08:26:12 +02:00
|
|
|
func (v *View) GetLatestUserSessionFailedEvent(sequence uint64, instanceID string) (*repository.FailedEvent, error) {
|
|
|
|
return v.latestFailedEvent(userSessionTable, instanceID, sequence)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-25 08:01:13 +02:00
|
|
|
func (v *View) ProcessedUserSessionFailedEvent(failedEvent *repository.FailedEvent) error {
|
2020-05-18 12:06:36 +02:00
|
|
|
return v.saveFailedEvent(failedEvent)
|
|
|
|
}
|