2020-11-12 21:50:01 +00:00
|
|
|
package iam
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-11-23 18:31:12 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2020-11-12 21:50:01 +00:00
|
|
|
iam_model "github.com/caos/zitadel/internal/iam/model"
|
2020-11-23 18:31:12 +00:00
|
|
|
"github.com/caos/zitadel/internal/tracing"
|
2020-11-12 21:50:01 +00:00
|
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
|
|
)
|
|
|
|
|
2020-11-25 19:04:32 +00:00
|
|
|
func (r *Repository) AddMember(ctx context.Context, member *iam_model.IAMMember) (*iam_model.IAMMember, error) {
|
|
|
|
//TODO: check if roles valid
|
|
|
|
|
2020-11-12 21:50:01 +00:00
|
|
|
if !member.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-W8m4l", "Errors.IAM.MemberInvalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
iam, err := r.iamByID(ctx, member.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
idx, _ := iam.Members.MemberByUserID(member.UserID)
|
|
|
|
if idx > -1 {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-GPhuz", "Errors.IAM.MemberAlreadyExisting")
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg := iam_repo.AggregateFromReadModel(iam).
|
2020-11-17 12:44:37 +00:00
|
|
|
PushMemberAdded(ctx, member.UserID, member.Roles...)
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
events, err := r.eventstore.PushAggregates(ctx, iamAgg)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = iam.AppendAndReduce(events...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, addedMember := iam.Members.MemberByUserID(member.UserID)
|
|
|
|
if member == nil {
|
2020-11-25 19:04:32 +00:00
|
|
|
return nil, errors.ThrowInternal(nil, "IAM-nuoDN", "Errors.Internal")
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
return readModelToMember(addedMember), nil
|
|
|
|
}
|
|
|
|
|
2020-11-25 19:04:32 +00:00
|
|
|
//ChangeMember updates an existing member
|
|
|
|
func (r *Repository) ChangeMember(ctx context.Context, member *iam_model.IAMMember) (*iam_model.IAMMember, error) {
|
|
|
|
//TODO: check if roles valid
|
|
|
|
|
2020-11-12 21:50:01 +00:00
|
|
|
if !member.IsValid() {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-LiaZi", "Errors.IAM.MemberInvalid")
|
|
|
|
}
|
|
|
|
|
2020-11-18 20:22:15 +00:00
|
|
|
existingMember, err := r.memberWriteModelByID(ctx, member.AggregateID, member.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
iam := iam_repo.AggregateFromWriteModel(&existingMember.Member.WriteModel).
|
2020-11-23 18:31:12 +00:00
|
|
|
PushMemberChangedFromExisting(ctx, existingMember, member.Roles...)
|
2020-11-23 10:36:58 +00:00
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
events, err := r.eventstore.PushAggregates(ctx, iam)
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-25 13:12:44 +00:00
|
|
|
existingMember.AppendEvents(events...)
|
2020-11-25 19:04:32 +00:00
|
|
|
if err = existingMember.Reduce(); err != nil {
|
2020-11-23 10:36:58 +00:00
|
|
|
return nil, err
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
2020-11-23 10:36:58 +00:00
|
|
|
|
2020-11-25 19:04:32 +00:00
|
|
|
return writeModelToMember(existingMember), nil
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 19:04:32 +00:00
|
|
|
func (r *Repository) RemoveMember(ctx context.Context, member *iam_model.IAMMember) error {
|
2020-11-12 21:50:01 +00:00
|
|
|
iam, err := r.iamByID(ctx, member.AggregateID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
i, _ := iam.Members.MemberByUserID(member.UserID)
|
|
|
|
if i == -1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
iamAgg := iam_repo.AggregateFromReadModel(iam).
|
|
|
|
PushEvents(iam_repo.NewMemberRemovedEvent(ctx, member.UserID))
|
|
|
|
|
|
|
|
events, err := r.eventstore.PushAggregates(ctx, iamAgg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return iam.AppendAndReduce(events...)
|
|
|
|
}
|
2020-11-23 18:31:12 +00:00
|
|
|
|
|
|
|
func (r *Repository) MemberByID(ctx context.Context, iamID, userID string) (member *iam_repo.MemberReadModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2020-11-27 10:30:56 +00:00
|
|
|
member = new(iam_repo.MemberReadModel)
|
|
|
|
|
|
|
|
//query view
|
|
|
|
|
2020-11-23 18:31:12 +00:00
|
|
|
query := eventstore.NewSearchQueryFactory(eventstore.ColumnsEvent, iam_repo.AggregateType).
|
|
|
|
AggregateIDs(iamID).
|
|
|
|
EventData(map[string]interface{}{
|
|
|
|
"userId": userID,
|
2020-11-27 10:30:56 +00:00
|
|
|
}).SequenceGreater(member.ProcessedSequence)
|
2020-11-23 18:31:12 +00:00
|
|
|
|
|
|
|
err = r.eventstore.FilterToReducer(ctx, query, member)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return member, nil
|
|
|
|
}
|
2020-11-25 19:04:32 +00:00
|
|
|
|
|
|
|
func (r *Repository) memberWriteModelByID(ctx context.Context, iamID, userID string) (member *iam_repo.MemberWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel := iam_repo.NewMemberReadModel(iamID, userID)
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-30 05:40:56 +00:00
|
|
|
if writeModel.Member.IsRemoved {
|
2020-11-25 19:04:32 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "IAM-D8JxR", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|