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"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2022-03-28 08:05:09 +00:00
|
|
|
)
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func passwordComplexityPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
2022-03-28 08:05:09 +00: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 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(nil, "USER-uQ96e", "Errors.Internal")
|
2022-03-28 08:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func customPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
|
|
|
policy := NewOrgPasswordComplexityPolicyWriteModel(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 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return &policy.PasswordComplexityPolicyWriteModel, err
|
|
|
|
}
|
|
|
|
|
2022-04-12 14:20:17 +00:00
|
|
|
func defaultPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*PasswordComplexityPolicyWriteModel, error) {
|
|
|
|
policy := NewInstancePasswordComplexityPolicyWriteModel(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 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
policy.AppendEvents(events...)
|
|
|
|
err = policy.Reduce()
|
|
|
|
return &policy.PasswordComplexityPolicyWriteModel, err
|
|
|
|
}
|