Files
zitadel/internal/query/projection/settings_relational.go
T
jmaretteandGitHub f34cc45eac feat(oidc): add RFC 7591 dynamic client registration (#12313)
# Which Problems Are Solved

ZITADEL cannot currently be used as an OAuth 2.0 Authorization Server by
clients that need to register themselves at runtime:

- There is no [OAuth 2.0 Dynamic Client Registration (RFC
7591)](https://datatracker.ietf.org/doc/html/rfc7591) endpoint, and no
`registration_endpoint` is advertised in the discovery document.
- This blocks [Model Context Protocol
(MCP)](https://modelcontextprotocol.io) clients (Claude Desktop,
claude.ai, Cursor, the MCP SDKs), whose OAuth 2.1 authorization profile
expects an Authorization Server that supports dynamic client
registration. They self-register before any user context exists and only
then start the authorization code + PKCE flow.

# How the Problems Are Solved

## Control plane: instance security settings

No feature flag and no runtime config. A
`DynamicClientRegistrationSettings` message is nested under the instance
`SecuritySettings` in `settings/v2`, next to `embedded_iframe` and
`enable_impersonation`:

| `enabled` | `allow_unauthenticated` | Behaviour |
| --- | --- | --- |
| `false` (default) | — | `POST /oauth/v2/register` returns `404`,
`registration_endpoint` absent from discovery |
| `true` | `false` (default) | Token-gated registration, client homed in
the token's organization |
| `true` | `true` | Open registration for MCP hosts, client homed in the
instance default organization |

Both values are carried on `authz.Instance` and loaded with the
instance, exactly like `EnableImpersonation`, so the discovery document
and the endpoint read them per request without an extra query.
`allow_unauthenticated` implies `enabled` in both implementations, so a
single setting closes the endpoint.

Adding the two columns bumps the projection table to
`projections.security_policies3`, following what the impersonation
setting did for `security_policies2`.

## Authorization: a dedicated permission, not `project.app.write`

- New org-scoped permission **`project.app.register_dynamic`**, granted
by default to `ORG_OWNER`, `IAM_OWNER` and `IAM_ORG_MANAGER` — in both
`InternalAuthZ` and `SystemAuthZ`, mirroring where `project.app.write`
already lives.
- New built-in role **`ORG_DYNAMIC_CLIENT_REGISTRAR`** carrying only
that permission, so a service user can self-register clients without
gaining write access to the organization's existing applications.
- Open mode (`allow_unauthenticated = true`) requires no token and no
permission: that is the MCP self-register path.
- A valid token whose user lacks the permission is answered with **`403
insufficient_scope`** (RFC 6750 §3.1) rather than `401 invalid_token`,
so a caller can tell "your token is bad" from "your token may not do
this". `verifyAccessToken` also reports invalid tokens as permission
denied, so the authorization failure is marked explicitly to keep the
two apart.

The check runs once, at the endpoint, right after the token is verified
and before any state is created, so it also gates the auto-provisioning
of the organization's DCR project. The permission itself lives in the
command layer next to the others
(`Commands.CheckPermissionRegisterDynamicClient`). As the OIDC endpoints
are not behind the authorization interceptor, the caller identity is
derived from the verified token, the way token exchange does for the
impersonating actor.

## Registration itself

- **Registered clients are ordinary OIDC applications.** The endpoint
reuses the existing application-creation events
(`NewApplicationAddedEvent` and `NewOIDCConfigAddedEvent`), so the whole
token, authorization and introspection flow keeps working unchanged.
They are stored in a dedicated, auto-provisioned project named `ZITADEL
DCR` per organization (name lookup), so they do not pollute the
`IAMProject` or other projects.
- Input is validated with the existing `GetOIDCV1Compliance` rules;
`private_key_jwt` and `jwks`/`jwks_uri` are rejected with
`invalid_client_metadata`.
- **Endpoint wiring without forking the library.** `NewServer` calls
`op.RegisterServer` instead of `op.RegisterLegacyServer`, so the new
route can be added through the same `op.WithSetRouter` hook as the
authorize callback. `op.RegisterLegacyServer` appends a middleware after
the caller options, which chi forbids once a route has been registered;
`op.NewIssuerInterceptor` reproduces the issuer middleware it would
otherwise add. The `/oauth/v2` routing prefix is unchanged.

# Additional Changes

- **`application_type` inference for custom-scheme redirects** (last
commit, isolated). `application_type` is an OpenID Connect member; RFC
7591 does not define it, so clients that only implement RFC 7591 omit
it. Native MCP hosts doing so register custom-scheme redirect URIs,
which ZITADEL reserves for native applications, and the OIDC default of
`web` rejected them with `invalid_redirect_uri`. Inference is limited to
custom schemes and to an absent `application_type`, so every request
accepted today keeps its current application type and auth method — an
`http` loopback redirect still yields a web application. Happy to drop
this commit if you would rather keep it out of this PR.
- **Docs**: a Dynamic Client Registration integration guide, a
`registration_endpoint` section on the OpenID Connect endpoints page,
and the new role in the administrators table (plus its description in
the Console translations).

# Additional Context

- Part of #9810. This is **Phase 1 (DCR)**; #12315 adds RFC 7592 and
#12316 adds CIMD, both stacked on this branch and rebased once this
lands.
- Implements the control-plane and authorization model requested in the
review of this PR, itself a refinement of the direction in
https://github.com/zitadel/zitadel/issues/9810#issuecomment-4891176997.
- **Out of scope (deliberately)**: RFC 7592, CIMD, persisting free-form
metadata (`logo_uri`, `contacts`, and similar), and `private_key_jwt` /
`jwks_uri` (rejected cleanly). No new event types.
- Follow-ups explicitly left out, per the review: DCR client lifecycle
and richer audit, consent and custom scopes, JWT / `aud` (RFC 8707), and
RFC 8414 AS metadata (currently absent from both ZITADEL and
`zitadel/oidc`).
2026-07-30 14:43:47 +00:00

1603 lines
64 KiB
Go

package projection
import (
"context"
"database/sql"
"net/url"
"time"
"github.com/muhlemmer/gu"
"github.com/zitadel/zitadel/backend/v3/domain"
v3_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
legacy_domain "github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
"github.com/zitadel/zitadel/internal/repository/instance"
"github.com/zitadel/zitadel/internal/repository/org"
settings "github.com/zitadel/zitadel/internal/repository/organization_settings"
"github.com/zitadel/zitadel/internal/repository/policy"
"github.com/zitadel/zitadel/internal/zerrors"
)
func (p *relationalTablesProjection) reduceLoginPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LoginPolicyAddedEvent
switch e := event.(type) {
case *instance.LoginPolicyAddedEvent:
policyEvent = e.LoginPolicyAddedEvent
case *org.LoginPolicyAddedEvent:
policyEvent = e.LoginPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-YYPxS", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicyAddedEventType, instance.LoginPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
passwordlessType := domain.PasswordlessType(policyEvent.PasswordlessType)
setting := domain.LoginSettings{
Settings: domain.Settings{
InstanceID: policyEvent.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
LoginSettingsAttributes: domain.LoginSettingsAttributes{
AllowUserNamePassword: &policyEvent.AllowUserNamePassword,
AllowRegister: &policyEvent.AllowRegister,
AllowExternalIDP: &policyEvent.AllowExternalIDP,
ForceMFA: &policyEvent.ForceMFA,
ForceMFALocalOnly: &policyEvent.ForceMFALocalOnly,
HidePasswordReset: &policyEvent.HidePasswordReset,
IgnoreUnknownUsernames: &policyEvent.IgnoreUnknownUsernames,
AllowDomainDiscovery: &policyEvent.AllowDomainDiscovery,
DisableLoginWithEmail: &policyEvent.DisableLoginWithEmail,
DisableLoginWithPhone: &policyEvent.DisableLoginWithPhone,
PasswordlessType: &passwordlessType,
DefaultRedirectURI: &policyEvent.DefaultRedirectURI,
PasswordCheckLifetime: &policyEvent.PasswordCheckLifetime,
ExternalLoginCheckLifetime: &policyEvent.ExternalLoginCheckLifetime,
MFAInitSkipLifetime: &policyEvent.MFAInitSkipLifetime,
SecondFactorCheckLifetime: &policyEvent.SecondFactorCheckLifetime,
MultiFactorCheckLifetime: &policyEvent.MultiFactorCheckLifetime,
MFAType: nil,
SecondFactorTypes: nil,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &setting)
}), nil
}
func (p *relationalTablesProjection) reduceLoginPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LoginPolicyChangedEvent
switch e := event.(type) {
case *instance.LoginPolicyChangedEvent:
policyEvent = e.LoginPolicyChangedEvent
case *org.LoginPolicyChangedEvent:
policyEvent = e.LoginPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-BHd86", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicyChangedEventType, instance.LoginPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rLk9y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
var passwordlessType *domain.PasswordlessType
if policyEvent.PasswordlessType != nil {
passwordlessType = gu.Ptr(domain.PasswordlessType(*policyEvent.PasswordlessType))
}
settings := domain.LoginSettings{
Settings: domain.Settings{
InstanceID: policyEvent.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
LoginSettingsAttributes: domain.LoginSettingsAttributes{
AllowUserNamePassword: policyEvent.AllowUserNamePassword,
AllowRegister: policyEvent.AllowRegister,
AllowExternalIDP: policyEvent.AllowExternalIDP,
ForceMFA: policyEvent.ForceMFA,
ForceMFALocalOnly: policyEvent.ForceMFALocalOnly,
HidePasswordReset: policyEvent.HidePasswordReset,
IgnoreUnknownUsernames: policyEvent.IgnoreUnknownUsernames,
AllowDomainDiscovery: policyEvent.AllowDomainDiscovery,
DisableLoginWithEmail: policyEvent.DisableLoginWithEmail,
DisableLoginWithPhone: policyEvent.DisableLoginWithPhone,
PasswordlessType: passwordlessType,
DefaultRedirectURI: policyEvent.DefaultRedirectURI,
PasswordCheckLifetime: policyEvent.PasswordCheckLifetime,
ExternalLoginCheckLifetime: policyEvent.ExternalLoginCheckLifetime,
MFAInitSkipLifetime: policyEvent.MFAInitSkipLifetime,
SecondFactorCheckLifetime: policyEvent.SecondFactorCheckLifetime,
MultiFactorCheckLifetime: policyEvent.MultiFactorCheckLifetime,
MFAType: nil,
SecondFactorTypes: nil,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLoginPolicyMFAAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.MultiFactorAddedEvent
switch e := event.(type) {
case *instance.LoginPolicyMultiFactorAddedEvent:
policyEvent = e.MultiFactorAddedEvent
case *org.LoginPolicyMultiFactorAddedEvent:
policyEvent = e.MultiFactorAddedEvent
orgId = &policyEvent.Aggregate().ID
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-WghuV", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicyMultiFactorAddedEventType, instance.LoginPolicyMultiFactorAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rLw7y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
return settingsRepo.SetColumns(ctx, v3_sql.SQLTx(tx),
&domain.Settings{
InstanceID: policyEvent.Agg.InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
settingsRepo.AddMFAType(domain.MultiFactorType(policyEvent.MFAType)),
)
}), nil
}
func (p *relationalTablesProjection) reduceLoginPolicyMFARemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.MultiFactorRemovedEvent
switch e := event.(type) {
case *instance.LoginPolicyMultiFactorRemovedEvent:
policyEvent = e.MultiFactorRemovedEvent
case *org.LoginPolicyMultiFactorRemovedEvent:
policyEvent = e.MultiFactorRemovedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-cHU7u", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicyMultiFactorRemovedEventType, instance.LoginPolicyMultiFactorRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rLi9y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
return settingsRepo.SetColumns(ctx, v3_sql.SQLTx(tx),
&domain.Settings{
InstanceID: policyEvent.Agg.InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
settingsRepo.RemoveMFAType(domain.MultiFactorType(policyEvent.MFAType)),
)
}), nil
}
func (*relationalTablesProjection) reduceLoginPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
loginPolicyRemovedEvent, ok := event.(*org.LoginPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-oRSvD", "reduce.wrong.event.type %s", org.LoginPolicyRemovedEventType)
}
return handler.NewStatement(loginPolicyRemovedEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-arg9y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
_, err := settingsRepo.Delete(
ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(loginPolicyRemovedEvent.Aggregate().InstanceID, &loginPolicyRemovedEvent.Aggregate().ID, domain.SettingTypeLogin, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceLoginPolicySecondFactorAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.SecondFactorAddedEvent
switch e := event.(type) {
case *instance.LoginPolicySecondFactorAddedEvent:
policyEvent = e.SecondFactorAddedEvent
case *org.LoginPolicySecondFactorAddedEvent:
policyEvent = e.SecondFactorAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-apB2E", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicySecondFactorAddedEventType, instance.LoginPolicySecondFactorAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-iLk4m", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
return settingsRepo.SetColumns(ctx, v3_sql.SQLTx(tx),
&domain.Settings{
InstanceID: policyEvent.Agg.InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
settingsRepo.AddSecondFactorTypes(domain.SecondFactorType(policyEvent.MFAType)),
)
}), nil
}
func (p *relationalTablesProjection) reduceLoginPolicySecondFactorRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.SecondFactorRemovedEvent
switch e := event.(type) {
case *instance.LoginPolicySecondFactorRemovedEvent:
policyEvent = e.SecondFactorRemovedEvent
case *org.LoginPolicySecondFactorRemovedEvent:
policyEvent = e.SecondFactorRemovedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-bYpmA", "reduce.wrong.event.type %v", []eventstore.EventType{org.LoginPolicySecondFactorRemovedEventType, instance.LoginPolicySecondFactorRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rnd0y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LoginSettingsRepository()
return settingsRepo.SetColumns(ctx, v3_sql.SQLTx(tx),
&domain.Settings{
InstanceID: policyEvent.Agg.InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
settingsRepo.RemoveSecondFactorTypes(domain.SecondFactorType(policyEvent.MFAType)),
)
}), nil
}
// label
func (p *relationalTablesProjection) reduceLabelPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyAddedEvent
switch e := event.(type) {
case *org.LabelPolicyAddedEvent:
policyEvent = e.LabelPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.LabelPolicyAddedEvent:
policyEvent = e.LabelPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-CSE7A", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyAddedEventType, instance.LabelPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
themeMode := domain.BrandingPolicyThemeMode(policyEvent.ThemeMode)
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: policyEvent.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
PrimaryColorLight: &policyEvent.PrimaryColor,
BackgroundColorLight: &policyEvent.BackgroundColor,
WarnColorLight: &policyEvent.WarnColor,
FontColorLight: &policyEvent.FontColor,
PrimaryColorDark: &policyEvent.PrimaryColorDark,
BackgroundColorDark: &policyEvent.BackgroundColorDark,
WarnColorDark: &policyEvent.WarnColorDark,
FontColorDark: &policyEvent.FontColorDark,
HideLoginNameSuffix: &policyEvent.HideLoginNameSuffix,
ErrorMsgPopup: &policyEvent.ErrorMsgPopup,
DisableWatermark: &policyEvent.DisableWatermark,
ThemeMode: &themeMode,
LogoURLLight: nil,
IconURLLight: nil,
LogoURLDark: nil,
IconURLDark: nil,
FontURL: nil,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyChangedEvent
switch e := event.(type) {
case *org.LabelPolicyChangedEvent:
policyEvent = e.LabelPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.LabelPolicyChangedEvent:
policyEvent = e.LabelPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-qgVug", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyChangedEventType, instance.LabelPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-lhb9y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
var themeMode *domain.BrandingPolicyThemeMode
if policyEvent.ThemeMode != nil {
themeMode = gu.Ptr(domain.BrandingPolicyThemeMode(*policyEvent.ThemeMode))
}
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: policyEvent.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
PrimaryColorLight: policyEvent.PrimaryColor,
BackgroundColorLight: policyEvent.BackgroundColor,
WarnColorLight: policyEvent.WarnColor,
FontColorLight: policyEvent.FontColor,
PrimaryColorDark: policyEvent.PrimaryColorDark,
BackgroundColorDark: policyEvent.BackgroundColorDark,
WarnColorDark: policyEvent.WarnColorDark,
FontColorDark: policyEvent.FontColorDark,
HideLoginNameSuffix: policyEvent.HideLoginNameSuffix,
ErrorMsgPopup: policyEvent.ErrorMsgPopup,
DisableWatermark: policyEvent.DisableWatermark,
ThemeMode: themeMode,
LogoURLLight: nil,
IconURLLight: nil,
LogoURLDark: nil,
IconURLDark: nil,
FontURL: nil,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.LabelPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-ATMBz", "reduce.wrong.event.type %s", org.LabelPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-r7k0y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypeBranding, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyActivated(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyActivatedEvent
switch e := event.(type) {
case *org.LabelPolicyActivatedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyActivatedEvent
case *instance.LabelPolicyActivatedEvent:
policyEvent = e.LabelPolicyActivatedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-7Kd8U", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyActivatedEventType, instance.LabelPolicyActivatedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-r7k0y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
_, err := settingsRepo.ActivateAt(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, orgId, domain.SettingTypeBranding, domain.SettingStatePreview),
policyEvent.Creation,
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyLogoAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyLogoAddedEvent
switch e := event.(type) {
case *org.LabelPolicyLogoAddedEvent:
orgId = &e.Aggregate().ResourceOwner
policyEvent = e.LabelPolicyLogoAddedEvent
case *instance.LabelPolicyLogoAddedEvent:
policyEvent = e.LabelPolicyLogoAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-kg8H4", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyLogoAddedEventType, instance.LabelPolicyLogoAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
url, err := url.Parse(policyEvent.StoreKey)
if err != nil {
return err
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
LogoURLLight: url,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyLogoDarkAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyLogoDarkAddedEvent
switch e := event.(type) {
case *org.LabelPolicyLogoDarkAddedEvent:
orgId = &e.Aggregate().ResourceOwner
policyEvent = e.LabelPolicyLogoDarkAddedEvent
case *instance.LabelPolicyLogoDarkAddedEvent:
policyEvent = e.LabelPolicyLogoDarkAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-kg8H4", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyLogoDarkAddedEventType, instance.LabelPolicyLogoDarkAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
url, err := url.Parse(policyEvent.StoreKey)
if err != nil {
return err
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
LogoURLDark: url,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyLogoRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyLogoRemovedEvent
switch e := event.(type) {
case *org.LabelPolicyLogoRemovedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyLogoRemovedEvent
case *instance.LabelPolicyLogoRemovedEvent:
policyEvent = e.LabelPolicyLogoRemovedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-kg8H4", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyLogoRemovedEventType, instance.LabelPolicyLogoRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
LogoURLLight: &url.URL{},
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyLogoDarkRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyLogoDarkRemovedEvent
switch e := event.(type) {
case *org.LabelPolicyLogoDarkRemovedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyLogoDarkRemovedEvent
case *instance.LabelPolicyLogoDarkRemovedEvent:
policyEvent = e.LabelPolicyLogoDarkRemovedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-kg8H4", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyLogoDarkRemovedEventType, instance.LabelPolicyLogoDarkRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
LogoURLDark: &url.URL{},
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyIconAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyIconAddedEvent
switch e := event.(type) {
case *org.LabelPolicyIconAddedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyIconAddedEvent
case *instance.LabelPolicyIconAddedEvent:
policyEvent = e.LabelPolicyIconAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-e2JFz", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyIconDarkAddedEventType, instance.LabelPolicyIconDarkAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
url, err := url.Parse(policyEvent.StoreKey)
if err != nil {
return err
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
IconURLLight: url,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyIconDarkAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyIconDarkAddedEvent
switch e := event.(type) {
case *org.LabelPolicyIconDarkAddedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyIconDarkAddedEvent
case *instance.LabelPolicyIconDarkAddedEvent:
policyEvent = e.LabelPolicyIconDarkAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-e2JFz", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyIconDarkAddedEventType, instance.LabelPolicyIconDarkAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
url, err := url.Parse(policyEvent.StoreKey)
if err != nil {
return err
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
IconURLDark: url,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyIconRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyIconRemovedEvent
switch e := event.(type) {
case *org.LabelPolicyIconRemovedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyIconRemovedEvent
case *instance.LabelPolicyIconRemovedEvent:
policyEvent = e.LabelPolicyIconRemovedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-gfgbY", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyIconRemovedEventType, instance.LabelPolicyIconRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
IconURLLight: &url.URL{},
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyIconDarkRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyIconDarkRemovedEvent
switch e := event.(type) {
case *org.LabelPolicyIconDarkRemovedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyIconDarkRemovedEvent
case *instance.LabelPolicyIconDarkRemovedEvent:
policyEvent = e.LabelPolicyIconDarkRemovedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-gfgbY", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyIconDarkRemovedEventType, instance.LabelPolicyIconDarkRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
IconURLDark: &url.URL{},
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyFontAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyFontAddedEvent
switch e := event.(type) {
case *org.LabelPolicyFontAddedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyFontAddedEvent
case *instance.LabelPolicyFontAddedEvent:
policyEvent = e.LabelPolicyFontAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-65i9W", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyFontAddedEventType, instance.LabelPolicyFontAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
url, err := url.Parse(policyEvent.StoreKey)
if err != nil {
return err
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
FontURL: url,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLabelPolicyFontRemoved(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LabelPolicyFontRemovedEvent
switch e := event.(type) {
case *org.LabelPolicyFontRemovedEvent:
orgId = &event.Aggregate().ID
policyEvent = e.LabelPolicyFontRemovedEvent
case *instance.LabelPolicyFontRemovedEvent:
policyEvent = e.LabelPolicyFontRemovedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-xf32J", "reduce.wrong.event.type %v", []eventstore.EventType{org.LabelPolicyFontRemovedEventType, instance.LabelPolicyFontRemovedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.BrandingSettingsRepository()
settings := domain.BrandingSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
BrandingSettingsAttributes: domain.BrandingSettingsAttributes{
FontURL: &url.URL{},
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePasswordComplexityPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PasswordComplexityPolicyAddedEvent
switch e := event.(type) {
case *org.PasswordComplexityPolicyAddedEvent:
policyEvent = e.PasswordComplexityPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PasswordComplexityPolicyAddedEvent:
policyEvent = e.PasswordComplexityPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-KTHmJ", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordComplexityPolicyAddedEventType, instance.PasswordComplexityPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordComplexitySettingsRepository()
settings := domain.PasswordComplexitySettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
PasswordComplexitySettingsAttributes: domain.PasswordComplexitySettingsAttributes{
MinLength: &policyEvent.MinLength,
HasLowercase: &policyEvent.HasLowercase,
HasUppercase: &policyEvent.HasUppercase,
HasNumber: &policyEvent.HasNumber,
HasSymbol: &policyEvent.HasSymbol,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePasswordComplexityPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PasswordComplexityPolicyChangedEvent
switch e := event.(type) {
case *org.PasswordComplexityPolicyChangedEvent:
policyEvent = e.PasswordComplexityPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PasswordComplexityPolicyChangedEvent:
policyEvent = e.PasswordComplexityPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-cf3Xb", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordComplexityPolicyChangedEventType, instance.PasswordComplexityPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rLrfy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordComplexitySettingsRepository()
settings := domain.PasswordComplexitySettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
PasswordComplexitySettingsAttributes: domain.PasswordComplexitySettingsAttributes{
MinLength: policyEvent.MinLength,
HasLowercase: policyEvent.HasLowercase,
HasUppercase: policyEvent.HasUppercase,
HasNumber: policyEvent.HasNumber,
HasSymbol: policyEvent.HasSymbol,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePasswordComplexityPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.PasswordComplexityPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-wttCd", "reduce.wrong.event.type %s", org.PasswordComplexityPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordComplexitySettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypePasswordComplexity, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reducePasswordAgePolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PasswordAgePolicyAddedEvent
switch e := event.(type) {
case *org.PasswordAgePolicyAddedEvent:
policyEvent = e.PasswordAgePolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PasswordAgePolicyAddedEvent:
policyEvent = e.PasswordAgePolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-CJqF0", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordAgePolicyAddedEventType, instance.PasswordAgePolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hONE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordExpirySettingsRepository()
settings := domain.PasswordExpirySettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
PasswordExpirySettingsAttributes: domain.PasswordExpirySettingsAttributes{
ExpireWarnDays: &policyEvent.ExpireWarnDays,
MaxAgeDays: &policyEvent.MaxAgeDays,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePasswordAgePolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PasswordAgePolicyChangedEvent
switch e := event.(type) {
case *org.PasswordAgePolicyChangedEvent:
policyEvent = e.PasswordAgePolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PasswordAgePolicyChangedEvent:
policyEvent = e.PasswordAgePolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-i7FZt", "reduce.wrong.event.type %v", []eventstore.EventType{org.PasswordAgePolicyChangedEventType, instance.PasswordAgePolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-Mlk6y", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordExpirySettingsRepository()
settings := domain.PasswordExpirySettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
PasswordExpirySettingsAttributes: domain.PasswordExpirySettingsAttributes{
ExpireWarnDays: policyEvent.ExpireWarnDays,
MaxAgeDays: policyEvent.MaxAgeDays,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePasswordAgePolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.PasswordAgePolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-EtHWB", "reduce.wrong.event.type %s", org.PasswordAgePolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.PasswordExpirySettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypePasswordExpiry, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceOrgLockoutPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.LockoutPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.LockoutPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LockoutSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypeLockout, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceLockoutPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LockoutPolicyAddedEvent
switch e := event.(type) {
case *org.LockoutPolicyAddedEvent:
policyEvent = e.LockoutPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.LockoutPolicyAddedEvent:
policyEvent = e.LockoutPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-d8mZO", "reduce.wrong.event.type, %v", []eventstore.EventType{org.LockoutPolicyAddedEventType, instance.LockoutPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-5hnNE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LockoutSettingsRepository()
settings := domain.LockoutSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
LockoutSettingsAttributes: domain.LockoutSettingsAttributes{
MaxPasswordAttempts: &policyEvent.MaxPasswordAttempts,
MaxOTPAttempts: &policyEvent.MaxOTPAttempts,
ShowLockOutFailures: &policyEvent.ShowLockOutFailures,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceLockoutPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.LockoutPolicyChangedEvent
switch e := event.(type) {
case *org.LockoutPolicyChangedEvent:
policyEvent = e.LockoutPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.LockoutPolicyChangedEvent:
policyEvent = e.LockoutPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-gT3BQ", "reduce.wrong.event.type, %v", []eventstore.EventType{org.LockoutPolicyChangedEventType, instance.LockoutPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rbsxy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LockoutSettingsRepository()
settings := domain.LockoutSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
LockoutSettingsAttributes: domain.LockoutSettingsAttributes{
MaxPasswordAttempts: policyEvent.MaxPasswordAttempts,
MaxOTPAttempts: policyEvent.MaxOTPAttempts,
ShowLockOutFailures: policyEvent.ShowLockOutFailures,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceDomainPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.DomainPolicyAddedEvent
switch e := event.(type) {
case *org.DomainPolicyAddedEvent:
policyEvent = e.DomainPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.DomainPolicyAddedEvent:
policyEvent = e.DomainPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-8se7M", "reduce.wrong.event.type %v", []eventstore.EventType{org.DomainPolicyAddedEventType, instance.DomainPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-chduE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.DomainSettingsRepository()
settings := domain.DomainSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
DomainSettingsAttributes: domain.DomainSettingsAttributes{
LoginNameIncludesDomain: &policyEvent.UserLoginMustBeDomain,
RequireOrgDomainVerification: &policyEvent.ValidateOrgDomains,
SMTPSenderAddressMatchesInstanceDomain: &policyEvent.SMTPSenderAddressMatchesInstanceDomain,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceDomainPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.DomainPolicyChangedEvent
switch e := event.(type) {
case *org.DomainPolicyChangedEvent:
policyEvent = e.DomainPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.DomainPolicyChangedEvent:
policyEvent = e.DomainPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-qgVug", "reduce.wrong.event.type %v", []eventstore.EventType{org.DomainPolicyChangedEventType, instance.DomainPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rbsxy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.DomainSettingsRepository()
settings := domain.DomainSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
DomainSettingsAttributes: domain.DomainSettingsAttributes{
LoginNameIncludesDomain: policyEvent.UserLoginMustBeDomain,
RequireOrgDomainVerification: policyEvent.ValidateOrgDomains,
SMTPSenderAddressMatchesInstanceDomain: policyEvent.SMTPSenderAddressMatchesInstanceDomain,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceOrgDomainPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.DomainPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.LockoutPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.DomainSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypeDomain, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceNotificationPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.NotificationPolicyAddedEvent
switch e := event.(type) {
case *org.NotificationPolicyAddedEvent:
policyEvent = e.NotificationPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.NotificationPolicyAddedEvent:
policyEvent = e.NotificationPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-8se7M", "reduce.wrong.event.type %v", []eventstore.EventType{org.NotificationPolicyAddedEventType, instance.NotificationPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-chduE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.NotificationSettingsRepository()
settings := domain.NotificationSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
NotificationSettingsAttributes: domain.NotificationSettingsAttributes{
PasswordChange: &policyEvent.PasswordChange,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceNotificationPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.NotificationPolicyChangedEvent
switch e := event.(type) {
case *org.NotificationPolicyChangedEvent:
policyEvent = e.NotificationPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.NotificationPolicyChangedEvent:
policyEvent = e.NotificationPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-qgVug", "reduce.wrong.event.type %v", []eventstore.EventType{org.NotificationPolicyChangedEventType, instance.NotificationPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rbsxy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.NotificationSettingsRepository()
settings := domain.NotificationSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
NotificationSettingsAttributes: domain.NotificationSettingsAttributes{
PasswordChange: policyEvent.PasswordChange,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceOrgNotificationPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.NotificationPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.NotificationPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.NotificationSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypeNotification, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reducePrivacyPolicyAdded(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PrivacyPolicyAddedEvent
switch e := event.(type) {
case *org.PrivacyPolicyAddedEvent:
policyEvent = e.PrivacyPolicyAddedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PrivacyPolicyAddedEvent:
policyEvent = e.PrivacyPolicyAddedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-8se7M", "reduce.wrong.event.type %v", []eventstore.EventType{org.PrivacyPolicyAddedEventType, instance.PrivacyPolicyAddedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-chduE", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LegalAndSupportSettingsRepository()
email := string(policyEvent.SupportEmail)
settings := domain.LegalAndSupportSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
LegalAndSupportSettingsAttributes: domain.LegalAndSupportSettingsAttributes{
TOSLink: &policyEvent.TOSLink,
PrivacyPolicyLink: &policyEvent.PrivacyLink,
HelpLink: &policyEvent.HelpLink,
SupportEmail: &email,
DocsLink: &policyEvent.DocsLink,
CustomLink: &policyEvent.CustomLink,
CustomLinkText: &policyEvent.CustomLinkText,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reducePrivacyPolicyChanged(event eventstore.Event) (*handler.Statement, error) {
var orgId *string
var policyEvent policy.PrivacyPolicyChangedEvent
switch e := event.(type) {
case *org.PrivacyPolicyChangedEvent:
policyEvent = e.PrivacyPolicyChangedEvent
orgId = &policyEvent.Aggregate().ResourceOwner
case *instance.PrivacyPolicyChangedEvent:
policyEvent = e.PrivacyPolicyChangedEvent
default:
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-qgVug", "reduce.wrong.event.type %v", []eventstore.EventType{org.PrivacyPolicyChangedEventType, instance.PrivacyPolicyChangedEventType})
}
return handler.NewStatement(&policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rbsxy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LegalAndSupportSettingsRepository()
var email string
if policyEvent.SupportEmail != nil {
email = string(*policyEvent.SupportEmail)
}
settings := domain.LegalAndSupportSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: orgId,
UpdatedAt: policyEvent.Creation,
},
LegalAndSupportSettingsAttributes: domain.LegalAndSupportSettingsAttributes{
TOSLink: policyEvent.TOSLink,
PrivacyPolicyLink: policyEvent.PrivacyLink,
HelpLink: policyEvent.HelpLink,
SupportEmail: &email,
DocsLink: policyEvent.DocsLink,
CustomLink: policyEvent.CustomLink,
CustomLinkText: policyEvent.CustomLinkText,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceOrgPrivacyPolicyRemoved(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*org.PrivacyPolicyRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-Bqut9", "reduce.wrong.event.type %s", org.PrivacyPolicyRemovedEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-UrdHy", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.LegalAndSupportSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(policyEvent.Aggregate().InstanceID, &policyEvent.Aggregate().ID, domain.SettingTypeLegalAndSupport, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceSecurityPolicySet(event eventstore.Event) (*handler.Statement, error) {
policyEvent, ok := event.(*instance.SecurityPolicySetEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-83U8p", "reduce.wrong.event.type %s", instance.SecurityPolicySetEventType)
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-lhPul", "reduce.wrong.db.pool %T", ex)
}
var allowedOrigins []string
if policyEvent.AllowedOrigins != nil {
allowedOrigins = *policyEvent.AllowedOrigins
}
settingsRepo := repository.SecuritySettingsRepository()
settings := domain.SecuritySettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: nil,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
SecuritySettingsAttributes: domain.SecuritySettingsAttributes{
EnableIframeEmbedding: policyEvent.EnableIframeEmbedding,
AllowedOrigins: allowedOrigins,
EnableImpersonation: policyEvent.EnableImpersonation,
EnableDynamicClientRegistration: policyEvent.EnableDynamicClientRegistration,
AllowUnauthenticatedDynamicClientRegistration: policyEvent.AllowUnauthenticatedDynamicClientRegistration,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceOrganizationSettingsSet(event eventstore.Event) (*handler.Statement, error) {
policyEvent, err := assertEvent[*settings.OrganizationSettingsSetEvent](event)
if err != nil {
return nil, err
}
return handler.NewStatement(policyEvent, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-lhPul", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.OrganizationSettingsRepository()
settings := domain.OrganizationSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: &event.Aggregate().ID,
CreatedAt: policyEvent.Creation,
UpdatedAt: policyEvent.Creation,
},
OrganizationSettingsAttributes: domain.OrganizationSettingsAttributes{
OrganizationScopedUsernames: &policyEvent.OrganizationScopedUsernames,
},
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceOrganizationSettingsRemoved(event eventstore.Event) (*handler.Statement, error) {
e, err := assertEvent[*settings.OrganizationSettingsRemovedEvent](event)
if err != nil {
return nil, err
}
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-rHiHb", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.OrganizationSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(e.Aggregate().InstanceID, &e.Aggregate().ID, domain.SettingTypeOrganization, domain.SettingStateActive),
)
return err
}), nil
}
func (p *relationalTablesProjection) reduceSecretGeneratorAdded(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorAddedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-yTuWKA", "reduce.wrong.event.type %s", instance.SecretGeneratorAddedEventType)
}
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-jNhP6P", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.SecretGeneratorSettingsRepository()
secretGeneratorAttrs, err := setSecretGeneratorSettingsAttrs(
e.GeneratorType,
&e.Length,
&e.IncludeLowerLetters,
&e.IncludeUpperLetters,
&e.IncludeDigits,
&e.IncludeSymbols,
&e.Expiry,
)
if err != nil {
return err
}
settings := domain.SecretGeneratorSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: nil,
CreatedAt: e.Creation,
UpdatedAt: e.Creation,
},
SecretGeneratorSettingsAttributes: secretGeneratorAttrs,
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceSecretGeneratorChanged(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorChangedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-HhaZbQ", "reduce.wrong.event.type %s", instance.SecretGeneratorChangedEventType)
}
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-3SidEv", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.SecretGeneratorSettingsRepository()
secretGeneratorAttrs, err := setSecretGeneratorSettingsAttrs(
e.GeneratorType,
e.Length,
e.IncludeLowerLetters,
e.IncludeUpperLetters,
e.IncludeDigits,
e.IncludeSymbols,
e.Expiry,
)
if err != nil {
return err
}
settings := domain.SecretGeneratorSettings{
Settings: domain.Settings{
InstanceID: event.Aggregate().InstanceID,
OrganizationID: nil,
UpdatedAt: e.Creation,
},
SecretGeneratorSettingsAttributes: secretGeneratorAttrs,
}
return settingsRepo.Set(ctx, v3_sql.SQLTx(tx), &settings)
}), nil
}
func (p *relationalTablesProjection) reduceSecretGeneratorRemoved(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*instance.SecretGeneratorRemovedEvent)
if !ok {
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-fmiIf", "reduce.wrong.event.type %s", instance.SecretGeneratorRemovedEventType)
}
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, _ string) error {
tx, ok := ex.(*sql.Tx)
if !ok {
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-xOUC9O", "reduce.wrong.db.pool %T", ex)
}
settingsRepo := repository.SecretGeneratorSettingsRepository()
_, err := settingsRepo.Delete(ctx, v3_sql.SQLTx(tx),
settingsRepo.UniqueCondition(e.Aggregate().InstanceID, nil, domain.SettingTypeSecretGenerator, domain.SettingStateActive),
)
return err
}), nil
}
func setSecretGeneratorSettingsAttrs(generatorType legacy_domain.SecretGeneratorType, length *uint, includeLowerLetters, includeUpperLetters, includeDigits, includeSymbols *bool, expiry *time.Duration) (domain.SecretGeneratorSettingsAttributes, error) {
var secretGeneratorSettingsAttrs domain.SecretGeneratorSettingsAttributes
attrs := domain.SecretGeneratorAttrs{
Length: length,
IncludeLowerLetters: includeLowerLetters,
IncludeUpperLetters: includeUpperLetters,
IncludeDigits: includeDigits,
IncludeSymbols: includeSymbols,
}
attrsWithExpiry := domain.SecretGeneratorAttrsWithExpiry{
SecretGeneratorAttrs: attrs,
Expiry: expiry,
}
switch generatorType {
case legacy_domain.SecretGeneratorTypeAppSecret:
secretGeneratorSettingsAttrs.ClientSecret = &domain.ClientSecretAttributes{
SecretGeneratorAttrs: attrs,
}
case legacy_domain.SecretGeneratorTypeInitCode:
secretGeneratorSettingsAttrs.InitializeUserCode = &domain.InitializeUserCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeVerifyEmailCode:
secretGeneratorSettingsAttrs.EmailVerificationCode = &domain.EmailVerificationCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeVerifyPhoneCode:
secretGeneratorSettingsAttrs.PhoneVerificationCode = &domain.PhoneVerificationCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypePasswordlessInitCode:
secretGeneratorSettingsAttrs.PasswordlessInitCode = &domain.PasswordlessInitCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypePasswordResetCode:
secretGeneratorSettingsAttrs.PasswordVerificationCode = &domain.PasswordVerificationCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeVerifyDomain:
secretGeneratorSettingsAttrs.DomainVerification = &domain.DomainVerificationAttributes{
SecretGeneratorAttrs: attrs,
}
case legacy_domain.SecretGeneratorTypeOTPSMS:
secretGeneratorSettingsAttrs.OTPSMS = &domain.OTPSMSAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeOTPEmail:
secretGeneratorSettingsAttrs.OTPEmail = &domain.OTPEmailAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeInviteCode:
secretGeneratorSettingsAttrs.InviteCode = &domain.InviteCodeAttributes{
SecretGeneratorAttrsWithExpiry: attrsWithExpiry,
}
case legacy_domain.SecretGeneratorTypeSigningKey:
// do nothing as this secret generator is not persisted in the settings
case legacy_domain.SecretGeneratorTypeUnspecified:
return domain.SecretGeneratorSettingsAttributes{}, zerrors.ThrowInvalidArgumentf(nil, "HANDL-2n3fK", "unspecified secret generator type")
default:
return domain.SecretGeneratorSettingsAttributes{}, zerrors.ThrowInvalidArgumentf(nil, "HANDL-9mG7f", "unknown secret generator type %s", generatorType)
}
return secretGeneratorSettingsAttrs, nil
}