2020-05-13 12:22:29 +00:00
|
|
|
package eventsourcing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-06-15 14:50:09 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
"github.com/caos/zitadel/internal/config/systemdefaults"
|
|
|
|
|
2020-06-15 14:50:09 +00:00
|
|
|
"github.com/caos/logging"
|
2020-05-13 12:22:29 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
es_models "github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
es_sdk "github.com/caos/zitadel/internal/eventstore/sdk"
|
2020-05-28 11:28:36 +00:00
|
|
|
"github.com/caos/zitadel/internal/id"
|
2020-05-13 12:22:29 +00:00
|
|
|
org_model "github.com/caos/zitadel/internal/org/model"
|
2020-06-05 05:50:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/org/repository/eventsourcing/model"
|
2020-06-15 14:50:09 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2020-05-13 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type OrgEventstore struct {
|
|
|
|
eventstore.Eventstore
|
2020-06-16 09:40:18 +00:00
|
|
|
IAMDomain string
|
|
|
|
idGenerator id.Generator
|
|
|
|
defaultOrgIamPolicy *org_model.OrgIamPolicy
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type OrgConfig struct {
|
|
|
|
eventstore.Eventstore
|
2020-06-16 09:40:18 +00:00
|
|
|
IAMDomain string
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func StartOrg(conf OrgConfig, defaults systemdefaults.SystemDefaults) *OrgEventstore {
|
|
|
|
policy := defaults.DefaultPolicies.OrgIam
|
|
|
|
policy.Default = true
|
2020-08-06 12:38:19 +00:00
|
|
|
policy.IamDomain = conf.IAMDomain
|
2020-05-28 11:28:36 +00:00
|
|
|
return &OrgEventstore{
|
2020-06-16 09:40:18 +00:00
|
|
|
Eventstore: conf.Eventstore,
|
|
|
|
idGenerator: id.SonyFlakeGenerator,
|
|
|
|
IAMDomain: conf.IAMDomain,
|
|
|
|
defaultOrgIamPolicy: &policy,
|
2020-05-28 11:28:36 +00:00
|
|
|
}
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
func (es *OrgEventstore) PrepareCreateOrg(ctx context.Context, orgModel *org_model.Org) (*model.Org, []*es_models.Aggregate, error) {
|
2020-05-13 12:22:29 +00:00
|
|
|
if orgModel == nil || !orgModel.IsValid() {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, nil, errors.ThrowInvalidArgument(nil, "EVENT-OeLSk", "Errors.Org.Invalid")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
orgModel.AddIAMDomain(es.IAMDomain)
|
|
|
|
|
2020-05-28 11:28:36 +00:00
|
|
|
id, err := es.idGenerator.Next()
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, nil, errors.ThrowInternal(err, "EVENT-OwciI", "Errors.Internal")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2020-05-28 11:28:36 +00:00
|
|
|
orgModel.AggregateID = id
|
2020-06-05 05:50:04 +00:00
|
|
|
org := model.OrgFromModel(orgModel)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
aggregates, err := orgCreatedAggregates(ctx, es.AggregateCreator(), org)
|
|
|
|
|
|
|
|
return org, aggregates, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) CreateOrg(ctx context.Context, orgModel *org_model.Org) (*org_model.Org, error) {
|
|
|
|
org, aggregates, err := es.PrepareCreateOrg(ctx, orgModel)
|
|
|
|
err = es_sdk.PushAggregates(ctx, es.PushAggregates, org.AppendEvents, aggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgToModel(org), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) OrgByID(ctx context.Context, org *org_model.Org) (*org_model.Org, error) {
|
|
|
|
if org == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "EVENT-gQTYP", "Errors.Org.Empty")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
query, err := OrgByIDQuery(org.AggregateID, org.Sequence)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
esOrg := model.OrgFromModel(org)
|
2020-05-13 12:22:29 +00:00
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, esOrg.AppendEvents, query)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if esOrg.Sequence == 0 {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "EVENT-kVLb2", "Errors.Org.NotFound")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgToModel(esOrg), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) IsOrgUnique(ctx context.Context, name, domain string) (isUnique bool, err error) {
|
|
|
|
var found bool
|
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, isUniqueValidation(&found), OrgNameUniqueQuery(name))
|
|
|
|
if (err != nil && !errors.IsNotFound(err)) || found {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = es_sdk.Filter(ctx, es.FilterEvents, isUniqueValidation(&found), OrgDomainUniqueQuery(domain))
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return !found, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isUniqueValidation(unique *bool) func(events ...*es_models.Event) error {
|
|
|
|
return func(events ...*es_models.Event) error {
|
|
|
|
if len(events) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
*unique = *unique || events[0].Type == model.OrgDomainReserved || events[0].Type == model.OrgNameReserved
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) DeactivateOrg(ctx context.Context, orgID string) (*org_model.Org, error) {
|
|
|
|
existingOrg, err := es.OrgByID(ctx, org_model.NewOrg(orgID))
|
|
|
|
if err != nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "EVENT-oL9nT", "Errors.Org.NotFound")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
org := model.OrgFromModel(existingOrg)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
aggregate := orgDeactivateAggregate(es.AggregateCreator(), org)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, org.AppendEvents, aggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgToModel(org), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) ReactivateOrg(ctx context.Context, orgID string) (*org_model.Org, error) {
|
|
|
|
existingOrg, err := es.OrgByID(ctx, org_model.NewOrg(orgID))
|
|
|
|
if err != nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowInvalidArgument(nil, "EVENT-oL9nT", "Errors.Org.Empty")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
org := model.OrgFromModel(existingOrg)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
aggregate := orgReactivateAggregate(es.AggregateCreator(), org)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, org.AppendEvents, aggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
|
|
|
|
}
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgToModel(org), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 09:40:18 +00:00
|
|
|
func (es *OrgEventstore) AddOrgDomain(ctx context.Context, domain *org_model.OrgDomain) (*org_model.OrgDomain, error) {
|
|
|
|
if !domain.IsValid() {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-8sFJW", "Errors.Org.InvalidDomain")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(domain.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoOrg := model.OrgFromModel(existing)
|
|
|
|
repoDomain := model.OrgDomainFromModel(domain)
|
|
|
|
aggregate := OrgDomainAddedAggregate(es.Eventstore.AggregateCreator(), repoOrg, repoDomain)
|
|
|
|
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoOrg.AppendEvents, aggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, d := model.GetDomain(repoOrg.Domains, domain.Domain); d != nil {
|
|
|
|
return model.OrgDomainToModel(d), nil
|
|
|
|
}
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowInternal(nil, "EVENT-ISOP0", "Errors.Internal")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) RemoveOrgDomain(ctx context.Context, domain *org_model.OrgDomain) error {
|
|
|
|
if domain.Domain == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-SJsK3", "Errors.Org.DomainMissing")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(domain.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-07-07 12:23:09 +00:00
|
|
|
_, d := existing.GetDomain(domain)
|
|
|
|
if d == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-Sjdi3", "Errors.Org.DomainNotOnOrg")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
2020-07-07 12:23:09 +00:00
|
|
|
if d.Primary {
|
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-Sjdi3", "Errors.Org.PrimaryDomainNotDeletable")
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
repoOrg := model.OrgFromModel(existing)
|
|
|
|
repoDomain := model.OrgDomainFromModel(domain)
|
|
|
|
orgAggregates, err := OrgDomainRemovedAggregate(ctx, es.Eventstore.AggregateCreator(), repoOrg, repoDomain)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = es_sdk.PushAggregates(ctx, es.PushAggregates, repoOrg.AppendEvents, orgAggregates...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func (es *OrgEventstore) OrgChanges(ctx context.Context, id string, lastSequence uint64, limit uint64, sortAscending bool) (*org_model.OrgChanges, error) {
|
|
|
|
query := ChangesQuery(id, lastSequence, limit, sortAscending)
|
2020-06-15 14:50:09 +00:00
|
|
|
|
|
|
|
events, err := es.Eventstore.FilterEvents(context.Background(), query)
|
|
|
|
if err != nil {
|
|
|
|
logging.Log("EVENT-ZRffs").WithError(err).Warn("eventstore unavailable")
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowInternal(err, "EVENT-328b1", "Errors.Org.NotFound")
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
if len(events) == 0 {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "EVENT-FpQqK", "Errors.Changes.NotFound")
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:32:49 +00:00
|
|
|
changes := make([]*org_model.OrgChange, len(events))
|
2020-06-15 14:50:09 +00:00
|
|
|
|
2020-07-09 14:32:49 +00:00
|
|
|
for i, event := range events {
|
|
|
|
creationDate, err := ptypes.TimestampProto(event.CreationDate)
|
2020-06-15 14:50:09 +00:00
|
|
|
logging.Log("EVENT-qxIR7").OnError(err).Debug("unable to parse timestamp")
|
|
|
|
change := &org_model.OrgChange{
|
|
|
|
ChangeDate: creationDate,
|
2020-07-09 14:32:49 +00:00
|
|
|
EventType: event.Type.String(),
|
|
|
|
ModifierId: event.EditorUser,
|
|
|
|
Sequence: event.Sequence,
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:32:49 +00:00
|
|
|
if event.Data != nil {
|
|
|
|
org := new(model.Org)
|
|
|
|
err := json.Unmarshal(event.Data, org)
|
|
|
|
logging.Log("EVENT-XCLEm").OnError(err).Debug("unable to unmarshal data")
|
|
|
|
change.Data = org
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-09 14:32:49 +00:00
|
|
|
changes[i] = change
|
|
|
|
if lastSequence < event.Sequence {
|
|
|
|
lastSequence = event.Sequence
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-09 14:32:49 +00:00
|
|
|
return &org_model.OrgChanges{
|
|
|
|
Changes: changes,
|
2020-06-15 14:50:09 +00:00
|
|
|
LastSequence: lastSequence,
|
2020-07-09 14:32:49 +00:00
|
|
|
}, nil
|
2020-06-15 14:50:09 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 09:25:38 +00:00
|
|
|
func ChangesQuery(orgID string, latestSequence, limit uint64, sortAscending bool) *es_models.SearchQuery {
|
2020-06-15 14:50:09 +00:00
|
|
|
query := es_models.NewSearchQuery().
|
2020-06-25 09:25:38 +00:00
|
|
|
AggregateTypeFilter(model.OrgAggregate)
|
|
|
|
|
|
|
|
if !sortAscending {
|
2020-07-28 07:42:21 +00:00
|
|
|
query.OrderDesc()
|
2020-06-25 09:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
query.LatestSequenceFilter(latestSequence).
|
|
|
|
AggregateIDFilter(orgID).
|
|
|
|
SetLimit(limit)
|
2020-06-15 14:50:09 +00:00
|
|
|
return query
|
|
|
|
}
|
|
|
|
|
2020-05-13 12:22:29 +00:00
|
|
|
func (es *OrgEventstore) OrgMemberByIDs(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
|
|
|
if member == nil || member.UserID == "" || member.AggregateID == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-ld93d", "Errors.Org.MemberIDMissing")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
org, err := es.OrgByID(ctx, &org_model.Org{ObjectRoot: member.ObjectRoot, Members: []*org_model.OrgMember{member}})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, currentMember := range org.Members {
|
|
|
|
if currentMember.UserID == member.UserID {
|
|
|
|
return currentMember, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "EVENT-SXji6", "Errors.Org.MemberNotFound")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 14:43:22 +00:00
|
|
|
func (es *OrgEventstore) PrepareAddOrgMember(ctx context.Context, member *org_model.OrgMember, resourceOwner string) (*model.OrgMember, *es_models.Aggregate, error) {
|
2020-05-13 12:22:29 +00:00
|
|
|
if member == nil || !member.IsValid() {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, nil, errors.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Errors.Org.InvalidMember")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
repoMember := model.OrgMemberFromModel(member)
|
2020-06-11 14:43:22 +00:00
|
|
|
addAggregate, err := orgMemberAddedAggregate(ctx, es.Eventstore.AggregateCreator(), repoMember, resourceOwner)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
return repoMember, addAggregate, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) AddOrgMember(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
2020-06-11 14:43:22 +00:00
|
|
|
repoMember, addAggregate, err := es.PrepareAddOrgMember(ctx, member, "")
|
2020-05-13 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = es_sdk.PushAggregates(ctx, es.PushAggregates, repoMember.AppendEvents, addAggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgMemberToModel(repoMember), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) ChangeOrgMember(ctx context.Context, member *org_model.OrgMember) (*org_model.OrgMember, error) {
|
|
|
|
if member == nil || !member.IsValid() {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-9dk45", "Errors.Org.InvalidMember")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
existingMember, err := es.OrgMemberByIDs(ctx, member)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
member.ObjectRoot = existingMember.ObjectRoot
|
2020-06-05 05:50:04 +00:00
|
|
|
repoMember := model.OrgMemberFromModel(member)
|
|
|
|
repoExistingMember := model.OrgMemberFromModel(existingMember)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
orgAggregate := orgMemberChangedAggregate(es.Eventstore.AggregateCreator(), repoExistingMember, repoMember)
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:50:04 +00:00
|
|
|
return model.OrgMemberToModel(repoMember), nil
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) RemoveOrgMember(ctx context.Context, member *org_model.OrgMember) error {
|
|
|
|
if member == nil || member.UserID == "" {
|
2020-06-22 11:51:44 +00:00
|
|
|
return errors.ThrowInvalidArgument(nil, "EVENT-d43fs", "Errors.Org.UserIDMissing")
|
2020-05-13 12:22:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
existingMember, err := es.OrgMemberByIDs(ctx, member)
|
|
|
|
if errors.IsNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
member.ObjectRoot = existingMember.ObjectRoot
|
2020-06-05 05:50:04 +00:00
|
|
|
repoMember := model.OrgMemberFromModel(member)
|
2020-05-13 12:22:29 +00:00
|
|
|
|
|
|
|
orgAggregate := orgMemberRemovedAggregate(es.Eventstore.AggregateCreator(), repoMember)
|
|
|
|
return es_sdk.Push(ctx, es.PushAggregates, repoMember.AppendEvents, orgAggregate)
|
|
|
|
}
|
2020-06-16 09:40:18 +00:00
|
|
|
|
|
|
|
func (es *OrgEventstore) GetOrgIamPolicy(ctx context.Context, orgID string) (*org_model.OrgIamPolicy, error) {
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(orgID))
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing != nil && existing.OrgIamPolicy != nil {
|
|
|
|
return existing.OrgIamPolicy, nil
|
|
|
|
}
|
|
|
|
return es.defaultOrgIamPolicy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) AddOrgIamPolicy(ctx context.Context, policy *org_model.OrgIamPolicy) (*org_model.OrgIamPolicy, error) {
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(policy.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.OrgIamPolicy != nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-7Usj3", "Errors.Org.PolicyAlreadyExists")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
repoOrg := model.OrgFromModel(existing)
|
|
|
|
repoPolicy := model.OrgIamPolicyFromModel(policy)
|
|
|
|
orgAggregate := OrgIamPolicyAddedAggregate(es.Eventstore.AggregateCreator(), repoOrg, repoPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoOrg.AppendEvents, orgAggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return model.OrgIamPolicyToModel(repoOrg.OrgIamPolicy), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) ChangeOrgIamPolicy(ctx context.Context, policy *org_model.OrgIamPolicy) (*org_model.OrgIamPolicy, error) {
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(policy.AggregateID))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if existing.OrgIamPolicy == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return nil, errors.ThrowPreconditionFailed(nil, "EVENT-8juSd", "Errors.Org.PolicyNotExisting")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
repoOrg := model.OrgFromModel(existing)
|
|
|
|
repoPolicy := model.OrgIamPolicyFromModel(policy)
|
|
|
|
orgAggregate := OrgIamPolicyChangedAggregate(es.Eventstore.AggregateCreator(), repoOrg, repoPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = es_sdk.Push(ctx, es.PushAggregates, repoOrg.AppendEvents, orgAggregate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return model.OrgIamPolicyToModel(repoOrg.OrgIamPolicy), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *OrgEventstore) RemoveOrgIamPolicy(ctx context.Context, orgID string) error {
|
|
|
|
existing, err := es.OrgByID(ctx, org_model.NewOrg(orgID))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if existing.OrgIamPolicy == nil {
|
2020-06-22 11:51:44 +00:00
|
|
|
return errors.ThrowPreconditionFailed(nil, "EVENT-z6Dse", "Errors.Org.PolicyNotExisting")
|
2020-06-16 09:40:18 +00:00
|
|
|
}
|
|
|
|
repoOrg := model.OrgFromModel(existing)
|
|
|
|
orgAggregate := OrgIamPolicyRemovedAggregate(es.Eventstore.AggregateCreator(), repoOrg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return es_sdk.Push(ctx, es.PushAggregates, repoOrg.AppendEvents, orgAggregate)
|
|
|
|
}
|