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"
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
addedMember := iam_repo.NewMemberWriteModel(member.AggregateID, member.UserID)
|
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedMember)
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-30 06:56:38 +00:00
|
|
|
if addedMember.Member.IsActive {
|
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "IAM-PtXi1", "Errors.IAM.Member.AlreadyExists")
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
iamAgg := iam_repo.AggregateFromWriteModel(&addedMember.WriteModel).
|
2020-11-17 12:44:37 +00:00
|
|
|
PushMemberAdded(ctx, member.UserID, member.Roles...)
|
2020-11-12 21:50:01 +00:00
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
err = r.eventstore.PushAggregate(ctx, addedMember, iamAgg)
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
return writeModelToMember(addedMember), nil
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
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 06:56:38 +00:00
|
|
|
iam := iam_repo.AggregateFromWriteModel(&existingMember.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-30 06:56:38 +00:00
|
|
|
m, err := r.memberWriteModelByID(ctx, member.AggregateID, member.UserID)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
2020-11-12 21:50:01 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-30 06:56:38 +00:00
|
|
|
if errors.IsNotFound(err) {
|
2020-11-12 21:50:01 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
iamAgg := iam_repo.AggregateFromWriteModel(&m.WriteModel).
|
2020-11-12 21:50:01 +00:00
|
|
|
PushEvents(iam_repo.NewMemberRemovedEvent(ctx, member.UserID))
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
return r.eventstore.PushAggregate(ctx, m, iamAgg)
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
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-30 07:35:40 +00:00
|
|
|
member = iam_repo.NewMemberReadModel(iamID, userID)
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, member)
|
2020-11-23 18:31:12 +00:00
|
|
|
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) }()
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
writeModel := iam_repo.NewMemberWriteModel(iamID, userID)
|
2020-11-25 19:04:32 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-30 06:56:38 +00:00
|
|
|
if !writeModel.Member.IsActive {
|
2020-11-25 19:04:32 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "IAM-D8JxR", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|