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/api/authz"
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2022-04-12 16:20:17 +02:00
|
|
|
func passwordComplexityPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
2022-03-28 10:05:09 +02:00
|
|
|
wm, err := customPasswordComplexityPolicy(ctx, filter)
|
|
|
|
if err != nil || wm != nil && wm.State.Exists() {
|
|
|
|
return wm, err
|
|
|
|
}
|
|
|
|
wm, err = defaultPasswordComplexityPolicy(ctx, filter)
|
|
|
|
if err != nil || wm != nil {
|
|
|
|
return wm, err
|
|
|
|
}
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-uQ96e", "Errors.Internal")
|
2022-03-28 10:05:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 16:20:17 +02:00
|
|
|
func customPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
|
|
|
policy := NewOrgPasswordComplexityPolicyWriteModel(authz.GetCtxData(ctx).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 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return &policy.PasswordComplexityPolicyWriteModel, err
|
|
|
|
}
|
|
|
|
|
2022-04-12 16:20:17 +02:00
|
|
|
func defaultPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
|
|
|
policy := NewInstancePasswordComplexityPolicyWriteModel(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 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return &policy.PasswordComplexityPolicyWriteModel, err
|
|
|
|
}
|