2020-05-13 12:22:29 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-08-06 13:03:03 +00:00
|
|
|
"github.com/caos/logging"
|
2020-08-18 06:57:16 +00:00
|
|
|
"strings"
|
2020-06-25 09:25:38 +00:00
|
|
|
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/authz"
|
2020-05-13 12:22:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
2020-08-18 06:57:16 +00:00
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
2020-08-06 13:03:03 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-05-26 14:46:16 +00:00
|
|
|
mgmt_view "github.com/caos/zitadel/internal/management/repository/eventsourcing/view"
|
2020-07-08 11:56:37 +00:00
|
|
|
global_model "github.com/caos/zitadel/internal/model"
|
2020-05-13 12:22:29 +00:00
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
|
|
|
org_es "github.com/caos/zitadel/internal/org/repository/eventsourcing"
|
2020-08-06 13:03:03 +00:00
|
|
|
org_es_model "github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
2020-07-08 11:56:37 +00:00
|
|
|
"github.com/caos/zitadel/internal/org/repository/view/model"
|
|
|
|
usr_es "github.com/caos/zitadel/internal/user/repository/eventsourcing"
|
2020-05-13 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
2020-08-06 12:38:19 +00:00
|
|
|
const (
|
|
|
|
orgOwnerRole = "ORG_OWNER"
|
|
|
|
)
|
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
type OrgRepository struct {
|
2020-05-26 14:46:16 +00:00
|
|
|
SearchLimit uint64
|
2020-05-13 12:22:29 +00:00
|
|
|
*org_es.OrgEventstore
|
2020-06-29 07:37:10 +00:00
|
|
|
UserEvents *usr_es.UserEventstore
|
|
|
|
View *mgmt_view.View
|
|
|
|
Roles []string
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *OrgRepository) OrgByID(ctx context.Context, id string) (*org_model.OrgView, error) {
|
|
|
|
org, err := repo.View.OrgByID(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.OrgToModel(org), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *OrgRepository) OrgByDomainGlobal(ctx context.Context, domain string) (*org_model.OrgView, error) {
|
2020-06-16 09:40:18 +00:00
|
|
|
verifiedDomain, err := repo.View.VerifiedOrgDomain(domain)
|
2020-05-26 14:46:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
return repo.OrgByID(ctx, verifiedDomain.OrgID)
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 12:38:19 +00:00
|
|
|
func (repo *OrgRepository) CreateOrg(ctx context.Context, name string) (*org_model.Org, error) {
|
2020-08-18 06:57:16 +00:00
|
|
|
org, aggregates, err := repo.OrgEventstore.PrepareCreateOrg(ctx, &org_model.Org{Name: name}, nil)
|
2020-08-06 12:38:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
member := org_model.NewOrgMemberWithRoles(org.AggregateID, authz.GetCtxData(ctx).UserID, 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.Eventstore.PushAggregates, org.AppendEvents, aggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return org_es_model.OrgToModel(org), nil
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
func (repo *OrgRepository) UpdateOrg(ctx context.Context, org *org_model.Org) (*org_model.Org, error) {
|
|
|
|
return nil, errors.ThrowUnimplemented(nil, "EVENT-RkurR", "not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) DeactivateOrg(ctx context.Context, id string) (*org_model.Org, error) {
|
|
|
|
return repo.OrgEventstore.DeactivateOrg(ctx, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) ReactivateOrg(ctx context.Context, id string) (*org_model.Org, error) {
|
|
|
|
return repo.OrgEventstore.ReactivateOrg(ctx, id)
|
|
|
|
}
|
|
|
|
|
2020-06-19 05:27:16 +00:00
|
|
|
func (repo *OrgRepository) GetMyOrgIamPolicy(ctx context.Context) (*org_model.OrgIamPolicy, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
return repo.OrgEventstore.GetOrgIamPolicy(ctx, authz.GetCtxData(ctx).OrgID)
|
2020-06-19 05:27:16 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func (repo *OrgRepository) SearchMyOrgDomains(ctx context.Context, request *org_model.OrgDomainSearchRequest) (*org_model.OrgDomainSearchResponse, error) {
|
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-07-08 11:56:37 +00:00
|
|
|
request.Queries = append(request.Queries, &org_model.OrgDomainSearchQuery{Key: org_model.OrgDomainSearchKeyOrgID, Method: global_model.SearchMethodEquals, Value: authz.GetCtxData(ctx).OrgID})
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence, err := repo.View.GetLatestOrgDomainSequence()
|
|
|
|
logging.Log("EVENT-SLowp").OnError(err).Warn("could not read latest org domain sequence")
|
2020-06-16 09:40:18 +00:00
|
|
|
domains, count, err := repo.View.SearchOrgDomains(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &org_model.OrgDomainSearchResponse{
|
2020-06-16 09:40:18 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
|
|
|
Result: model.OrgDomainsToModel(domains),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
result.Sequence = sequence.CurrentSequence
|
|
|
|
result.Timestamp = sequence.CurrentTimestamp
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) AddMyOrgDomain(ctx context.Context, domain *org_model.OrgDomain) (*org_model.OrgDomain, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
domain.AggregateID = authz.GetCtxData(ctx).OrgID
|
2020-06-16 09:40:18 +00:00
|
|
|
return repo.OrgEventstore.AddOrgDomain(ctx, domain)
|
|
|
|
}
|
|
|
|
|
2020-08-06 13:03:03 +00:00
|
|
|
func (repo *OrgRepository) GenerateMyOrgDomainValidation(ctx context.Context, domain *org_model.OrgDomain) (string, string, error) {
|
|
|
|
domain.AggregateID = authz.GetCtxData(ctx).OrgID
|
|
|
|
return repo.OrgEventstore.GenerateOrgDomainValidation(ctx, domain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) ValidateMyOrgDomain(ctx context.Context, domain *org_model.OrgDomain) error {
|
|
|
|
domain.AggregateID = authz.GetCtxData(ctx).OrgID
|
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.UserEvents.PrepareDomainClaimed(ctx, userIDs)
|
|
|
|
}
|
|
|
|
return repo.OrgEventstore.ValidateOrgDomain(ctx, domain, users)
|
2020-08-06 13:03:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) SetMyPrimaryOrgDomain(ctx context.Context, domain *org_model.OrgDomain) error {
|
|
|
|
domain.AggregateID = authz.GetCtxData(ctx).OrgID
|
|
|
|
return repo.OrgEventstore.SetPrimaryOrgDomain(ctx, domain)
|
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func (repo *OrgRepository) RemoveMyOrgDomain(ctx context.Context, domain string) error {
|
2020-07-08 11:56:37 +00:00
|
|
|
d := org_model.NewOrgDomain(authz.GetCtxData(ctx).OrgID, domain)
|
2020-06-16 09:40:18 +00:00
|
|
|
return repo.OrgEventstore.RemoveOrgDomain(ctx, d)
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func (repo *OrgRepository) OrgChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool) (*org_model.OrgChanges, error) {
|
|
|
|
changes, err := repo.OrgEventstore.OrgChanges(ctx, id, lastSequence, limit, sortAscending)
|
2020-06-15 14:50:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-29 07:37:10 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
change.ModifierName = change.ModifierId
|
|
|
|
user, _ := repo.UserEvents.UserByID(ctx, change.ModifierId)
|
|
|
|
if user != nil {
|
|
|
|
change.ModifierName = user.DisplayName
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 14:50:09 +00:00
|
|
|
return changes, nil
|
|
|
|
}
|
|
|
|
|
2020-06-23 05:06:07 +00:00
|
|
|
func (repo *OrgRepository) OrgMemberByID(ctx context.Context, orgID, userID string) (*org_model.OrgMemberView, error) {
|
|
|
|
member, err := repo.View.OrgMemberByIDs(orgID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return model.OrgMemberToModel(member), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 08:47:50 +00:00
|
|
|
func (repo *OrgRepository) AddMyOrgMember(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
member.AggregateID = authz.GetCtxData(ctx).OrgID
|
2020-05-13 12:22:29 +00:00
|
|
|
return repo.OrgEventstore.AddOrgMember(ctx, member)
|
|
|
|
}
|
|
|
|
|
2020-06-10 08:47:50 +00:00
|
|
|
func (repo *OrgRepository) ChangeMyOrgMember(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
2020-07-08 11:56:37 +00:00
|
|
|
member.AggregateID = authz.GetCtxData(ctx).OrgID
|
2020-05-13 12:22:29 +00:00
|
|
|
return repo.OrgEventstore.ChangeOrgMember(ctx, member)
|
|
|
|
}
|
|
|
|
|
2020-06-10 08:47:50 +00:00
|
|
|
func (repo *OrgRepository) RemoveMyOrgMember(ctx context.Context, userID string) error {
|
2020-07-08 11:56:37 +00:00
|
|
|
member := org_model.NewOrgMember(authz.GetCtxData(ctx).OrgID, userID)
|
2020-05-13 12:22:29 +00:00
|
|
|
return repo.OrgEventstore.RemoveOrgMember(ctx, member)
|
|
|
|
}
|
2020-05-26 14:46:16 +00:00
|
|
|
|
2020-06-10 08:47:50 +00:00
|
|
|
func (repo *OrgRepository) SearchMyOrgMembers(ctx context.Context, request *org_model.OrgMemberSearchRequest) (*org_model.OrgMemberSearchResponse, error) {
|
2020-05-26 14:46:16 +00:00
|
|
|
request.EnsureLimit(repo.SearchLimit)
|
2020-07-08 11:56:37 +00:00
|
|
|
request.Queries[len(request.Queries)-1] = &org_model.OrgMemberSearchQuery{Key: org_model.OrgMemberSearchKeyOrgID, Method: global_model.SearchMethodEquals, Value: authz.GetCtxData(ctx).OrgID}
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence, err := repo.View.GetLatestOrgMemberSequence()
|
|
|
|
logging.Log("EVENT-Smu3d").OnError(err).Warn("could not read latest org member sequence")
|
2020-05-26 14:46:16 +00:00
|
|
|
members, count, err := repo.View.SearchOrgMembers(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
result := &org_model.OrgMemberSearchResponse{
|
2020-05-26 14:46:16 +00:00
|
|
|
Offset: request.Offset,
|
|
|
|
Limit: request.Limit,
|
|
|
|
TotalResult: uint64(count),
|
2020-06-16 09:40:18 +00:00
|
|
|
Result: model.OrgMembersToModel(members),
|
2020-07-15 11:24:36 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
result.Sequence = sequence.CurrentSequence
|
|
|
|
result.Timestamp = sequence.CurrentTimestamp
|
|
|
|
}
|
|
|
|
return result, nil
|
2020-05-26 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *OrgRepository) GetOrgMemberRoles() []string {
|
|
|
|
roles := make([]string, 0)
|
|
|
|
for _, roleMap := range repo.Roles {
|
|
|
|
if strings.HasPrefix(roleMap, "ORG") {
|
|
|
|
roles = append(roles, roleMap)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return roles
|
|
|
|
}
|