2022-03-28 08:05:09 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
2023-12-21 09:03:37 +00:00
|
|
|
// Deprecated: User commands.domainPolicyWriteModel directly, to remove use of eventstore.Filter function
|
2022-12-13 06:37:50 +00:00
|
|
|
func domainPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer, orgID string) (*PolicyDomainWriteModel, error) {
|
|
|
|
wm, err := orgDomainPolicy(ctx, filter, orgID)
|
2022-12-06 08:01:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
2022-12-06 08:01:31 +00: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 08:05:09 +00:00
|
|
|
}
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-Ggk9n", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 09:03:37 +00: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 06:37:50 +00:00
|
|
|
func orgDomainPolicy(ctx context.Context, filter preparation.FilterToQueryReducer, orgID string) (*OrgDomainPolicyWriteModel, error) {
|
|
|
|
policy := NewOrgDomainPolicyWriteModel(orgID)
|
2022-03-28 08:05:09 +00:00
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2022-12-06 08:01:31 +00:00
|
|
|
return policy, nil
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
2022-12-06 08:01:31 +00:00
|
|
|
return policy, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 09:03:37 +00:00
|
|
|
// Deprecated: Use commands.instanceDomainPolicyWriteModel directly, to remove use of eventstore.Filter function
|
2022-12-06 08:01:31 +00:00
|
|
|
func instanceDomainPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*InstanceDomainPolicyWriteModel, error) {
|
2022-04-12 14:20:17 +00:00
|
|
|
policy := NewInstanceDomainPolicyWriteModel(ctx)
|
2022-03-28 08:05:09 +00:00
|
|
|
events, err := filter(ctx, policy.Query())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2022-12-06 08:01:31 +00: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 08:05:09 +00:00
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
2022-12-06 08:01:31 +00:00
|
|
|
return policy, err
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|