2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-04 13:52:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-10 09:48:40 +00:00
|
|
|
func (r *CommandSide) AddOrgIAMPolicy(ctx context.Context, resourceOwner string, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
|
|
|
addedPolicy := NewORGOrgIAMPolicyWriteModel(resourceOwner)
|
2021-01-08 10:33:45 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&addedPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
event, err := r.addOrgIAMPolicy(ctx, orgAgg, addedPolicy, policy)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, event)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return orgWriteModelToOrgIAMPolicy(addedPolicy), nil
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
func (r *CommandSide) addOrgIAMPolicy(ctx context.Context, orgAgg *eventstore.Aggregate, addedPolicy *ORGOrgIAMPolicyWriteModel, policy *domain.OrgIAMPolicy) (eventstore.EventPusher, error) {
|
2021-01-08 10:33:45 +00:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "ORG-1M8ds", "Errors.Org.OrgIAMPolicy.AlreadyExists")
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return org.NewOrgIAMPolicyAddedEvent(ctx, orgAgg, policy.UserLoginMustBeDomain), nil
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 09:48:40 +00:00
|
|
|
func (r *CommandSide) ChangeOrgIAMPolicy(ctx context.Context, resourceOwner string, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
|
|
|
existingPolicy, err := r.orgIAMPolicyWriteModelByID(ctx, resourceOwner)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "ORG-2N9sd", "Errors.Org.OrgIAMPolicy.NotFound")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, orgAgg, policy.UserLoginMustBeDomain)
|
2021-01-04 13:52:13 +00:00
|
|
|
if !hasChanged {
|
2021-01-07 15:06:45 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "ORG-3M9ds", "Errors.Org.LabelPolicy.NotChanged")
|
2021-01-04 13:52:13 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, changedEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return orgWriteModelToOrgIAMPolicy(existingPolicy), nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 10:24:15 +00:00
|
|
|
func (r *CommandSide) RemoveOrgIAMPolicy(ctx context.Context, orgID string) error {
|
|
|
|
existingPolicy, err := r.orgIAMPolicyWriteModelByID(ctx, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return caos_errs.ThrowNotFound(nil, "ORG-Dvsh3", "Errors.Org.OrgIAMPolicy.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
_, err = r.eventstore.PushEvents(ctx, org.NewOrgIAMPolicyRemovedEvent(ctx, orgAgg))
|
|
|
|
return err
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
func (r *CommandSide) getOrgIAMPolicy(ctx context.Context, orgID string) (*domain.OrgIAMPolicy, error) {
|
|
|
|
policy, err := r.orgIAMPolicyWriteModelByID(ctx, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if policy.State == domain.PolicyStateActive {
|
|
|
|
return orgWriteModelToOrgIAMPolicy(policy), nil
|
|
|
|
}
|
|
|
|
return r.getDefaultOrgIAMPolicy(ctx)
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) orgIAMPolicyWriteModelByID(ctx context.Context, orgID string) (policy *ORGOrgIAMPolicyWriteModel, err error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
writeModel := NewORGOrgIAMPolicyWriteModel(orgID)
|
2021-01-04 13:52:13 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|