mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
f680dd934d
* chore: rename package errors to zerrors * rename package errors to gerrors * fix error related linting issues * fix zitadel error assertion * fix gosimple linting issues * fix deprecated linting issues * resolve gci linting issues * fix import structure --------- Co-authored-by: Elio Bischof <elio@zitadel.com>
126 lines
3.9 KiB
Go
126 lines
3.9 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"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"
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
|
)
|
|
|
|
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, zerrors.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()
|
|
}
|