mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-24 00:28:18 +00:00
3539418a4a
* fix: handle UserLoginMustBeDomain changes correctly * fix: remove verified domains (and not only primary) as suffix * fix: ensure testability by changing map to slice * cleanup * reduce complexity of DomainPolicyUsernamesWriteModel.Reduce() * add test for removed org policy
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
caos_errs "github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
"github.com/zitadel/zitadel/internal/repository/policy"
|
|
)
|
|
|
|
type InstanceDomainPolicyWriteModel struct {
|
|
PolicyDomainWriteModel
|
|
}
|
|
|
|
func NewInstanceDomainPolicyWriteModel(ctx context.Context) *InstanceDomainPolicyWriteModel {
|
|
return &InstanceDomainPolicyWriteModel{
|
|
PolicyDomainWriteModel{
|
|
WriteModel: eventstore.WriteModel{
|
|
AggregateID: authz.GetInstance(ctx).InstanceID(),
|
|
ResourceOwner: authz.GetInstance(ctx).InstanceID(),
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) AppendEvents(events ...eventstore.Event) {
|
|
for _, event := range events {
|
|
switch e := event.(type) {
|
|
case *instance.DomainPolicyAddedEvent:
|
|
wm.PolicyDomainWriteModel.AppendEvents(&e.DomainPolicyAddedEvent)
|
|
case *instance.DomainPolicyChangedEvent:
|
|
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(
|
|
instance.DomainPolicyAddedEventType,
|
|
instance.DomainPolicyChangedEventType).
|
|
Builder()
|
|
}
|
|
|
|
func (wm *InstanceDomainPolicyWriteModel) NewChangedEvent(
|
|
ctx context.Context,
|
|
aggregate *eventstore.Aggregate,
|
|
userLoginMustBeDomain,
|
|
validateOrgDomain,
|
|
smtpSenderAddresssMatchesInstanceDomain bool) (changedEvent *instance.DomainPolicyChangedEvent, usernameChange bool, err error) {
|
|
changes := make([]policy.DomainPolicyChanges, 0)
|
|
if wm.UserLoginMustBeDomain != userLoginMustBeDomain {
|
|
usernameChange = true
|
|
changes = append(changes, policy.ChangeUserLoginMustBeDomain(userLoginMustBeDomain))
|
|
}
|
|
if wm.ValidateOrgDomains != validateOrgDomain {
|
|
changes = append(changes, policy.ChangeValidateOrgDomains(validateOrgDomain))
|
|
}
|
|
if wm.SMTPSenderAddressMatchesInstanceDomain != smtpSenderAddresssMatchesInstanceDomain {
|
|
changes = append(changes, policy.ChangeSMTPSenderAddressMatchesInstanceDomain(smtpSenderAddresssMatchesInstanceDomain))
|
|
}
|
|
if len(changes) == 0 {
|
|
return nil, false, caos_errs.ThrowPreconditionFailed(nil, "INSTANCE-pl9fN", "Errors.IAM.DomainPolicy.NotChanged")
|
|
}
|
|
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{},
|
|
}
|
|
}
|
|
|
|
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()
|
|
}
|