2020-05-20 12:28:08 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2020-07-07 17:31:51 +00:00
|
|
|
"context"
|
2020-05-20 12:28:08 +00:00
|
|
|
|
|
|
|
"github.com/caos/logging"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-07-28 07:42:21 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-05-20 12:28:08 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
2020-07-28 07:42:21 +00:00
|
|
|
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"
|
2020-05-20 12:28:08 +00:00
|
|
|
view_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NotifyUser struct {
|
|
|
|
handler
|
|
|
|
eventstore eventstore.Eventstore
|
2020-07-07 17:31:51 +00:00
|
|
|
orgEvents *org_events.OrgEventstore
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
userTable = "notification.notify_users"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *NotifyUser) ViewModel() string {
|
|
|
|
return userTable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifyUser) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestNotifyUserSequence()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-07 17:31:51 +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-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 17:31:51 +00:00
|
|
|
func (u *NotifyUser) 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 (u *NotifyUser) ProcessUser(event *models.Event) (err error) {
|
2020-05-20 12:28:08 +00:00
|
|
|
user := new(view_model.NotifyUser)
|
|
|
|
switch event.Type {
|
|
|
|
case es_model.UserAdded,
|
|
|
|
es_model.UserRegistered:
|
|
|
|
user.AppendEvent(event)
|
2020-07-07 17:31:51 +00:00
|
|
|
u.fillLoginNames(user)
|
2020-05-20 12:28:08 +00:00
|
|
|
case es_model.UserProfileChanged,
|
|
|
|
es_model.UserEmailChanged,
|
|
|
|
es_model.UserEmailVerified,
|
|
|
|
es_model.UserPhoneChanged,
|
2020-07-06 13:48:24 +00:00
|
|
|
es_model.UserPhoneVerified,
|
|
|
|
es_model.UserPhoneRemoved:
|
2020-07-07 17:31:51 +00:00
|
|
|
user, err = u.view.NotifyUserByID(event.AggregateID)
|
2020-05-20 12:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = user.AppendEvent(event)
|
2020-08-27 15:18:23 +00:00
|
|
|
case es_model.DomainClaimed,
|
|
|
|
es_model.UserUserNameChanged:
|
|
|
|
user, err = u.view.NotifyUserByID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = user.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
u.fillLoginNames(user)
|
2020-05-28 04:49:22 +00:00
|
|
|
case es_model.UserRemoved:
|
2020-07-07 17:31:51 +00:00
|
|
|
err = u.view.DeleteNotifyUser(event.AggregateID, event.Sequence)
|
2020-05-20 12:28:08 +00:00
|
|
|
default:
|
2020-07-07 17:31:51 +00:00
|
|
|
return u.view.ProcessedNotifyUserSequence(event.Sequence)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return u.view.PutNotifyUser(user, user.Sequence)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *NotifyUser) 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-07-07 17:31:51 +00:00
|
|
|
return u.fillLoginNamesOnOrgUsers(event)
|
|
|
|
case org_es_model.OrgDomainPrimarySet:
|
|
|
|
return u.fillPreferredLoginNamesOnOrgUsers(event)
|
|
|
|
default:
|
|
|
|
return u.view.ProcessedNotifyUserSequence(event.Sequence)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *NotifyUser) 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-07-07 17:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
users, err := u.view.NotifyUsersByOrgID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
user.SetLoginNames(policy, org.Domains)
|
|
|
|
err := u.view.PutNotifyUser(user, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-08-06 13:03:03 +00:00
|
|
|
return u.view.ProcessedNotifyUserSequence(event.Sequence)
|
2020-07-07 17:31:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *NotifyUser) 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-07-07 17:31:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !policy.UserLoginMustBeDomain {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
users, err := u.view.NotifyUsersByOrgID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, user := range users {
|
|
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
|
|
|
err := u.view.PutNotifyUser(user, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *NotifyUser) fillLoginNames(user *view_model.NotifyUser) (err error) {
|
|
|
|
org, err := u.orgEvents.OrgByID(context.Background(), org_model.NewOrg(user.ResourceOwner))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
policy, err := u.orgEvents.GetOrgIAMPolicy(context.Background(), user.ResourceOwner)
|
2020-05-20 12:28:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 17:31:51 +00:00
|
|
|
user.SetLoginNames(policy, org.Domains)
|
|
|
|
user.PreferredLoginName = user.GenerateLoginName(org.GetPrimaryDomain().Domain, policy.UserLoginMustBeDomain)
|
|
|
|
return nil
|
2020-05-20 12:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *NotifyUser) OnError(event *models.Event, err error) error {
|
|
|
|
logging.LogWithFields("SPOOL-9spwf", "id", event.AggregateID).WithError(err).Warn("something went wrong in notify user handler")
|
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestNotifyUserFailedEvent, p.view.ProcessedNotifyUserFailedEvent, p.view.ProcessedNotifyUserSequence, p.errorCountUntilSkip)
|
|
|
|
}
|