2021-01-18 10:24:15 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-18 10:24:15 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
2021-01-18 10:24:15 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) AddLabelPolicy(ctx context.Context, resourceOwner string, policy *domain.LabelPolicy) (*domain.LabelPolicy, error) {
|
2021-03-19 10:12:56 +00:00
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "Org-Fn8ds", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if !policy.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "Org-Md9sf", "Errors.Org.LabelPolicy.Invalid")
|
|
|
|
}
|
2021-02-10 09:48:40 +00:00
|
|
|
addedPolicy := NewOrgLabelPolicyWriteModel(resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if addedPolicy.State == domain.PolicyStateActive {
|
|
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "Org-2B0ps", "Errors.Org.LabelPolicy.AlreadyExists")
|
|
|
|
}
|
|
|
|
|
|
|
|
orgAgg := OrgAggregateFromWriteModel(&addedPolicy.LabelPolicyWriteModel.WriteModel)
|
2021-03-25 13:41:07 +00:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, org.NewLabelPolicyAddedEvent(ctx, orgAgg, policy.PrimaryColor, policy.SecondaryColor, policy.HideLoginNameSuffix))
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedPolicy, pushedEvents...)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToLabelPolicy(&addedPolicy.LabelPolicyWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeLabelPolicy(ctx context.Context, resourceOwner string, policy *domain.LabelPolicy) (*domain.LabelPolicy, error) {
|
2021-03-19 10:12:56 +00:00
|
|
|
if resourceOwner == "" {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "Org-3N9fs", "Errors.ResourceOwnerMissing")
|
|
|
|
}
|
|
|
|
if !policy.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "Org-dM9fs", "Errors.Org.LabelPolicy.Invalid")
|
|
|
|
}
|
2021-02-10 09:48:40 +00:00
|
|
|
existingPolicy := NewOrgLabelPolicyWriteModel(resourceOwner)
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, existingPolicy)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "Org-0K9dq", "Errors.Org.LabelPolicy.NotFound")
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.LabelPolicyWriteModel.WriteModel)
|
2021-03-25 13:41:07 +00:00
|
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, orgAgg, policy.PrimaryColor, policy.SecondaryColor, policy.HideLoginNameSuffix)
|
2021-01-18 10:24:15 +00:00
|
|
|
if !hasChanged {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "Org-4M9vs", "Errors.Org.LabelPolicy.NotChanged")
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, changedEvent)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToLabelPolicy(&existingPolicy.LabelPolicyWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
func (c *Commands) RemoveLabelPolicy(ctx context.Context, orgID string) (*domain.ObjectDetails, error) {
|
2021-03-19 10:12:56 +00:00
|
|
|
if orgID == "" {
|
2021-03-25 13:41:07 +00:00
|
|
|
return nil, caos_errs.ThrowInvalidArgument(nil, "Org-Mf9sf", "Errors.ResourceOwnerMissing")
|
2021-03-19 10:12:56 +00:00
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
existingPolicy := NewOrgLabelPolicyWriteModel(orgID)
|
2021-04-12 15:03:09 +00:00
|
|
|
removeEvent, err := c.removeLabelPolicy(ctx, existingPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, removeEvent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(existingPolicy, pushedEvents...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToObjectDetails(&existingPolicy.LabelPolicyWriteModel.WriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) removeLabelPolicy(ctx context.Context, existingPolicy *OrgLabelPolicyWriteModel) (*org.LabelPolicyRemovedEvent, error) {
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, existingPolicy)
|
2021-01-18 10:24:15 +00:00
|
|
|
if err != nil {
|
2021-03-25 13:41:07 +00:00
|
|
|
return nil, err
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
if existingPolicy.State == domain.PolicyStateUnspecified || existingPolicy.State == domain.PolicyStateRemoved {
|
2021-03-25 13:41:07 +00:00
|
|
|
return nil, caos_errs.ThrowNotFound(nil, "Org-3M9df", "Errors.Org.LabelPolicy.NotFound")
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.WriteModel)
|
2021-04-12 15:03:09 +00:00
|
|
|
return org.NewLabelPolicyRemovedEvent(ctx, orgAgg), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) removeLabelPolicyIfExists(ctx context.Context, orgID string) (*org.LabelPolicyRemovedEvent, error) {
|
|
|
|
existingPolicy, err := c.orgLabelPolicyWriteModelByID(ctx, orgID)
|
2021-03-25 13:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-12 15:03:09 +00:00
|
|
|
if existingPolicy.State != domain.PolicyStateActive {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
orgAgg := OrgAggregateFromWriteModel(&existingPolicy.WriteModel)
|
|
|
|
return org.NewLabelPolicyRemovedEvent(ctx, orgAgg), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Commands) orgLabelPolicyWriteModelByID(ctx context.Context, orgID string) (*OrgLabelPolicyWriteModel, error) {
|
|
|
|
policy := NewOrgLabelPolicyWriteModel(orgID)
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, policy)
|
2021-03-25 13:41:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-12 15:03:09 +00:00
|
|
|
return policy, nil
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|