mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-01 11:52:15 +00:00
* fix: split command query side * fix: split command query side * fix: members in correct pkg structure * fix: label policy in correct pkg structure * fix: structure * fix: structure of login policy * fix: identityprovider structure * fix: org iam policy structure * fix: password age policy structure * fix: password complexity policy structure * fix: password lockout policy structure * fix: idp structure * fix: user events structure * fix: user write model * fix: profile email changed command * fix: address changed command * fix: user states * fix: user * fix: org structure and add human * begin iam setup command side * setup * step2 * step2 * fix: add user * step2 * isvalid * fix: folder structure v2 business Co-authored-by: Fabiennne <[email protected]>
184 lines
7.6 KiB
Go
184 lines
7.6 KiB
Go
package command
|
|
|
|
import (
|
|
"context"
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
)
|
|
|
|
func (r *CommandSide) AddDefaultLoginPolicy(ctx context.Context, policy *iam_model.LoginPolicy) (*iam_model.LoginPolicy, error) {
|
|
addedPolicy := NewIAMLoginPolicyWriteModel(policy.AggregateID)
|
|
iamAgg, err := r.addDefaultLoginPolicy(ctx, addedPolicy, policy)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = r.eventstore.PushAggregate(ctx, addedPolicy, iamAgg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return writeModelToLoginPolicy(addedPolicy), nil
|
|
}
|
|
|
|
func (r *CommandSide) addDefaultLoginPolicy(ctx context.Context, addedPolicy *IAMLoginPolicyWriteModel, policy *iam_model.LoginPolicy) (*iam_repo.Aggregate, error) {
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedPolicy)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if addedPolicy.IsActive {
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.AlreadyExists")
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedPolicy.LoginPolicyWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyAddedEvent(ctx, policy.AllowUsernamePassword, policy.AllowRegister, policy.AllowExternalIdp, policy.ForceMFA, domain.PasswordlessType(policy.PasswordlessType)))
|
|
|
|
return iamAgg, nil
|
|
}
|
|
|
|
func (r *CommandSide) ChangeDefaultLoginPolicy(ctx context.Context, policy *iam_model.LoginPolicy) (*iam_model.LoginPolicy, error) {
|
|
if !policy.IsValid() {
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-6M0od", "Errors.IAM.LoginPolicyInvalid")
|
|
}
|
|
|
|
existingPolicy, err := r.defaultLoginPolicyWriteModelByID(ctx, policy.AggregateID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if !existingPolicy.IsActive {
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-M0sif", "Errors.IAM.LoginPolicy.NotFound")
|
|
}
|
|
changedEvent, hasChanged := existingPolicy.NewChangedEvent(ctx, policy.AllowUsernamePassword, policy.AllowRegister, policy.AllowExternalIdp, policy.ForceMFA, domain.PasswordlessType(policy.PasswordlessType))
|
|
if !hasChanged {
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-5M9vdd", "Errors.IAM.LoginPolicy.NotChanged")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&existingPolicy.LoginPolicyWriteModel.WriteModel)
|
|
iamAgg.PushEvents(changedEvent)
|
|
|
|
err = r.eventstore.PushAggregate(ctx, existingPolicy, iamAgg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return writeModelToLoginPolicy(existingPolicy), nil
|
|
}
|
|
|
|
func (r *CommandSide) AddIDPProviderToDefaultLoginPolicy(ctx context.Context, idpProvider *iam_model.IDPProvider) (*iam_model.IDPProvider, error) {
|
|
idpModel := NewIAMIdentityProviderWriteModel(idpProvider.AggregateID, idpProvider.IDPConfigID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, idpModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if idpModel.IsActive {
|
|
return nil, caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.IDP.AlreadyExists")
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&idpModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewIdentityProviderAddedEvent(ctx, idpProvider.IDPConfigID, domain.IdentityProviderType(idpProvider.Type)))
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, idpModel, iamAgg); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return writeModelToIDPProvider(idpModel), nil
|
|
}
|
|
|
|
func (r *CommandSide) RemoveIDPProviderFromDefaultLoginPolicy(ctx context.Context, idpProvider *iam_model.IDPProvider) error {
|
|
idpModel := NewIAMIdentityProviderWriteModel(idpProvider.AggregateID, idpProvider.IDPConfigID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, idpModel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !idpModel.IsActive {
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-39fjs", "Errors.IAM.LoginPolicy.IDP.NotExisting")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&idpModel.IdentityProviderWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewIdentityProviderRemovedEvent(ctx, idpProvider.IDPConfigID))
|
|
|
|
return r.eventstore.PushAggregate(ctx, idpModel, iamAgg)
|
|
}
|
|
|
|
func (r *CommandSide) AddSecondFactorToDefaultLoginPolicy(ctx context.Context, iamID string, secondFactor iam_model.SecondFactorType) (iam_model.SecondFactorType, error) {
|
|
secondFactorModel := NewIAMSecondFactorWriteModel(iamID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, secondFactorModel)
|
|
if err != nil {
|
|
return iam_model.SecondFactorTypeUnspecified, err
|
|
}
|
|
|
|
if secondFactorModel.IsActive {
|
|
return iam_model.SecondFactorTypeUnspecified, caos_errs.ThrowAlreadyExists(nil, "IAM-2B0ps", "Errors.IAM.LoginPolicy.MFA.AlreadyExists")
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&secondFactorModel.SecondFactorWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicySecondFactorAddedEvent(ctx, domain.SecondFactorType(secondFactor)))
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, secondFactorModel, iamAgg); err != nil {
|
|
return iam_model.SecondFactorTypeUnspecified, err
|
|
}
|
|
|
|
return iam_model.SecondFactorType(secondFactorModel.MFAType), nil
|
|
}
|
|
|
|
func (r *CommandSide) RemoveSecondFactorFromDefaultLoginPolicy(ctx context.Context, iamID string, secondFactor iam_model.SecondFactorType) error {
|
|
secondFactorModel := NewIAMSecondFactorWriteModel(iamID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, secondFactorModel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if !secondFactorModel.IsActive {
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-3M9od", "Errors.IAM.LoginPolicy.MFA.NotExisting")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&secondFactorModel.SecondFactorWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicySecondFactorRemovedEvent(ctx, domain.SecondFactorType(secondFactor)))
|
|
|
|
return r.eventstore.PushAggregate(ctx, secondFactorModel, iamAgg)
|
|
}
|
|
|
|
func (r *CommandSide) AddMultiFactorToDefaultLoginPolicy(ctx context.Context, iamID string, multiFactor iam_model.MultiFactorType) (iam_model.MultiFactorType, error) {
|
|
multiFactorModel := NewIAMMultiFactorWriteModel(iamID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, multiFactorModel)
|
|
if err != nil {
|
|
return iam_model.MultiFactorTypeUnspecified, err
|
|
}
|
|
if multiFactorModel.IsActive {
|
|
return iam_model.MultiFactorTypeUnspecified, caos_errs.ThrowAlreadyExists(nil, "IAM-3M9od", "Errors.IAM.LoginPolicy.MFA.AlreadyExists")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&multiFactorModel.MultiFactoryWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyMultiFactorAddedEvent(ctx, domain.MultiFactorType(multiFactor)))
|
|
|
|
if err = r.eventstore.PushAggregate(ctx, multiFactorModel, iamAgg); err != nil {
|
|
return iam_model.MultiFactorTypeUnspecified, err
|
|
}
|
|
|
|
return iam_model.MultiFactorType(multiFactorModel.MultiFactoryWriteModel.MFAType), nil
|
|
}
|
|
|
|
func (r *CommandSide) RemoveMultiFactorFromDefaultLoginPolicy(ctx context.Context, iamID string, multiFactor iam_model.MultiFactorType) error {
|
|
multiFactorModel := NewIAMMultiFactorWriteModel(iamID)
|
|
err := r.eventstore.FilterToQueryReducer(ctx, multiFactorModel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if multiFactorModel.IsActive {
|
|
return caos_errs.ThrowAlreadyExists(nil, "IAM-3M9df", "Errors.IAM.LoginPolicy.MFA.NotExisting")
|
|
}
|
|
iamAgg := IAMAggregateFromWriteModel(&multiFactorModel.MultiFactoryWriteModel.WriteModel)
|
|
iamAgg.PushEvents(iam_repo.NewLoginPolicyMultiFactorRemovedEvent(ctx, domain.MultiFactorType(multiFactor)))
|
|
|
|
return r.eventstore.PushAggregate(ctx, multiFactorModel, iamAgg)
|
|
}
|
|
|
|
func (r *CommandSide) defaultLoginPolicyWriteModelByID(ctx context.Context, iamID string) (policy *IAMLoginPolicyWriteModel, err error) {
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
writeModel := NewIAMLoginPolicyWriteModel(iamID)
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return writeModel, nil
|
|
}
|