2021-01-04 13:52:13 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
2021-01-05 08:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
2021-01-08 10:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/repository/org"
|
2021-01-04 13:52:13 +00:00
|
|
|
)
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) AddOrgIAMPolicy(ctx context.Context, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
addedPolicy := NewORGOrgIAMPolicyWriteModel(policy.AggregateID)
|
2021-01-08 10:33:45 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&addedPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
|
|
|
err := r.addOrgIAMPolicy(ctx, orgAgg, addedPolicy, policy)
|
2021-01-04 13:52:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-01-04 13:52:13 +00:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "ORG-5M0ds", "Errors.Org.OrgIAMPolicy.AlreadyExists")
|
|
|
|
}
|
2021-01-08 10:33:45 +00:00
|
|
|
orgAgg.PushEvents(org.NewOrgIAMPolicyAddedEvent(ctx, policy.UserLoginMustBeDomain))
|
2021-01-04 13:52:13 +00:00
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, addedPolicy, orgAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return orgWriteModelToOrgIAMPolicy(addedPolicy), nil
|
|
|
|
}
|
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
func (r *CommandSide) addOrgIAMPolicy(ctx context.Context, orgAgg *org.Aggregate, addedPolicy *ORGOrgIAMPolicyWriteModel, policy *domain.OrgIAMPolicy) error {
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-01-15 08:32:59 +00:00
|
|
|
return caos_errs.ThrowAlreadyExists(nil, "ORG-1M8ds", "Errors.Org.OrgIAMPolicy.AlreadyExists")
|
2021-01-08 10:33:45 +00:00
|
|
|
}
|
|
|
|
orgAgg.PushEvents(org.NewOrgIAMPolicyAddedEvent(ctx, policy.UserLoginMustBeDomain))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) ChangeOrgIAMPolicy(ctx context.Context, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
existingPolicy, err := r.orgIAMPolicyWriteModelByID(ctx, policy.AggregateID)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, policy.UserLoginMustBeDomain)
|
|
|
|
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-01-08 10:33:45 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
2021-01-04 13:52:13 +00:00
|
|
|
orgAgg.PushEvents(changedEvent)
|
|
|
|
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingPolicy, orgAgg)
|
|
|
|
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)
|
|
|
|
orgAgg.PushEvents(org.NewOrgIAMPolicyRemovedEvent(ctx))
|
|
|
|
|
|
|
|
return r.eventstore.PushAggregate(ctx, existingPolicy, orgAgg)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|