zitadel/internal/query/label_policy.go
Silvan b8bec25129
fix: use query side for requests (#2818)
* refactor(domain): add user type

* fix(projections): start with login names

* fix(login_policy): correct handling of user domain claimed event

* fix(projections): add members

* refactor: simplify member projections

* add migration for members

* add metadata to member projections

* refactor: login name projection

* fix: set correct suffixes on login name projections

* test(projections): login name reduces

* fix: correct cols in reduce member

* test(projections): org, iam, project members

* member additional cols and conds as opt,
add project grant members

* fix(migration): members

* fix(migration): correct database name

* migration version

* migs

* better naming for member cond and col

* split project and project grant members

* prepare member columns

* feat(queries): membership query

* test(queries): membership prepare

* fix(queries): multiple projections for latest sequence

* fix(api): use query for membership queries in auth and management

* feat: org member queries

* fix(api): use query for iam member calls

* fix(queries): org members

* fix(queries): project members

* fix(queries): project grant members

* refactor: remove unsued methods in repo-interfaces

* start

* fix(query): membership

* fix(auth): list my project orgs

* fix(query): member queries and user avatar column

* refactor(auth): MyProjectOrgs

* fix(queries): member and membership stmts

* fix user test

* fix(management): use query for project (-grant) members

* fix(admin): use query for member calls

* fix(api): add domain to org mapping

* remove old idp

* membership

* refactor: remove old files

* idp

* refactor: use query for idps and idp user links

* refactor(eventstore): rename EventPusher to Command, EventReader to Event, PushEvents to Push and FilterEvents to Filter

* gloabl org check for org roles

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-01-13 08:58:14 +01:00

301 lines
8.5 KiB
Go

package query
import (
"context"
"database/sql"
errs "errors"
"time"
sq "github.com/Masterminds/squirrel"
"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(): q.iamID,
},
},
sq.Eq{
LabelPolicyColState.identifier(): domain.LabelPolicyStateActive,
},
}).
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(): q.iamID,
},
},
sq.Eq{
LabelPolicyColState.identifier(): domain.LabelPolicyStatePreview,
},
}).
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(): q.iamID,
LabelPolicyColState.identifier(): domain.LabelPolicyStateActive,
}).
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(): q.iamID,
LabelPolicyColState.identifier(): domain.LabelPolicyStatePreview,
}).
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,
}
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
}
}