mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-22 21:08:47 +00:00
New eventstore policies (#1084)
* feat: login policy * feat: password complexity policy * feat: org iam policy * feat: label policy * feat: add and change policies * feat: second factors * feat: second and multi factors * feat: better naming * feat: better naming
This commit is contained in:
@@ -0,0 +1,127 @@
|
||||
package label
|
||||
|
||||
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 (
|
||||
LabelPolicyAddedEventType = "policy.label.added"
|
||||
LabelPolicyChangedEventType = "policy.label.changed"
|
||||
LabelPolicyRemovedEventType = "policy.label.removed"
|
||||
)
|
||||
|
||||
type AddedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
PrimaryColor string `json:"primaryColor,omitempty"`
|
||||
SecondaryColor string `json:"secondaryColor,omitempty"`
|
||||
}
|
||||
|
||||
func (e *AddedEvent) CheckPrevious() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *AddedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func NewAddedEvent(
|
||||
base *eventstore.BaseEvent,
|
||||
primaryColor,
|
||||
secondaryColor string,
|
||||
) *AddedEvent {
|
||||
|
||||
return &AddedEvent{
|
||||
BaseEvent: *base,
|
||||
PrimaryColor: primaryColor,
|
||||
SecondaryColor: secondaryColor,
|
||||
}
|
||||
}
|
||||
|
||||
func AddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &AddedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "POLIC-puqv4", "unable to unmarshal label policy")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type ChangedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
|
||||
PrimaryColor string `json:"primaryColor,omitempty"`
|
||||
SecondaryColor string `json:"secondaryColor,omitempty"`
|
||||
}
|
||||
|
||||
func (e *ChangedEvent) CheckPrevious() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *ChangedEvent) Data() interface{} {
|
||||
return e
|
||||
}
|
||||
|
||||
func NewChangedEvent(
|
||||
base *eventstore.BaseEvent,
|
||||
current *WriteModel,
|
||||
primaryColor,
|
||||
secondaryColor string,
|
||||
) *ChangedEvent {
|
||||
|
||||
e := &ChangedEvent{
|
||||
BaseEvent: *base,
|
||||
}
|
||||
if primaryColor != "" && current.PrimaryColor != primaryColor {
|
||||
e.PrimaryColor = primaryColor
|
||||
}
|
||||
if secondaryColor != "" && current.SecondaryColor != secondaryColor {
|
||||
e.SecondaryColor = secondaryColor
|
||||
}
|
||||
|
||||
return e
|
||||
}
|
||||
|
||||
func ChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
e := &ChangedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}
|
||||
|
||||
err := json.Unmarshal(event.Data, e)
|
||||
if err != nil {
|
||||
return nil, errors.ThrowInternal(err, "POLIC-qhfFb", "unable to unmarshal label policy")
|
||||
}
|
||||
|
||||
return e, nil
|
||||
}
|
||||
|
||||
type RemovedEvent struct {
|
||||
eventstore.BaseEvent `json:"-"`
|
||||
}
|
||||
|
||||
func (e *RemovedEvent) CheckPrevious() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (e *RemovedEvent) Data() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewRemovedEvent(base *eventstore.BaseEvent) *RemovedEvent {
|
||||
return &RemovedEvent{
|
||||
BaseEvent: *base,
|
||||
}
|
||||
}
|
||||
|
||||
func RemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
||||
return &RemovedEvent{
|
||||
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package label
|
||||
|
||||
import "github.com/caos/zitadel/internal/eventstore/v2"
|
||||
|
||||
type ReadModel struct {
|
||||
eventstore.ReadModel
|
||||
|
||||
PrimaryColor string
|
||||
SecondaryColor string
|
||||
}
|
||||
|
||||
func (rm *ReadModel) Reduce() error {
|
||||
for _, event := range rm.Events {
|
||||
switch e := event.(type) {
|
||||
case *AddedEvent:
|
||||
rm.PrimaryColor = e.PrimaryColor
|
||||
rm.SecondaryColor = e.SecondaryColor
|
||||
case *ChangedEvent:
|
||||
rm.PrimaryColor = e.PrimaryColor
|
||||
rm.SecondaryColor = e.SecondaryColor
|
||||
}
|
||||
}
|
||||
return rm.ReadModel.Reduce()
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package label
|
||||
|
||||
import (
|
||||
"github.com/caos/zitadel/internal/eventstore/v2"
|
||||
)
|
||||
|
||||
type WriteModel struct {
|
||||
eventstore.WriteModel
|
||||
|
||||
PrimaryColor string
|
||||
SecondaryColor string
|
||||
}
|
||||
|
||||
func (wm *WriteModel) Reduce() error {
|
||||
for _, event := range wm.Events {
|
||||
switch e := event.(type) {
|
||||
case *AddedEvent:
|
||||
wm.PrimaryColor = e.PrimaryColor
|
||||
wm.SecondaryColor = e.SecondaryColor
|
||||
case *ChangedEvent:
|
||||
wm.PrimaryColor = e.PrimaryColor
|
||||
wm.SecondaryColor = e.SecondaryColor
|
||||
}
|
||||
}
|
||||
return wm.WriteModel.Reduce()
|
||||
}
|
||||
Reference in New Issue
Block a user