2020-05-13 12:22:29 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-15 11:24:36 +00:00
|
|
|
"github.com/caos/logging"
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
admin_model "github.com/caos/zitadel/internal/admin/model"
|
2020-05-26 14:46:16 +00:00
|
|
|
admin_view "github.com/caos/zitadel/internal/admin/repository/eventsourcing/view"
|
2020-05-13 12:22:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/sdk"
|
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
org_es "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
2020-06-23 12:47:47 +00:00
|
|
|
"github.com/caos/zitadel/internal/org/repository/view/model"
|
|
|
|
policy_model "github.com/caos/zitadel/internal/policy/model"
|
2020-05-29 06:44:01 +00:00
|
|
|
policy_es "github.com/caos/zitadel/internal/policy/repository/eventsourcing"
|
2020-05-13 12:22:29 +00:00
|
|
|
usr_es "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
|
|
|
)
|
|
|
|
|
2020-05-29 06:44:01 +00:00
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
orgOwnerRole = "ORG_OWNER"
|
2020-05-29 06:44:01 +00:00
|
|
|
)
|
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
type OrgRepo struct {
|
2020-05-29 06:44:01 +00:00
|
|
|
Eventstore eventstore.Eventstore
|
|
|
|
OrgEventstore *org_es.OrgEventstore
|
|
|
|
UserEventstore *usr_es.UserEventstore
|
|
|
|
PolicyEventstore *policy_es.PolicyEventstore
|
2020-05-26 14:46:16 +00:00
|
|
|
|
|
|
|
View *admin_view.View
|
|
|
|
|
|
|
|
SearchLimit uint64
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) SetUpOrg(ctx context.Context, setUp *admin_model.SetupOrg) (*admin_model.SetupOrg, error) {
|
2020-06-23 12:47:47 +00:00
|
|
|
pwPolicy, err := repo.PolicyEventstore.GetPasswordComplexityPolicy(ctx, policy_model.DefaultPolicy)
|
2020-06-16 09:40:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-23 12:47:47 +00:00
|
|
|
orgPolicy, err := repo.OrgEventstore.GetOrgIamPolicy(ctx, policy_model.DefaultPolicy)
|
2020-05-29 06:44:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
org, aggregates, err := repo.OrgEventstore.PrepareCreateOrg(ctx, setUp.Org)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
user, userAggregates, err := repo.UserEventstore.PrepareCreateUser(ctx, setUp.User, pwPolicy, orgPolicy, org.AggregateID)
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-05-28 04:49:22 +00:00
|
|
|
aggregates = append(aggregates, userAggregates...)
|
2020-05-13 12:22:29 +00:00
|
|
|
setupModel := &Setup{Org: org, User: user}
|
|
|
|
|
2020-06-11 14:43:22 +00:00
|
|
|
member := org_model.NewOrgMemberWithRoles(org.AggregateID, user.AggregateID, orgOwnerRole)
|
|
|
|
_, memberAggregate, err := repo.OrgEventstore.PrepareAddOrgMember(ctx, member, org.AggregateID)
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
aggregates = append(aggregates, memberAggregate)
|
|
|
|
|
|
|
|
err = sdk.PushAggregates(ctx, repo.Eventstore.PushAggregates, setupModel.AppendEvents, aggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return SetupToModel(setupModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) OrgByID(ctx context.Context, id string) (*org_model.Org, error) {
|
|
|
|
return repo.OrgEventstore.OrgByID(ctx, org_model.NewOrg(id))
|
|
|
|
}
|
|
|
|
|
2020-05-26 14:46:16 +00:00
|
|
|
func (repo *OrgRepo) SearchOrgs(ctx context.Context, query *org_model.OrgSearchRequest) (*org_model.OrgSearchResult, error) {
|
|
|
|
query.EnsureLimit(repo.SearchLimit)
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence, err := repo.View.GetLatestOrgSequence()
|
|
|
|
logging.Log("EVENT-LXo9w").OnError(err).Warn("could not read latest iam sequence")
|
2020-05-26 14:46:16 +00:00
|
|
|
orgs, count, err := repo.View.SearchOrgs(query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &org_model.OrgSearchResult{
|
2020-05-26 14:46:16 +00:00
|
|
|
Offset: query.Offset,
|
|
|
|
Limit: query.Limit,
|
|
|
|
TotalResult: uint64(count),
|
2020-06-16 09:40:18 +00:00
|
|
|
Result: model.OrgsToModel(orgs),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
result.Sequence = sequence.CurrentSequence
|
|
|
|
result.Timestamp = sequence.CurrentTimestamp
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) IsOrgUnique(ctx context.Context, name, domain string) (isUnique bool, err error) {
|
|
|
|
return repo.OrgEventstore.IsOrgUnique(ctx, name, domain)
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
|
|
|
|
func (repo *OrgRepo) GetOrgIamPolicyByID(ctx context.Context, id string) (*org_model.OrgIamPolicy, error) {
|
|
|
|
return repo.OrgEventstore.GetOrgIamPolicy(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) CreateOrgIamPolicy(ctx context.Context, policy *org_model.OrgIamPolicy) (*org_model.OrgIamPolicy, error) {
|
|
|
|
return repo.OrgEventstore.AddOrgIamPolicy(ctx, policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) ChangeOrgIamPolicy(ctx context.Context, policy *org_model.OrgIamPolicy) (*org_model.OrgIamPolicy, error) {
|
|
|
|
return repo.OrgEventstore.ChangeOrgIamPolicy(ctx, policy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepo) RemoveOrgIamPolicy(ctx context.Context, id string) error {
|
|
|
|
return repo.OrgEventstore.RemoveOrgIamPolicy(ctx, id)
|
|
|
|
}
|