2020-05-11 10:16:29 +00:00
|
|
|
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/spooler"
|
2020-05-26 14:46:16 +00:00
|
|
|
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"
|
2020-05-11 10:16:29 +00:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
type ProjectGrant struct {
|
2020-05-11 10:16:29 +00:00
|
|
|
handler
|
|
|
|
eventstore eventstore.Eventstore
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
2020-05-26 14:46:16 +00:00
|
|
|
orgEvents *org_event.OrgEventstore
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2020-06-15 12:50:39 +00:00
|
|
|
grantedProjectTable = "management.project_grants"
|
2020-05-11 10:16:29 +00:00
|
|
|
)
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrant) ViewModel() string {
|
2020-05-11 10:16:29 +00:00
|
|
|
return grantedProjectTable
|
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrant) EventQuery() (*models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestProjectGrantSequence()
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
return proj_event.ProjectQuery(sequence.CurrentSequence), nil
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 12:47:47 +00:00
|
|
|
func (p *ProjectGrant) Reduce(event *models.Event) (err error) {
|
2020-06-15 12:50:39 +00:00
|
|
|
grantedProject := new(view_model.ProjectGrantView)
|
2020-05-11 10:16:29 +00:00
|
|
|
switch event.Type {
|
|
|
|
case es_model.ProjectChanged:
|
2020-06-15 12:50:39 +00:00
|
|
|
project, err := p.view.ProjectByID(event.AggregateID)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
return p.updateExistingProjects(project)
|
2020-05-11 10:16:29 +00:00
|
|
|
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
|
2020-05-26 14:46:16 +00:00
|
|
|
|
|
|
|
org, err := p.orgEvents.OrgByID(context.TODO(), org_model.NewOrg(grantedProject.OrgID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-19 13:32:03 +00:00
|
|
|
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:
|
2020-05-11 10:16:29 +00:00
|
|
|
grant := new(view_model.ProjectGrant)
|
2020-08-13 07:43:47 +00:00
|
|
|
err = grant.SetData(event)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
grantedProject, err = p.view.ProjectGrantByID(grant.GrantID)
|
2020-05-11 10:16:29 +00:00
|
|
|
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
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return p.view.DeleteProjectGrant(grant.GrantID, event.Sequence)
|
2020-08-05 16:32:25 +00:00
|
|
|
case es_model.ProjectRemoved:
|
|
|
|
return p.view.DeleteProjectGrantsByProjectID(event.AggregateID)
|
2020-05-11 10:16:29 +00:00
|
|
|
default:
|
2020-06-15 12:50:39 +00:00
|
|
|
return p.view.ProcessedProjectGrantSequence(event.Sequence)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-15 12:50:39 +00:00
|
|
|
return p.view.PutProjectGrant(grantedProject)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 13:32:03 +00:00
|
|
|
func (p *ProjectGrant) fillOrgData(grantedProject *view_model.ProjectGrantView, org, resourceOwner *org_model.Org) {
|
2020-05-26 14:46:16 +00:00
|
|
|
grantedProject.OrgName = org.Name
|
2020-06-19 13:32:03 +00:00
|
|
|
grantedProject.ResourceOwnerName = resourceOwner.Name
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrant) getProject(projectID string) (*proj_model.Project, error) {
|
2020-05-11 10:16:29 +00:00
|
|
|
return p.projectEvents.ProjectByID(context.Background(), projectID)
|
|
|
|
}
|
|
|
|
|
2020-07-01 05:18:05 +00:00
|
|
|
func (p *ProjectGrant) updateExistingProjects(project *view_model.ProjectView) error {
|
|
|
|
projectGrants, err := p.view.ProjectGrantsByProjectID(project.ProjectID)
|
2020-05-11 10:16:29 +00:00
|
|
|
if err != nil {
|
|
|
|
logging.LogWithFields("SPOOL-los03", "id", project.ProjectID).WithError(err).Warn("could not update existing projects")
|
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
for _, existing := range projectGrants {
|
2020-05-11 10:16:29 +00:00
|
|
|
existing.Name = project.Name
|
2020-06-15 12:50:39 +00:00
|
|
|
err := p.view.PutProjectGrant(existing)
|
2020-07-01 05:18:05 +00:00
|
|
|
if err != nil {
|
|
|
|
logging.LogWithFields("SPOOL-sjwi3", "id", existing.ProjectID).WithError(err).Warn("could not update existing project")
|
|
|
|
return err
|
|
|
|
}
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
2020-07-01 05:18:05 +00:00
|
|
|
|
|
|
|
return p.view.ProcessedProjectGrantSequence(project.Sequence)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 12:50:39 +00:00
|
|
|
func (p *ProjectGrant) OnError(event *models.Event, err error) error {
|
2020-05-11 10:16:29 +00:00
|
|
|
logging.LogWithFields("SPOOL-is8wa", "id", event.AggregateID).WithError(err).Warn("something went wrong in granted projecthandler")
|
2020-06-15 12:50:39 +00:00
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestProjectGrantFailedEvent, p.view.ProcessedProjectGrantFailedEvent, p.view.ProcessedProjectGrantSequence, p.errorCountUntilSkip)
|
2020-05-11 10:16:29 +00:00
|
|
|
}
|