mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 03:24:26 +00:00
41e1a7cc7b
* one concurrent task * disable spooler * fix: improve concurrency in spooler * fix: dont block lock * fix: break if lock failed * fix: check if handler is working * fix: worker id * fix: test * fix: use limit for spoolers configured in startup.yaml * fix test * fix: factory * fix(key): only reduce if not expired * fix(searchQueryFactory): check for string-slice in aggregateID * fix(migrations): combine migrations * fix: allow saving multiple objects in one request * fix(eventstore): logging * fix(eventstore): rethink insert i locks table * fix: ignore failed tests for the moment * fix: tuubel * fix: for tests in io * fix: ignore tests for io * fix: rename concurrent tasks to workers * fix: incomment tests and remove some tests * fix: refert changes for io * refactor(eventstore): combine types of sql in one file * refactor(eventstore): logs, TODO's, tests * fix(eventstore): sql package * test(eventstore): add tests for search query factory * chore: logs * fix(spooler): optimize lock query chore(migrations): rename locks.object_type to view_name chore(migrations): refactor migrations * test: incomment tests * fix: rename PrepareSaves to PrepareBulkSave * chore: go dependencies * fix(migrations): add id in events table * refactor(lock): less magic numbers Co-authored-by: Livio Amstutz <livio.a@gmail.com>
165 lines
4.7 KiB
Go
165 lines
4.7 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
org_events "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
|
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
view_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
|
)
|
|
|
|
type User struct {
|
|
handler
|
|
eventstore eventstore.Eventstore
|
|
orgEvents *org_events.OrgEventstore
|
|
}
|
|
|
|
const (
|
|
userTable = "management.users"
|
|
)
|
|
|
|
func (p *User) ViewModel() string {
|
|
return userTable
|
|
}
|
|
|
|
func (p *User) EventQuery() (*models.SearchQuery, error) {
|
|
sequence, err := p.view.GetLatestUserSequence()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return es_models.NewSearchQuery().
|
|
AggregateTypeFilter(es_model.UserAggregate, org_es_model.OrgAggregate).
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
}
|
|
|
|
func (u *User) Reduce(event *models.Event) (err error) {
|
|
switch event.AggregateType {
|
|
case es_model.UserAggregate:
|
|
return u.ProcessUser(event)
|
|
case org_es_model.OrgAggregate:
|
|
return u.ProcessOrg(event)
|
|
default:
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (p *User) ProcessUser(event *models.Event) (err error) {
|
|
user := new(view_model.UserView)
|
|
switch event.Type {
|
|
case es_model.UserAdded,
|
|
es_model.UserRegistered:
|
|
user.AppendEvent(event)
|
|
p.fillLoginNames(user)
|
|
case es_model.UserProfileChanged,
|
|
es_model.UserEmailChanged,
|
|
es_model.UserEmailVerified,
|
|
es_model.UserPhoneChanged,
|
|
es_model.UserPhoneVerified,
|
|
es_model.UserPhoneRemoved,
|
|
es_model.UserAddressChanged,
|
|
es_model.UserDeactivated,
|
|
es_model.UserReactivated,
|
|
es_model.UserLocked,
|
|
es_model.UserUnlocked,
|
|
es_model.MfaOtpAdded,
|
|
es_model.MfaOtpVerified,
|
|
es_model.MfaOtpRemoved:
|
|
user, err = p.view.UserByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = user.AppendEvent(event)
|
|
case es_model.UserRemoved:
|
|
err = p.view.DeleteUser(event.AggregateID, event.Sequence)
|
|
default:
|
|
return p.view.ProcessedUserSequence(event.Sequence)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.view.PutUser(user, user.Sequence)
|
|
}
|
|
|
|
func (u *User) ProcessOrg(event *models.Event) (err error) {
|
|
switch event.Type {
|
|
case org_es_model.OrgDomainVerified,
|
|
org_es_model.OrgDomainRemoved,
|
|
org_es_model.OrgIamPolicyAdded,
|
|
org_es_model.OrgIamPolicyChanged,
|
|
org_es_model.OrgIamPolicyRemoved:
|
|
return u.fillLoginNamesOnOrgUsers(event)
|
|
case org_es_model.OrgDomainPrimarySet:
|
|
return u.fillPreferredLoginNamesOnOrgUsers(event)
|
|
default:
|
|
return u.view.ProcessedUserSequence(event.Sequence)
|
|
}
|
|
}
|
|
|
|
func (u *User) fillLoginNamesOnOrgUsers(event *models.Event) error {
|
|
org, err := u.orgEvents.OrgByID(context.Background(), org_model.NewOrg(event.ResourceOwner))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
policy, err := u.orgEvents.GetOrgIamPolicy(context.Background(), event.ResourceOwner)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
users, err := u.view.UsersByOrgID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, user := range users {
|
|
user.SetLoginNames(policy, org.Domains)
|
|
}
|
|
return u.view.PutUsers(users, event.Sequence)
|
|
}
|
|
|
|
func (u *User) fillPreferredLoginNamesOnOrgUsers(event *models.Event) error {
|
|
org, err := u.orgEvents.OrgByID(context.Background(), org_model.NewOrg(event.ResourceOwner))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
policy, err := u.orgEvents.GetOrgIamPolicy(context.Background(), event.ResourceOwner)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !policy.UserLoginMustBeDomain {
|
|
return nil
|
|
}
|
|
users, err := u.view.UsersByOrgID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, user := range users {
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
|
}
|
|
return u.view.PutUsers(users, event.Sequence)
|
|
}
|
|
|
|
func (u *User) fillLoginNames(user *view_model.UserView) (err error) {
|
|
org, err := u.orgEvents.OrgByID(context.Background(), org_model.NewOrg(user.ResourceOwner))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
policy, err := u.orgEvents.GetOrgIamPolicy(context.Background(), user.ResourceOwner)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
user.SetLoginNames(policy, org.Domains)
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
|
return nil
|
|
}
|
|
|
|
func (p *User) OnError(event *models.Event, err error) error {
|
|
logging.LogWithFields("SPOOL-is8wa", "id", event.AggregateID).WithError(err).Warn("something went wrong in user handler")
|
|
return spooler.HandleError(event, err, p.view.GetLatestUserFailedEvent, p.view.ProcessedUserFailedEvent, p.view.ProcessedUserSequence, p.errorCountUntilSkip)
|
|
}
|