2020-07-30 12:37:55 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/logging"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-07-30 12:37:55 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-07-30 12:37:55 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
2020-12-18 15:47:45 +00:00
|
|
|
iam_es_model "github.com/caos/zitadel/internal/iam/repository/eventsourcing/model"
|
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
org_event "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
2020-07-30 12:37:55 +00:00
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
2020-12-18 15:47:45 +00:00
|
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
proj_es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
2020-07-30 12:37:55 +00:00
|
|
|
usr_model "github.com/caos/zitadel/internal/user/model"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/user/repository/eventsourcing/model"
|
2020-07-30 12:37:55 +00:00
|
|
|
usr_es_model "github.com/caos/zitadel/internal/user/repository/view/model"
|
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
userMembershipTable = "management.user_memberships"
|
|
|
|
)
|
|
|
|
|
2020-07-30 12:37:55 +00:00
|
|
|
type UserMembership struct {
|
|
|
|
handler
|
|
|
|
orgEvents *org_event.OrgEventstore
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
subscription *eventstore.Subscription
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newUserMembership(
|
|
|
|
handler handler,
|
|
|
|
orgEvents *org_event.OrgEventstore,
|
|
|
|
projectEvents *proj_event.ProjectEventstore,
|
|
|
|
) *UserMembership {
|
|
|
|
h := &UserMembership{
|
|
|
|
handler: handler,
|
|
|
|
orgEvents: orgEvents,
|
|
|
|
projectEvents: projectEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
h.subscribe()
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMembership) subscribe() {
|
|
|
|
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range m.subscription.Events {
|
|
|
|
query.ReduceEvent(m, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-07-30 12:37:55 +00:00
|
|
|
|
|
|
|
func (m *UserMembership) ViewModel() string {
|
|
|
|
return userMembershipTable
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *UserMembership) AggregateTypes() []es_models.AggregateType {
|
|
|
|
return []es_models.AggregateType{iam_es_model.IAMAggregate, org_es_model.OrgAggregate, proj_es_model.ProjectAggregate, model.UserAggregate}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *UserMembership) CurrentSequence(event *models.Event) (uint64, error) {
|
|
|
|
sequence, err := u.view.GetLatestUserMembershipSequence(string(event.AggregateType))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMembership) EventQuery() (*es_models.SearchQuery, error) {
|
|
|
|
sequence, err := m.view.GetLatestUserMembershipSequence("")
|
2020-07-30 12:37:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return es_models.NewSearchQuery().
|
2020-12-18 15:47:45 +00:00
|
|
|
AggregateTypeFilter(m.AggregateTypes()...).
|
2020-07-30 12:37:55 +00:00
|
|
|
LatestSequenceFilter(sequence.CurrentSequence), nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) Reduce(event *es_models.Event) (err error) {
|
2020-07-30 12:37:55 +00:00
|
|
|
switch event.AggregateType {
|
2020-08-26 07:56:23 +00:00
|
|
|
case iam_es_model.IAMAggregate:
|
2020-12-21 17:42:34 +00:00
|
|
|
err = m.processIAM(event)
|
2020-07-30 12:37:55 +00:00
|
|
|
case org_es_model.OrgAggregate:
|
|
|
|
err = m.processOrg(event)
|
|
|
|
case proj_es_model.ProjectAggregate:
|
|
|
|
err = m.processProject(event)
|
2020-10-07 06:16:42 +00:00
|
|
|
case model.UserAggregate:
|
|
|
|
err = m.processUser(event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-21 17:42:34 +00:00
|
|
|
func (m *UserMembership) processIAM(event *es_models.Event) (err error) {
|
2020-08-25 14:08:51 +00:00
|
|
|
member := new(usr_es_model.UserMembershipView)
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch event.Type {
|
2020-08-26 07:56:23 +00:00
|
|
|
case iam_es_model.IAMMemberAdded:
|
2020-08-25 14:08:51 +00:00
|
|
|
m.fillIamDisplayName(member)
|
2020-08-26 07:56:23 +00:00
|
|
|
case iam_es_model.IAMMemberChanged:
|
2020-08-25 14:08:51 +00:00
|
|
|
member, err = m.view.UserMembershipByIDs(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeIam)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = member.AppendEvent(event)
|
2020-08-26 07:56:23 +00:00
|
|
|
case iam_es_model.IAMMemberRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembership(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeIam, event)
|
2020-08-25 14:08:51 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.ProcessedUserMembershipSequence(event)
|
2020-08-25 14:08:51 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.PutUserMembership(member, event)
|
2020-08-25 14:08:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMembership) fillIamDisplayName(member *usr_es_model.UserMembershipView) {
|
|
|
|
member.DisplayName = member.AggregateID
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) processOrg(event *es_models.Event) (err error) {
|
2020-07-30 12:37:55 +00:00
|
|
|
member := new(usr_es_model.UserMembershipView)
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch event.Type {
|
|
|
|
case org_es_model.OrgMemberAdded:
|
|
|
|
err = m.fillOrgDisplayName(member)
|
|
|
|
case org_es_model.OrgMemberChanged:
|
|
|
|
member, err = m.view.UserMembershipByIDs(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeOrganisation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
case org_es_model.OrgMemberRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembership(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeOrganisation, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
case org_es_model.OrgChanged:
|
2020-10-16 05:49:38 +00:00
|
|
|
return m.updateOrgDisplayName(event)
|
2020-07-30 12:37:55 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.ProcessedUserMembershipSequence(event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.PutUserMembership(member, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMembership) fillOrgDisplayName(member *usr_es_model.UserMembershipView) (err error) {
|
|
|
|
org, err := m.orgEvents.OrgByID(context.Background(), org_model.NewOrg(member.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
member.DisplayName = org.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) updateOrgDisplayName(event *es_models.Event) error {
|
2020-07-30 12:37:55 +00:00
|
|
|
org, err := m.orgEvents.OrgByID(context.Background(), org_model.NewOrg(event.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships, err := m.view.UserMembershipsByAggregateID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, membership := range memberships {
|
|
|
|
membership.DisplayName = org.Name
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.BulkPutUserMemberships(memberships, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) processProject(event *es_models.Event) (err error) {
|
2020-07-30 12:37:55 +00:00
|
|
|
member := new(usr_es_model.UserMembershipView)
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
switch event.Type {
|
|
|
|
case proj_es_model.ProjectMemberAdded, proj_es_model.ProjectGrantMemberAdded:
|
|
|
|
err = m.fillProjectDisplayName(member)
|
|
|
|
case proj_es_model.ProjectMemberChanged:
|
|
|
|
member, err = m.view.UserMembershipByIDs(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeProject)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
case proj_es_model.ProjectMemberRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembership(member.UserID, event.AggregateID, event.AggregateID, usr_model.MemberTypeProject, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
case proj_es_model.ProjectGrantMemberChanged:
|
|
|
|
member, err = m.view.UserMembershipByIDs(member.UserID, event.AggregateID, member.ObjectID, usr_model.MemberTypeProjectGrant)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = member.AppendEvent(event)
|
|
|
|
case proj_es_model.ProjectGrantMemberRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembership(member.UserID, event.AggregateID, member.ObjectID, usr_model.MemberTypeProjectGrant, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
case proj_es_model.ProjectChanged:
|
2020-10-16 05:49:38 +00:00
|
|
|
return m.updateProjectDisplayName(event)
|
2020-10-13 05:46:44 +00:00
|
|
|
case proj_es_model.ProjectRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembershipsByAggregateID(event.AggregateID, event)
|
2020-10-13 05:46:44 +00:00
|
|
|
case proj_es_model.ProjectGrantRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembershipsByAggregateIDAndObjectID(event.AggregateID, member.ObjectID, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.ProcessedUserMembershipSequence(event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.PutUserMembership(member, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *UserMembership) fillProjectDisplayName(member *usr_es_model.UserMembershipView) (err error) {
|
|
|
|
project, err := m.projectEvents.ProjectByID(context.Background(), member.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
member.DisplayName = project.Name
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) updateProjectDisplayName(event *es_models.Event) error {
|
2020-07-30 12:37:55 +00:00
|
|
|
project, err := m.projectEvents.ProjectByID(context.Background(), event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
memberships, err := m.view.UserMembershipsByAggregateID(event.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, membership := range memberships {
|
|
|
|
membership.DisplayName = project.Name
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.BulkPutUserMemberships(memberships, event)
|
2020-07-30 12:37:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) processUser(event *es_models.Event) (err error) {
|
2020-10-07 06:16:42 +00:00
|
|
|
switch event.Type {
|
|
|
|
case model.UserRemoved:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.DeleteUserMembershipsByUserID(event.AggregateID, event)
|
2020-10-07 06:16:42 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return m.view.ProcessedUserMembershipSequence(event)
|
2020-10-07 06:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (m *UserMembership) OnError(event *es_models.Event, err error) error {
|
2020-10-16 05:49:38 +00:00
|
|
|
logging.LogWithFields("SPOOL-Fwer2", "id", event.AggregateID).WithError(err).Warn("something went wrong in user membership handler")
|
2020-07-30 12:37:55 +00:00
|
|
|
return spooler.HandleError(event, err, m.view.GetLatestUserMembershipFailedEvent, m.view.ProcessedUserMembershipFailedEvent, m.view.ProcessedUserMembershipSequence, m.errorCountUntilSkip)
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
|
|
|
|
func (m *UserMembership) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(m.view.UpdateUserMembershipSpoolerRunTimestamp)
|
|
|
|
}
|