zitadel/internal/repository/instance/policy_security.go
Tim Möhlmann f680dd934d
refactor: rename package errors to zerrors (#7039)
* chore: rename package errors to zerrors

* rename package errors to gerrors

* fix error related linting issues

* fix zitadel error assertion

* fix gosimple linting issues

* fix deprecated linting issues

* resolve gci linting issues

* fix import structure

---------

Co-authored-by: Elio Bischof <elio@zitadel.com>
2023-12-08 15:30:55 +01:00

79 lines
2.0 KiB
Go

package instance
import (
"context"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
const (
securityPolicyPrefix = "policy.security."
SecurityPolicySetEventType = instanceEventTypePrefix + securityPolicyPrefix + "set"
)
type SecurityPolicySetEvent struct {
eventstore.BaseEvent `json:"-"`
Enabled *bool `json:"enabled,omitempty"`
AllowedOrigins *[]string `json:"allowedOrigins,omitempty"`
}
func NewSecurityPolicySetEvent(
ctx context.Context,
aggregate *eventstore.Aggregate,
changes []SecurityPolicyChanges,
) (*SecurityPolicySetEvent, error) {
if len(changes) == 0 {
return nil, zerrors.ThrowPreconditionFailed(nil, "POLICY-EWsf3", "Errors.NoChangesFound")
}
event := &SecurityPolicySetEvent{
BaseEvent: *eventstore.NewBaseEventForPush(
ctx,
aggregate,
SecurityPolicySetEventType,
),
}
for _, change := range changes {
change(event)
}
return event, nil
}
type SecurityPolicyChanges func(event *SecurityPolicySetEvent)
func ChangeSecurityPolicyEnabled(enabled bool) func(event *SecurityPolicySetEvent) {
return func(e *SecurityPolicySetEvent) {
e.Enabled = &enabled
}
}
func ChangeSecurityPolicyAllowedOrigins(allowedOrigins []string) func(event *SecurityPolicySetEvent) {
return func(e *SecurityPolicySetEvent) {
if len(allowedOrigins) == 0 {
allowedOrigins = []string{}
}
e.AllowedOrigins = &allowedOrigins
}
}
func (e *SecurityPolicySetEvent) Payload() interface{} {
return e
}
func (e *SecurityPolicySetEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
return nil
}
func SecurityPolicySetEventMapper(event eventstore.Event) (eventstore.Event, error) {
securityPolicyAdded := &SecurityPolicySetEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := event.Unmarshal(securityPolicyAdded)
if err != nil {
return nil, zerrors.ThrowInternal(err, "IAM-soiwj", "unable to unmarshal oidc config added")
}
return securityPolicyAdded, nil
}