2023-11-22 10:29:38 +01:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2023-12-05 12:12:01 +01:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-11-22 10:29:38 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/repository/restrictions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type restrictionsWriteModel struct {
|
|
|
|
eventstore.WriteModel
|
2023-12-05 12:12:01 +01:00
|
|
|
disallowPublicOrgRegistration bool
|
|
|
|
allowedLanguages []language.Tag
|
2023-11-22 10:29:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// newRestrictionsWriteModel aggregateId is filled by reducing unit matching events
|
|
|
|
func newRestrictionsWriteModel(instanceId, resourceOwner string) *restrictionsWriteModel {
|
|
|
|
return &restrictionsWriteModel{
|
|
|
|
WriteModel: eventstore.WriteModel{
|
|
|
|
InstanceID: instanceId,
|
|
|
|
ResourceOwner: resourceOwner,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *restrictionsWriteModel) Query() *eventstore.SearchQueryBuilder {
|
|
|
|
query := eventstore.NewSearchQueryBuilder(eventstore.ColumnsEvent).
|
|
|
|
ResourceOwner(wm.ResourceOwner).
|
|
|
|
InstanceID(wm.InstanceID).
|
|
|
|
AddQuery().
|
|
|
|
AggregateTypes(restrictions.AggregateType).
|
|
|
|
EventTypes(restrictions.SetEventType)
|
|
|
|
|
|
|
|
return query.Builder()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wm *restrictionsWriteModel) Reduce() error {
|
|
|
|
for _, event := range wm.Events {
|
|
|
|
wm.ChangeDate = event.CreatedAt()
|
2023-12-05 12:12:01 +01:00
|
|
|
e, ok := event.(*restrictions.SetEvent)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if e.DisallowPublicOrgRegistration != nil {
|
|
|
|
wm.disallowPublicOrgRegistration = *e.DisallowPublicOrgRegistration
|
|
|
|
}
|
|
|
|
if e.AllowedLanguages != nil {
|
|
|
|
wm.allowedLanguages = *e.AllowedLanguages
|
2023-11-22 10:29:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wm.WriteModel.Reduce()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewChanges returns all changes that need to be applied to the aggregate.
|
|
|
|
// nil properties in setRestrictions are ignored
|
|
|
|
func (wm *restrictionsWriteModel) NewChanges(setRestrictions *SetRestrictions) (changes []restrictions.RestrictionsChange) {
|
|
|
|
if setRestrictions == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
changes = make([]restrictions.RestrictionsChange, 0, 1)
|
2023-12-05 12:12:01 +01:00
|
|
|
if setRestrictions.DisallowPublicOrgRegistration != nil && (wm.disallowPublicOrgRegistration != *setRestrictions.DisallowPublicOrgRegistration) {
|
|
|
|
changes = append(changes, restrictions.ChangeDisallowPublicOrgRegistration(*setRestrictions.DisallowPublicOrgRegistration))
|
|
|
|
}
|
|
|
|
if setRestrictions.AllowedLanguages != nil && domain.LanguagesDiffer(wm.allowedLanguages, setRestrictions.AllowedLanguages) {
|
|
|
|
changes = append(changes, restrictions.ChangeAllowedLanguages(setRestrictions.AllowedLanguages))
|
2023-11-22 10:29:38 +01:00
|
|
|
}
|
|
|
|
return changes
|
|
|
|
}
|