2021-01-04 13:52:13 +00:00
|
|
|
package policy
|
2020-12-11 14:49:19 +00:00
|
|
|
|
|
|
|
import (
|
2023-10-19 10:19:10 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2020-12-11 14:49:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
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:"-"`
|
|
|
|
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain bool `json:"userLoginMustBeDomain,omitempty"`
|
|
|
|
ValidateOrgDomains bool `json:"validateOrgDomains,omitempty"`
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain bool `json:"smtpSenderAddressMatchesInstanceDomain,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyAddedEvent) Payload() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyAddedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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,
|
2022-05-16 14:08:47 +00:00
|
|
|
validateOrgDomains,
|
|
|
|
smtpSenderAddressMatchesInstanceDomain 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{
|
2022-05-16 14:08:47 +00:00
|
|
|
BaseEvent: *base,
|
|
|
|
UserLoginMustBeDomain: userLoginMustBeDomain,
|
|
|
|
ValidateOrgDomains: validateOrgDomains,
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain: smtpSenderAddressMatchesInstanceDomain,
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DomainPolicyAddedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e := &DomainPolicyAddedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 14:49:19 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-TvSmA", "unable to unmarshal policy")
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
|
2022-05-16 14:08:47 +00:00
|
|
|
UserLoginMustBeDomain *bool `json:"userLoginMustBeDomain,omitempty"`
|
|
|
|
ValidateOrgDomains *bool `json:"validateOrgDomains,omitempty"`
|
|
|
|
SMTPSenderAddressMatchesInstanceDomain *bool `json:"smtpSenderAddressMatchesInstanceDomain,omitempty"`
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyChangedEvent) Payload() interface{} {
|
2020-12-11 14:49:19 +00:00
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyChangedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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 {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowPreconditionFailed(nil, "POLICY-DAf3h", "Errors.NoChangesFound")
|
2021-01-18 10:24:15 +00:00
|
|
|
}
|
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-05-16 14:08:47 +00:00
|
|
|
func ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddressMatchesInstanceDomain bool) func(*DomainPolicyChangedEvent) {
|
|
|
|
return func(e *DomainPolicyChangedEvent) {
|
|
|
|
e.SMTPSenderAddressMatchesInstanceDomain = &smtpSenderAddressMatchesInstanceDomain
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DomainPolicyChangedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
e := &DomainPolicyChangedEvent{
|
2020-12-11 14:49:19 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
err := event.Unmarshal(e)
|
2020-12-11 14:49:19 +00:00
|
|
|
if err != nil {
|
2023-12-08 14:30:55 +00:00
|
|
|
return nil, zerrors.ThrowInternal(err, "POLIC-0Pl9d", "unable to unmarshal policy")
|
2020-12-11 14:49:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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:"-"`
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyRemovedEvent) Payload() interface{} {
|
2021-01-18 10:24:15 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (e *DomainPolicyRemovedEvent) UniqueConstraints() []*eventstore.UniqueConstraint {
|
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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func DomainPolicyRemovedEventMapper(event eventstore.Event) (eventstore.Event, error) {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &DomainPolicyRemovedEvent{
|
2021-01-18 10:24:15 +00:00
|
|
|
BaseEvent: *eventstore.BaseEventFromRepo(event),
|
|
|
|
}, nil
|
|
|
|
}
|