zitadel/internal/v2/command/iam_member.go

104 lines
3.1 KiB
Go
Raw Normal View History

package command
import (
"context"
"reflect"
"github.com/caos/zitadel/internal/errors"
caos_errs "github.com/caos/zitadel/internal/errors"
iam_model "github.com/caos/zitadel/internal/iam/model"
2020-12-03 09:21:00 +00:00
"github.com/caos/zitadel/internal/telemetry/tracing"
iam_repo "github.com/caos/zitadel/internal/v2/repository/iam"
)
func (r *CommandSide) AddIAMMember(ctx context.Context, member *iam_model.IAMMember) (*iam_model.IAMMember, error) {
2020-11-25 19:04:32 +00:00
//TODO: check if roles valid
if !member.IsValid() {
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-W8m4l", "Errors.IAM.MemberInvalid")
}
addedMember := NewIAMMemberWriteModel(member.AggregateID, member.UserID)
2020-11-30 06:56:38 +00:00
err := r.eventstore.FilterToQueryReducer(ctx, addedMember)
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")
}
iamAgg := IAMAggregateFromWriteModel(&addedMember.MemberWriteModel.WriteModel)
iamAgg.PushEvents(iam_repo.NewMemberAddedEvent(ctx, member.UserID, member.Roles...))
2020-11-30 06:56:38 +00:00
err = r.eventstore.PushAggregate(ctx, addedMember, iamAgg)
if err != nil {
return nil, err
}
2020-11-30 06:56:38 +00:00
return writeModelToMember(addedMember), nil
}
//ChangeIAMMember updates an existing member
func (r *CommandSide) ChangeIAMMember(ctx context.Context, member *iam_model.IAMMember) (*iam_model.IAMMember, error) {
2020-11-25 19:04:32 +00:00
//TODO: check if roles valid
if !member.IsValid() {
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-LiaZi", "Errors.IAM.MemberInvalid")
}
existingMember, err := r.iamMemberWriteModelByID(ctx, member.AggregateID, member.UserID)
2020-11-18 20:22:15 +00:00
if err != nil {
return nil, err
}
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
events, err := r.eventstore.PushAggregates(ctx, iamAgg)
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-23 10:36:58 +00:00
2020-11-25 19:04:32 +00:00
return writeModelToMember(existingMember), nil
}
func (r *CommandSide) RemoveIAMMember(ctx context.Context, member *iam_model.IAMMember) error {
m, err := r.iamMemberWriteModelByID(ctx, member.AggregateID, member.UserID)
2020-11-30 06:56:38 +00:00
if err != nil && !errors.IsNotFound(err) {
return err
}
2020-11-30 06:56:38 +00:00
if errors.IsNotFound(err) {
return nil
}
iamAgg := IAMAggregateFromWriteModel(&m.MemberWriteModel.WriteModel)
iamAgg.PushEvents(iam_repo.NewMemberRemovedEvent(ctx, member.UserID))
2020-11-30 06:56:38 +00:00
return r.eventstore.PushAggregate(ctx, m, iamAgg)
}
2020-11-23 18:31:12 +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) }()
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
}