zitadel/internal/query/label_policy.go
Livio Amstutz 958362e6c9
feat: handle instance from context (#3382)
* commander

* commander

* selber!

* move to packages

* fix(errors): implement Is interface

* test: command

* test: commands

* add init steps

* setup tenant

* add default step yaml

* possibility to set password

* merge v2 into v2-commander

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: rename iam command side to instance

* fix: search query builder can filter events in memory

* fix: filters for add member

* fix(setup): add `ExternalSecure` to config

* chore: name iam to instance

* fix: matching

* remove unsued func

* base url

* base url

* test(command): filter funcs

* test: commands

* fix: rename orgiampolicy to domain policy

* start from init

* commands

* config

* fix indexes and add constraints

* fixes

* fix: merge conflicts

* fix: protos

* fix: md files

* setup

* add deprecated org iam policy again

* typo

* fix search query

* fix filter

* Apply suggestions from code review

* remove custom org from org setup

* add todos for verification

* change apps creation

* simplify package structure

* fix error

* move preparation helper for tests

* fix unique constraints

* fix config mapping in setup

* fix error handling in encryption_keys.go

* fix projection config

* fix query from old views to projection

* fix setup of mgmt api

* set iam project and fix instance projection

* fix tokens view

* fix steps.yaml and defaults.yaml

* fix projections

* change instance context to interface

* instance interceptors and additional events in setup

* cleanup

* tests for interceptors

* fix label policy

* add todo

* single api endpoint in environment.json

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: fabi <fabienne.gerschwiler@gmail.com>
2022-03-29 11:53:19 +02:00

310 lines
8.9 KiB
Go

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/caos/zitadel/internal/api/authz"
"github.com/caos/zitadel/internal/domain"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/query/projection"
)
type LabelPolicy struct {
ID string
CreationDate time.Time
ChangeDate time.Time
Sequence uint64
State domain.LabelPolicyState
IsDefault bool
ResourceOwner string
HideLoginNameSuffix bool
FontURL string
WatermarkDisabled bool
ShouldErrorPopup bool
Dark Theme
Light Theme
}
type Theme struct {
PrimaryColor string
WarnColor string
BackgroundColor string
FontColor string
LogoURL string
IconURL string
}
func (q *Queries) ActiveLabelPolicyByOrg(ctx context.Context, orgID string) (*LabelPolicy, error) {
stmt, scan := prepareLabelPolicyQuery()
query, args, err := stmt.Where(
sq.And{
sq.Or{
sq.Eq{
LabelPolicyColID.identifier(): orgID,
},
sq.Eq{
LabelPolicyColID.identifier(): domain.IAMID,
},
},
sq.Eq{
LabelPolicyColState.identifier(): domain.LabelPolicyStateActive,
LabelPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
},
}).
OrderBy(LabelPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-V22un", "unable to create sql stmt")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) PreviewLabelPolicyByOrg(ctx context.Context, orgID string) (*LabelPolicy, error) {
stmt, scan := prepareLabelPolicyQuery()
query, args, err := stmt.Where(
sq.And{
sq.Or{
sq.Eq{
LabelPolicyColID.identifier(): orgID,
},
sq.Eq{
LabelPolicyColID.identifier(): domain.IAMID,
},
},
sq.Eq{
LabelPolicyColState.identifier(): domain.LabelPolicyStatePreview,
LabelPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
},
}).
OrderBy(LabelPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-AG5eq", "unable to create sql stmt")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) DefaultActiveLabelPolicy(ctx context.Context) (*LabelPolicy, error) {
stmt, scan := prepareLabelPolicyQuery()
query, args, err := stmt.Where(sq.Eq{
LabelPolicyColID.identifier(): domain.IAMID,
LabelPolicyColState.identifier(): domain.LabelPolicyStateActive,
LabelPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}).
OrderBy(LabelPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-mN0Ci", "unable to create sql stmt")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) DefaultPreviewLabelPolicy(ctx context.Context) (*LabelPolicy, error) {
stmt, scan := prepareLabelPolicyQuery()
query, args, err := stmt.Where(sq.Eq{
LabelPolicyColID.identifier(): domain.IAMID,
LabelPolicyColState.identifier(): domain.LabelPolicyStatePreview,
LabelPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}).
OrderBy(LabelPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-B3JQR", "unable to create sql stmt")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
var (
labelPolicyTable = table{
name: projection.LabelPolicyTable,
}
LabelPolicyColCreationDate = Column{
name: projection.LabelPolicyCreationDateCol,
}
LabelPolicyColChangeDate = Column{
name: projection.LabelPolicyChangeDateCol,
}
LabelPolicyColSequence = Column{
name: projection.LabelPolicySequenceCol,
}
LabelPolicyColID = Column{
name: projection.LabelPolicyIDCol,
}
LabelPolicyColState = Column{
name: projection.LabelPolicyStateCol,
}
LabelPolicyColIsDefault = Column{
name: projection.LabelPolicyIsDefaultCol,
}
LabelPolicyColResourceOwner = Column{
name: projection.LabelPolicyResourceOwnerCol,
}
LabelPolicyColInstanceID = Column{
name: projection.LabelPolicyInstanceIDCol,
}
LabelPolicyColHideLoginNameSuffix = Column{
name: projection.LabelPolicyHideLoginNameSuffixCol,
}
LabelPolicyColFontURL = Column{
name: projection.LabelPolicyFontURLCol,
}
LabelPolicyColWatermarkDisabled = Column{
name: projection.LabelPolicyWatermarkDisabledCol,
}
LabelPolicyColShouldErrorPopup = Column{
name: projection.LabelPolicyShouldErrorPopupCol,
}
LabelPolicyColLightPrimaryColor = Column{
name: projection.LabelPolicyLightPrimaryColorCol,
}
LabelPolicyColLightWarnColor = Column{
name: projection.LabelPolicyLightWarnColorCol,
}
LabelPolicyColLightBackgroundColor = Column{
name: projection.LabelPolicyLightBackgroundColorCol,
}
LabelPolicyColLightFontColor = Column{
name: projection.LabelPolicyLightFontColorCol,
}
LabelPolicyColLightLogoURL = Column{
name: projection.LabelPolicyLightLogoURLCol,
}
LabelPolicyColLightIconURL = Column{
name: projection.LabelPolicyLightIconURLCol,
}
LabelPolicyColDarkPrimaryColor = Column{
name: projection.LabelPolicyDarkPrimaryColorCol,
}
LabelPolicyColDarkWarnColor = Column{
name: projection.LabelPolicyDarkWarnColorCol,
}
LabelPolicyColDarkBackgroundColor = Column{
name: projection.LabelPolicyDarkBackgroundColorCol,
}
LabelPolicyColDarkFontColor = Column{
name: projection.LabelPolicyDarkFontColorCol,
}
LabelPolicyColDarkLogoURL = Column{
name: projection.LabelPolicyDarkLogoURLCol,
}
LabelPolicyColDarkIconURL = Column{
name: projection.LabelPolicyDarkIconURLCol,
}
)
func prepareLabelPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*LabelPolicy, error)) {
return sq.Select(
LabelPolicyColCreationDate.identifier(),
LabelPolicyColChangeDate.identifier(),
LabelPolicyColSequence.identifier(),
LabelPolicyColID.identifier(),
LabelPolicyColState.identifier(),
LabelPolicyColIsDefault.identifier(),
LabelPolicyColResourceOwner.identifier(),
LabelPolicyColHideLoginNameSuffix.identifier(),
LabelPolicyColFontURL.identifier(),
LabelPolicyColWatermarkDisabled.identifier(),
LabelPolicyColShouldErrorPopup.identifier(),
LabelPolicyColLightPrimaryColor.identifier(),
LabelPolicyColLightWarnColor.identifier(),
LabelPolicyColLightBackgroundColor.identifier(),
LabelPolicyColLightFontColor.identifier(),
LabelPolicyColLightLogoURL.identifier(),
LabelPolicyColLightIconURL.identifier(),
LabelPolicyColDarkPrimaryColor.identifier(),
LabelPolicyColDarkWarnColor.identifier(),
LabelPolicyColDarkBackgroundColor.identifier(),
LabelPolicyColDarkFontColor.identifier(),
LabelPolicyColDarkLogoURL.identifier(),
LabelPolicyColDarkIconURL.identifier(),
).
From(labelPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*LabelPolicy, error) {
policy := new(LabelPolicy)
var (
fontURL = sql.NullString{}
lightPrimaryColor = sql.NullString{}
lightWarnColor = sql.NullString{}
lightBackgroundColor = sql.NullString{}
lightFontColor = sql.NullString{}
lightLogoURL = sql.NullString{}
lightIconURL = sql.NullString{}
darkPrimaryColor = sql.NullString{}
darkWarnColor = sql.NullString{}
darkBackgroundColor = sql.NullString{}
darkFontColor = sql.NullString{}
darkLogoURL = sql.NullString{}
darkIconURL = sql.NullString{}
)
err := row.Scan(
&policy.CreationDate,
&policy.ChangeDate,
&policy.Sequence,
&policy.ID,
&policy.State,
&policy.IsDefault,
&policy.ResourceOwner,
&policy.HideLoginNameSuffix,
&fontURL,
&policy.WatermarkDisabled,
&policy.ShouldErrorPopup,
&lightPrimaryColor,
&lightWarnColor,
&lightBackgroundColor,
&lightFontColor,
&lightLogoURL,
&lightIconURL,
&darkPrimaryColor,
&darkWarnColor,
&darkBackgroundColor,
&darkFontColor,
&darkLogoURL,
&darkIconURL,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-bJEsm", "Errors.Org.PolicyNotExisting")
}
return nil, errors.ThrowInternal(err, "QUERY-awLM6", "Errors.Internal")
}
policy.FontURL = fontURL.String
policy.Light.PrimaryColor = lightPrimaryColor.String
policy.Light.WarnColor = lightWarnColor.String
policy.Light.BackgroundColor = lightBackgroundColor.String
policy.Light.FontColor = lightFontColor.String
policy.Light.LogoURL = lightLogoURL.String
policy.Light.IconURL = lightIconURL.String
policy.Dark.PrimaryColor = darkPrimaryColor.String
policy.Dark.WarnColor = darkWarnColor.String
policy.Dark.BackgroundColor = darkBackgroundColor.String
policy.Dark.FontColor = darkFontColor.String
policy.Dark.LogoURL = darkLogoURL.String
policy.Dark.IconURL = darkIconURL.String
return policy, nil
}
}