2022-01-26 10:16:33 +01:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-12-01 09:18:53 +01:00
|
|
|
"github.com/zitadel/zitadel/internal/telemetry/tracing"
|
2022-01-26 10:16:33 +01:00
|
|
|
)
|
|
|
|
|
2022-12-01 09:18:53 +01:00
|
|
|
func (q *Queries) MyZitadelPermissions(ctx context.Context, orgID, userID string) (_ *domain.Permissions, err error) {
|
|
|
|
ctx, span := tracing.NewSpan(ctx)
|
|
|
|
defer func() { span.EndWithError(err) }()
|
|
|
|
|
2022-01-26 10:16:33 +01:00
|
|
|
userIDQuery, err := NewMembershipUserIDQuery(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-05 07:58:09 +02:00
|
|
|
orgIDsQuery, err := NewMembershipResourceOwnersSearchQuery(orgID, authz.GetInstance(ctx).InstanceID())
|
2022-03-24 14:00:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-27 09:55:44 +02:00
|
|
|
grantedOrgIDQuery, err := NewMembershipGrantedOrgIDSearchQuery(orgID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 10:16:33 +01:00
|
|
|
memberships, err := q.Memberships(ctx, &MembershipSearchQuery{
|
2022-07-27 09:55:44 +02:00
|
|
|
Queries: []SearchQuery{userIDQuery, Or(orgIDsQuery, grantedOrgIDQuery)},
|
2023-11-20 16:21:08 +01:00
|
|
|
}, false)
|
2022-01-26 10:16:33 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
permissions := &domain.Permissions{Permissions: []string{}}
|
|
|
|
for _, membership := range memberships.Memberships {
|
|
|
|
for _, role := range membership.Roles {
|
|
|
|
permissions = q.mapRoleToPermission(permissions, membership, role)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) mapRoleToPermission(permissions *domain.Permissions, membership *Membership, role string) *domain.Permissions {
|
|
|
|
for _, mapping := range q.zitadelRoles {
|
|
|
|
if mapping.Role == role {
|
|
|
|
ctxID := ""
|
|
|
|
if membership.Project != nil {
|
|
|
|
ctxID = membership.Project.ProjectID
|
|
|
|
} else if membership.ProjectGrant != nil {
|
|
|
|
ctxID = membership.ProjectGrant.GrantID
|
|
|
|
}
|
|
|
|
permissions.AppendPermissions(ctxID, mapping.Permissions...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions
|
|
|
|
}
|