2021-01-04 14:52:13 +01:00
|
|
|
package policy
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 15:13:04 +01:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PasswordAgePolicyAddedEventType = "policy.password.age.added"
|
|
|
|
PasswordAgePolicyChangedEventType = "policy.password.age.changed"
|
|
|
|
PasswordAgePolicyRemovedEventType = "policy.password.age.removed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type PasswordAgePolicyAddedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 10:47:55 +01:00
|
|
|
ExpireWarnDays uint64 `json:"expireWarnDays,omitempty"`
|
|
|
|
MaxAgeDays uint64 `json:"maxAgeDays,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *PasswordAgePolicyAddedEvent) Data() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *PasswordAgePolicyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *PasswordAgePolicyAddedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewPasswordAgePolicyAddedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
expireWarnDays,
|
|
|
|
maxAgeDays uint64,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *PasswordAgePolicyAddedEvent {
|
2020-12-11 15:49:19 +01:00
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
return &PasswordAgePolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
ExpireWarnDays: expireWarnDays,
|
|
|
|
MaxAgeDays: maxAgeDays,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func PasswordAgePolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &PasswordAgePolicyAddedEvent{
|
2020-12-11 15:49:19 +01: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 14:52:13 +01:00
|
|
|
type PasswordAgePolicyChangedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 11:12:56 +01:00
|
|
|
ExpireWarnDays *uint64 `json:"expireWarnDays,omitempty"`
|
|
|
|
MaxAgeDays *uint64 `json:"maxAgeDays,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *PasswordAgePolicyChangedEvent) Data() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *PasswordAgePolicyChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *PasswordAgePolicyChangedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewPasswordAgePolicyChangedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
2021-01-18 11:24:15 +01:00
|
|
|
changes []PasswordAgePolicyChanges,
|
|
|
|
) (*PasswordAgePolicyChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAgt5", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &PasswordAgePolicyChangedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
2021-01-18 11:24:15 +01: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 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func PasswordAgePolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &PasswordAgePolicyChangedEvent{
|
2020-12-11 15:49:19 +01: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 14:52:13 +01:00
|
|
|
type PasswordAgePolicyRemovedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func (e *PasswordAgePolicyRemovedEvent) Data() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:49:38 +01:00
|
|
|
func (e *PasswordAgePolicyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-27 12:58:18 +02:00
|
|
|
func (e *PasswordAgePolicyRemovedEvent) Assets() []*eventstore.Asset {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:24:15 +01:00
|
|
|
func NewPasswordAgePolicyRemovedEvent(base *eventstore.BaseEvent) *PasswordAgePolicyRemovedEvent {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &PasswordAgePolicyRemovedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func PasswordAgePolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-01-18 11:24:15 +01:00
|
|
|
return &PasswordAgePolicyRemovedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
2021-01-18 11:24:15 +01:00
|
|
|
}, nil
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|