2020-10-16 05:49:38 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/caos/logging"
|
2020-12-18 15:47:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2020-10-16 05:49:38 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/query"
|
2020-10-16 05:49:38 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/spooler"
|
|
|
|
proj_event "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
2020-12-18 15:47:45 +00:00
|
|
|
proj_events "github.com/caos/zitadel/internal/project/repository/eventsourcing"
|
|
|
|
"github.com/caos/zitadel/internal/project/repository/eventsourcing/model"
|
2020-10-16 05:49:38 +00:00
|
|
|
view_model "github.com/caos/zitadel/internal/project/repository/view/model"
|
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
const (
|
|
|
|
projectRoleTable = "auth.project_roles"
|
|
|
|
)
|
|
|
|
|
2020-10-16 05:49:38 +00:00
|
|
|
type ProjectRole struct {
|
|
|
|
handler
|
|
|
|
projectEvents *proj_event.ProjectEventstore
|
2020-12-18 15:47:45 +00:00
|
|
|
subscription *eventstore.Subscription
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func newProjectRole(
|
|
|
|
handler handler,
|
|
|
|
projectEvents *proj_events.ProjectEventstore,
|
|
|
|
) *ProjectRole {
|
|
|
|
h := &ProjectRole{
|
|
|
|
handler: handler,
|
|
|
|
projectEvents: projectEvents,
|
|
|
|
}
|
|
|
|
|
|
|
|
h.subscribe()
|
|
|
|
|
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *ProjectRole) subscribe() {
|
|
|
|
k.subscription = k.es.Subscribe(k.AggregateTypes()...)
|
|
|
|
go func() {
|
|
|
|
for event := range k.subscription.Events {
|
|
|
|
query.ReduceEvent(k, event)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-10-16 05:49:38 +00:00
|
|
|
|
|
|
|
func (p *ProjectRole) ViewModel() string {
|
|
|
|
return projectRoleTable
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (_ *ProjectRole) AggregateTypes() []es_models.AggregateType {
|
|
|
|
return []es_models.AggregateType{model.ProjectAggregate}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectRole) CurrentSequence(event *models.Event) (uint64, error) {
|
|
|
|
sequence, err := p.view.GetLatestProjectRoleSequence(string(event.AggregateType))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return sequence.CurrentSequence, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectRole) EventQuery() (*es_models.SearchQuery, error) {
|
|
|
|
sequence, err := p.view.GetLatestProjectRoleSequence("")
|
2020-10-16 05:49:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return proj_events.ProjectQuery(sequence.CurrentSequence), nil
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (p *ProjectRole) Reduce(event *es_models.Event) (err error) {
|
2020-10-16 05:49:38 +00:00
|
|
|
role := new(view_model.ProjectRoleView)
|
|
|
|
switch event.Type {
|
2020-12-18 15:47:45 +00:00
|
|
|
case model.ProjectRoleAdded:
|
2020-10-16 05:49:38 +00:00
|
|
|
err = role.AppendEvent(event)
|
2020-12-18 15:47:45 +00:00
|
|
|
case model.ProjectRoleChanged:
|
2020-10-16 05:49:38 +00:00
|
|
|
err = role.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
role, err = p.view.ProjectRoleByIDs(event.AggregateID, event.ResourceOwner, role.Key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = role.AppendEvent(event)
|
2020-12-18 15:47:45 +00:00
|
|
|
case model.ProjectRoleRemoved:
|
2020-10-16 05:49:38 +00:00
|
|
|
err = role.SetData(event)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return p.view.DeleteProjectRole(event.AggregateID, event.ResourceOwner, role.Key, event)
|
|
|
|
case model.ProjectRemoved:
|
2020-12-21 17:42:34 +00:00
|
|
|
err := p.view.DeleteProjectRolesByProjectID(event.AggregateID)
|
|
|
|
if err == nil {
|
|
|
|
return p.view.ProcessedProjectRoleSequence(event)
|
|
|
|
}
|
2020-10-16 05:49:38 +00:00
|
|
|
default:
|
2020-12-18 15:47:45 +00:00
|
|
|
return p.view.ProcessedProjectRoleSequence(event)
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-18 15:47:45 +00:00
|
|
|
return p.view.PutProjectRole(role, event)
|
2020-10-16 05:49:38 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (p *ProjectRole) OnError(event *es_models.Event, err error) error {
|
2020-10-16 05:49:38 +00:00
|
|
|
logging.LogWithFields("SPOOL-lso9w", "id", event.AggregateID).WithError(err).Warn("something went wrong in project role handler")
|
|
|
|
return spooler.HandleError(event, err, p.view.GetLatestProjectRoleFailedEvent, p.view.ProcessedProjectRoleFailedEvent, p.view.ProcessedProjectRoleSequence, p.errorCountUntilSkip)
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
|
|
|
|
func (p *ProjectRole) OnSuccess() error {
|
|
|
|
return spooler.HandleSuccess(p.view.UpdateProjectRoleSpoolerRunTimestamp)
|
|
|
|
}
|