2021-10-29 14:33:00 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
errs "errors"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
sq "github.com/Masterminds/squirrel"
|
2021-11-21 19:22:25 +00:00
|
|
|
|
2021-10-29 14:33:00 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/query/projection"
|
|
|
|
)
|
|
|
|
|
2021-11-21 19:22:25 +00:00
|
|
|
type Features struct {
|
2021-10-29 14:33:00 +00:00
|
|
|
AggregateID string
|
|
|
|
ChangeDate time.Time
|
|
|
|
Sequence uint64
|
|
|
|
IsDefault bool
|
|
|
|
TierName string
|
|
|
|
TierDescription string
|
|
|
|
State domain.FeaturesState
|
|
|
|
StateDescription string
|
|
|
|
AuditLogRetention time.Duration
|
|
|
|
LoginPolicyFactors bool
|
|
|
|
LoginPolicyIDP bool
|
|
|
|
LoginPolicyPasswordless bool
|
|
|
|
LoginPolicyRegistration bool
|
|
|
|
LoginPolicyUsernameLogin bool
|
|
|
|
LoginPolicyPasswordReset bool
|
|
|
|
PasswordComplexityPolicy bool
|
|
|
|
LabelPolicyPrivateLabel bool
|
|
|
|
LabelPolicyWatermark bool
|
|
|
|
CustomDomain bool
|
|
|
|
PrivacyPolicy bool
|
|
|
|
MetadataUser bool
|
|
|
|
CustomTextMessage bool
|
|
|
|
CustomTextLogin bool
|
|
|
|
LockoutPolicy bool
|
|
|
|
Actions bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2021-11-21 19:22:25 +00:00
|
|
|
featureTable = table{
|
2021-10-29 14:33:00 +00:00
|
|
|
name: projection.FeatureTable,
|
|
|
|
}
|
|
|
|
FeatureColumnAggregateID = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureAggregateIDCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureColumnChangeDate = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureChangeDateCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureColumnSequence = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureSequenceCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureColumnIsDefault = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureIsDefaultCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureTierName = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureTierNameCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureTierDescription = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureTierDescriptionCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureState = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureStateCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureStateDescription = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureStateDescriptionCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureAuditLogRetention = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureAuditLogRetentionCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyFactors = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyFactorsCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyIDP = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyIDPCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyPasswordless = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyPasswordlessCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyRegistration = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyRegistrationCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyUsernameLogin = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyUsernameLoginCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLoginPolicyPasswordReset = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLoginPolicyPasswordResetCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeaturePasswordComplexityPolicy = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeaturePasswordComplexityPolicyCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLabelPolicyPrivateLabel = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLabelPolicyPrivateLabelCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLabelPolicyWatermark = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLabelPolicyWatermarkCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureCustomDomain = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureCustomDomainCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeaturePrivacyPolicy = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeaturePrivacyPolicyCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureMetadataUser = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureMetadataUserCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureCustomTextMessage = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureCustomTextMessageCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureCustomTextLogin = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureCustomTextLoginCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureLockoutPolicy = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureLockoutPolicyCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
FeatureActions = Column{
|
2021-11-21 19:22:25 +00:00
|
|
|
name: projection.FeatureActionsCol,
|
|
|
|
table: featureTable,
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-11-21 19:22:25 +00:00
|
|
|
func (q *Queries) FeaturesByOrgID(ctx context.Context, orgID string) (*Features, error) {
|
|
|
|
query, scan := prepareFeaturesQuery()
|
2021-10-29 14:33:00 +00:00
|
|
|
stmt, args, err := query.Where(
|
|
|
|
sq.Or{
|
|
|
|
sq.Eq{
|
|
|
|
FeatureColumnAggregateID.identifier(): orgID,
|
|
|
|
},
|
|
|
|
sq.Eq{
|
|
|
|
FeatureColumnAggregateID.identifier(): domain.IAMID,
|
|
|
|
},
|
|
|
|
}).
|
|
|
|
OrderBy(FeatureColumnIsDefault.identifier()).
|
|
|
|
Limit(1).ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-P9gwg", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
|
|
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
|
|
|
return scan(row)
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:22:25 +00:00
|
|
|
func (q *Queries) DefaultFeatures(ctx context.Context) (*Features, error) {
|
|
|
|
query, scan := prepareFeaturesQuery()
|
2021-10-29 14:33:00 +00:00
|
|
|
stmt, args, err := query.Where(sq.Eq{
|
|
|
|
FeatureColumnAggregateID.identifier(): domain.IAMID,
|
2021-11-21 19:22:25 +00:00
|
|
|
}).ToSql()
|
2021-10-29 14:33:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-1Ndlg", "Errors.Query.SQLStatement")
|
|
|
|
}
|
|
|
|
|
|
|
|
row := q.client.QueryRowContext(ctx, stmt, args...)
|
|
|
|
return scan(row)
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:22:25 +00:00
|
|
|
func prepareFeaturesQuery() (sq.SelectBuilder, func(*sql.Row) (*Features, error)) {
|
2021-10-29 14:33:00 +00:00
|
|
|
return sq.Select(
|
|
|
|
FeatureColumnAggregateID.identifier(),
|
|
|
|
FeatureColumnChangeDate.identifier(),
|
|
|
|
FeatureColumnSequence.identifier(),
|
|
|
|
FeatureColumnIsDefault.identifier(),
|
|
|
|
FeatureTierName.identifier(),
|
|
|
|
FeatureTierDescription.identifier(),
|
|
|
|
FeatureState.identifier(),
|
|
|
|
FeatureStateDescription.identifier(),
|
|
|
|
FeatureAuditLogRetention.identifier(),
|
|
|
|
FeatureLoginPolicyFactors.identifier(),
|
|
|
|
FeatureLoginPolicyIDP.identifier(),
|
|
|
|
FeatureLoginPolicyPasswordless.identifier(),
|
|
|
|
FeatureLoginPolicyRegistration.identifier(),
|
|
|
|
FeatureLoginPolicyUsernameLogin.identifier(),
|
|
|
|
FeatureLoginPolicyPasswordReset.identifier(),
|
|
|
|
FeaturePasswordComplexityPolicy.identifier(),
|
|
|
|
FeatureLabelPolicyPrivateLabel.identifier(),
|
|
|
|
FeatureLabelPolicyWatermark.identifier(),
|
|
|
|
FeatureCustomDomain.identifier(),
|
|
|
|
FeaturePrivacyPolicy.identifier(),
|
|
|
|
FeatureMetadataUser.identifier(),
|
|
|
|
FeatureCustomTextMessage.identifier(),
|
|
|
|
FeatureCustomTextLogin.identifier(),
|
|
|
|
FeatureLockoutPolicy.identifier(),
|
|
|
|
FeatureActions.identifier(),
|
2021-11-21 19:22:25 +00:00
|
|
|
).From(featureTable.identifier()).PlaceholderFormat(sq.Dollar),
|
|
|
|
func(row *sql.Row) (*Features, error) {
|
|
|
|
p := new(Features)
|
|
|
|
tierName := sql.NullString{}
|
|
|
|
tierDescription := sql.NullString{}
|
|
|
|
stateDescription := sql.NullString{}
|
2021-10-29 14:33:00 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&p.AggregateID,
|
|
|
|
&p.ChangeDate,
|
|
|
|
&p.Sequence,
|
|
|
|
&p.IsDefault,
|
2021-11-21 19:22:25 +00:00
|
|
|
&tierName,
|
|
|
|
&tierDescription,
|
2021-10-29 14:33:00 +00:00
|
|
|
&p.State,
|
2021-11-21 19:22:25 +00:00
|
|
|
&stateDescription,
|
2021-10-29 14:33:00 +00:00
|
|
|
&p.AuditLogRetention,
|
|
|
|
&p.LoginPolicyFactors,
|
|
|
|
&p.LoginPolicyIDP,
|
|
|
|
&p.LoginPolicyPasswordless,
|
|
|
|
&p.LoginPolicyRegistration,
|
|
|
|
&p.LoginPolicyUsernameLogin,
|
|
|
|
&p.LoginPolicyPasswordReset,
|
|
|
|
&p.PasswordComplexityPolicy,
|
|
|
|
&p.LabelPolicyPrivateLabel,
|
|
|
|
&p.LabelPolicyWatermark,
|
|
|
|
&p.CustomDomain,
|
|
|
|
&p.PrivacyPolicy,
|
|
|
|
&p.MetadataUser,
|
|
|
|
&p.CustomTextMessage,
|
|
|
|
&p.CustomTextLogin,
|
|
|
|
&p.LockoutPolicy,
|
|
|
|
&p.Actions,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
if errs.Is(err, sql.ErrNoRows) {
|
2021-11-21 19:22:25 +00:00
|
|
|
return nil, errors.ThrowNotFound(err, "QUERY-M9fse", "Errors.Features.NotFound")
|
2021-10-29 14:33:00 +00:00
|
|
|
}
|
|
|
|
return nil, errors.ThrowInternal(err, "QUERY-3o9gd", "Errors.Internal")
|
|
|
|
}
|
2021-11-21 19:22:25 +00:00
|
|
|
p.TierName = tierName.String
|
|
|
|
p.TierDescription = tierDescription.String
|
|
|
|
p.StateDescription = stateDescription.String
|
2021-10-29 14:33:00 +00:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 19:22:25 +00:00
|
|
|
|
|
|
|
func (f *Features) EnabledFeatureTypes() []string {
|
|
|
|
list := make([]string, 0)
|
|
|
|
if f.LoginPolicyFactors {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyFactors)
|
|
|
|
}
|
|
|
|
if f.LoginPolicyIDP {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyIDP)
|
|
|
|
}
|
|
|
|
if f.LoginPolicyPasswordless {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyPasswordless)
|
|
|
|
}
|
|
|
|
if f.LoginPolicyRegistration {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyRegistration)
|
|
|
|
}
|
|
|
|
if f.LoginPolicyUsernameLogin {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyUsernameLogin)
|
|
|
|
}
|
|
|
|
if f.LoginPolicyPasswordReset {
|
|
|
|
list = append(list, domain.FeatureLoginPolicyPasswordReset)
|
|
|
|
}
|
|
|
|
if f.PasswordComplexityPolicy {
|
|
|
|
list = append(list, domain.FeaturePasswordComplexityPolicy)
|
|
|
|
}
|
|
|
|
if f.LabelPolicyPrivateLabel {
|
|
|
|
list = append(list, domain.FeatureLabelPolicyPrivateLabel)
|
|
|
|
}
|
|
|
|
if f.LabelPolicyWatermark {
|
|
|
|
list = append(list, domain.FeatureLabelPolicyWatermark)
|
|
|
|
}
|
|
|
|
if f.CustomDomain {
|
|
|
|
list = append(list, domain.FeatureCustomDomain)
|
|
|
|
}
|
|
|
|
if f.PrivacyPolicy {
|
|
|
|
list = append(list, domain.FeaturePrivacyPolicy)
|
|
|
|
}
|
|
|
|
if f.MetadataUser {
|
|
|
|
list = append(list, domain.FeatureMetadataUser)
|
|
|
|
}
|
|
|
|
if f.CustomTextMessage {
|
|
|
|
list = append(list, domain.FeatureCustomTextMessage)
|
|
|
|
}
|
|
|
|
if f.CustomTextLogin {
|
|
|
|
list = append(list, domain.FeatureCustomTextLogin)
|
|
|
|
}
|
|
|
|
if f.LockoutPolicy {
|
|
|
|
list = append(list, domain.FeatureLockoutPolicy)
|
|
|
|
}
|
|
|
|
if f.Actions {
|
|
|
|
list = append(list, domain.FeatureActions)
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|