2021-01-04 14:52:13 +01:00
|
|
|
package policy
|
2020-12-11 15:49:19 +01:00
|
|
|
|
|
|
|
import (
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-24 17:21:34 +01:00
|
|
|
DomainPolicyAddedEventType = "policy.domain.added"
|
|
|
|
DomainPolicyChangedEventType = "policy.domain.changed"
|
|
|
|
DomainPolicyRemovedEventType = "policy.domain.removed"
|
2020-12-11 15:49:19 +01:00
|
|
|
)
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type DomainPolicyAddedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2022-05-16 16:08:47 +02:00
|
|
|
UserLoginMustBeDomain bool `json:"userLoginMustBeDomain,omitempty"`
|
|
|
|
ValidateOrgDomains bool `json:"validateOrgDomains,omitempty"`
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain bool `json:"smtpSenderAddressMatchesInstanceDomain,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyAddedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func NewDomainPolicyAddedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
2022-04-13 11:24:03 +02:00
|
|
|
userLoginMustBeDomain,
|
2022-05-16 16:08:47 +02:00
|
|
|
validateOrgDomains,
|
|
|
|
smtpSenderAddressMatchesInstanceDomain bool,
|
2022-03-24 17:21:34 +01:00
|
|
|
) *DomainPolicyAddedEvent {
|
2020-12-11 15:49:19 +01:00
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
return &DomainPolicyAddedEvent{
|
2022-05-16 16:08:47 +02:00
|
|
|
BaseEvent: *base,
|
|
|
|
UserLoginMustBeDomain: userLoginMustBeDomain,
|
|
|
|
ValidateOrgDomains: validateOrgDomains,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: smtpSenderAddressMatchesInstanceDomain,
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func DomainPolicyAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e := &DomainPolicyAddedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-TvSmA", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type DomainPolicyChangedEvent struct {
|
2020-12-11 15:49:19 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
|
2022-05-16 16:08:47 +02:00
|
|
|
UserLoginMustBeDomain *bool `json:"userLoginMustBeDomain,omitempty"`
|
|
|
|
ValidateOrgDomains *bool `json:"validateOrgDomains,omitempty"`
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain *bool `json:"smtpSenderAddressMatchesInstanceDomain,omitempty"`
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyChangedEvent) Payload() interface{} {
|
2020-12-11 15:49:19 +01:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func NewDomainPolicyChangedEvent(
|
2020-12-11 15:49:19 +01:00
|
|
|
base *eventstore.BaseEvent,
|
2022-04-13 11:24:03 +02:00
|
|
|
changes []DomainPolicyChanges,
|
2022-03-24 17:21:34 +01:00
|
|
|
) (*DomainPolicyChangedEvent, error) {
|
2021-01-18 11:24:15 +01:00
|
|
|
if len(changes) == 0 {
|
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "POLICY-DAf3h", "Errors.NoChangesFound")
|
|
|
|
}
|
2022-03-24 17:21:34 +01:00
|
|
|
changeEvent := &DomainPolicyChangedEvent{
|
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
|
|
|
|
}
|
|
|
|
|
2022-04-13 11:24:03 +02:00
|
|
|
type DomainPolicyChanges func(*DomainPolicyChangedEvent)
|
2021-01-18 11:24:15 +01:00
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func ChangeUserLoginMustBeDomain(userLoginMustBeDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
2021-01-18 11:24:15 +01:00
|
|
|
e.UserLoginMustBeDomain = &userLoginMustBeDomain
|
|
|
|
}
|
2020-12-11 15:49:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-13 11:24:03 +02:00
|
|
|
func ChangeValidateOrgDomains(validateOrgDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
|
|
|
e.ValidateOrgDomains = &validateOrgDomain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-16 16:08:47 +02:00
|
|
|
func ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddressMatchesInstanceDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
|
|
|
e.SMTPSenderAddressMatchesInstanceDomain = &smtpSenderAddressMatchesInstanceDomain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func DomainPolicyChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
e := &DomainPolicyChangedEvent{
|
2020-12-11 15:49:19 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 15:49:19 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "POLIC-0Pl9d", "unable to unmarshal policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
return e, nil
|
|
|
|
}
|
2021-01-18 11:24:15 +01:00
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
type DomainPolicyRemovedEvent struct {
|
2021-01-18 11:24:15 +01:00
|
|
|
eventstore.BaseEvent `json:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyRemovedEvent) Payload() interface{} {
|
2021-01-18 11:24:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func (e *DomainPolicyRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
2021-01-21 10:49:38 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-24 17:21:34 +01:00
|
|
|
func NewDomainPolicyRemovedEvent(base *eventstore.BaseEvent) *DomainPolicyRemovedEvent {
|
|
|
|
return &DomainPolicyRemovedEvent{
|
2021-01-18 11:24:15 +01:00
|
|
|
BaseEvent: *base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
func DomainPolicyRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 17:21:34 +01:00
|
|
|
return &DomainPolicyRemovedEvent{
|
2021-01-18 11:24:15 +01:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|