2022-01-26 09:16:33 +00:00
|
|
|
package eventstore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
2022-05-25 12:07:16 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
2022-05-24 09:18:25 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-01-26 09:16:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type UserMembershipRepo struct {
|
2022-05-25 12:07:16 +00:00
|
|
|
Queries *query.Queries
|
2022-01-26 09:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (repo *UserMembershipRepo) SearchMyMemberships(ctx context.Context, orgID string, shouldTriggerBulk bool) (_ []*authz.Membership, err error) {
|
2022-05-24 09:18:25 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
2023-10-19 10:19:10 +00:00
|
|
|
|
|
|
|
memberships, err := repo.searchUserMemberships(ctx, orgID, shouldTriggerBulk)
|
2022-01-26 09:16:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return userMembershipsToMemberships(memberships), nil
|
|
|
|
}
|
|
|
|
|
2023-10-19 10:19:10 +00:00
|
|
|
func (repo *UserMembershipRepo) searchUserMemberships(ctx context.Context, orgID string, shouldTriggerBulk bool) (_ []*query.Membership, err error) {
|
2022-05-24 09:18:25 +00:00
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
2022-01-26 09:16:33 +00:00
|
|
|
ctxData := authz.GetCtxData(ctx)
|
2022-05-25 12:07:16 +00:00
|
|
|
userIDQuery, err := query.NewMembershipUserIDQuery(ctxData.UserID)
|
2022-01-26 09:16:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:29 +00:00
|
|
|
orgIDsQuery, err := query.NewMembershipResourceOwnersSearchQuery(orgID, authz.GetInstance(ctx).InstanceID())
|
2022-01-26 09:16:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-04-25 07:02:29 +00:00
|
|
|
grantedIDQuery, err := query.NewMembershipGrantedOrgIDSearchQuery(orgID)
|
2022-07-27 07:55:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-05-25 12:07:16 +00:00
|
|
|
memberships, err := repo.Queries.Memberships(ctx, &query.MembershipSearchQuery{
|
2022-07-27 07:55:44 +00:00
|
|
|
Queries: []query.SearchQuery{userIDQuery, query.Or(orgIDsQuery, grantedIDQuery)},
|
2023-10-19 10:19:10 +00:00
|
|
|
}, false, shouldTriggerBulk)
|
2022-05-25 12:07:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-01-26 09:16:33 +00:00
|
|
|
}
|
2022-05-25 12:07:16 +00:00
|
|
|
return memberships.Memberships, nil
|
2022-01-26 09:16:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-25 12:07:16 +00:00
|
|
|
func userMembershipToMembership(membership *query.Membership) *authz.Membership {
|
|
|
|
if membership.IAM != nil {
|
|
|
|
return &authz.Membership{
|
2023-10-25 15:10:45 +00:00
|
|
|
MemberType: authz.MemberTypeIAM,
|
2022-05-25 12:07:16 +00:00
|
|
|
AggregateID: membership.IAM.IAMID,
|
|
|
|
ObjectID: membership.IAM.IAMID,
|
|
|
|
Roles: membership.Roles,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if membership.Org != nil {
|
|
|
|
return &authz.Membership{
|
2023-10-25 15:10:45 +00:00
|
|
|
MemberType: authz.MemberTypeOrganization,
|
2022-05-25 12:07:16 +00:00
|
|
|
AggregateID: membership.Org.OrgID,
|
|
|
|
ObjectID: membership.Org.OrgID,
|
|
|
|
Roles: membership.Roles,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if membership.Project != nil {
|
|
|
|
return &authz.Membership{
|
|
|
|
MemberType: authz.MemberTypeProject,
|
|
|
|
AggregateID: membership.Project.ProjectID,
|
|
|
|
ObjectID: membership.Project.ProjectID,
|
|
|
|
Roles: membership.Roles,
|
|
|
|
}
|
|
|
|
}
|
2022-01-26 09:16:33 +00:00
|
|
|
return &authz.Membership{
|
2022-05-25 12:07:16 +00:00
|
|
|
MemberType: authz.MemberTypeProjectGrant,
|
|
|
|
AggregateID: membership.ProjectGrant.ProjectID,
|
|
|
|
ObjectID: membership.ProjectGrant.GrantID,
|
2022-01-26 09:16:33 +00:00
|
|
|
Roles: membership.Roles,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-25 12:07:16 +00:00
|
|
|
func userMembershipsToMemberships(memberships []*query.Membership) []*authz.Membership {
|
2022-01-26 09:16:33 +00:00
|
|
|
result := make([]*authz.Membership, len(memberships))
|
|
|
|
for i, m := range memberships {
|
|
|
|
result[i] = userMembershipToMembership(m)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|