2022-03-28 10:05:09 +02:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 10:05:09 +02:00
|
|
|
)
|
|
|
|
|
2023-12-21 10:03:37 +01:00
|
|
|
// Deprecated: User commands.domainPolicyWriteModel directly, to remove use of eventstore.Filter function
|
2022-12-13 07:37:50 +01:00
|
|
|
func domainPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, orgID string) (*PolicyDomainWriteModel, error) {
|
|
|
|
wm, err := orgDomainPolicy(ctx, filter, orgID)
|
2022-12-06 09:01:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
2022-12-06 09:01:31 +01:00
|
|
|
if wm != nil && wm.State.Exists() {
|
|
|
|
return &wm.PolicyDomainWriteModel, err
|
|
|
|
}
|
|
|
|
instanceWriteModel, err := instanceDomainPolicy(ctx, filter)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if instanceWriteModel != nil && instanceWriteModel.State.Exists() {
|
|
|
|
return &instanceWriteModel.PolicyDomainWriteModel, err
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-Ggk9n", "Errors.Internal")
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 10:03:37 +01:00
|
|
|
func (c *Commands) domainPolicyWriteModel(ctx context.Context, orgID string) (*PolicyDomainWriteModel, error) {
|
|
|
|
wm, err := c.orgDomainPolicyWriteModel(ctx, orgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if wm != nil && wm.State.Exists() {
|
|
|
|
return &wm.PolicyDomainWriteModel, err
|
|
|
|
}
|
|
|
|
instanceWriteModel, err := c.instanceDomainPolicyWriteModel(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if instanceWriteModel != nil && instanceWriteModel.State.Exists() {
|
|
|
|
return &instanceWriteModel.PolicyDomainWriteModel, err
|
|
|
|
}
|
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-Ggk9n", "Errors.Internal")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deprecated: Use commands.orgDomainPolicyWriteModel directly, to remove use of eventstore.Filter function
|
2022-12-13 07:37:50 +01:00
|
|
|
func orgDomainPolicy(ctx context.Context, filter preparation.FilterToQueryReducer, orgID string) (*OrgDomainPolicyWriteModel, error) {
|
|
|
|
policy := NewOrgDomainPolicyWriteModel(orgID)
|
2022-03-28 10:05:09 +02:00
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2022-12-06 09:01:31 +01:00
|
|
|
return policy, nil
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
2022-12-06 09:01:31 +01:00
|
|
|
return policy, err
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
2023-12-21 10:03:37 +01:00
|
|
|
// Deprecated: Use commands.instanceDomainPolicyWriteModel directly, to remove use of eventstore.Filter function
|
2022-12-06 09:01:31 +01:00
|
|
|
func instanceDomainPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*InstanceDomainPolicyWriteModel, error) {
|
2022-04-12 16:20:17 +02:00
|
|
|
policy := NewInstanceDomainPolicyWriteModel(ctx)
|
2022-03-28 10:05:09 +02:00
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2022-12-06 09:01:31 +01:00
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return policy, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func domainPolicyUsernames(ctx context.Context, filter preparation.FilterToQueryReducer, orgID string) (*DomainPolicyUsernamesWriteModel, error) {
|
|
|
|
policy := NewDomainPolicyUsernamesWriteModel(orgID)
|
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return policy, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return policy, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func domainPolicyOrgs(ctx context.Context, filter preparation.FilterToQueryReducer) (*DomainPolicyOrgsWriteModel, error) {
|
|
|
|
policy := NewDomainPolicyOrgsWriteModel()
|
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
|
|
|
return policy, nil
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
2022-12-06 09:01:31 +01:00
|
|
|
return policy, err
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|