mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-13 11:34:26 +00:00
51 lines
1.6 KiB
Go
51 lines
1.6 KiB
Go
|
package command
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/caos/zitadel/internal/api/authz"
|
||
|
"github.com/caos/zitadel/internal/command"
|
||
|
"github.com/caos/zitadel/internal/command/v2/preparation"
|
||
|
"github.com/caos/zitadel/internal/errors"
|
||
|
)
|
||
|
|
||
|
func passwordComplexityPolicyWriteModel(ctx context.Context, filter preparation.FilterToQueryReducer) (*command.PasswordComplexityPolicyWriteModel, error) {
|
||
|
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
|
||
|
}
|
||
|
return nil, errors.ThrowInternal(nil, "USER-uQ96e", "Errors.Internal")
|
||
|
}
|
||
|
|
||
|
func customPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*command.PasswordComplexityPolicyWriteModel, error) {
|
||
|
policy := command.NewOrgPasswordComplexityPolicyWriteModel(authz.GetCtxData(ctx).OrgID)
|
||
|
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
|
||
|
}
|
||
|
|
||
|
func defaultPasswordComplexityPolicy(ctx context.Context, filter preparation.FilterToQueryReducer) (*command.PasswordComplexityPolicyWriteModel, error) {
|
||
|
policy := command.NewInstancePasswordComplexityPolicyWriteModel()
|
||
|
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
|
||
|
}
|