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