2020-05-18 10:06:36 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-06-16 09:40:18 +00:00
|
|
|
"context"
|
2020-07-08 07:48:11 +00:00
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
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"
|
2020-05-18 10:06:36 +00:00
|
|
|
es_model "github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
view_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
|
|
|
handler
|
|
|
|
eventstore eventstore.Eventstore
|
2020-06-16 09:40:18 +00:00
|
|
|
orgEvents *org_events.OrgEventstore
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
userTable = "auth.users"
|
|
|
|
)
|
|
|
|
|
2020-08-18 06:57:16 +00:00
|
|
|
func (u *User) ViewModel() string {
|
2020-05-18 10:06:36 +00:00
|
|
|
return userTable
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:57:16 +00:00
|
|
|
func (u *User) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := u.view.GetLatestUserSequence()
|
2020-05-18 10:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return es_models.NewSearchQuery().
|
|
|
|
AggregateTypeFilter(es_model.UserAggregate, org_es_model.OrgAggregate).
|
2020-07-15 11:24:36 +00:00
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
func (u *User) Reduce(event *models.Event) (err error) {
|
2020-06-16 09:40:18 +00:00
|
|
|
switch event.AggregateType {
|
|
|
|
case es_model.UserAggregate:
|
|
|
|
return u.ProcessUser(event)
|
|
|
|
case org_es_model.OrgAggregate:
|
|
|
|
return u.ProcessOrg(event)
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:57:16 +00:00
|
|
|
func (u *User) ProcessUser(event *models.Event) (err error) {
|
2020-05-18 10:06:36 +00:00
|
|
|
user := new(view_model.UserView)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.UserAdded,
|
|
|
|
es_model.UserRegistered:
|
2020-08-13 07:43:47 +00:00
|
|
|
err = user.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
u.fillLoginNames(user)
|
2020-05-18 10:06:36 +00:00
|
|
|
case es_model.UserProfileChanged,
|
|
|
|
es_model.UserEmailChanged,
|
|
|
|
es_model.UserEmailVerified,
|
|
|
|
es_model.UserPhoneChanged,
|
|
|
|
es_model.UserPhoneVerified,
|
2020-07-06 13:48:24 +00:00
|
|
|
es_model.UserPhoneRemoved,
|
2020-05-18 10:06:36 +00:00
|
|
|
es_model.UserAddressChanged,
|
|
|
|
es_model.UserDeactivated,
|
|
|
|
es_model.UserReactivated,
|
|
|
|
es_model.UserLocked,
|
|
|
|
es_model.UserUnlocked,
|
|
|
|
es_model.MfaOtpAdded,
|
|
|
|
es_model.MfaOtpVerified,
|
2020-06-05 05:50:04 +00:00
|
|
|
es_model.MfaOtpRemoved,
|
|
|
|
es_model.MfaInitSkipped,
|
|
|
|
es_model.UserPasswordChanged:
|
2020-08-18 06:57:16 +00:00
|
|
|
user, err = u.view.UserByID(event.AggregateID)
|
2020-05-18 10:06:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = user.AppendEvent(event)
|
2020-08-18 06:57:16 +00:00
|
|
|
case es_model.DomainClaimed:
|
|
|
|
user, err = u.view.UserByID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = user.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = u.fillLoginNames(user)
|
2020-05-28 04:49:22 +00:00
|
|
|
case es_model.UserRemoved:
|
2020-08-18 06:57:16 +00:00
|
|
|
err = u.view.DeleteUser(event.AggregateID, event.Sequence)
|
2020-05-18 10:06:36 +00:00
|
|
|
default:
|
2020-08-18 06:57:16 +00:00
|
|
|
return u.view.ProcessedUserSequence(event.Sequence)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
return u.view.PutUser(user, user.Sequence)
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
policy, err := u.orgEvents.GetOrgIAMPolicy(context.Background(), user.ResourceOwner)
|
2020-06-16 09:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-17 05:25:04 +00:00
|
|
|
user.SetLoginNames(policy, org.Domains)
|
2020-06-22 08:38:29 +00:00
|
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
2020-06-16 09:40:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) ProcessOrg(event *models.Event) (err error) {
|
|
|
|
switch event.Type {
|
|
|
|
case org_es_model.OrgDomainVerified,
|
|
|
|
org_es_model.OrgDomainRemoved,
|
2020-08-26 07:56:23 +00:00
|
|
|
org_es_model.OrgIAMPolicyAdded,
|
|
|
|
org_es_model.OrgIAMPolicyChanged,
|
|
|
|
org_es_model.OrgIAMPolicyRemoved:
|
2020-06-16 09:40:18 +00:00
|
|
|
return u.fillLoginNamesOnOrgUsers(event)
|
2020-06-17 05:25:04 +00:00
|
|
|
case org_es_model.OrgDomainPrimarySet:
|
|
|
|
return u.fillPreferredLoginNamesOnOrgUsers(event)
|
2020-06-16 09:40:18 +00:00
|
|
|
default:
|
|
|
|
return u.view.ProcessedUserSequence(event.Sequence)
|
|
|
|
}
|
|
|
|
}
|
2020-06-17 05:25:04 +00:00
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
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
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
policy, err := u.orgEvents.GetOrgIAMPolicy(context.Background(), event.ResourceOwner)
|
2020-06-16 09:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users, err := u.view.UsersByOrgID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
2020-06-17 05:25:04 +00:00
|
|
|
user.SetLoginNames(policy, org.Domains)
|
2020-06-16 09:40:18 +00:00
|
|
|
err := u.view.PutUser(user, event.Sequence)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 05:25:04 +00:00
|
|
|
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
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
policy, err := u.orgEvents.GetOrgIAMPolicy(context.Background(), event.ResourceOwner)
|
2020-06-17 05:25:04 +00:00
|
|
|
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 {
|
2020-06-22 08:38:29 +00:00
|
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
2020-06-17 05:25:04 +00:00
|
|
|
err := u.view.PutUser(user, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-18 06:57:16 +00:00
|
|
|
func (u *User) OnError(event *models.Event, err error) error {
|
2020-05-18 10:06:36 +00:00
|
|
|
logging.LogWithFields("SPOOL-is8wa", "id", event.AggregateID).WithError(err).Warn("something went wrong in user handler")
|
2020-08-18 06:57:16 +00:00
|
|
|
return spooler.HandleError(event, err, u.view.GetLatestUserFailedEvent, u.view.ProcessedUserFailedEvent, u.view.ProcessedUserSequence, u.errorCountUntilSkip)
|
2020-05-18 10:06:36 +00:00
|
|
|
}
|