2022-03-24 16:21:34 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-12-06 08:01:31 +00:00
|
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
2022-12-06 08:01:31 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
2022-03-24 16:21:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceDomainPolicyWriteModel struct {
|
|
|
|
PolicyDomainWriteModel
|
|
|
|
}
|
|
|
|
|
2022-04-05 05:58:09 +00:00
|
|
|
func NewInstanceDomainPolicyWriteModel(ctx context.Context) *InstanceDomainPolicyWriteModel {
|
2022-03-24 16:21:34 +00:00
|
|
|
return &InstanceDomainPolicyWriteModel{
|
|
|
|
PolicyDomainWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
2022-04-05 05:58:09 +00:00
|
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
2022-03-24 16:21:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
for _, event := range events {
|
|
|
|
switch e := event.(type) {
|
2022-03-28 08:05:09 +00:00
|
|
|
case *instance.DomainPolicyAddedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyAddedEvent)
|
2022-03-28 08:05:09 +00:00
|
|
|
case *instance.DomainPolicyChangedEvent:
|
2022-03-24 16:21:34 +00:00
|
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyChangedEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) Reduce() error {
|
|
|
|
return wm.PolicyDomainWriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(instance.AggregateType).
|
|
|
|
AggregateIDs(wm.PolicyDomainWriteModel.AggregateID).
|
|
|
|
EventTypes(
|
2022-03-28 08:05:09 +00:00
|
|
|
instance.DomainPolicyAddedEventType,
|
|
|
|
instance.DomainPolicyChangedEventType).
|
2022-03-24 16:21:34 +00:00
|
|
|
Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) NewChangedEvent(
|
|
|
|
ctx context.Context,
|
|
|
|
aggregate *eventstore.Aggregate,
|
2022-04-13 09:24:03 +00:00
|
|
|
userLoginMustBeDomain,
|
2022-05-16 14:08:47 +00:00
|
|
|
validateOrgDomain,
|
2022-12-06 08:01:31 +00:00
|
|
|
smtpSenderAddresssMatchesInstanceDomain bool) (changedEvent *instance.DomainPolicyChangedEvent, usernameChange bool, err error) {
|
2022-04-13 09:24:03 +00:00
|
|
|
changes := make([]policy.DomainPolicyChanges, 0)
|
2022-03-24 16:21:34 +00:00
|
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
2022-12-06 08:01:31 +00:00
|
|
|
usernameChange = true
|
2022-03-24 16:21:34 +00:00
|
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
|
|
|
}
|
2022-04-13 09:24:03 +00:00
|
|
|
if wm.ValidateOrgDomains != validateOrgDomain {
|
|
|
|
changes = append(changes, policy.ChangeValidateOrgDomains(validateOrgDomain))
|
|
|
|
}
|
2022-05-16 14:08:47 +00:00
|
|
|
if wm.SMTPSenderAddressMatchesInstanceDomain != smtpSenderAddresssMatchesInstanceDomain {
|
|
|
|
changes = append(changes, policy.ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddresssMatchesInstanceDomain))
|
|
|
|
}
|
2022-03-24 16:21:34 +00:00
|
|
|
if len(changes) == 0 {
|
2022-12-06 08:01:31 +00:00
|
|
|
return nil, false, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-pl9fN", "Errors.IAM.DomainPolicy.NotChanged")
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
2022-12-06 08:01:31 +00:00
|
|
|
changedEvent, err = instance.NewDomainPolicyChangedEvent(ctx, aggregate, changes)
|
|
|
|
return changedEvent, usernameChange, err
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainPolicyOrgsWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
|
|
|
|
|
|
|
OrgIDs []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDomainPolicyOrgsWriteModel() *DomainPolicyOrgsWriteModel {
|
|
|
|
return &DomainPolicyOrgsWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{},
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|
2022-12-06 08:01:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *DomainPolicyOrgsWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
|
|
wm.WriteModel.AppendEvents(events...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *DomainPolicyOrgsWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
switch e := event.(type) {
|
|
|
|
case *org.OrgAddedEvent:
|
|
|
|
wm.OrgIDs = append(wm.OrgIDs, e.Aggregate().ID)
|
|
|
|
case *org.DomainPolicyAddedEvent:
|
|
|
|
for i, orgID := range wm.OrgIDs {
|
|
|
|
if orgID == e.Aggregate().ID {
|
|
|
|
wm.OrgIDs[i] = wm.OrgIDs[len(wm.OrgIDs)-1]
|
|
|
|
wm.OrgIDs = wm.OrgIDs[:len(wm.OrgIDs)-1]
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case *org.DomainPolicyRemovedEvent:
|
|
|
|
wm.OrgIDs = append(wm.OrgIDs, e.Aggregate().ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *DomainPolicyOrgsWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
return eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(org.AggregateType).
|
|
|
|
EventTypes(
|
|
|
|
org.OrgAddedEventType,
|
|
|
|
org.DomainPolicyAddedEventType,
|
|
|
|
org.DomainPolicyRemovedEventType).
|
|
|
|
Builder()
|
2022-03-24 16:21:34 +00:00
|
|
|
}
|