2021-01-12 11:59:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-02-18 13:48:27 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v2"
|
2021-01-12 11:59:51 +00:00
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/errors"
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
|
|
|
"github.com/caos/zitadel/internal/telemetry/tracing"
|
|
|
|
"github.com/caos/zitadel/internal/v2/domain"
|
|
|
|
"github.com/caos/zitadel/internal/v2/repository/project"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (r *CommandSide) AddProjectMember(ctx context.Context, member *domain.Member, resourceOwner string) (*domain.Member, error) {
|
|
|
|
addedMember := NewProjectMemberWriteModel(member.AggregateID, member.UserID, resourceOwner)
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&addedMember.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
event, err := r.addProjectMember(ctx, projectAgg, addedMember, member)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, event)
|
|
|
|
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-18 13:48:27 +00:00
|
|
|
func (r *CommandSide) addProjectMember(ctx context.Context, projectAgg *eventstore.Aggregate, addedMember *ProjectMemberWriteModel, member *domain.Member) (eventstore.EventPusher, error) {
|
2021-01-12 11:59:51 +00:00
|
|
|
//TODO: check if roles valid
|
|
|
|
|
|
|
|
if !member.IsValid() {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "PROJECT-W8m4l", "Errors.Project.Member.Invalid")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 05:35:26 +00:00
|
|
|
err := r.checkUserExists(ctx, addedMember.UserID, "")
|
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-28 05:35:26 +00:00
|
|
|
}
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, addedMember)
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, err
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
if addedMember.State == domain.MemberStateActive {
|
2021-02-18 13:48:27 +00:00
|
|
|
return nil, errors.ThrowAlreadyExists(nil, "PROJECT-PtXi1", "Errors.Project.Member.AlreadyExists")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
return project.NewProjectMemberAddedEvent(ctx, projectAgg, member.UserID, member.Roles...), nil
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//ChangeProjectMember updates an existing member
|
|
|
|
func (r *CommandSide) ChangeProjectMember(ctx context.Context, member *domain.Member, resourceOwner string) (*domain.Member, error) {
|
|
|
|
//TODO: check if roles valid
|
|
|
|
|
|
|
|
if !member.IsValid() {
|
2021-01-28 05:35:26 +00:00
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "PROJECT-LiaZi", "Errors.Project.Member.Invalid")
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
existingMember, err := r.projectMemberWriteModelByID(ctx, member.AggregateID, member.UserID, resourceOwner)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if reflect.DeepEqual(existingMember.Roles, member.Roles) {
|
|
|
|
return nil, caos_errs.ThrowPreconditionFailed(nil, "PROJECT-LiaZi", "Errors.Project.Member.RolesNotChanged")
|
|
|
|
}
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&existingMember.MemberWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
pushedEvents, err := r.eventstore.PushEvents(ctx, project.NewProjectMemberChangedEvent(ctx, projectAgg, member.UserID, member.Roles...))
|
2021-01-12 11:59:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-02-18 13:48:27 +00:00
|
|
|
err = AppendAndReduce(existingMember, pushedEvents...)
|
|
|
|
if err != nil {
|
2021-01-12 11:59:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return memberWriteModelToMember(&existingMember.MemberWriteModel), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) RemoveProjectMember(ctx context.Context, projectID, userID, resourceOwner string) error {
|
|
|
|
m, err := r.projectMemberWriteModelByID(ctx, projectID, userID, resourceOwner)
|
|
|
|
if err != nil && !errors.IsNotFound(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if errors.IsNotFound(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
projectAgg := ProjectAggregateFromWriteModel(&m.MemberWriteModel.WriteModel)
|
2021-02-18 13:48:27 +00:00
|
|
|
_, err = r.eventstore.PushEvents(ctx, project.NewProjectMemberRemovedEvent(ctx, projectAgg, userID))
|
|
|
|
return err
|
2021-01-12 11:59:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *CommandSide) projectMemberWriteModelByID(ctx context.Context, projectID, userID, resourceOwner string) (member *ProjectMemberWriteModel, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
|
|
|
writeModel := NewProjectMemberWriteModel(projectID, userID, resourceOwner)
|
|
|
|
err = r.eventstore.FilterToQueryReducer(ctx, writeModel)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if writeModel.State == domain.MemberStateUnspecified || writeModel.State == domain.MemberStateRemoved {
|
|
|
|
return nil, errors.ThrowNotFound(nil, "PROJECT-D8JxR", "Errors.NotFound")
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeModel, nil
|
|
|
|
}
|