mirror of
https://github.com/zitadel/zitadel.git
synced 2025-11-14 15:13:15 +00:00
feat: Policy (#79)
* policy added * Make it executable * Make it executable, corrections * password age policy added * password lockout policy added * corrections * policy added * Make it executable * Make it executable, corrections * password age policy added * password lockout policy added * corrections * fix(repository): remove second policy * complaints corrected * Init tests * add some tests * more tests added * systemfefaults added * default values load added * check for default value added * fixes * fixed * create policy if not exists * eventstore tests added Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
This commit is contained in:
83
internal/policy/repository/eventsourcing/eventstore_age.go
Normal file
83
internal/policy/repository/eventsourcing/eventstore_age.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package eventsourcing
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"github.com/caos/zitadel/internal/api/auth"
|
||||
caos_errs "github.com/caos/zitadel/internal/errors"
|
||||
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
||||
pol_model "github.com/caos/zitadel/internal/policy/model"
|
||||
)
|
||||
|
||||
func (es *PolicyEventstore) GetPasswordAgePolicy(ctx context.Context, id string) (*pol_model.PasswordAgePolicy, error) {
|
||||
policy := es.policyCache.getAgePolicy(id)
|
||||
|
||||
query := PasswordAgePolicyQuery(id, policy.Sequence)
|
||||
err := es_sdk.Filter(ctx, es.FilterEvents, policy.AppendEvents, query)
|
||||
if caos_errs.IsNotFound(err) && es.passwordAgePolicyDefault.Description != "" {
|
||||
policy.Description = es.passwordAgePolicyDefault.Description
|
||||
policy.MaxAgeDays = es.passwordAgePolicyDefault.MaxAgeDays
|
||||
policy.ExpireWarnDays = es.passwordAgePolicyDefault.ExpireWarnDays
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
es.policyCache.cacheAgePolicy(policy)
|
||||
return PasswordAgePolicyToModel(policy), nil
|
||||
}
|
||||
|
||||
func (es *PolicyEventstore) CreatePasswordAgePolicy(ctx context.Context, policy *pol_model.PasswordAgePolicy) (*pol_model.PasswordAgePolicy, error) {
|
||||
if !policy.IsValid() {
|
||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-fbX5K", "Description is required")
|
||||
}
|
||||
ctxData := auth.GetCtxData(ctx)
|
||||
existingPolicy, err := es.GetPasswordAgePolicy(ctx, ctxData.OrgID)
|
||||
if err != nil && !caos_errs.IsNotFound(err) {
|
||||
return nil, err
|
||||
}
|
||||
if existingPolicy != nil && existingPolicy.Sequence > 0 {
|
||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-yDJ5I", "Policy allready exists")
|
||||
}
|
||||
|
||||
id, err := idGenerator.NextID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
policy.AggregateID = strconv.FormatUint(id, 10)
|
||||
|
||||
repoPolicy := PasswordAgePolicyFromModel(policy)
|
||||
|
||||
createAggregate := PasswordAgePolicyCreateAggregate(es.AggregateCreator(), repoPolicy)
|
||||
err = es_sdk.Push(ctx, es.PushAggregates, repoPolicy.AppendEvents, createAggregate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
es.policyCache.cacheAgePolicy(repoPolicy)
|
||||
return PasswordAgePolicyToModel(repoPolicy), nil
|
||||
}
|
||||
|
||||
func (es *PolicyEventstore) UpdatePasswordAgePolicy(ctx context.Context, policy *pol_model.PasswordAgePolicy) (*pol_model.PasswordAgePolicy, error) {
|
||||
if !policy.IsValid() {
|
||||
return nil, caos_errs.ThrowPreconditionFailed(nil, "EVENT-44jB3", "Description is required")
|
||||
}
|
||||
ctxData := auth.GetCtxData(ctx)
|
||||
existingPolicy, err := es.GetPasswordAgePolicy(ctx, ctxData.OrgID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if existingPolicy.Sequence <= 0 {
|
||||
return es.CreatePasswordAgePolicy(ctx, policy)
|
||||
}
|
||||
repoExisting := PasswordAgePolicyFromModel(existingPolicy)
|
||||
repoNew := PasswordAgePolicyFromModel(policy)
|
||||
|
||||
updateAggregate := PasswordAgePolicyUpdateAggregate(es.AggregateCreator(), repoExisting, repoNew)
|
||||
err = es_sdk.Push(ctx, es.PushAggregates, repoExisting.AppendEvents, updateAggregate)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
es.policyCache.cacheAgePolicy(repoExisting)
|
||||
return PasswordAgePolicyToModel(repoExisting), nil
|
||||
}
|
||||
Reference in New Issue
Block a user