2021-01-06 09:47:55 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2021-01-12 11:59:51 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/repository/iam"
|
2021-01-06 09:47:55 +00:00
|
|
|
)
|
|
|
|
|
2021-03-01 07:48:50 +00:00
|
|
|
//TODO: private as soon as setup uses query
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) GetIAM(ctx context.Context) (*domain.IAM, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
iamWriteModel := NewIAMWriteModel()
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, iamWriteModel)
|
2021-01-06 09:47:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writeModelToIAM(iamWriteModel), nil
|
|
|
|
}
|
2021-01-12 11:59:51 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) setGlobalOrg(ctx context.Context, iamAgg *eventstore.Aggregate, iamWriteModel *IAMWriteModel, orgID string) (eventstore.EventPusher, error) {
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, iamWriteModel)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
if iamWriteModel.GlobalOrgID != "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-HGG24", "Errors.IAM.GlobalOrgAlreadySet")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return iam.NewGlobalOrgSetEventEvent(ctx, iamAgg, orgID), nil
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) setIAMProject(ctx context.Context, iamAgg *eventstore.Aggregate, iamWriteModel *IAMWriteModel, projectID string) (eventstore.EventPusher, error) {
|
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, iamWriteModel)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
if iamWriteModel.ProjectID != "" {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-EGbw2", "Errors.IAM.IAMProjectAlreadySet")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
return iam.NewIAMProjectSetEvent(ctx, iamAgg, projectID), nil
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|