2020-05-18 10:06:36 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-25 16:26:21 +00:00
|
|
|
|
2022-04-26 23:01:45 +00: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 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
usr_view "github.com/zitadel/zitadel/internal/user/repository/view"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserRepo struct {
|
2022-05-30 11:27:52 +00:00
|
|
|
SearchLimit uint64
|
2023-10-19 10:19:10 +00:00
|
|
|
Eventstore *eventstore.Eventstore
|
2022-05-30 11:27:52 +00:00
|
|
|
View *view.View
|
|
|
|
Query *query.Queries
|
|
|
|
SystemDefaults systemdefaults.SystemDefaults
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) Health(ctx context.Context) error {
|
2021-02-22 13:08:47 +00:00
|
|
|
return repo.Eventstore.Health(ctx)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 10:30:30 +00:00
|
|
|
func (repo *UserRepo) UserSessionUserIDsByAgentID(ctx context.Context, agentID string) ([]string, error) {
|
2022-04-19 06:26:12 +00:00
|
|
|
userSessions, err := repo.View.UserSessionsByAgentID(agentID, authz.GetInstance(ctx).InstanceID())
|
2020-06-29 07:49:40 +00:00
|
|
|
if err != nil {
|
2021-02-08 10:30:30 +00:00
|
|
|
return nil, err
|
2020-06-29 07:49:40 +00:00
|
|
|
}
|
2021-07-19 13:12:00 +00: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 07:49:40 +00:00
|
|
|
}
|
2021-02-08 10:30:30 +00:00
|
|
|
return userIDs, nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (repo *UserRepo) UserEventsByID(ctx context.Context, id string, sequence uint64, eventTypes []eventstore.EventType) ([]eventstore.Event, error) {
|
2023-07-27 12:10:19 +00:00
|
|
|
return repo.getUserEvents(ctx, id, sequence, eventTypes)
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (r *UserRepo) getUserEvents(ctx context.Context, userID string, sequence uint64, eventTypes []eventstore.EventType) ([]eventstore.Event, error) {
|
2023-07-27 12:10:19 +00:00
|
|
|
query, err := usr_view.UserByIDQuery(userID, authz.GetInstance(ctx).InstanceID(), sequence, eventTypes)
|
2021-02-22 13:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return r.Eventstore.Filter(ctx, query)
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|