2020-05-18 10:06:36 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2022-07-07 12:58:00 +00:00
|
|
|
"context"
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
"github.com/zitadel/logging"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2022-07-07 12:58:00 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2022-04-26 23:01:45 +00:00
|
|
|
usr_model "github.com/zitadel/zitadel/internal/user/model"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view"
|
|
|
|
"github.com/zitadel/zitadel/internal/user/repository/view/model"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-11-30 16:01:17 +00:00
|
|
|
userTable = "auth.users2"
|
2020-05-18 10:06:36 +00:00
|
|
|
)
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UserByID(userID, instanceID string) (*model.UserView, error) {
|
|
|
|
return view.UserByID(v.Db, userTable, userID, instanceID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByLoginName(ctx context.Context, loginName, instanceID string) (*model.UserView, error) {
|
2022-07-07 12:58:00 +00:00
|
|
|
loginNameQuery, err := query.NewUserLoginNamesSearchQuery(loginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, loginNameQuery)
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByLoginNameAndResourceOwner(ctx context.Context, loginName, resourceOwner, instanceID string) (*model.UserView, error) {
|
2022-07-07 12:58:00 +00:00
|
|
|
loginNameQuery, err := query.NewUserLoginNamesSearchQuery(loginName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resourceOwnerQuery, err := query.NewUserResourceOwnerSearchQuery(resourceOwner, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, loginNameQuery, resourceOwnerQuery)
|
2022-07-07 12:58:00 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByEmail(ctx context.Context, email, instanceID string) (*model.UserView, error) {
|
2023-01-16 13:07:31 +00:00
|
|
|
emailQuery, err := query.NewUserVerifiedEmailSearchQuery(email, query.TextEqualsIgnoreCase)
|
2022-10-17 19:19:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, emailQuery)
|
2022-10-17 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByEmailAndResourceOwner(ctx context.Context, email, resourceOwner, instanceID string) (*model.UserView, error) {
|
2022-10-17 19:19:15 +00:00
|
|
|
emailQuery, err := query.NewUserVerifiedEmailSearchQuery(email, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resourceOwnerQuery, err := query.NewUserResourceOwnerSearchQuery(resourceOwner, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, emailQuery, resourceOwnerQuery)
|
2022-10-17 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByPhone(ctx context.Context, phone, instanceID string) (*model.UserView, error) {
|
2022-10-17 19:19:15 +00:00
|
|
|
phoneQuery, err := query.NewUserVerifiedPhoneSearchQuery(phone, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, phoneQuery)
|
2022-10-17 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) UserByPhoneAndResourceOwner(ctx context.Context, phone, resourceOwner, instanceID string) (*model.UserView, error) {
|
2022-10-17 19:19:15 +00:00
|
|
|
phoneQuery, err := query.NewUserVerifiedPhoneSearchQuery(phone, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resourceOwnerQuery, err := query.NewUserResourceOwnerSearchQuery(resourceOwner, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
return v.userByID(ctx, instanceID, phoneQuery, resourceOwnerQuery)
|
2022-10-17 19:19:15 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00:00
|
|
|
func (v *View) userByID(ctx context.Context, instanceID string, queries ...query.SearchQuery) (*model.UserView, error) {
|
2022-11-30 16:01:17 +00:00
|
|
|
queriedUser, err := v.query.GetNotifyUser(ctx, true, false, queries...)
|
2022-07-07 12:58:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := view.UserByID(v.Db, userTable, queriedUser.ID, instanceID)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-04-28 14:28:13 +00:00
|
|
|
sequence, err := v.GetLatestUserSequence(ctx, instanceID)
|
|
|
|
logging.WithFields("instanceID", instanceID).
|
|
|
|
OnError(err).
|
|
|
|
Errorf("could not get current sequence for userByID")
|
2022-07-07 12:58:00 +00:00
|
|
|
user = new(model.UserView)
|
2023-04-28 14:28:13 +00:00
|
|
|
if sequence != nil {
|
2023-10-19 10:19:10 +00:00
|
|
|
user.Sequence = sequence.Sequence
|
2023-04-28 14:28:13 +00:00
|
|
|
}
|
2022-07-07 12:58:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-27 12:10:19 +00:00
|
|
|
query, err := view.UserByIDQuery(queriedUser.ID, instanceID, user.Sequence, user.EventTypes())
|
2022-07-07 12:58:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
events, err := v.es.Filter(ctx, query)
|
2022-07-07 12:58:00 +00:00
|
|
|
if err != nil && user.Sequence == 0 {
|
|
|
|
return nil, err
|
|
|
|
} else if err != nil {
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
userCopy := *user
|
|
|
|
|
|
|
|
for _, event := range events {
|
|
|
|
if err := user.AppendEvent(event); err != nil {
|
|
|
|
return &userCopy, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.State == int32(usr_model.UserStateDeleted) {
|
|
|
|
return nil, errors.ThrowNotFound(nil, "VIEW-r4y8r", "Errors.User.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
2020-09-18 11:26:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UsersByOrgID(orgID, instanceID string) ([]*model.UserView, error) {
|
|
|
|
return view.UsersByOrgID(v.Db, userTable, orgID, instanceID)
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) PutUser(user *model.UserView, event eventstore.Event) error {
|
|
|
|
return view.PutUser(v.Db, userTable, user)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) PutUsers(users []*model.UserView, event eventstore.Event) error {
|
|
|
|
return view.PutUsers(v.Db, userTable, users...)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) DeleteUser(userID, instanceID string, event eventstore.Event) error {
|
2022-04-19 06:26:12 +00:00
|
|
|
err := view.DeleteUser(v.Db, userTable, userID, instanceID)
|
2020-12-21 17:42:34 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) DeleteInstanceUsers(event eventstore.Event) error {
|
|
|
|
err := view.DeleteInstanceUsers(v.Db, userTable, event.Aggregate().InstanceID)
|
2022-10-26 13:06:48 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return nil
|
2022-10-26 13:06:48 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) UpdateOrgOwnerRemovedUsers(event eventstore.Event) error {
|
|
|
|
err := view.UpdateOrgOwnerRemovedUsers(v.Db, userTable, event.Aggregate().InstanceID, event.Aggregate().ID)
|
2022-11-30 16:01:17 +00:00
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-19 10:19:10 +00:00
|
|
|
return nil
|
2022-04-19 06:26:12 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (v *View) GetLatestUserSequence(ctx context.Context, instanceID string) (_ *query.CurrentState, err error) {
|
|
|
|
q := &query.CurrentStateSearchQueries{
|
|
|
|
Queries: make([]query.SearchQuery, 2),
|
|
|
|
}
|
|
|
|
q.Queries[0], err = query.NewCurrentStatesInstanceIDSearchQuery(instanceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
q.Queries[1], err = query.NewCurrentStatesProjectionSearchQuery(userTable)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
states, err := v.query.SearchCurrentStates(ctx, q)
|
|
|
|
if err != nil || states.SearchResponse.Count == 0 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return states.CurrentStates[0], nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|