- More robust front authentification check to display the right button - Remove trust relay to avoid query loop Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
242 lines
8.6 KiB
Go
242 lines
8.6 KiB
Go
package trust_v1
|
|
|
|
// This file will be automatically regenerated based on the schema, any resolver implementations
|
|
// will be copied through when generating and any unknown code will be moved to the end.
|
|
// Code generated by github.com/99designs/gqlgen version v0.17.76
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/getprobo/probo/pkg/coredata"
|
|
"github.com/getprobo/probo/pkg/gid"
|
|
"github.com/getprobo/probo/pkg/page"
|
|
"github.com/getprobo/probo/pkg/server/api/trust/v1/auth"
|
|
"github.com/getprobo/probo/pkg/server/api/trust/v1/schema"
|
|
"github.com/getprobo/probo/pkg/server/api/trust/v1/types"
|
|
"github.com/getprobo/probo/pkg/trust"
|
|
)
|
|
|
|
// Framework is the resolver for the framework field.
|
|
func (r *auditResolver) Framework(ctx context.Context, obj *types.Audit) (*types.Framework, error) {
|
|
trust := r.TrustService(ctx, obj.ID.TenantID())
|
|
|
|
audit, err := trust.Audits.Get(ctx, obj.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot load audit: %w", err)
|
|
}
|
|
|
|
framework, err := trust.Frameworks.Get(ctx, audit.FrameworkID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot load framework: %w", err)
|
|
}
|
|
|
|
return types.NewFramework(framework), nil
|
|
}
|
|
|
|
// Report is the resolver for the report field.
|
|
func (r *auditResolver) Report(ctx context.Context, obj *types.Audit) (*types.Report, error) {
|
|
trust := r.TrustService(ctx, obj.ID.TenantID())
|
|
|
|
audit, err := trust.Audits.Get(ctx, obj.ID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot load audit: %w", err)
|
|
}
|
|
|
|
if audit.ReportID == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
report, err := trust.Reports.Get(ctx, *audit.ReportID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot load report: %w", err)
|
|
}
|
|
|
|
return types.NewReport(report), nil
|
|
}
|
|
|
|
// CreateTrustCenterAccess is the resolver for the createTrustCenterAccess field.
|
|
func (r *mutationResolver) CreateTrustCenterAccess(ctx context.Context, input types.CreateTrustCenterAccessInput) (*types.CreateTrustCenterAccessPayload, error) {
|
|
trustSvc := r.trustCenterSvc.WithTenant(input.TrustCenterID.TenantID())
|
|
|
|
access, err := trustSvc.TrustCenterAccesses.Create(ctx, &trust.CreateTrustCenterAccessRequest{
|
|
TrustCenterID: input.TrustCenterID,
|
|
Email: input.Email,
|
|
Name: input.Name,
|
|
})
|
|
if err != nil {
|
|
panic(fmt.Errorf("cannot create trust center access: %w", err))
|
|
}
|
|
|
|
return &types.CreateTrustCenterAccessPayload{
|
|
TrustCenterAccess: &types.TrustCenterAccess{
|
|
ID: access.ID,
|
|
Email: access.Email,
|
|
Name: access.Name,
|
|
CreatedAt: access.CreatedAt,
|
|
UpdatedAt: access.UpdatedAt,
|
|
},
|
|
}, nil
|
|
}
|
|
|
|
// ExportDocumentPDF is the resolver for the exportDocumentPDF field.
|
|
func (r *mutationResolver) ExportDocumentPDF(ctx context.Context, input types.ExportDocumentPDFInput) (*types.ExportDocumentPDFPayload, error) {
|
|
if err := auth.ValidateTenantAccess(ctx, r, userTenantContextKey, input.DocumentID.TenantID()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
trust := r.trustCenterSvc.WithTenant(input.DocumentID.TenantID())
|
|
|
|
pdf, err := trust.Documents.ExportPDF(ctx, input.DocumentID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot export document PDF: %w", err)
|
|
}
|
|
|
|
return &types.ExportDocumentPDFPayload{
|
|
Data: fmt.Sprintf("data:application/pdf;base64,%s", base64.StdEncoding.EncodeToString(pdf)),
|
|
}, nil
|
|
}
|
|
|
|
// LogoURL is the resolver for the logoUrl field.
|
|
func (r *organizationResolver) LogoURL(ctx context.Context, obj *types.Organization) (*string, error) {
|
|
trust := r.TrustService(ctx, obj.ID.TenantID())
|
|
|
|
return trust.Organizations.GenerateLogoURL(ctx, obj.ID, 1*time.Hour)
|
|
}
|
|
|
|
// TrustCenterBySlug is the resolver for the trustCenterBySlug field.
|
|
func (r *queryResolver) TrustCenterBySlug(ctx context.Context, slug string) (*types.TrustCenter, error) {
|
|
publicTrust := r.trustCenterSvc.WithTenant(gid.NewTenantID())
|
|
|
|
trustCenter, err := publicTrust.TrustCenters.GetBySlug(ctx, slug)
|
|
if err != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
if !trustCenter.Active {
|
|
return nil, nil
|
|
}
|
|
|
|
result := types.NewTrustCenter(trustCenter)
|
|
|
|
orgTrust := r.trustCenterSvc.WithTenant(trustCenter.TenantID)
|
|
org, err := orgTrust.Organizations.Get(ctx, trustCenter.OrganizationID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot get organization: %w", err)
|
|
}
|
|
|
|
result.Organization = types.NewOrganization(org)
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// DownloadURL is the resolver for the downloadUrl field.
|
|
func (r *reportResolver) DownloadURL(ctx context.Context, obj *types.Report) (*string, error) {
|
|
if err := auth.ValidateTenantAccess(ctx, r, userTenantContextKey, obj.ID.TenantID()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
trust := r.TrustService(ctx, obj.ID.TenantID())
|
|
|
|
url, err := trust.Reports.GenerateDownloadURL(ctx, obj.ID, r.trustAuthCfg.ReportURLDuration)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot generate download URL: %w", err)
|
|
}
|
|
|
|
return url, nil
|
|
}
|
|
|
|
// Organization is the resolver for the organization field.
|
|
func (r *trustCenterResolver) Organization(ctx context.Context, obj *types.TrustCenter) (*types.Organization, error) {
|
|
return obj.Organization, nil
|
|
}
|
|
|
|
// IsUserAuthenticated is the resolver for the isUserAuthenticated field.
|
|
func (r *trustCenterResolver) IsUserAuthenticated(ctx context.Context, obj *types.TrustCenter) (bool, error) {
|
|
if err := auth.ValidateTenantAccess(ctx, r, userTenantContextKey, obj.Organization.ID.TenantID()); err != nil {
|
|
return false, nil
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// Documents is the resolver for the documents field.
|
|
func (r *trustCenterResolver) Documents(ctx context.Context, obj *types.TrustCenter, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.DocumentConnection, error) {
|
|
trust := r.trustCenterSvc.WithTenant(obj.Organization.ID.TenantID())
|
|
|
|
pageOrderBy := page.OrderBy[coredata.DocumentOrderField]{
|
|
Field: coredata.DocumentOrderFieldTitle,
|
|
Direction: page.OrderDirectionAsc,
|
|
}
|
|
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
|
|
|
documentPage, err := trust.Documents.ListForOrganizationId(ctx, obj.Organization.ID, cursor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot list public documents: %w", err)
|
|
}
|
|
|
|
return types.NewDocumentConnection(documentPage), nil
|
|
}
|
|
|
|
// Audits is the resolver for the audits field.
|
|
func (r *trustCenterResolver) Audits(ctx context.Context, obj *types.TrustCenter, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.AuditConnection, error) {
|
|
trust := r.trustCenterSvc.WithTenant(obj.Organization.ID.TenantID())
|
|
|
|
pageOrderBy := page.OrderBy[coredata.AuditOrderField]{
|
|
Field: coredata.AuditOrderFieldValidFrom,
|
|
Direction: page.OrderDirectionDesc,
|
|
}
|
|
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
|
|
|
auditPage, err := trust.Audits.ListForOrganizationId(ctx, obj.Organization.ID, cursor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot list public audits: %w", err)
|
|
}
|
|
|
|
return types.NewAuditConnection(auditPage), nil
|
|
}
|
|
|
|
// Vendors is the resolver for the vendors field.
|
|
func (r *trustCenterResolver) Vendors(ctx context.Context, obj *types.TrustCenter, first *int, after *page.CursorKey, last *int, before *page.CursorKey) (*types.VendorConnection, error) {
|
|
trust := r.trustCenterSvc.WithTenant(obj.Organization.ID.TenantID())
|
|
|
|
pageOrderBy := page.OrderBy[coredata.VendorOrderField]{
|
|
Field: coredata.VendorOrderFieldName,
|
|
Direction: page.OrderDirectionAsc,
|
|
}
|
|
cursor := types.NewCursor(first, after, last, before, pageOrderBy)
|
|
|
|
vendorPage, err := trust.Vendors.ListForOrganizationId(ctx, obj.Organization.ID, cursor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot list public vendors: %w", err)
|
|
}
|
|
|
|
return types.NewVendorConnection(vendorPage), nil
|
|
}
|
|
|
|
// Audit returns schema.AuditResolver implementation.
|
|
func (r *Resolver) Audit() schema.AuditResolver { return &auditResolver{r} }
|
|
|
|
// Mutation returns schema.MutationResolver implementation.
|
|
func (r *Resolver) Mutation() schema.MutationResolver { return &mutationResolver{r} }
|
|
|
|
// Organization returns schema.OrganizationResolver implementation.
|
|
func (r *Resolver) Organization() schema.OrganizationResolver { return &organizationResolver{r} }
|
|
|
|
// Query returns schema.QueryResolver implementation.
|
|
func (r *Resolver) Query() schema.QueryResolver { return &queryResolver{r} }
|
|
|
|
// Report returns schema.ReportResolver implementation.
|
|
func (r *Resolver) Report() schema.ReportResolver { return &reportResolver{r} }
|
|
|
|
// TrustCenter returns schema.TrustCenterResolver implementation.
|
|
func (r *Resolver) TrustCenter() schema.TrustCenterResolver { return &trustCenterResolver{r} }
|
|
|
|
type auditResolver struct{ *Resolver }
|
|
type mutationResolver struct{ *Resolver }
|
|
type organizationResolver struct{ *Resolver }
|
|
type queryResolver struct{ *Resolver }
|
|
type reportResolver struct{ *Resolver }
|
|
type trustCenterResolver struct{ *Resolver }
|