zitadel/internal/command/org_flow.go
Livio Amstutz ed80a8bb1e
feat: actions (#2377)
* feat(actions): begin api

* feat(actions): begin api

* api and projections

* fix: handle multiple statements for a single event in projections

* export func type

* fix test

* update to new reduce interface

* flows in login

* feat: jwt idp

* feat: command side

* feat: add tests

* actions and flows

* fill idp views with jwt idps and return apis

* add jwtEndpoint to jwt idp

* begin jwt request handling

* add feature

* merge

* merge

* handle jwt idp

* cleanup

* bug fixes

* autoregister

* get token from specific header name

* fix: proto

* fixes

* i18n

* begin tests

* fix and log http proxy

* remove docker cache

* fixes

* usergrants in actions api

* tests adn cleanup

* cleanup

* fix add user grant

* set login context

* i18n

Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2021-09-27 13:43:49 +02:00

84 lines
3.1 KiB
Go

package command
import (
"context"
"reflect"
"github.com/caos/zitadel/internal/domain"
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/repository/org"
)
func (c *Commands) ClearFlow(ctx context.Context, flowType domain.FlowType, resourceOwner string) (*domain.ObjectDetails, error) {
if !flowType.Valid() || resourceOwner == "" {
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-Dfw2h", "Errors.Flow.FlowTypeMissing")
}
existingFlow, err := c.getOrgFlowWriteModelByType(ctx, flowType, resourceOwner)
if err != nil {
return nil, err
}
if len(existingFlow.Triggers) == 0 {
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-DgGh3", "Errors.Flow.Empty")
}
orgAgg := OrgAggregateFromWriteModel(&existingFlow.WriteModel)
pushedEvents, err := c.eventstore.PushEvents(ctx, org.NewFlowClearedEvent(ctx, orgAgg, flowType))
if err != nil {
return nil, err
}
err = AppendAndReduce(existingFlow, pushedEvents...)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&existingFlow.WriteModel), nil
}
func (c *Commands) SetTriggerActions(ctx context.Context, flowType domain.FlowType, triggerType domain.TriggerType, actionIDs []string, resourceOwner string) (*domain.ObjectDetails, error) {
if !flowType.Valid() || !triggerType.Valid() || resourceOwner == "" {
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-Dfhj5", "Errors.Flow.FlowTypeMissing")
}
if !flowType.HasTrigger(triggerType) {
return nil, caos_errs.ThrowInvalidArgument(nil, "COMMAND-Dfgh6", "Errors.Flow.WrongTriggerType")
}
existingFlow, err := c.getOrgFlowWriteModelByType(ctx, flowType, resourceOwner)
if err != nil {
return nil, err
}
if reflect.DeepEqual(existingFlow.Triggers[triggerType], actionIDs) {
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-Nfh52", "Errors.Flow.NoChanges")
}
if len(actionIDs) > 0 {
exists, err := c.actionsIDsExist(ctx, actionIDs, resourceOwner)
if err != nil {
return nil, err
}
if !exists {
return nil, caos_errs.ThrowPreconditionFailed(nil, "COMMAND-dg422", "Errors.Flow.ActionIDsNotExist")
}
}
orgAgg := OrgAggregateFromWriteModel(&existingFlow.WriteModel)
pushedEvents, err := c.eventstore.PushEvents(ctx, org.NewTriggerActionsSetEvent(ctx, orgAgg, flowType, triggerType, actionIDs))
if err != nil {
return nil, err
}
err = AppendAndReduce(existingFlow, pushedEvents...)
if err != nil {
return nil, err
}
return writeModelToObjectDetails(&existingFlow.WriteModel), nil
}
func (c *Commands) getOrgFlowWriteModelByType(ctx context.Context, flowType domain.FlowType, resourceOwner string) (*OrgFlowWriteModel, error) {
flowWriteModel := NewOrgFlowWriteModel(flowType, resourceOwner)
err := c.eventstore.FilterToQueryReducer(ctx, flowWriteModel)
if err != nil {
return nil, err
}
return flowWriteModel, nil
}
func (c *Commands) actionsIDsExist(ctx context.Context, ids []string, resourceOwner string) (bool, error) {
actionIDsModel := NewActionsExistModel(ids, resourceOwner)
err := c.eventstore.FilterToQueryReducer(ctx, actionIDsModel)
return len(actionIDsModel.actionIDs) == len(actionIDsModel.checkedIDs), err
}