Silvan 3025ac577b
feat: org queries (#136)
* search orgs

* org by domain

* member spooler

* member

* get roles

* tests

* types duration

* use default func for renew

* correct database

* reorder migrations

* delete unused consts

* move get roles to internal

* use prepared org by domain

* implement org in other objects

* add eventstores
2020-05-26 16:46:16 +02:00

134 lines
4.3 KiB
Go

package handler
import (
"context"
"time"
"github.com/caos/logging"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/models"
"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"
)
type GrantedProject struct {
handler
eventstore eventstore.Eventstore
projectEvents *proj_event.ProjectEventstore
orgEvents *org_event.OrgEventstore
}
const (
grantedProjectTable = "management.granted_projects"
)
func (p *GrantedProject) MinimumCycleDuration() time.Duration { return p.cycleDuration }
func (p *GrantedProject) ViewModel() string {
return grantedProjectTable
}
func (p *GrantedProject) EventQuery() (*models.SearchQuery, error) {
sequence, err := p.view.GetLatestGrantedProjectSequence()
if err != nil {
return nil, err
}
return proj_event.ProjectQuery(sequence), nil
}
func (p *GrantedProject) Process(event *models.Event) (err error) {
grantedProject := new(view_model.GrantedProjectView)
switch event.Type {
case es_model.ProjectAdded:
grantedProject.AppendEvent(event)
case es_model.ProjectChanged:
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, event.ResourceOwner)
if err != nil {
return err
}
err = grantedProject.AppendEvent(event)
if err != nil {
return err
}
p.updateExistingProjects(grantedProject)
case es_model.ProjectDeactivated, es_model.ProjectReactivated:
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, event.ResourceOwner)
if err != nil {
return err
}
err = grantedProject.AppendEvent(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
}
p.fillOrgData(grantedProject, org)
case es_model.ProjectGrantChanged:
grant := new(view_model.ProjectGrant)
err := grant.SetData(event)
if err != nil {
return err
}
grantedProject, err = p.view.GrantedProjectByIDs(event.AggregateID, grant.GrantedOrgID)
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.DeleteGrantedProject(event.AggregateID, grant.GrantedOrgID, event.Sequence)
default:
return p.view.ProcessedGrantedProjectSequence(event.Sequence)
}
if err != nil {
return err
}
return p.view.PutGrantedProject(grantedProject)
}
func (p *GrantedProject) fillOrgData(grantedProject *view_model.GrantedProjectView, org *org_model.Org) {
grantedProject.OrgDomain = org.Domain
grantedProject.OrgName = org.Name
}
func (p *GrantedProject) getProject(projectID string) (*proj_model.Project, error) {
return p.projectEvents.ProjectByID(context.Background(), projectID)
}
func (p *GrantedProject) updateExistingProjects(project *view_model.GrantedProjectView) {
projects, err := p.view.GrantedProjectsByID(project.ProjectID)
if err != nil {
logging.LogWithFields("SPOOL-los03", "id", project.ProjectID).WithError(err).Warn("could not update existing projects")
}
for _, existing := range projects {
existing.Name = project.Name
err := p.view.PutGrantedProject(existing)
logging.LogWithFields("SPOOL-sjwi3", "id", existing.ProjectID).WithError(err).Warn("could not update existing project")
}
}
func (p *GrantedProject) OnError(event *models.Event, err error) error {
logging.LogWithFields("SPOOL-is8wa", "id", event.AggregateID).WithError(err).Warn("something went wrong in granted projecthandler")
return spooler.HandleError(event, err, p.view.GetLatestGrantedProjectFailedEvent, p.view.ProcessedGrantedProjectFailedEvent, p.view.ProcessedGrantedProjectSequence, p.errorCountUntilSkip)
}