2021-01-04 13:52:13 +00:00
|
|
|
package policy
|
2020-12-11 14:49:19 +00:00
|
|
|
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type PasswordAgePolicyAddedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 09:47:55 +00:00
|
|
|
ExpireWarnDays uint64 `json:"expireWarnDays,omitempty"`
|
|
|
|
MaxAgeDays uint64 `json:"maxAgeDays,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *PasswordAgePolicyAddedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewPasswordAgePolicyAddedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays uint64,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *PasswordAgePolicyAddedEvent {
|
2020-12-11 14:49:19 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
return &PasswordAgePolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
ExpireWarnDays: expireWarnDays,
|
|
|
|
MaxAgeDays: maxAgeDays,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func PasswordAgePolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &PasswordAgePolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type PasswordAgePolicyChangedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
ExpireWarnDays *uint64 `json:"expireWarnDays,omitempty"`
|
|
|
|
MaxAgeDays *uint64 `json:"maxAgeDays,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *PasswordAgePolicyChangedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewPasswordAgePolicyChangedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
2021-01-18 10:24:15 +00:00
|
|
|
changes []PasswordAgePolicyChanges,
|
|
|
|
) (*PasswordAgePolicyChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAgt5", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &PasswordAgePolicyChangedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
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
|
|
|
|
}
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func PasswordAgePolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &PasswordAgePolicyChangedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type PasswordAgePolicyRemovedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *PasswordAgePolicyRemovedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 10:24:15 +00:00
|
|
|
func NewPasswordAgePolicyRemovedEvent(base *eventstore.BaseEvent) *PasswordAgePolicyRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &PasswordAgePolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func PasswordAgePolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-01-18 10:24:15 +00:00
|
|
|
return &PasswordAgePolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-01-18 10:24:15 +00:00
|
|
|
}, nil
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|