2021-06-07 05:20:47 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
2022-01-13 07:58:14 +00:00
|
|
|
"github.com/caos/zitadel/internal/query"
|
2022-03-24 16:21:34 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/instance"
|
2021-06-07 05:20:47 +00:00
|
|
|
"github.com/caos/zitadel/internal/repository/org"
|
|
|
|
"github.com/caos/zitadel/internal/repository/project"
|
|
|
|
)
|
|
|
|
|
2022-01-13 07:58:14 +00:00
|
|
|
func (c *Commands) removeUserMemberships(ctx context.Context, memberships []*query.Membership) (_ []eventstore.Command, err error) {
|
2022-01-03 08:19:07 +00:00
|
|
|
events := make([]eventstore.Command, 0)
|
2021-06-07 05:20:47 +00:00
|
|
|
for _, membership := range memberships {
|
2022-01-13 07:58:14 +00:00
|
|
|
if membership.IAM != nil {
|
2022-04-05 05:58:09 +00:00
|
|
|
iamAgg := instance.NewAggregate(membership.IAM.IAMID)
|
2022-03-24 16:21:34 +00:00
|
|
|
removeEvent := c.removeInstanceMember(ctx, &iamAgg.Aggregate, membership.UserID, true)
|
2021-06-07 05:20:47 +00:00
|
|
|
events = append(events, removeEvent)
|
2022-01-13 07:58:14 +00:00
|
|
|
} else if membership.Org != nil {
|
|
|
|
iamAgg := org.NewAggregate(membership.Org.OrgID, membership.ResourceOwner)
|
2021-06-07 05:20:47 +00:00
|
|
|
removeEvent := c.removeOrgMember(ctx, &iamAgg.Aggregate, membership.UserID, true)
|
|
|
|
events = append(events, removeEvent)
|
2022-01-13 07:58:14 +00:00
|
|
|
} else if membership.Project != nil {
|
|
|
|
projectAgg := project.NewAggregate(membership.Project.ProjectID, membership.ResourceOwner)
|
2021-06-07 05:20:47 +00:00
|
|
|
removeEvent := c.removeProjectMember(ctx, &projectAgg.Aggregate, membership.UserID, true)
|
|
|
|
events = append(events, removeEvent)
|
2022-01-13 07:58:14 +00:00
|
|
|
} else if membership.ProjectGrant != nil {
|
|
|
|
projectAgg := project.NewAggregate(membership.ProjectGrant.ProjectID, membership.ResourceOwner)
|
|
|
|
removeEvent := c.removeProjectGrantMember(ctx, &projectAgg.Aggregate, membership.UserID, membership.ProjectGrant.GrantID, true)
|
2021-06-07 05:20:47 +00:00
|
|
|
events = append(events, removeEvent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return events, nil
|
|
|
|
}
|