mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
3118a99c1e
* start sub * start implement subsciptions * start subscription * implementation for member done * admin done * fix: tests * extend handlers * prepary notification * no errors in adminapi * changed current sequence in all packages * ignore mocks * works * subscriptions as singleton * tests * refactor: rename function scope var * fix: process ALL previous sequences * fix: spooler and pubsub * handler check * fix: process events until all done * fix break on query err * fix: handler * fix: process sequence or return error * check aggregate id * fix: log only in error case * fix tests * fix: handlers * fix: spooler * fix: spooler * fix: tests * fix: continue Co-authored-by: Livio Amstutz <livio.a@gmail.com>
170 lines
5.0 KiB
Go
170 lines
5.0 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/caos/logging"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
org_event "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
|
proj_model "github.com/caos/zitadel/internal/project/model"
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
es_model "github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
|
view_model "github.com/caos/zitadel/internal/project/repository/view/model"
|
|
)
|
|
|
|
const (
|
|
grantedProjectTable = "management.project_grants"
|
|
)
|
|
|
|
type ProjectGrant struct {
|
|
handler
|
|
projectEvents *proj_event.ProjectEventstore
|
|
orgEvents *org_event.OrgEventstore
|
|
subscription *eventstore.Subscription
|
|
}
|
|
|
|
func newProjectGrant(
|
|
handler handler,
|
|
projectEvents *proj_event.ProjectEventstore,
|
|
orgEvents *org_event.OrgEventstore,
|
|
) *ProjectGrant {
|
|
h := &ProjectGrant{
|
|
handler: handler,
|
|
projectEvents: projectEvents,
|
|
orgEvents: orgEvents,
|
|
}
|
|
|
|
h.subscribe()
|
|
|
|
return h
|
|
}
|
|
|
|
func (m *ProjectGrant) subscribe() {
|
|
m.subscription = m.es.Subscribe(m.AggregateTypes()...)
|
|
go func() {
|
|
for event := range m.subscription.Events {
|
|
query.ReduceEvent(m, event)
|
|
}
|
|
}()
|
|
}
|
|
|
|
func (p *ProjectGrant) ViewModel() string {
|
|
return grantedProjectTable
|
|
}
|
|
|
|
func (_ *ProjectGrant) AggregateTypes() []models.AggregateType {
|
|
return []models.AggregateType{es_model.ProjectAggregate}
|
|
}
|
|
|
|
func (p *ProjectGrant) CurrentSequence(event *models.Event) (uint64, error) {
|
|
sequence, err := p.view.GetLatestProjectGrantSequence(string(event.AggregateType))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return sequence.CurrentSequence, nil
|
|
}
|
|
|
|
func (p *ProjectGrant) EventQuery() (*models.SearchQuery, error) {
|
|
sequence, err := p.view.GetLatestProjectGrantSequence("")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return proj_event.ProjectQuery(sequence.CurrentSequence), nil
|
|
}
|
|
|
|
func (p *ProjectGrant) Reduce(event *models.Event) (err error) {
|
|
grantedProject := new(view_model.ProjectGrantView)
|
|
switch event.Type {
|
|
case es_model.ProjectChanged:
|
|
project, err := p.view.ProjectByID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.updateExistingProjects(project, event)
|
|
case es_model.ProjectGrantAdded:
|
|
err = grantedProject.AppendEvent(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
project, err := p.getProject(grantedProject.ProjectID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantedProject.Name = project.Name
|
|
|
|
org, err := p.orgEvents.OrgByID(context.TODO(), org_model.NewOrg(grantedProject.OrgID))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resourceOwner, err := p.orgEvents.OrgByID(context.TODO(), org_model.NewOrg(grantedProject.ResourceOwner))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.fillOrgData(grantedProject, org, resourceOwner)
|
|
case es_model.ProjectGrantChanged, es_model.ProjectGrantCascadeChanged:
|
|
grant := new(view_model.ProjectGrant)
|
|
err = grant.SetData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
grantedProject, err = p.view.ProjectGrantByID(grant.GrantID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = grantedProject.AppendEvent(event)
|
|
case es_model.ProjectGrantRemoved:
|
|
grant := new(view_model.ProjectGrant)
|
|
err := grant.SetData(event)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.view.DeleteProjectGrant(grant.GrantID, event)
|
|
case es_model.ProjectRemoved:
|
|
err = p.view.DeleteProjectGrantsByProjectID(event.AggregateID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.view.ProcessedProjectGrantSequence(event)
|
|
default:
|
|
return p.view.ProcessedProjectGrantSequence(event)
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return p.view.PutProjectGrant(grantedProject, event)
|
|
}
|
|
|
|
func (p *ProjectGrant) fillOrgData(grantedProject *view_model.ProjectGrantView, org, resourceOwner *org_model.Org) {
|
|
grantedProject.OrgName = org.Name
|
|
grantedProject.ResourceOwnerName = resourceOwner.Name
|
|
}
|
|
|
|
func (p *ProjectGrant) getProject(projectID string) (*proj_model.Project, error) {
|
|
return p.projectEvents.ProjectByID(context.Background(), projectID)
|
|
}
|
|
|
|
func (p *ProjectGrant) updateExistingProjects(project *view_model.ProjectView, event *models.Event) error {
|
|
projectGrants, err := p.view.ProjectGrantsByProjectID(project.ProjectID)
|
|
if err != nil {
|
|
logging.LogWithFields("SPOOL-los03", "id", project.ProjectID).WithError(err).Warn("could not update existing projects")
|
|
}
|
|
for _, existingGrant := range projectGrants {
|
|
existingGrant.Name = project.Name
|
|
}
|
|
return p.view.PutProjectGrants(projectGrants, event)
|
|
}
|
|
|
|
func (p *ProjectGrant) OnError(event *models.Event, err error) error {
|
|
logging.LogWithFields("SPOOL-sQqOg", "id", event.AggregateID).WithError(err).Warn("something went wrong in granted projecthandler")
|
|
return spooler.HandleError(event, err, p.view.GetLatestProjectGrantFailedEvent, p.view.ProcessedProjectGrantFailedEvent, p.view.ProcessedProjectGrantSequence, p.errorCountUntilSkip)
|
|
}
|
|
|
|
func (p *ProjectGrant) OnSuccess() error {
|
|
return spooler.HandleSuccess(p.view.UpdateProjectGrantSpoolerRunTimestamp)
|
|
}
|