fix: add default organization as query for org v2 list (#8719)

# Which Problems Are Solved

To get the default organization we have no direct solution.

# How the Problems Are Solved

Add default organization as query to the org v2 List.

# Additional Changes

None

# Additional Context

Closes #8616

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
Stefan Benz 2024-10-07 11:28:39 +02:00 committed by GitHub
parent f653589609
commit d03ad62edd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 55 additions and 7 deletions

View File

@ -42,6 +42,8 @@ func TestMain(m *testing.M) {
} }
func TestServer_AddOrganization(t *testing.T) { func TestServer_AddOrganization(t *testing.T) {
t.Parallel()
idpResp := Instance.AddGenericOAuthProvider(CTX, Instance.DefaultOrg.Id) idpResp := Instance.AddGenericOAuthProvider(CTX, Instance.DefaultOrg.Id)
tests := []struct { tests := []struct {

View File

@ -26,6 +26,8 @@ type orgAttr struct {
} }
func TestServer_ListOrganizations(t *testing.T) { func TestServer_ListOrganizations(t *testing.T) {
t.Parallel()
type args struct { type args struct {
ctx context.Context ctx context.Context
req *org.ListOrganizationsRequest req *org.ListOrganizationsRequest
@ -37,6 +39,38 @@ func TestServer_ListOrganizations(t *testing.T) {
want *org.ListOrganizationsResponse want *org.ListOrganizationsResponse
wantErr bool wantErr bool
}{ }{
{
name: "list org by default, ok",
args: args{
CTX,
&org.ListOrganizationsRequest{
Queries: []*org.SearchQuery{
DefaultOrganizationQuery(),
},
},
nil,
},
want: &org.ListOrganizationsResponse{
Details: &object.ListDetails{
TotalResult: 1,
Timestamp: timestamppb.Now(),
},
SortingColumn: 0,
Result: []*org.Organization{
{
Id: Instance.DefaultOrg.Id,
Name: Instance.DefaultOrg.Name,
PrimaryDomain: Instance.DefaultOrg.PrimaryDomain,
State: org.OrganizationState_ORGANIZATION_STATE_ACTIVE,
Details: &object.Details{
Sequence: Instance.DefaultOrg.Details.Sequence,
ChangeDate: Instance.DefaultOrg.Details.ChangeDate,
ResourceOwner: Instance.DefaultOrg.Details.ResourceOwner,
},
},
},
},
},
{ {
name: "list org by id, ok, multiple", name: "list org by id, ok, multiple",
args: args{ args: args{
@ -401,6 +435,12 @@ func TestServer_ListOrganizations(t *testing.T) {
} }
} }
func DefaultOrganizationQuery() *org.SearchQuery {
return &org.SearchQuery{Query: &org.SearchQuery_DefaultQuery{
DefaultQuery: &org.DefaultOrganizationQuery{},
}}
}
func OrganizationIdQuery(resourceowner string) *org.SearchQuery { func OrganizationIdQuery(resourceowner string) *org.SearchQuery {
return &org.SearchQuery{Query: &org.SearchQuery_IdQuery{ return &org.SearchQuery{Query: &org.SearchQuery_IdQuery{
IdQuery: &org.OrganizationIDQuery{ IdQuery: &org.OrganizationIDQuery{

View File

@ -3,6 +3,7 @@ package org
import ( import (
"context" "context"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/api/grpc/object/v2" "github.com/zitadel/zitadel/internal/api/grpc/object/v2"
"github.com/zitadel/zitadel/internal/domain" "github.com/zitadel/zitadel/internal/domain"
"github.com/zitadel/zitadel/internal/query" "github.com/zitadel/zitadel/internal/query"
@ -11,7 +12,7 @@ import (
) )
func (s *Server) ListOrganizations(ctx context.Context, req *org.ListOrganizationsRequest) (*org.ListOrganizationsResponse, error) { func (s *Server) ListOrganizations(ctx context.Context, req *org.ListOrganizationsRequest) (*org.ListOrganizationsResponse, error) {
queries, err := listOrgRequestToModel(req) queries, err := listOrgRequestToModel(ctx, req)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -25,9 +26,9 @@ func (s *Server) ListOrganizations(ctx context.Context, req *org.ListOrganizatio
}, nil }, nil
} }
func listOrgRequestToModel(req *org.ListOrganizationsRequest) (*query.OrgSearchQueries, error) { func listOrgRequestToModel(ctx context.Context, req *org.ListOrganizationsRequest) (*query.OrgSearchQueries, error) {
offset, limit, asc := object.ListQueryToQuery(req.Query) offset, limit, asc := object.ListQueryToQuery(req.Query)
queries, err := orgQueriesToQuery(req.Queries) queries, err := orgQueriesToQuery(ctx, req.Queries)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -42,10 +43,10 @@ func listOrgRequestToModel(req *org.ListOrganizationsRequest) (*query.OrgSearchQ
}, nil }, nil
} }
func orgQueriesToQuery(queries []*org.SearchQuery) (_ []query.SearchQuery, err error) { func orgQueriesToQuery(ctx context.Context, queries []*org.SearchQuery) (_ []query.SearchQuery, err error) {
q := make([]query.SearchQuery, len(queries)) q := make([]query.SearchQuery, len(queries))
for i, query := range queries { for i, query := range queries {
q[i], err = orgQueryToQuery(query) q[i], err = orgQueryToQuery(ctx, query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -53,7 +54,7 @@ func orgQueriesToQuery(queries []*org.SearchQuery) (_ []query.SearchQuery, err e
return q, nil return q, nil
} }
func orgQueryToQuery(orgQuery *org.SearchQuery) (query.SearchQuery, error) { func orgQueryToQuery(ctx context.Context, orgQuery *org.SearchQuery) (query.SearchQuery, error) {
switch q := orgQuery.Query.(type) { switch q := orgQuery.Query.(type) {
case *org.SearchQuery_DomainQuery: case *org.SearchQuery_DomainQuery:
return query.NewOrgDomainSearchQuery(object.TextMethodToQuery(q.DomainQuery.Method), q.DomainQuery.Domain) return query.NewOrgDomainSearchQuery(object.TextMethodToQuery(q.DomainQuery.Method), q.DomainQuery.Domain)
@ -63,6 +64,8 @@ func orgQueryToQuery(orgQuery *org.SearchQuery) (query.SearchQuery, error) {
return query.NewOrgStateSearchQuery(orgStateToDomain(q.StateQuery.State)) return query.NewOrgStateSearchQuery(orgStateToDomain(q.StateQuery.State))
case *org.SearchQuery_IdQuery: case *org.SearchQuery_IdQuery:
return query.NewOrgIDSearchQuery(q.IdQuery.Id) return query.NewOrgIDSearchQuery(q.IdQuery.Id)
case *org.SearchQuery_DefaultQuery:
return query.NewOrgIDSearchQuery(authz.GetInstance(ctx).DefaultOrganisationID())
default: default:
return nil, zerrors.ThrowInvalidArgument(nil, "ORG-vR9nC", "List.Query.Invalid") return nil, zerrors.ThrowInvalidArgument(nil, "ORG-vR9nC", "List.Query.Invalid")
} }

View File

@ -20,6 +20,7 @@ message SearchQuery {
OrganizationDomainQuery domain_query = 2; OrganizationDomainQuery domain_query = 2;
OrganizationStateQuery state_query = 3; OrganizationStateQuery state_query = 3;
OrganizationIDQuery id_query = 4; OrganizationIDQuery id_query = 4;
DefaultOrganizationQuery default_query = 5;
} }
} }
@ -81,3 +82,5 @@ enum OrganizationFieldName {
ORGANIZATION_FIELD_NAME_UNSPECIFIED = 0; ORGANIZATION_FIELD_NAME_UNSPECIFIED = 0;
ORGANIZATION_FIELD_NAME_NAME = 1; ORGANIZATION_FIELD_NAME_NAME = 1;
} }
message DefaultOrganizationQuery {}