zitadel/internal/policy/repository/eventsourcing/eventstore_age.go
Livio Amstutz 3549a8b64e
feat: port reduction (#323)
* move mgmt pkg

* begin package restructure

* rename auth package to authz

* begin start api

* move auth

* move admin

* fix merge

* configs and interceptors

* interceptor

* revert generate-grpc.sh

* some cleanups

* console

* move console

* fix tests and merging

* js linting

* merge

* merging and configs

* change k8s base to current ports

* fixes

* cleanup

* regenerate proto

* remove unnecessary whitespace

* missing param

* go mod tidy

* fix merging

* move login pkg

* cleanup

* move api pkgs again

* fix pkg naming

* fix generate-static.sh for login

* update workflow

* fixes

* logging

* remove duplicate

* comment for optional gateway interfaces

* regenerate protos

* fix proto imports for grpc web

* protos

* grpc web generate

* grpc web generate

* fix changes

* add translation interceptor

* fix merging

* regenerate mgmt proto
2020-07-08 13:56:37 +02:00

77 lines
2.6 KiB
Go

package eventsourcing
import (
"context"
"github.com/caos/zitadel/internal/api/authz"
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) {
ctxData := authz.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", "Errors.Policy.AlreadyExists")
}
id, err := es.idGenerator.Next()
if err != nil {
return nil, err
}
policy.AggregateID = id
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) {
ctxData := authz.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
}