2021-01-04 13:52:13 +00:00
|
|
|
package command
|
2020-11-12 21:50:01 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2021-01-04 13:52:13 +00:00
|
|
|
"reflect"
|
2020-11-12 21:50:01 +00:00
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/domain"
|
2020-11-12 21:50:01 +00:00
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2021-02-23 14:13:04 +00:00
|
|
|
iam_repo "github.com/caos/zitadel/internal/repository/iam"
|
2020-12-03 09:21:00 +00:00
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
2020-11-12 21:50:01 +00:00
|
|
|
)
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) AddIAMMember(ctx context.Context, member *domain.Member) (*domain.Member, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
addedMember := NewIAMMemberWriteModel(member.UserID)
|
|
|
|
iamAgg := IAMAggregateFromWriteModel(&addedMember.MemberWriteModel.WriteModel)
|
2021-02-24 10:17:39 +00:00
|
|
|
event, err := c.addIAMMember(ctx, iamAgg, addedMember, member)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, event)
|
2021-02-18 13:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = AppendAndReduce(addedMember, pushedEvents...)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return memberWriteModelToMember(&addedMember.MemberWriteModel), nil
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) addIAMMember(ctx context.Context, iamAgg *eventstore.Aggregate, addedMember *IAMMemberWriteModel, member *domain.Member) (eventstore.EventPusher, 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() {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "IAM-GR34U", "Errors.IAM.MemberInvalid")
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
err := c.eventstore.FilterToQueryReducer(ctx, addedMember)
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
2021-01-07 15:06:45 +00:00
|
|
|
if addedMember.State == domain.MemberStateActive {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "IAM-sdgQ4", "Errors.IAM.Member.AlreadyExists")
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return iam_repo.NewMemberAddedEvent(ctx, iamAgg, member.UserID, member.Roles...), nil
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 13:52:13 +00:00
|
|
|
//ChangeIAMMember updates an existing member
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) ChangeIAMMember(ctx context.Context, member *domain.Member) (*domain.Member, 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-02-24 10:17:39 +00:00
|
|
|
existingMember, err := c.iamMemberWriteModelByID(ctx, 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)
|
2021-02-24 10:17:39 +00:00
|
|
|
pushedEvents, err := c.eventstore.PushEvents(ctx, iam_repo.NewMemberChangedEvent(ctx, iamAgg, member.UserID, member.Roles...))
|
2020-11-12 21:50:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(existingMember, pushedEvents...)
|
|
|
|
if 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
|
|
|
|
2021-01-08 10:33:45 +00:00
|
|
|
return memberWriteModelToMember(&existingMember.MemberWriteModel), nil
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) RemoveIAMMember(ctx context.Context, userID string) error {
|
|
|
|
m, err := c.iamMemberWriteModelByID(ctx, 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-02-24 10:17:39 +00:00
|
|
|
_, err = c.eventstore.PushEvents(ctx, iam_repo.NewMemberRemovedEvent(ctx, iamAgg, userID))
|
2021-02-18 13:48:27 +00:00
|
|
|
return err
|
2020-11-12 21:50:01 +00:00
|
|
|
}
|
2020-11-23 18:31:12 +00:00
|
|
|
|
2021-02-24 10:17:39 +00:00
|
|
|
func (c *Commands) iamMemberWriteModelByID(ctx context.Context, 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-12 11:59:51 +00:00
|
|
|
writeModel := NewIAMMemberWriteModel(userID)
|
2021-02-24 10:17:39 +00:00
|
|
|
err = c.eventstore.FilterToQueryReducer(ctx, writeModel)
|
2020-11-25 19:04:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-07 15:06:45 +00:00
|
|
|
if writeModel.State == domain.MemberStateUnspecified || writeModel.State == domain.MemberStateRemoved {
|
2020-11-25 19:04:32 +00:00
|
|
|
return nil, errors.ThrowNotFound(nil, "IAM-D8JxR", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|