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/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/command/preparation"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func domainPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer) (*PolicyDomainWriteModel, error) {
|
2022-03-28 08:05:09 +00:00
|
|
|
wm, err := orgDomainPolicy(ctx, filter)
|
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
|
|
|
}
|
|
|
|
return nil, errors.ThrowInternal(nil, "USER-Ggk9n", "Errors.Internal")
|
|
|
|
}
|
|
|
|
|
2022-12-06 08:01:31 +00:00
|
|
|
func orgDomainPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*OrgDomainPolicyWriteModel, error) {
|
2022-04-12 14:20:17 +00:00
|
|
|
policy := NewOrgDomainPolicyWriteModel(authz.GetCtxData(ctx).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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|