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"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
LabelPolicyAddedEventType = "policy.label.added"
|
|
|
|
LabelPolicyChangedEventType = "policy.label.changed"
|
|
|
|
LabelPolicyRemovedEventType = "policy.label.removed"
|
|
|
|
)
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type LabelPolicyAddedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
PrimaryColor string `json:"primaryColor,omitempty"`
|
|
|
|
SecondaryColor string `json:"secondaryColor,omitempty"`
|
|
|
|
HideLoginNameSuffix bool `json:"hideLoginNameSuffix,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *LabelPolicyAddedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *LabelPolicyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewLabelPolicyAddedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
|
|
|
primaryColor,
|
|
|
|
secondaryColor string,
|
2021-03-25 13:41:07 +00:00
|
|
|
hideLoginNameSuffix bool,
|
2021-01-04 13:52:13 +00:00
|
|
|
) *LabelPolicyAddedEvent {
|
2020-12-11 14:49:19 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
return &LabelPolicyAddedEvent{
|
2021-03-25 13:41:07 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
PrimaryColor: primaryColor,
|
|
|
|
SecondaryColor: secondaryColor,
|
|
|
|
HideLoginNameSuffix: hideLoginNameSuffix,
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func LabelPolicyAddedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &LabelPolicyAddedEvent{
|
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-puqv4", "unable to unmarshal label policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type LabelPolicyChangedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
PrimaryColor *string `json:"primaryColor,omitempty"`
|
|
|
|
SecondaryColor *string `json:"secondaryColor,omitempty"`
|
|
|
|
HideLoginNameSuffix *bool `json:"hideLoginNameSuffix,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *LabelPolicyChangedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *LabelPolicyChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func NewLabelPolicyChangedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
2021-01-18 10:24:15 +00:00
|
|
|
changes []LabelPolicyChanges,
|
|
|
|
) (*LabelPolicyChangedEvent, error) {
|
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-Asfd3", "Errors.NoChangesFound")
|
|
|
|
}
|
|
|
|
changeEvent := &LabelPolicyChangedEvent{
|
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 LabelPolicyChanges func(*LabelPolicyChangedEvent)
|
|
|
|
|
|
|
|
func ChangePrimaryColor(primaryColor string) func(*LabelPolicyChangedEvent) {
|
|
|
|
return func(e *LabelPolicyChangedEvent) {
|
|
|
|
e.PrimaryColor = &primaryColor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ChangeSecondaryColor(secondaryColor string) func(*LabelPolicyChangedEvent) {
|
|
|
|
return func(e *LabelPolicyChangedEvent) {
|
|
|
|
e.SecondaryColor = &secondaryColor
|
|
|
|
}
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 13:41:07 +00:00
|
|
|
func ChangeHideLoginNameSuffix(hideLoginNameSuffix bool) func(*LabelPolicyChangedEvent) {
|
|
|
|
return func(e *LabelPolicyChangedEvent) {
|
|
|
|
e.HideLoginNameSuffix = &hideLoginNameSuffix
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func LabelPolicyChangedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
|
|
|
e := &LabelPolicyChangedEvent{
|
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-qhfFb", "unable to unmarshal label policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
type LabelPolicyRemovedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (e *LabelPolicyRemovedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:49:38 +00:00
|
|
|
func (e *LabelPolicyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-18 10:24:15 +00:00
|
|
|
func NewLabelPolicyRemovedEvent(base *eventstore.BaseEvent) *LabelPolicyRemovedEvent {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &LabelPolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-18 10:24:15 +00:00
|
|
|
func LabelPolicyRemovedEventMapper(event *repository.Event) (eventstore.EventReader, error) {
|
2021-01-04 13:52:13 +00:00
|
|
|
return &LabelPolicyRemovedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|