Skip to content

Commit

Permalink
Alloe calls to GetOrganization if the org has public projects
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller committed Nov 26, 2024
1 parent c97d6d1 commit 1d51e8c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 25 deletions.
16 changes: 8 additions & 8 deletions admin/server/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ func (s *Server) GetBillingSubscription(ctx context.Context, req *adminv1.GetBil
}

if org.BillingCustomerID == "" {
return &adminv1.GetBillingSubscriptionResponse{Organization: organizationToDTO(org)}, nil
return &adminv1.GetBillingSubscriptionResponse{Organization: organizationToDTO(org, true)}, nil
}

sub, err := s.admin.Biller.GetActiveSubscription(ctx, org.BillingCustomerID)
if err != nil {
if errors.Is(err, billing.ErrNotFound) {
return &adminv1.GetBillingSubscriptionResponse{Organization: organizationToDTO(org)}, nil
return &adminv1.GetBillingSubscriptionResponse{Organization: organizationToDTO(org, true)}, nil
}
return nil, status.Error(codes.Internal, err.Error())
}

return &adminv1.GetBillingSubscriptionResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
Subscription: subscriptionToDTO(sub),
BillingPortalUrl: sub.Customer.PortalURL,
}, nil
Expand Down Expand Up @@ -135,7 +135,7 @@ func (s *Server) UpdateBillingSubscription(ctx context.Context, req *adminv1.Upd
}

return &adminv1.UpdateBillingSubscriptionResponse{
Organization: organizationToDTO(updatedOrg),
Organization: organizationToDTO(updatedOrg, true),
Subscription: subscriptionToDTO(sub),
}, nil
}
Expand Down Expand Up @@ -220,7 +220,7 @@ func (s *Server) UpdateBillingSubscription(ctx context.Context, req *adminv1.Upd
}

return &adminv1.UpdateBillingSubscriptionResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
Subscription: subscriptionToDTO(sub),
}, nil
}
Expand Down Expand Up @@ -419,7 +419,7 @@ func (s *Server) RenewBillingSubscription(ctx context.Context, req *adminv1.Rene
}

return &adminv1.RenewBillingSubscriptionResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
Subscription: subscriptionToDTO(sub),
}, nil
}
Expand Down Expand Up @@ -563,12 +563,12 @@ func (s *Server) SudoUpdateOrganizationBillingCustomer(ctx context.Context, req

if sub == nil {
return &adminv1.SudoUpdateOrganizationBillingCustomerResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
}, nil
}

return &adminv1.SudoUpdateOrganizationBillingCustomerResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
Subscription: subscriptionToDTO(sub),
}, nil
}
Expand Down
47 changes: 31 additions & 16 deletions admin/server/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (s *Server) ListOrganizations(ctx context.Context, req *adminv1.ListOrganiz

pbs := make([]*adminv1.Organization, len(orgs))
for i, org := range orgs {
pbs[i] = organizationToDTO(org)
pbs[i] = organizationToDTO(org, false)
}

return &adminv1.ListOrganizationsResponse{Organizations: pbs, NextPageToken: nextToken}, nil
Expand All @@ -60,13 +60,23 @@ func (s *Server) GetOrganization(ctx context.Context, req *adminv1.GetOrganizati
}

claims := auth.GetClaims(ctx)
if !claims.OrganizationPermissions(ctx, org.ID).ReadOrg && !claims.Superuser(ctx) {
return nil, status.Error(codes.PermissionDenied, "not allowed to read org")
perms := claims.OrganizationPermissions(ctx, org.ID)
if !perms.ReadOrg && !claims.Superuser(ctx) {
ok, err := s.admin.DB.CheckOrganizationHasPublicProjects(ctx, org.ID)
if err != nil {
return nil, err
}
if !ok {
return nil, status.Error(codes.PermissionDenied, "not allowed to read org")
}

perms.ReadOrg = true
perms.ReadProjects = true
}

return &adminv1.GetOrganizationResponse{
Organization: organizationToDTO(org),
Permissions: claims.OrganizationPermissions(ctx, org.ID),
Organization: organizationToDTO(org, perms.ManageOrg),
Permissions: perms,
}, nil
}

Expand Down Expand Up @@ -119,7 +129,7 @@ func (s *Server) CreateOrganization(ctx context.Context, req *adminv1.CreateOrga
}

return &adminv1.CreateOrganizationResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
}, nil
}

Expand Down Expand Up @@ -211,7 +221,7 @@ func (s *Server) UpdateOrganization(ctx context.Context, req *adminv1.UpdateOrga
}

return &adminv1.UpdateOrganizationResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
}, nil
}

Expand Down Expand Up @@ -873,7 +883,7 @@ func (s *Server) SudoUpdateOrganizationQuotas(ctx context.Context, req *adminv1.
}

return &adminv1.SudoUpdateOrganizationQuotasResponse{
Organization: organizationToDTO(updatedOrg),
Organization: organizationToDTO(updatedOrg, true),
}, nil
}

Expand Down Expand Up @@ -914,12 +924,12 @@ func (s *Server) SudoUpdateOrganizationCustomDomain(ctx context.Context, req *ad
}

return &adminv1.SudoUpdateOrganizationCustomDomainResponse{
Organization: organizationToDTO(org),
Organization: organizationToDTO(org, true),
}, nil
}

func organizationToDTO(o *database.Organization) *adminv1.Organization {
return &adminv1.Organization{
func organizationToDTO(o *database.Organization, privileged bool) *adminv1.Organization {
res := &adminv1.Organization{
Id: o.ID,
Name: o.Name,
DisplayName: o.DisplayName,
Expand All @@ -933,12 +943,17 @@ func organizationToDTO(o *database.Organization) *adminv1.Organization {
OutstandingInvites: int32(o.QuotaOutstandingInvites),
StorageLimitBytesPerDeployment: o.QuotaStorageLimitBytesPerDeployment,
},
BillingCustomerId: o.BillingCustomerID,
PaymentCustomerId: o.PaymentCustomerID,
BillingEmail: o.BillingEmail,
CreatedOn: timestamppb.New(o.CreatedOn),
UpdatedOn: timestamppb.New(o.UpdatedOn),
CreatedOn: timestamppb.New(o.CreatedOn),
UpdatedOn: timestamppb.New(o.UpdatedOn),
}

if privileged {
res.BillingCustomerId = o.BillingCustomerID
res.PaymentCustomerId = o.PaymentCustomerID
res.BillingEmail = o.BillingEmail
}

return res
}

func valOrEmptyString(v *int) string {
Expand Down
2 changes: 1 addition & 1 deletion admin/server/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (s *Server) SudoGetResource(ctx context.Context, req *adminv1.SudoGetResour
if err != nil {
return nil, err
}
res.Resource = &adminv1.SudoGetResourceResponse_Org{Org: organizationToDTO(org)}
res.Resource = &adminv1.SudoGetResourceResponse_Org{Org: organizationToDTO(org, true)}
case *adminv1.SudoGetResourceRequest_ProjectId:
proj, err := s.admin.DB.FindProject(ctx, id.ProjectId)
if err != nil {
Expand Down

0 comments on commit 1d51e8c

Please sign in to comment.