2020-05-18 12:06:36 +02:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-25 17:26:21 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/auth/repository/eventsourcing/view"
|
|
|
|
"github.com/zitadel/zitadel/internal/config/systemdefaults"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
usr_view "github.com/zitadel/zitadel/internal/user/repository/view"
|
2020-05-18 12:06:36 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserRepo struct {
|
2022-05-30 13:27:52 +02:00
|
|
|
SearchLimit uint64
|
2023-10-19 12:19:10 +02:00
|
|
|
Eventstore *eventstore.Eventstore
|
2022-05-30 13:27:52 +02:00
|
|
|
View *view.View
|
|
|
|
Query *query.Queries
|
|
|
|
SystemDefaults systemdefaults.SystemDefaults
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) Health(ctx context.Context) error {
|
2021-02-22 14:08:47 +01:00
|
|
|
return repo.Eventstore.Health(ctx)
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2021-02-08 11:30:30 +01:00
|
|
|
func (repo *UserRepo) UserSessionUserIDsByAgentID(ctx context.Context, agentID string) ([]string, error) {
|
2022-04-19 08:26:12 +02:00
|
|
|
userSessions, err := repo.View.UserSessionsByAgentID(agentID, authz.GetInstance(ctx).InstanceID())
|
2020-06-29 09:49:40 +02:00
|
|
|
if err != nil {
|
2021-02-08 11:30:30 +01:00
|
|
|
return nil, err
|
2020-06-29 09:49:40 +02:00
|
|
|
}
|
2021-07-19 15:12:00 +02:00
|
|
|
userIDs := make([]string, 0, len(userSessions))
|
|
|
|
for _, session := range userSessions {
|
|
|
|
if session.State == int32(domain.UserSessionStateActive) {
|
|
|
|
userIDs = append(userIDs, session.UserID)
|
|
|
|
}
|
2020-06-29 09:49:40 +02:00
|
|
|
}
|
2021-02-08 11:30:30 +01:00
|
|
|
return userIDs, nil
|
2020-05-18 12:06:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (repo *UserRepo) UserEventsByID(ctx context.Context, id string, sequence uint64, eventTypes []eventstore.EventType) ([]eventstore.Event, error) {
|
2023-07-27 14:10:19 +02:00
|
|
|
return repo.getUserEvents(ctx, id, sequence, eventTypes)
|
2021-02-22 14:08:47 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (r *UserRepo) getUserEvents(ctx context.Context, userID string, sequence uint64, eventTypes []eventstore.EventType) ([]eventstore.Event, error) {
|
2023-07-27 14:10:19 +02:00
|
|
|
query, err := usr_view.UserByIDQuery(userID, authz.GetInstance(ctx).InstanceID(), sequence, eventTypes)
|
2021-02-22 14:08:47 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 12:19:10 +02:00
|
|
|
return r.Eventstore.Filter(ctx, query)
|
2021-02-22 14:08:47 +01:00
|
|
|
}
|