2021-01-04 14:52:13 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-01-03 09:19:07 +01:00
|
|
|
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-04 14:52:13 +01:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
2021-01-04 14:52:13 +01:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
)
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) AddDefaultOrgIAMPolicy(ctx context.Context, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
2021-01-12 12:59:51 +01:00
|
|
|
addedPolicy := NewIAMOrgIAMPolicyWriteModel()
|
2021-01-06 10:47:55 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.WriteModel)
|
2021-02-24 11:17:39 +01:00
|
|
|
event, err := c.addDefaultOrgIAMPolicy(ctx, iamAgg, addedPolicy, policy)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, event)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToOrgIAMPolicy(addedPolicy), nil
|
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
func (c *Commands) addDefaultOrgIAMPolicy(ctx context.Context, iamAgg *eventstore.Aggregate, addedPolicy *IAMOrgIAMPolicyWriteModel, policy *domain.OrgIAMPolicy) (eventstore.Command, error) {
|
2021-02-24 11:17:39 +01:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
2021-01-06 10:47:55 +01:00
|
|
|
if err != nil {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, err
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
2021-01-07 16:06:45 +01:00
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
2021-02-18 14:48:27 +01:00
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-Lk0dS", "Errors.IAM.OrgIAMPolicy.AlreadyExists")
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
2021-02-18 14:48:27 +01:00
|
|
|
return iam_repo.NewOrgIAMPolicyAddedEvent(ctx, iamAgg, policy.UserLoginMustBeDomain), nil
|
2021-01-06 10:47:55 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) ChangeDefaultOrgIAMPolicy(ctx context.Context, policy *domain.OrgIAMPolicy) (*domain.OrgIAMPolicy, error) {
|
|
|
|
existingPolicy, err := c.defaultOrgIAMPolicyWriteModelByID(ctx)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
if !existingPolicy.State.Exists() {
|
2021-01-07 16:06:45 +01:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "IAM-0Pl0d", "Errors.IAM.OrgIAMPolicy.NotFound")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2021-02-18 14:48:27 +01:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.PolicyOrgIAMWriteModel.WriteModel)
|
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, iamAgg, policy.UserLoginMustBeDomain)
|
2021-01-04 14:52:13 +01:00
|
|
|
if !hasChanged {
|
2021-01-07 16:06:45 +01:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-4M9vs", "Errors.IAM.LabelPolicy.NotChanged")
|
2021-01-04 14:52:13 +01:00
|
|
|
}
|
|
|
|
|
2022-01-03 09:19:07 +01:00
|
|
|
pushedEvents, err := c.eventstore.Push(ctx, changedEvent)
|
2021-02-18 14:48:27 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToOrgIAMPolicy(existingPolicy), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) getDefaultOrgIAMPolicy(ctx context.Context) (*domain.OrgIAMPolicy, error) {
|
|
|
|
policyWriteModel, err := c.defaultOrgIAMPolicyWriteModelByID(ctx)
|
2021-01-08 11:33:45 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-19 11:12:56 +01:00
|
|
|
if !policyWriteModel.State.Exists() {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "IAM-3n8fs", "Errors.IAM.PasswordComplexityPolicy.NotFound")
|
|
|
|
}
|
2021-01-08 11:33:45 +01:00
|
|
|
policy := writeModelToOrgIAMPolicy(policyWriteModel)
|
|
|
|
policy.Default = true
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:17:39 +01:00
|
|
|
func (c *Commands) defaultOrgIAMPolicyWriteModelByID(ctx context.Context) (policy *IAMOrgIAMPolicyWriteModel, err error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-12 12:59:51 +01:00
|
|
|
writeModel := NewIAMOrgIAMPolicyWriteModel()
|
2021-02-24 11:17:39 +01:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2021-01-04 14:52:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModel, nil
|
|
|
|
}
|