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"
|
2024-10-31 14:57:17 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2022-04-26 23:01:45 +00:00
|
|
|
"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
|
|
|
}
|
|
|
|
|
2024-10-31 14:57:17 +00:00
|
|
|
func (repo *UserRepo) UserSessionsByAgentID(ctx context.Context, agentID string) ([]command.HumanSignOutSession, error) {
|
|
|
|
sessions, 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
|
|
|
}
|
2024-10-31 14:57:17 +00:00
|
|
|
signoutSessions := make([]command.HumanSignOutSession, 0, len(sessions))
|
|
|
|
for _, session := range sessions {
|
|
|
|
if session.State.V == domain.UserSessionStateActive && session.ID.Valid {
|
|
|
|
signoutSessions = append(signoutSessions, command.HumanSignOutSession{
|
|
|
|
ID: session.ID.String,
|
|
|
|
UserID: session.UserID,
|
|
|
|
})
|
2021-07-19 13:12:00 +00:00
|
|
|
}
|
2020-06-29 07:49:40 +00:00
|
|
|
}
|
2024-10-31 14:57:17 +00:00
|
|
|
return signoutSessions, 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())
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:57:17 +00:00
|
|
|
func (repo *UserRepo) ActiveUserSessionsBySessionID(ctx context.Context, sessionID string) (userAgentID string, signoutSessions []command.HumanSignOutSession, err error) {
|
|
|
|
userAgentID, sessions, err := repo.View.ActiveUserSessionsBySessionID(ctx, sessionID, authz.GetInstance(ctx).InstanceID())
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
signoutSessions = make([]command.HumanSignOutSession, 0, len(sessions))
|
|
|
|
for sessionID, userID := range sessions {
|
|
|
|
signoutSessions = append(signoutSessions, command.HumanSignOutSession{
|
|
|
|
ID: sessionID,
|
|
|
|
UserID: userID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return userAgentID, signoutSessions, nil
|
2024-09-04 10:14:50 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|