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
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2020-12-11 14:49:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/repository"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-08 10:33:45 +00:00
|
|
|
//TODO: use for org events as suffix (when possible)
|
2022-03-24 16:21:34 +00:00
|
|
|
DomainPolicyAddedEventType = "policy.domain.added"
|
|
|
|
DomainPolicyChangedEventType = "policy.domain.changed"
|
|
|
|
DomainPolicyRemovedEventType = "policy.domain.removed"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type DomainPolicyAddedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 09:47:55 +00:00
|
|
|
UserLoginMustBeDomain bool `json:"userLoginMustBeDomain,omitempty"`
|
2022-04-13 09:24:03 +00:00
|
|
|
ValidateOrgDomains bool `json:"validateOrgDomains,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyAddedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyAddedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewDomainPolicyAddedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
2022-04-13 09:24:03 +00:00
|
|
|
userLoginMustBeDomain,
|
|
|
|
validateOrgDomains bool,
|
2022-03-24 16:21:34 +00:00
|
|
|
) *DomainPolicyAddedEvent {
|
2020-12-11 14:49:19 +00:00
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
return &DomainPolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
UserLoginMustBeDomain: userLoginMustBeDomain,
|
2022-04-13 09:24:03 +00:00
|
|
|
ValidateOrgDomains: validateOrgDomains,
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func DomainPolicyAddedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
|
|
e := &DomainPolicyAddedEvent{
|
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-TvSmA", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type DomainPolicyChangedEvent struct {
|
2020-12-11 14:49:19 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2021-01-06 10:12:56 +00:00
|
|
|
UserLoginMustBeDomain *bool `json:"userLoginMustBeDomain,omitempty"`
|
2022-04-13 09:24:03 +00:00
|
|
|
ValidateOrgDomains *bool `json:"validateOrgDomains,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyChangedEvent) Data() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyChangedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewDomainPolicyChangedEvent(
|
2020-12-11 14:49:19 +00:00
|
|
|
base *eventstore.BaseEvent,
|
2022-04-13 09:24:03 +00:00
|
|
|
changes []DomainPolicyChanges,
|
2022-03-24 16:21:34 +00:00
|
|
|
) (*DomainPolicyChangedEvent, error) {
|
2021-01-18 10:24:15 +00:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAf3h", "Errors.NoChangesFound")
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
changeEvent := &DomainPolicyChangedEvent{
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-13 09:24:03 +00:00
|
|
|
type DomainPolicyChanges func(*DomainPolicyChangedEvent)
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func ChangeUserLoginMustBeDomain(userLoginMustBeDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
2021-01-18 10:24:15 +00:00
|
|
|
e.UserLoginMustBeDomain = &userLoginMustBeDomain
|
|
|
|
}
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 09:24:03 +00:00
|
|
|
func ChangeValidateOrgDomains(validateOrgDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
|
|
|
e.ValidateOrgDomains = &validateOrgDomain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func DomainPolicyChangedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
|
|
e := &DomainPolicyChangedEvent{
|
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-0Pl9d", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-18 10:24:15 +00:00
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
type DomainPolicyRemovedEvent struct {
|
2021-01-18 10:24:15 +00:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyRemovedEvent) Data() interface{} {
|
2021-01-18 10:24:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func (e *DomainPolicyRemovedEvent) UniqueConstraints() []*eventstore.EventUniqueConstraint {
|
2021-01-21 09:49:38 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func NewDomainPolicyRemovedEvent(base *eventstore.BaseEvent) *DomainPolicyRemovedEvent {
|
|
|
|
return &DomainPolicyRemovedEvent{
|
2021-01-18 10:24:15 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-24 16:21:34 +00:00
|
|
|
func DomainPolicyRemovedEventMapper(event *repository.Event) (eventstore.Event, error) {
|
|
|
|
return &DomainPolicyRemovedEvent{
|
2021-01-18 10:24:15 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|