2021-01-04 14:52:13 +01:00
|
|
|
package policy
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
PasswordComplexityPolicyAddedEventType = "policy.password.complexity.added"
|
|
|
|
PasswordComplexityPolicyChangedEventType = "policy.password.complexity.changed"
|
|
|
|
PasswordComplexityPolicyRemovedEventType = "policy.password.complexity.removed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type PasswordComplexityPolicyAddedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
|
|
|
MinLength uint64 `json:"minLength,omitempty"`
|
2021-01-06 10:47:55 +01:00
|
|
|
HasLowercase bool `json:"hasLowercase,omitempty"`
|
2021-01-18 11:24:15 +01:00
|
|
|
HasUppercase bool `json:"hasUppercase,omitempty"`
|
2021-01-06 10:47:55 +01:00
|
|
|
HasNumber bool `json:"hasNumber,omitempty"`
|
|
|
|
HasSymbol bool `json:"hasSymbol,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyAddedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewPasswordComplexityPolicyAddedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
minLength uint64,
|
|
|
|
hasLowerCase,
|
|
|
|
hasUpperCase,
|
|
|
|
hasNumber,
|
|
|
|
hasSymbol bool,
|
2021-01-04 14:52:13 +01:00
|
|
|
) *PasswordComplexityPolicyAddedEvent {
|
|
|
|
return &PasswordComplexityPolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
MinLength: minLength,
|
|
|
|
HasLowercase: hasLowerCase,
|
2021-01-18 11:24:15 +01:00
|
|
|
HasUppercase: hasUpperCase,
|
2020-12-11 15:49:19 +01:00
|
|
|
HasNumber: hasNumber,
|
|
|
|
HasSymbol: hasSymbol,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func PasswordComplexityPolicyAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &PasswordComplexityPolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-wYxlM", "unable to unmarshal policy")
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type PasswordComplexityPolicyChangedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 11:12:56 +01:00
|
|
|
MinLength *uint64 `json:"minLength,omitempty"`
|
|
|
|
HasLowercase *bool `json:"hasLowercase,omitempty"`
|
2021-01-18 11:24:15 +01:00
|
|
|
HasUppercase *bool `json:"hasUppercase,omitempty"`
|
2021-01-06 11:12:56 +01:00
|
|
|
HasNumber *bool `json:"hasNumber,omitempty"`
|
|
|
|
HasSymbol *bool `json:"hasSymbol,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyChangedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewPasswordComplexityPolicyChangedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
2021-01-18 11:24:15 +01:00
|
|
|
changes []PasswordComplexityPolicyChanges,
|
|
|
|
) (*PasswordComplexityPolicyChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "POLICY-Rdhu3", "Errors.NoChangesFound")
|
2021-01-18 11:24:15 +01:00
|
|
|
}
|
|
|
|
changeEvent := &PasswordComplexityPolicyChangedEvent{
|
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 PasswordComplexityPolicyChanges func(*PasswordComplexityPolicyChangedEvent)
|
|
|
|
|
|
|
|
func ChangeMinLength(minLength uint64) func(*PasswordComplexityPolicyChangedEvent) {
|
|
|
|
return func(e *PasswordComplexityPolicyChangedEvent) {
|
|
|
|
e.MinLength = &minLength
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeHasLowercase(hasLowercase bool) func(*PasswordComplexityPolicyChangedEvent) {
|
|
|
|
return func(e *PasswordComplexityPolicyChangedEvent) {
|
|
|
|
e.HasLowercase = &hasLowercase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeHasUppercase(hasUppercase bool) func(*PasswordComplexityPolicyChangedEvent) {
|
|
|
|
return func(e *PasswordComplexityPolicyChangedEvent) {
|
|
|
|
e.HasUppercase = &hasUppercase
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeHasNumber(hasNumber bool) func(*PasswordComplexityPolicyChangedEvent) {
|
|
|
|
return func(e *PasswordComplexityPolicyChangedEvent) {
|
|
|
|
e.HasNumber = &hasNumber
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeHasSymbol(hasSymbol bool) func(*PasswordComplexityPolicyChangedEvent) {
|
|
|
|
return func(e *PasswordComplexityPolicyChangedEvent) {
|
|
|
|
e.HasSymbol = &hasSymbol
|
|
|
|
}
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func PasswordComplexityPolicyChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
e := &PasswordComplexityPolicyChangedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
2023-12-08 16:30:55 +02:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-zBGB0", "unable to unmarshal policy")
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
type PasswordComplexityPolicyRemovedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyRemovedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *PasswordComplexityPolicyRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 14:52:13 +01:00
|
|
|
func NewPasswordComplexityPolicyRemovedEvent(base *eventstore.BaseEvent) *PasswordComplexityPolicyRemovedEvent {
|
|
|
|
return &PasswordComplexityPolicyRemovedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func PasswordComplexityPolicyRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2021-01-04 14:52:13 +01:00
|
|
|
return &PasswordComplexityPolicyRemovedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|