2020-05-18 10:06:36 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2022-07-07 12:58:00 +00:00
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
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"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
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
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UserByLoginName(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
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.userByID(instanceID, loginNameQuery)
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UserByLoginNameAndResourceOwner(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
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.userByID(instanceID, loginNameQuery, resourceOwnerQuery)
|
|
|
|
}
|
|
|
|
|
2022-10-17 19:19:15 +00:00
|
|
|
func (v *View) UserByEmail(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
|
|
|
|
}
|
|
|
|
return v.userByID(instanceID, emailQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) UserByEmailAndResourceOwner(email, resourceOwner, instanceID string) (*model.UserView, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.userByID(instanceID, emailQuery, resourceOwnerQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) UserByPhone(phone, instanceID string) (*model.UserView, error) {
|
|
|
|
phoneQuery, err := query.NewUserVerifiedPhoneSearchQuery(phone, query.TextEquals)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return v.userByID(instanceID, phoneQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) UserByPhoneAndResourceOwner(phone, resourceOwner, instanceID string) (*model.UserView, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return v.userByID(instanceID, phoneQuery, resourceOwnerQuery)
|
|
|
|
}
|
|
|
|
|
2022-07-07 12:58:00 +00:00
|
|
|
func (v *View) userByID(instanceID string, queries ...query.SearchQuery) (*model.UserView, error) {
|
|
|
|
ctx := authz.WithInstanceID(context.Background(), instanceID)
|
|
|
|
|
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 {
|
|
|
|
user = new(model.UserView)
|
|
|
|
}
|
|
|
|
|
|
|
|
query, err := view.UserByIDQuery(queriedUser.ID, instanceID, user.Sequence)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
events, err := v.es.FilterEvents(ctx, query)
|
|
|
|
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
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UserIDsByDomain(domain, instanceID string) ([]string, error) {
|
|
|
|
return view.UserIDsByDomain(v.Db, userTable, domain, instanceID)
|
2020-08-18 06:57:16 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
func (v *View) SearchUsers(request *usr_model.UserSearchRequest) ([]*model.UserView, uint64, error) {
|
2020-05-18 10:06:36 +00:00
|
|
|
return view.SearchUsers(v.Db, userTable, request)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetGlobalUserByLoginName(email, instanceID string) (*model.UserView, error) {
|
|
|
|
return view.GetGlobalUserByLoginName(v.Db, userTable, email, instanceID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) UserMFAs(userID, instanceID string) ([]*usr_model.MultiFactor, error) {
|
|
|
|
return view.UserMFAs(v.Db, userTable, userID, instanceID)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) PutUser(user *model.UserView, event *models.Event) error {
|
2020-05-18 10:06:36 +00:00
|
|
|
err := view.PutUser(v.Db, userTable, user)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedUserSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) PutUsers(users []*model.UserView, event *models.Event) error {
|
2020-08-26 11:01:37 +00:00
|
|
|
err := view.PutUsers(v.Db, userTable, users...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedUserSequence(event)
|
2020-08-26 11:01:37 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) DeleteUser(userID, instanceID string, event *models.Event) error {
|
|
|
|
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
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return v.ProcessedUserSequence(event)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 13:06:48 +00:00
|
|
|
func (v *View) DeleteInstanceUsers(event *models.Event) error {
|
|
|
|
err := view.DeleteInstanceUsers(v.Db, userTable, event.InstanceID)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.ProcessedUserSequence(event)
|
|
|
|
}
|
|
|
|
|
2022-11-30 16:01:17 +00:00
|
|
|
func (v *View) UpdateOrgOwnerRemovedUsers(event *models.Event) error {
|
|
|
|
err := view.UpdateOrgOwnerRemovedUsers(v.Db, userTable, event.InstanceID, event.AggregateID)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return v.ProcessedUserSequence(event)
|
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetLatestUserSequence(instanceID string) (*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequence(userTable, instanceID)
|
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
func (v *View) GetLatestUserSequences(instanceIDs []string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return v.latestSequences(userTable, instanceIDs)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) ProcessedUserSequence(event *models.Event) error {
|
|
|
|
return v.saveCurrentSequence(userTable, event)
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
func (v *View) UpdateUserSpoolerRunTimestamp(instanceIDs []string) error {
|
|
|
|
return v.updateSpoolerRunSequence(userTable, instanceIDs)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetLatestUserFailedEvent(sequence uint64, instanceID string) (*repository.FailedEvent, error) {
|
|
|
|
return v.latestFailedEvent(userTable, instanceID, sequence)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
func (v *View) ProcessedUserFailedEvent(failedEvent *repository.FailedEvent) error {
|
2020-05-18 10:06:36 +00:00
|
|
|
return v.saveFailedEvent(failedEvent)
|
|
|
|
}
|