zitadel/internal/query/projection/restrictions.go
Elio Bischof dd33538c0a
feat: restrict languages (#6931)
* feat: return 404 or 409 if org reg disallowed

* fix: system limit permissions

* feat: add iam limits api

* feat: disallow public org registrations on default instance

* add integration test

* test: integration

* fix test

* docs: describe public org registrations

* avoid updating docs deps

* fix system limits integration test

* silence integration tests

* fix linting

* ignore strange linter complaints

* review

* improve reset properties naming

* redefine the api

* use restrictions aggregate

* test query

* simplify and test projection

* test commands

* fix unit tests

* move integration test

* support restrictions on default instance

* also test GetRestrictions

* self review

* lint

* abstract away resource owner

* fix tests

* configure supported languages

* fix allowed languages

* fix tests

* default lang must not be restricted

* preferred language must be allowed

* change preferred languages

* check languages everywhere

* lint

* test command side

* lint

* add integration test

* add integration test

* restrict supported ui locales

* lint

* lint

* cleanup

* lint

* allow undefined preferred language

* fix integration tests

* update main

* fix env var

* ignore linter

* ignore linter

* improve integration test config

* reduce cognitive complexity

* compile

* check for duplicates

* remove useless restriction checks

* review

* revert restriction renaming

* fix language restrictions

* lint

* generate

* allow custom texts for supported langs for now

* fix tests

* cleanup

* cleanup

* cleanup

* lint

* unsupported preferred lang is allowed

* fix integration test

* finish reverting to old property name

* finish reverting to old property name

* load languages

* refactor(i18n): centralize translators and fs

* lint

* amplify no validations on preferred languages

* fix integration test

* lint

* fix resetting allowed languages

* test unchanged restrictions
2023-12-05 11:12:01 +00:00

103 lines
3.9 KiB
Go

package projection
import (
"context"
"github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/restrictions"
)
const (
RestrictionsProjectionTable = "projections.restrictions"
RestrictionsColumnAggregateID = "aggregate_id"
RestrictionsColumnCreationDate = "creation_date"
RestrictionsColumnChangeDate = "change_date"
RestrictionsColumnResourceOwner = "resource_owner"
RestrictionsColumnInstanceID = "instance_id"
RestrictionsColumnSequence = "sequence"
RestrictionsColumnDisallowPublicOrgRegistration = "disallow_public_org_registration"
RestrictionsColumnAllowedLanguages = "allowed_languages"
)
type restrictionsProjection struct{}
func newRestrictionsProjection(ctx context.Context, config handler.Config) *handler.Handler {
return handler.NewHandler(ctx, &config, &restrictionsProjection{})
}
func (*restrictionsProjection) Name() string {
return RestrictionsProjectionTable
}
func (*restrictionsProjection) Init() *old_handler.Check {
return handler.NewTableCheck(
handler.NewTable([]*handler.InitColumn{
handler.NewColumn(RestrictionsColumnAggregateID, handler.ColumnTypeText),
handler.NewColumn(RestrictionsColumnCreationDate, handler.ColumnTypeTimestamp),
handler.NewColumn(RestrictionsColumnChangeDate, handler.ColumnTypeTimestamp),
handler.NewColumn(RestrictionsColumnResourceOwner, handler.ColumnTypeText),
handler.NewColumn(RestrictionsColumnInstanceID, handler.ColumnTypeText),
handler.NewColumn(RestrictionsColumnSequence, handler.ColumnTypeInt64),
handler.NewColumn(RestrictionsColumnDisallowPublicOrgRegistration, handler.ColumnTypeBool, handler.Nullable()),
handler.NewColumn(RestrictionsColumnAllowedLanguages, handler.ColumnTypeTextArray, handler.Nullable()),
},
handler.NewPrimaryKey(RestrictionsColumnInstanceID, RestrictionsColumnResourceOwner),
),
)
}
func (p *restrictionsProjection) Reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: restrictions.AggregateType,
EventReducers: []handler.EventReducer{
{
Event: restrictions.SetEventType,
Reduce: p.reduceRestrictionsSet,
},
},
},
{
Aggregate: instance.AggregateType,
EventReducers: []handler.EventReducer{
{
Event: instance.InstanceRemovedEventType,
Reduce: reduceInstanceRemovedHelper(RestrictionsColumnInstanceID),
},
},
},
}
}
func (p *restrictionsProjection) reduceRestrictionsSet(event eventstore.Event) (*handler.Statement, error) {
e, err := assertEvent[*restrictions.SetEvent](event)
if err != nil {
return nil, err
}
conflictCols := []handler.Column{
handler.NewCol(RestrictionsColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(RestrictionsColumnResourceOwner, e.Aggregate().ResourceOwner),
}
updateCols := []handler.Column{
handler.NewCol(RestrictionsColumnInstanceID, e.Aggregate().InstanceID),
handler.NewCol(RestrictionsColumnResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(RestrictionsColumnCreationDate, handler.OnlySetValueOnInsert(RestrictionsProjectionTable, e.CreationDate())),
handler.NewCol(RestrictionsColumnChangeDate, e.CreationDate()),
handler.NewCol(RestrictionsColumnSequence, e.Sequence()),
handler.NewCol(RestrictionsColumnAggregateID, e.Aggregate().ID),
}
if e.DisallowPublicOrgRegistration != nil {
updateCols = append(updateCols, handler.NewCol(RestrictionsColumnDisallowPublicOrgRegistration, *e.DisallowPublicOrgRegistration))
}
if e.AllowedLanguages != nil {
updateCols = append(updateCols, handler.NewCol(RestrictionsColumnAllowedLanguages, domain.LanguagesToStrings(*e.AllowedLanguages)))
}
return handler.NewUpsertStatement(e, conflictCols, updateCols), nil
}