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