2020-06-05 05:50:04 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-15 11:24:36 +00:00
|
|
|
"github.com/caos/logging"
|
2020-09-18 11:26:28 +00:00
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
|
|
|
iam_view_model "github.com/caos/zitadel/internal/iam/repository/view/model"
|
2020-08-18 06:57:16 +00:00
|
|
|
|
2020-08-06 12:38:19 +00:00
|
|
|
auth_model "github.com/caos/zitadel/internal/auth/model"
|
2020-06-05 05:50:04 +00:00
|
|
|
auth_view "github.com/caos/zitadel/internal/auth/repository/eventsourcing/view"
|
2020-08-18 06:57:16 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-08-06 12:38:19 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-06-05 05:50:04 +00:00
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
org_es "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
2020-06-16 09:40:18 +00:00
|
|
|
"github.com/caos/zitadel/internal/org/repository/view/model"
|
2020-08-06 12:38:19 +00:00
|
|
|
policy_model "github.com/caos/zitadel/internal/policy/model"
|
|
|
|
policy_es "github.com/caos/zitadel/internal/policy/repository/eventsourcing"
|
|
|
|
usr_es "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
orgOwnerRole = "ORG_OWNER"
|
2020-06-05 05:50:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgRepository struct {
|
2020-08-06 12:38:19 +00:00
|
|
|
SearchLimit uint64
|
|
|
|
OrgEventstore *org_es.OrgEventstore
|
|
|
|
UserEventstore *usr_es.UserEventstore
|
|
|
|
PolicyEventstore *policy_es.PolicyEventstore
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
View *auth_view.View
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) SearchOrgs(ctx context.Context, request *org_model.OrgSearchRequest) (*org_model.OrgSearchResult, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence, err := repo.View.GetLatestOrgSequence()
|
|
|
|
logging.Log("EVENT-7Udhz").OnError(err).Warn("could not read latest org sequence")
|
2020-06-05 05:50:04 +00:00
|
|
|
members, count, err := repo.View.SearchOrgs(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &org_model.OrgSearchResult{
|
2020-06-05 05:50:04 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
2020-09-18 11:26:28 +00:00
|
|
|
TotalResult: count,
|
2020-06-16 09:40:18 +00:00
|
|
|
Result: model.OrgsToModel(members),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
result.Sequence = sequence.CurrentSequence
|
|
|
|
result.Timestamp = sequence.CurrentTimestamp
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-06-05 05:50:04 +00:00
|
|
|
}
|
2020-08-06 12:38:19 +00:00
|
|
|
|
|
|
|
func (repo *OrgRepository) RegisterOrg(ctx context.Context, register *auth_model.RegisterOrg) (*auth_model.RegisterOrg, error) {
|
|
|
|
pwPolicy, err := repo.PolicyEventstore.GetPasswordComplexityPolicy(ctx, policy_model.DefaultPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-26 07:56:23 +00:00
|
|
|
orgPolicy, err := repo.OrgEventstore.GetOrgIAMPolicy(ctx, policy_model.DefaultPolicy)
|
2020-08-06 12:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-18 06:57:16 +00:00
|
|
|
users := func(ctx context.Context, domain string) ([]*es_models.Aggregate, error) {
|
|
|
|
userIDs, err := repo.View.UserIDsByDomain(domain)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return repo.UserEventstore.PrepareDomainClaimed(ctx, userIDs)
|
|
|
|
}
|
|
|
|
org, aggregates, err := repo.OrgEventstore.PrepareCreateOrg(ctx, register.Org, users)
|
2020-08-06 12:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-09-18 11:26:28 +00:00
|
|
|
user, userAggregates, err := repo.UserEventstore.PrepareRegisterUser(ctx, register.User, nil, pwPolicy, orgPolicy, org.AggregateID)
|
2020-08-06 12:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
aggregates = append(aggregates, userAggregates...)
|
|
|
|
registerModel := &Register{Org: org, User: user}
|
|
|
|
|
|
|
|
member := org_model.NewOrgMemberWithRoles(org.AggregateID, user.AggregateID, orgOwnerRole)
|
|
|
|
_, memberAggregate, err := repo.OrgEventstore.PrepareAddOrgMember(ctx, member, org.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregates = append(aggregates, memberAggregate)
|
|
|
|
|
|
|
|
err = sdk.PushAggregates(ctx, repo.OrgEventstore.PushAggregates, registerModel.AppendEvents, aggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return RegisterToModel(registerModel), nil
|
|
|
|
}
|
|
|
|
|
2020-09-18 11:26:28 +00:00
|
|
|
func (repo *OrgRepository) GetDefaultOrgIamPolicy(ctx context.Context) *org_model.OrgIAMPolicy {
|
|
|
|
return repo.OrgEventstore.GetDefaultOrgIAMPolicy(ctx)
|
|
|
|
}
|
|
|
|
|
2020-08-26 07:56:23 +00:00
|
|
|
func (repo *OrgRepository) GetOrgIamPolicy(ctx context.Context, orgID string) (*org_model.OrgIAMPolicy, error) {
|
2020-09-18 11:26:28 +00:00
|
|
|
return repo.OrgEventstore.GetOrgIAMPolicy(ctx, orgID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) GetIDPConfigByID(ctx context.Context, idpConfigID string) (*iam_model.IDPConfigView, error) {
|
|
|
|
idpConfig, err := repo.View.IDPConfigByID(idpConfigID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iam_view_model.IDPConfigViewToModel(idpConfig), nil
|
2020-08-06 12:38:19 +00:00
|
|
|
}
|