mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-12 19:14:23 +00:00
fa9f581d56
* chore: move to new org * logging * fix: org rename caos -> zitadel Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
48 lines
1.3 KiB
Go
48 lines
1.3 KiB
Go
package query
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
)
|
|
|
|
func (q *Queries) MyZitadelPermissions(ctx context.Context, orgID, userID string) (*domain.Permissions, error) {
|
|
userIDQuery, err := NewMembershipUserIDQuery(userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
orgIDsQuery, err := NewMembershipResourceOwnersSearchQuery(orgID, authz.GetInstance(ctx).InstanceID())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
memberships, err := q.Memberships(ctx, &MembershipSearchQuery{
|
|
Queries: []SearchQuery{userIDQuery, orgIDsQuery},
|
|
})
|
|
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
|
|
}
|