zitadel/internal/v2/repository/policy/policy_password_age.go
Livio Amstutz 3eb909c4b4
feat: org and policies commands (#1167)
* add setup steps

* refactoring

* omitempty

* cleanup

* begin org

* create org

* setup org

* setup org

* merge

* fixes

* fixes

* fixes

* add project

* add oidc application

* fix app creation

* add resourceOwner to writemodels

* resource owner

* cleanup

* global org, iam project and iam member in setup

* logs

* logs

* logs

* cleanup

* Update internal/v2/command/project.go

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>

* check project state

* add org domain commands

* add org status changes and member commands

* fixes

* policies

* login policy

* fix iam project event

* mapper

* label policy

* change to command

* fix

* fix

* handle change event differently and lot of fixes

* fixes

* changedEvent handling

Co-authored-by: Fabi <38692350+fgerschwiler@users.noreply.github.com>
2021-01-18 11:24:15 +01:00

126 lines
3.2 KiB
Go

package policy
import (
"encoding/json"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore/v2"
"github.com/caos/zitadel/internal/eventstore/v2/repository"
)
const (
PasswordAgePolicyAddedEventType = "policy.password.age.added"
PasswordAgePolicyChangedEventType = "policy.password.age.changed"
PasswordAgePolicyRemovedEventType = "policy.password.age.removed"
)
type PasswordAgePolicyAddedEvent struct {
eventstore.BaseEvent `json:"-"`
ExpireWarnDays uint64 `json:"expireWarnDays,omitempty"`
MaxAgeDays uint64 `json:"maxAgeDays,omitempty"`
}
func (e *PasswordAgePolicyAddedEvent) Data() interface{} {
return e
}
func NewPasswordAgePolicyAddedEvent(
base *eventstore.BaseEvent,
expireWarnDays,
maxAgeDays uint64,
) *PasswordAgePolicyAddedEvent {
return &PasswordAgePolicyAddedEvent{
BaseEvent: *base,
ExpireWarnDays: expireWarnDays,
MaxAgeDays: maxAgeDays,
}
}
func PasswordAgePolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &PasswordAgePolicyAddedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-T3mGp", "unable to unmarshal policy")
}
return e, nil
}
type PasswordAgePolicyChangedEvent struct {
eventstore.BaseEvent `json:"-"`
ExpireWarnDays *uint64 `json:"expireWarnDays,omitempty"`
MaxAgeDays *uint64 `json:"maxAgeDays,omitempty"`
}
func (e *PasswordAgePolicyChangedEvent) Data() interface{} {
return e
}
func NewPasswordAgePolicyChangedEvent(
base *eventstore.BaseEvent,
changes []PasswordAgePolicyChanges,
) (*PasswordAgePolicyChangedEvent, error) {
if len(changes) == 0 {
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAgt5", "Errors.NoChangesFound")
}
changeEvent := &PasswordAgePolicyChangedEvent{
BaseEvent: *base,
}
for _, change := range changes {
change(changeEvent)
}
return changeEvent, nil
}
type PasswordAgePolicyChanges func(*PasswordAgePolicyChangedEvent)
func ChangeExpireWarnDays(expireWarnDay uint64) func(*PasswordAgePolicyChangedEvent) {
return func(e *PasswordAgePolicyChangedEvent) {
e.ExpireWarnDays = &expireWarnDay
}
}
func ChangeMaxAgeDays(maxAgeDays uint64) func(*PasswordAgePolicyChangedEvent) {
return func(e *PasswordAgePolicyChangedEvent) {
e.MaxAgeDays = &maxAgeDays
}
}
func PasswordAgePolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
e := &PasswordAgePolicyChangedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}
err := json.Unmarshal(event.Data, e)
if err != nil {
return nil, errors.ThrowInternal(err, "POLIC-PqaVq", "unable to unmarshal policy")
}
return e, nil
}
type PasswordAgePolicyRemovedEvent struct {
eventstore.BaseEvent `json:"-"`
}
func (e *PasswordAgePolicyRemovedEvent) Data() interface{} {
return nil
}
func NewPasswordAgePolicyRemovedEvent(base *eventstore.BaseEvent) *PasswordAgePolicyRemovedEvent {
return &PasswordAgePolicyRemovedEvent{
BaseEvent: *base,
}
}
func PasswordAgePolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
return &PasswordAgePolicyRemovedEvent{
BaseEvent: *eventstore.BaseEventFromRepo(event),
}, nil
}