2021-01-04 13:52:13 +00:00
|
|
|
package command
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-05 08:33:45 +00:00
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
2021-01-04 13:52:13 +00:00
|
|
|
"reflect"
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-12-03 09:21:00 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
2020-11-12 21:50:01 +00:00
|
|
|
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
|
|
|
|
)
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) AddIAMMember(ctx context.Context, member *domain.IAMMember) (*domain.IAMMember, error) {
|
2020-11-25 19:04:32 +00:00
|
|
|
//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")
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
addedMember := NewIAMMemberWriteModel(member.AggregateID, member.UserID)
|
2020-11-30 06:56:38 +00:00
|
|
|
err := r.eventstore.FilterToQueryReducer(ctx, addedMember)
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-12-15 10:43:09 +00:00
|
|
|
if addedMember.IsActive {
|
2020-11-30 06:56:38 +00:00
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "IAM-PtXi1", "Errors.IAM.Member.AlreadyExists")
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedMember.MemberWriteModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewMemberAddedEvent(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
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
//ChangeIAMMember updates an existing member
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) ChangeIAMMember(ctx context.Context, member *domain.IAMMember) (*domain.IAMMember, error) {
|
2020-11-25 19:04:32 +00:00
|
|
|
//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")
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
existingMember, err := r.iamMemberWriteModelByID(ctx, member.AggregateID, member.UserID)
|
2020-11-18 20:22:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
if reflect.DeepEqual(existingMember.Roles, member.Roles) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-LiaZi", "Errors.IAM.Member.RolesNotChanged")
|
|
|
|
}
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&existingMember.MemberWriteModel.WriteModel)
|
|
|
|
iamAgg.PushEvents(iam_repo.NewMemberChangedEvent(ctx, member.UserID, member.Roles...))
|
2020-11-23 10:36:58 +00:00
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
events, err := r.eventstore.PushAggregates(ctx, iamAgg)
|
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
|
|
|
}
|
|
|
|
|
2021-01-05 08:33:45 +00:00
|
|
|
func (r *CommandSide) RemoveIAMMember(ctx context.Context, userID string) error {
|
|
|
|
m, err := r.iamMemberWriteModelByID(ctx, r.iamID, userID)
|
2020-11-30 06:56:38 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
iamAgg := IAMAggregateFromWriteModel(&m.MemberWriteModel.WriteModel)
|
2021-01-05 08:33:45 +00:00
|
|
|
iamAgg.PushEvents(iam_repo.NewMemberRemovedEvent(ctx, userID))
|
2020-11-12 21:50:01 +00:00
|
|
|
|
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
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
func (r *CommandSide) iamMemberWriteModelByID(ctx context.Context, iamID, userID string) (member *IAMMemberWriteModel, err error) {
|
2020-11-25 19:04:32 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
writeModel := NewIAMMemberWriteModel(iamID, userID)
|
2020-11-25 19:04:32 +00:00
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-15 10:43:09 +00:00
|
|
|
if !writeModel.IsActive {
|
2020-11-25 19:04:32 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "IAM-D8JxR", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|