2020-05-18 10:06:36 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-12-14 10:07:47 +00:00
|
|
|
"time"
|
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"
|
2024-07-17 04:43:07 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
2022-04-26 23:01:45 +00:00
|
|
|
usr_view "github.com/zitadel/zitadel/internal/user/repository/view"
|
2024-07-17 04:43:07 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
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) {
|
2024-09-04 10:14:50 +00:00
|
|
|
userSessions, err := repo.View.UserSessionsByAgentID(ctx, 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 {
|
2024-05-28 08:59:49 +00:00
|
|
|
if session.State.V == domain.UserSessionStateActive {
|
2021-07-19 13:12:00 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-04 10:14:50 +00:00
|
|
|
func (repo *UserRepo) UserAgentIDBySessionID(ctx context.Context, sessionID string) (string, error) {
|
|
|
|
return repo.View.UserAgentIDBySessionID(ctx, sessionID, authz.GetInstance(ctx).InstanceID())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) ActiveUserIDsBySessionID(ctx context.Context, sessionID string) (userAgentID string, userIDs []string, err error) {
|
|
|
|
return repo.View.ActiveUserIDsBySessionID(ctx, sessionID, authz.GetInstance(ctx).InstanceID())
|
|
|
|
}
|
|
|
|
|
2023-12-14 10:07:47 +00:00
|
|
|
func (repo *UserRepo) UserEventsByID(ctx context.Context, id string, changeDate time.Time, eventTypes []eventstore.EventType) ([]eventstore.Event, error) {
|
|
|
|
query, err := usr_view.UserByIDQuery(id, authz.GetInstance(ctx).InstanceID(), changeDate, eventTypes)
|
2021-02-22 13:08:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-12-14 10:07:47 +00:00
|
|
|
return repo.Eventstore.Filter(ctx, query) //nolint:staticcheck
|
2021-02-22 13:08:47 +00:00
|
|
|
}
|
2024-07-17 04:43:07 +00:00
|
|
|
|
|
|
|
type passwordCodeCheck struct {
|
|
|
|
userID string
|
|
|
|
|
|
|
|
exists bool
|
|
|
|
events int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *passwordCodeCheck) Reduce() error {
|
|
|
|
p.exists = p.events > 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *passwordCodeCheck) AppendEvents(events ...eventstore.Event) {
|
|
|
|
p.events += len(events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *passwordCodeCheck) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(p.userID).
|
|
|
|
EventTypes(user.UserV1PasswordCodeAddedType, user.UserV1PasswordCodeSentType,
|
|
|
|
user.HumanPasswordCodeAddedType, user.HumanPasswordCodeSentType).
|
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) PasswordCodeExists(ctx context.Context, userID string) (exists bool, err error) {
|
|
|
|
model := &passwordCodeCheck{
|
|
|
|
userID: userID,
|
|
|
|
}
|
|
|
|
err = repo.Eventstore.FilterToQueryReducer(ctx, model)
|
|
|
|
if err != nil {
|
|
|
|
return false, zerrors.ThrowPermissionDenied(err, "EVENT-SJ642", "Errors.Internal")
|
|
|
|
}
|
|
|
|
return model.exists, nil
|
|
|
|
}
|
2024-09-11 10:53:55 +00:00
|
|
|
|
|
|
|
type inviteCodeCheck struct {
|
|
|
|
userID string
|
|
|
|
|
|
|
|
exists bool
|
|
|
|
events int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *inviteCodeCheck) Reduce() error {
|
|
|
|
p.exists = p.events > 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *inviteCodeCheck) AppendEvents(events ...eventstore.Event) {
|
|
|
|
p.events += len(events)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *inviteCodeCheck) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(user.AggregateType).
|
|
|
|
AggregateIDs(p.userID).
|
|
|
|
EventTypes(
|
|
|
|
user.HumanInviteCodeAddedType,
|
|
|
|
user.HumanInviteCodeSentType).
|
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *UserRepo) InviteCodeExists(ctx context.Context, userID string) (exists bool, err error) {
|
|
|
|
model := &inviteCodeCheck{
|
|
|
|
userID: userID,
|
|
|
|
}
|
|
|
|
err = repo.Eventstore.FilterToQueryReducer(ctx, model)
|
|
|
|
if err != nil {
|
|
|
|
return false, zerrors.ThrowPermissionDenied(err, "EVENT-GJ2os", "Errors.Internal")
|
|
|
|
}
|
|
|
|
return model.exists, nil
|
|
|
|
}
|