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