zitadel/internal/query/password_complexity_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

175 lines
5.1 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 PasswordComplexityPolicy struct {
ID string
Sequence uint64
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
State domain.PolicyState
MinLength uint64
HasLowercase bool
HasUppercase bool
HasNumber bool
HasSymbol bool
IsDefault bool
}
func (q *Queries) PasswordComplexityPolicyByOrg(ctx context.Context, orgID string) (*PasswordComplexityPolicy, error) {
stmt, scan := preparePasswordComplexityPolicyQuery()
query, args, err := stmt.Where(
sq.And{
sq.Eq{
PasswordComplexityColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
},
sq.Or{
sq.Eq{
PasswordComplexityColID.identifier(): orgID,
},
sq.Eq{
PasswordComplexityColID.identifier(): domain.IAMID,
},
},
}).
OrderBy(PasswordComplexityColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-lDnrk", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) DefaultPasswordComplexityPolicy(ctx context.Context) (*PasswordComplexityPolicy, error) {
stmt, scan := preparePasswordComplexityPolicyQuery()
query, args, err := stmt.Where(sq.Eq{
PasswordComplexityColID.identifier(): domain.IAMID,
PasswordComplexityColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}).
OrderBy(PasswordComplexityColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-h4Uyr", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
var (
passwordComplexityTable = table{
name: projection.PasswordComplexityTable,
}
PasswordComplexityColID = Column{
name: projection.ComplexityPolicyIDCol,
table: passwordComplexityTable,
}
PasswordComplexityColSequence = Column{
name: projection.ComplexityPolicySequenceCol,
table: passwordComplexityTable,
}
PasswordComplexityColCreationDate = Column{
name: projection.ComplexityPolicyCreationDateCol,
table: passwordComplexityTable,
}
PasswordComplexityColChangeDate = Column{
name: projection.ComplexityPolicyChangeDateCol,
table: passwordComplexityTable,
}
PasswordComplexityColResourceOwner = Column{
name: projection.ComplexityPolicyResourceOwnerCol,
table: passwordComplexityTable,
}
PasswordComplexityColInstanceID = Column{
name: projection.ComplexityPolicyInstanceIDCol,
table: passwordComplexityTable,
}
PasswordComplexityColMinLength = Column{
name: projection.ComplexityPolicyMinLengthCol,
table: passwordComplexityTable,
}
PasswordComplexityColHasLowercase = Column{
name: projection.ComplexityPolicyHasLowercaseCol,
table: passwordComplexityTable,
}
PasswordComplexityColHasUpperCase = Column{
name: projection.ComplexityPolicyHasUppercaseCol,
table: passwordComplexityTable,
}
PasswordComplexityColHasNumber = Column{
name: projection.ComplexityPolicyHasNumberCol,
table: passwordComplexityTable,
}
PasswordComplexityColHasSymbol = Column{
name: projection.ComplexityPolicyHasSymbolCol,
table: passwordComplexityTable,
}
PasswordComplexityColIsDefault = Column{
name: projection.ComplexityPolicyIsDefaultCol,
table: passwordComplexityTable,
}
PasswordComplexityColState = Column{
name: projection.ComplexityPolicyStateCol,
table: passwordComplexityTable,
}
)
func preparePasswordComplexityPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*PasswordComplexityPolicy, error)) {
return sq.Select(
PasswordComplexityColID.identifier(),
PasswordComplexityColSequence.identifier(),
PasswordComplexityColCreationDate.identifier(),
PasswordComplexityColChangeDate.identifier(),
PasswordComplexityColResourceOwner.identifier(),
PasswordComplexityColMinLength.identifier(),
PasswordComplexityColHasLowercase.identifier(),
PasswordComplexityColHasUpperCase.identifier(),
PasswordComplexityColHasNumber.identifier(),
PasswordComplexityColHasSymbol.identifier(),
PasswordComplexityColIsDefault.identifier(),
PasswordComplexityColState.identifier(),
).
From(passwordComplexityTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*PasswordComplexityPolicy, error) {
policy := new(PasswordComplexityPolicy)
err := row.Scan(
&policy.ID,
&policy.Sequence,
&policy.CreationDate,
&policy.ChangeDate,
&policy.ResourceOwner,
&policy.MinLength,
&policy.HasLowercase,
&policy.HasUppercase,
&policy.HasNumber,
&policy.HasSymbol,
&policy.IsDefault,
&policy.State,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-63mtI", "Errors.PasswordComplexity.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-uulCZ", "Errors.Internal")
}
return policy, nil
}
}