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

148 lines
3.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 DomainPolicy struct {
ID string
Sequence uint64
CreationDate time.Time
ChangeDate time.Time
ResourceOwner string
State domain.PolicyState
UserLoginMustBeDomain bool
IsDefault bool
}
var (
domainPolicyTable = table{
name: projection.DomainPolicyTable,
}
DomainPolicyColID = Column{
name: projection.DomainPolicyIDCol,
table: domainPolicyTable,
}
DomainPolicyColSequence = Column{
name: projection.DomainPolicySequenceCol,
table: domainPolicyTable,
}
DomainPolicyColCreationDate = Column{
name: projection.DomainPolicyCreationDateCol,
table: domainPolicyTable,
}
DomainPolicyColChangeDate = Column{
name: projection.DomainPolicyChangeDateCol,
table: domainPolicyTable,
}
DomainPolicyColResourceOwner = Column{
name: projection.DomainPolicyResourceOwnerCol,
table: domainPolicyTable,
}
DomainPolicyColInstanceID = Column{
name: projection.DomainPolicyInstanceIDCol,
table: domainPolicyTable,
}
DomainPolicyColUserLoginMustBeDomain = Column{
name: projection.DomainPolicyUserLoginMustBeDomainCol,
table: domainPolicyTable,
}
DomainPolicyColIsDefault = Column{
name: projection.DomainPolicyIsDefaultCol,
table: domainPolicyTable,
}
DomainPolicyColState = Column{
name: projection.DomainPolicyStateCol,
table: domainPolicyTable,
}
)
func (q *Queries) DomainPolicyByOrg(ctx context.Context, orgID string) (*DomainPolicy, error) {
stmt, scan := prepareDomainPolicyQuery()
query, args, err := stmt.Where(
sq.And{
sq.Eq{
DomainPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
},
sq.Or{
sq.Eq{
DomainPolicyColID.identifier(): orgID,
},
sq.Eq{
DomainPolicyColID.identifier(): domain.IAMID,
},
},
}).
OrderBy(DomainPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-D3CqT", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func (q *Queries) DefaultDomainPolicy(ctx context.Context) (*DomainPolicy, error) {
stmt, scan := prepareDomainPolicyQuery()
query, args, err := stmt.Where(sq.Eq{
DomainPolicyColID.identifier(): domain.IAMID,
DomainPolicyColInstanceID.identifier(): authz.GetInstance(ctx).InstanceID(),
}).
OrderBy(DomainPolicyColIsDefault.identifier()).
Limit(1).ToSql()
if err != nil {
return nil, errors.ThrowInternal(err, "QUERY-pM7lP", "Errors.Query.SQLStatement")
}
row := q.client.QueryRowContext(ctx, query, args...)
return scan(row)
}
func prepareDomainPolicyQuery() (sq.SelectBuilder, func(*sql.Row) (*DomainPolicy, error)) {
return sq.Select(
DomainPolicyColID.identifier(),
DomainPolicyColSequence.identifier(),
DomainPolicyColCreationDate.identifier(),
DomainPolicyColChangeDate.identifier(),
DomainPolicyColResourceOwner.identifier(),
DomainPolicyColUserLoginMustBeDomain.identifier(),
DomainPolicyColIsDefault.identifier(),
DomainPolicyColState.identifier(),
).
From(domainPolicyTable.identifier()).PlaceholderFormat(sq.Dollar),
func(row *sql.Row) (*DomainPolicy, error) {
policy := new(DomainPolicy)
err := row.Scan(
&policy.ID,
&policy.Sequence,
&policy.CreationDate,
&policy.ChangeDate,
&policy.ResourceOwner,
&policy.UserLoginMustBeDomain,
&policy.IsDefault,
&policy.State,
)
if err != nil {
if errs.Is(err, sql.ErrNoRows) {
return nil, errors.ThrowNotFound(err, "QUERY-K0Jr5", "Errors.DomainPolicy.NotFound")
}
return nil, errors.ThrowInternal(err, "QUERY-rIy6j", "Errors.Internal")
}
return policy, nil
}
}