Extract complianceportal package from trust and probo
Split the compliance portal into an admin-facing management side and a public-facing visitor side under pkg/complianceportal. Trust-center CRUD, domains, custom links, frameworks, files and accesses move out of pkg/probo, and the visitor read logic moves out of pkg/trust. IAM actions migrate onto compliance-portal scopes. Signed-off-by: Bryan Frimin <bryan@probo.com>
This commit is contained in:
111
pkg/complianceportal/visitor/audit_service.go
Normal file
111
pkg/complianceportal/visitor/audit_service.go
Normal file
@@ -0,0 +1,111 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) GetAudit(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
auditID gid.GID,
|
||||
) (*coredata.Audit, error) {
|
||||
audit := &coredata.Audit{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := audit.LoadByID(ctx, conn, scope, auditID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load audit: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return audit, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetAuditByReportFileID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
fileID gid.GID,
|
||||
) (*coredata.Audit, error) {
|
||||
audit := &coredata.Audit{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := audit.LoadByReportFileID(ctx, conn, scope, fileID); err != nil {
|
||||
return fmt.Errorf("cannot load audit: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return audit, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListAuditsForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.AuditOrderField],
|
||||
filter *coredata.AuditFilter,
|
||||
) (*page.Page[*coredata.Audit, coredata.AuditOrderField], error) {
|
||||
var audits coredata.Audits
|
||||
|
||||
if filter == nil {
|
||||
filter = coredata.NewAuditTrustCenterFilter()
|
||||
}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := audits.LoadByOrganizationID(ctx, conn, scope, organizationID, cursor, filter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load audits: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(audits, cursor), nil
|
||||
}
|
||||
40
pkg/complianceportal/visitor/compliance.md.tmpl
Normal file
40
pkg/complianceportal/visitor/compliance.md.tmpl
Normal file
@@ -0,0 +1,40 @@
|
||||
# {{ .OrgName }} — Compliance
|
||||
{{ if .Description }}
|
||||
{{ .Description }}
|
||||
{{ end }}{{ if .Details }}
|
||||
{{ range .Details }}- **{{ .Label }}**: {{ .Value }}
|
||||
{{ end }}{{ end }}{{ if .Frameworks }}
|
||||
## Compliance Frameworks
|
||||
|
||||
{{ range .Frameworks }}- {{ .Name }}{{ if .Description }}: {{ .Description }}{{ end }}
|
||||
{{ end }}{{ end }}{{ if .Documents }}
|
||||
## Documents
|
||||
|
||||
| Title | Type |
|
||||
|-------|------|
|
||||
{{ range .Documents }}| {{ cell .Title }} | {{ cell .Type }} |
|
||||
{{ end }}{{ end }}{{ if .Audits }}
|
||||
## Audits
|
||||
|
||||
| Name | Framework | Valid From | Valid Until |
|
||||
|------|-----------|------------|-------------|
|
||||
{{ range .Audits }}| {{ cell .Name }} | {{ cell .Framework }} | {{ .ValidFrom }} | {{ .ValidUntil }} |
|
||||
{{ end }}{{ end }}{{ if .Vendors }}
|
||||
## Subprocessors
|
||||
|
||||
| Name | Category | Countries | Website |
|
||||
|------|----------|-----------|---------|
|
||||
{{ range .Vendors }}| {{ cell .Name }} | {{ cell .Category }} | {{ cell .Countries }} | {{ cell .Website }} |
|
||||
{{ end }}{{ end }}{{ if .References }}
|
||||
## References
|
||||
|
||||
| Name | Description | Website |
|
||||
|------|-------------|---------|
|
||||
{{ range .References }}| {{ cell .Name }} | {{ cell .Description }} | {{ cell .Website }} |
|
||||
{{ end }}{{ end }}{{ if .CustomLinks }}
|
||||
## External Links
|
||||
|
||||
| Name | URL |
|
||||
|------|-----|
|
||||
{{ range .CustomLinks }}| {{ cell .Name }} | {{ cell .URL }} |
|
||||
{{ end }}{{ end }}
|
||||
57
pkg/complianceportal/visitor/compliance_framework_service.go
Normal file
57
pkg/complianceportal/visitor/compliance_framework_service.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) ListComplianceFrameworksByPortalID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
cursor *page.Cursor[coredata.ComplianceFrameworkOrderField],
|
||||
) (*page.Page[*coredata.ComplianceFramework, coredata.ComplianceFrameworkOrderField], error) {
|
||||
var complianceFrameworks coredata.ComplianceFrameworks
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := complianceFrameworks.LoadByTrustCenterID(ctx, conn, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load compliance frameworks: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(complianceFrameworks, cursor), nil
|
||||
}
|
||||
713
pkg/complianceportal/visitor/compliance_page_service.go
Normal file
713
pkg/complianceportal/visitor/compliance_page_service.go
Normal file
@@ -0,0 +1,713 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"go.gearno.de/x/ref"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
//go:embed compliance.md.tmpl
|
||||
var complianceTmplContent string
|
||||
|
||||
//go:embed sitemap.xml.tmpl
|
||||
var sitemapTmplContent string
|
||||
|
||||
//go:embed robots.txt.tmpl
|
||||
var robotsTmplContent string
|
||||
|
||||
var complianceTmpl = template.Must(
|
||||
template.New("compliance").
|
||||
Funcs(template.FuncMap{
|
||||
"cell": func(s string) string {
|
||||
s = strings.ReplaceAll(s, `|`, `\|`)
|
||||
s = strings.ReplaceAll(s, "\n", " ")
|
||||
s = strings.ReplaceAll(s, "\r", "")
|
||||
|
||||
return s
|
||||
},
|
||||
}).
|
||||
Parse(complianceTmplContent),
|
||||
)
|
||||
|
||||
var sitemapTmpl = template.Must(
|
||||
template.New("sitemap").Parse(sitemapTmplContent),
|
||||
)
|
||||
|
||||
var robotsTmpl = template.Must(
|
||||
template.New("robots").Parse(robotsTmplContent),
|
||||
)
|
||||
|
||||
type (
|
||||
compliancePageData struct {
|
||||
OrgName string
|
||||
Description string
|
||||
Details []compliancePageDetail
|
||||
Frameworks []compliancePageFramework
|
||||
Documents []compliancePageDocument
|
||||
Audits []compliancePageAudit
|
||||
ThirdParties []compliancePageThirdParty
|
||||
References []compliancePageReference
|
||||
CustomLinks []compliancePageCustomLink
|
||||
}
|
||||
|
||||
compliancePageDetail struct {
|
||||
Label string
|
||||
Value string
|
||||
}
|
||||
|
||||
compliancePageFramework struct {
|
||||
Name string
|
||||
Description string
|
||||
}
|
||||
|
||||
compliancePageDocument struct {
|
||||
Title string
|
||||
Type string
|
||||
}
|
||||
|
||||
compliancePageAudit struct {
|
||||
Name string
|
||||
Framework string
|
||||
ValidFrom string
|
||||
ValidUntil string
|
||||
}
|
||||
|
||||
compliancePageThirdParty struct {
|
||||
Name string
|
||||
Category string
|
||||
Countries string
|
||||
Website string
|
||||
}
|
||||
|
||||
compliancePageReference struct {
|
||||
Name string
|
||||
Description string
|
||||
Website string
|
||||
}
|
||||
|
||||
compliancePageCustomLink struct {
|
||||
Name string
|
||||
URL string
|
||||
}
|
||||
)
|
||||
|
||||
func (s *Service) RenderCompliancePageMarkdown(
|
||||
ctx context.Context,
|
||||
w io.Writer,
|
||||
trustCenterID gid.GID,
|
||||
scope coredata.Scoper,
|
||||
) error {
|
||||
org, err := s.GetPortalOrganization(ctx, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load organization for compliance page: %w", err)
|
||||
}
|
||||
|
||||
trustCenter, err := s.GetPortalByID(ctx, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center for compliance page: %w", err)
|
||||
}
|
||||
|
||||
data := &compliancePageData{
|
||||
OrgName: org.Name,
|
||||
}
|
||||
|
||||
if trustCenter.Description != nil && *trustCenter.Description != "" {
|
||||
data.Description = *trustCenter.Description
|
||||
}
|
||||
|
||||
if trustCenter.WebsiteURL != nil && *trustCenter.WebsiteURL != "" {
|
||||
data.Details = append(data.Details, compliancePageDetail{Label: "Website", Value: *trustCenter.WebsiteURL})
|
||||
}
|
||||
|
||||
if trustCenter.Email != nil && *trustCenter.Email != "" {
|
||||
data.Details = append(data.Details, compliancePageDetail{Label: "Email", Value: *trustCenter.Email})
|
||||
}
|
||||
|
||||
if trustCenter.HeadquarterAddress != nil && *trustCenter.HeadquarterAddress != "" {
|
||||
data.Details = append(data.Details, compliancePageDetail{Label: "Headquarters", Value: *trustCenter.HeadquarterAddress})
|
||||
}
|
||||
|
||||
data.Frameworks, err = s.fetchComplianceFrameworks(ctx, scope, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch compliance frameworks: %w", err)
|
||||
}
|
||||
|
||||
data.Documents, err = s.fetchDocuments(ctx, scope, org.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch documents: %w", err)
|
||||
}
|
||||
|
||||
data.Audits, err = s.fetchAudits(ctx, scope, org.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch audits: %w", err)
|
||||
}
|
||||
|
||||
data.ThirdParties, err = s.fetchThirdParties(ctx, scope, org.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch thirdParties: %w", err)
|
||||
}
|
||||
|
||||
data.References, err = s.fetchReferences(ctx, scope, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch references: %w", err)
|
||||
}
|
||||
|
||||
data.CustomLinks, err = s.fetchCustomLinks(ctx, scope, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch external links: %w", err)
|
||||
}
|
||||
|
||||
if err := complianceTmpl.Execute(w, data); err != nil {
|
||||
return fmt.Errorf("cannot render compliance page markdown: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type (
|
||||
sitemapData struct {
|
||||
BaseURL string
|
||||
Documents []string
|
||||
}
|
||||
|
||||
robotsData struct {
|
||||
Indexable bool
|
||||
BaseURL string
|
||||
}
|
||||
)
|
||||
|
||||
func (s *Service) RenderSitemap(
|
||||
ctx context.Context,
|
||||
w io.Writer,
|
||||
trustCenterID gid.GID,
|
||||
scope coredata.Scoper,
|
||||
baseURL string,
|
||||
) error {
|
||||
org, err := s.GetPortalOrganization(ctx, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load organization for sitemap: %w", err)
|
||||
}
|
||||
|
||||
data := &sitemapData{
|
||||
BaseURL: baseURL,
|
||||
}
|
||||
|
||||
data.Documents, err = s.fetchDocumentIDs(ctx, scope, org.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot fetch document IDs for sitemap: %w", err)
|
||||
}
|
||||
|
||||
if err := sitemapTmpl.Execute(w, data); err != nil {
|
||||
return fmt.Errorf("cannot render sitemap: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) RenderRobotsTxt(
|
||||
ctx context.Context,
|
||||
w io.Writer,
|
||||
searchEngineIndexing coredata.SearchEngineIndexing,
|
||||
baseURL string,
|
||||
) error {
|
||||
data := &robotsData{
|
||||
Indexable: searchEngineIndexing == coredata.SearchEngineIndexingIndexable,
|
||||
BaseURL: baseURL,
|
||||
}
|
||||
|
||||
if err := robotsTmpl.Execute(w, data); err != nil {
|
||||
return fmt.Errorf("cannot render robots.txt: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchDocumentIDs(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
orgID gid.GID,
|
||||
) ([]string, error) {
|
||||
seen := make(map[gid.GID]struct{})
|
||||
|
||||
var resourceIDs []gid.GID
|
||||
|
||||
appendResourceID := func(id gid.GID) {
|
||||
if _, ok := seen[id]; ok {
|
||||
return
|
||||
}
|
||||
|
||||
seen[id] = struct{}{}
|
||||
resourceIDs = append(resourceIDs, id)
|
||||
}
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.DocumentOrderField]{
|
||||
Field: coredata.DocumentOrderFieldTitle,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListDocumentsForOrganizationID(ctx, scope, orgID, cursor, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list documents: %w", err)
|
||||
}
|
||||
|
||||
for _, doc := range result.Data {
|
||||
if doc.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
continue
|
||||
}
|
||||
|
||||
appendResourceID(doc.ID)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.DocumentOrderFieldTitle)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
cursorKey = nil
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.TrustCenterFileOrderField]{
|
||||
Field: coredata.TrustCenterFileOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListPortalFilesForOrganizationID(
|
||||
ctx,
|
||||
scope,
|
||||
orgID,
|
||||
cursor,
|
||||
coredata.NewTrustCenterFileFilter(),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list trust center files: %w", err)
|
||||
}
|
||||
|
||||
for _, file := range result.Data {
|
||||
if file.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
continue
|
||||
}
|
||||
|
||||
appendResourceID(file.ID)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.TrustCenterFileOrderFieldCreatedAt)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
cursorKey = nil
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.AuditOrderField]{
|
||||
Field: coredata.AuditOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListAuditsForOrganizationID(ctx, scope, orgID, cursor, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list audits: %w", err)
|
||||
}
|
||||
|
||||
for _, audit := range result.Data {
|
||||
if audit.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
continue
|
||||
}
|
||||
|
||||
if audit.ReportFileID == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
appendResourceID(*audit.ReportFileID)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.AuditOrderFieldCreatedAt)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
aliases, err := s.resourceAlias.LoadByResourceIDs(ctx, scope, resourceIDs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot load resource aliases: %w", err)
|
||||
}
|
||||
|
||||
paths := make([]string, 0, len(resourceIDs))
|
||||
for _, resourceID := range resourceIDs {
|
||||
if alias, ok := aliases[resourceID]; ok {
|
||||
paths = append(paths, alias)
|
||||
continue
|
||||
}
|
||||
|
||||
paths = append(paths, resourceID.String())
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchComplianceFrameworks(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) ([]compliancePageFramework, error) {
|
||||
var frameworks []compliancePageFramework
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.ComplianceFrameworkOrderField]{
|
||||
Field: coredata.ComplianceFrameworkOrderFieldRank,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListComplianceFrameworksByPortalID(ctx, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list compliance frameworks: %w", err)
|
||||
}
|
||||
|
||||
for _, cf := range result.Data {
|
||||
if cf.Visibility != coredata.ComplianceFrameworkVisibilityPublic {
|
||||
continue
|
||||
}
|
||||
|
||||
fw, err := s.GetFramework(ctx, scope, cf.FrameworkID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get framework %s: %w", cf.FrameworkID, err)
|
||||
}
|
||||
|
||||
fi := compliancePageFramework{Name: fw.Name}
|
||||
if fw.Description != nil {
|
||||
fi.Description = *fw.Description
|
||||
}
|
||||
|
||||
frameworks = append(frameworks, fi)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.ComplianceFrameworkOrderFieldRank)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return frameworks, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchDocuments(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
orgID gid.GID,
|
||||
) ([]compliancePageDocument, error) {
|
||||
var docs []compliancePageDocument
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.DocumentOrderField]{
|
||||
Field: coredata.DocumentOrderFieldTitle,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListDocumentsForOrganizationID(ctx, scope, orgID, cursor, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list documents: %w", err)
|
||||
}
|
||||
|
||||
for _, doc := range result.Data {
|
||||
if doc.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
continue
|
||||
}
|
||||
|
||||
docs = append(
|
||||
docs,
|
||||
compliancePageDocument{
|
||||
Title: doc.Title,
|
||||
Type: doc.DocumentType.String(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.DocumentOrderFieldTitle)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return docs, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchAudits(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
orgID gid.GID,
|
||||
) ([]compliancePageAudit, error) {
|
||||
var audits []compliancePageAudit
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.AuditOrderField]{
|
||||
Field: coredata.AuditOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListAuditsForOrganizationID(ctx, scope, orgID, cursor, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list audits: %w", err)
|
||||
}
|
||||
|
||||
for _, audit := range result.Data {
|
||||
if audit.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
continue
|
||||
}
|
||||
|
||||
frameworkName := ""
|
||||
|
||||
fw, err := s.GetFramework(ctx, scope, audit.FrameworkID)
|
||||
if err == nil {
|
||||
frameworkName = fw.Name
|
||||
}
|
||||
|
||||
ai := compliancePageAudit{
|
||||
Name: ref.UnrefOrZero(audit.Name),
|
||||
Framework: frameworkName,
|
||||
}
|
||||
if audit.ValidFrom != nil {
|
||||
ai.ValidFrom = audit.ValidFrom.Format("2006-01-02")
|
||||
}
|
||||
|
||||
if audit.ValidUntil != nil {
|
||||
ai.ValidUntil = audit.ValidUntil.Format("2006-01-02")
|
||||
}
|
||||
|
||||
audits = append(audits, ai)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.AuditOrderFieldCreatedAt)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return audits, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchThirdParties(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
orgID gid.GID,
|
||||
) ([]compliancePageThirdParty, error) {
|
||||
var thirdParties []compliancePageThirdParty
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.ThirdPartyOrderField]{
|
||||
Field: coredata.ThirdPartyOrderFieldName,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListThirdPartiesForOrganizationID(ctx, scope, orgID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list thirdParties: %w", err)
|
||||
}
|
||||
|
||||
for _, v := range result.Data {
|
||||
var countries []string
|
||||
for _, c := range v.Countries {
|
||||
countries = append(countries, c.String())
|
||||
}
|
||||
|
||||
thirdParties = append(
|
||||
thirdParties,
|
||||
compliancePageThirdParty{
|
||||
Name: v.Name,
|
||||
Category: v.Category.String(),
|
||||
Countries: strings.Join(countries, ", "),
|
||||
Website: ref.UnrefOrZero(v.WebsiteURL),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.ThirdPartyOrderFieldName)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return thirdParties, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchReferences(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) ([]compliancePageReference, error) {
|
||||
var refs []compliancePageReference
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.TrustCenterReferenceOrderField]{
|
||||
Field: coredata.TrustCenterReferenceOrderFieldRank,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListPortalReferencesForPortalID(ctx, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list references: %w", err)
|
||||
}
|
||||
|
||||
for _, r := range result.Data {
|
||||
ri := compliancePageReference{
|
||||
Name: r.Name,
|
||||
Website: r.WebsiteURL,
|
||||
}
|
||||
if r.Description != nil {
|
||||
ri.Description = *r.Description
|
||||
}
|
||||
|
||||
refs = append(refs, ri)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.TrustCenterReferenceOrderFieldRank)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return refs, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchCustomLinks(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) ([]compliancePageCustomLink, error) {
|
||||
var links []compliancePageCustomLink
|
||||
|
||||
var cursorKey *page.CursorKey
|
||||
for {
|
||||
cursor := page.NewCursor(
|
||||
page.MaxCursorSize,
|
||||
cursorKey,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.ComplianceCustomLinkOrderField]{
|
||||
Field: coredata.ComplianceCustomLinkOrderFieldRank,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
)
|
||||
|
||||
result, err := s.ListCustomLinksForPortalID(ctx, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot list custom links: %w", err)
|
||||
}
|
||||
|
||||
for _, l := range result.Data {
|
||||
links = append(
|
||||
links,
|
||||
compliancePageCustomLink{
|
||||
Name: l.Name,
|
||||
URL: l.URL,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if !result.Info.HasNext {
|
||||
break
|
||||
}
|
||||
|
||||
last := result.Data[len(result.Data)-1]
|
||||
ck := last.CursorKey(coredata.ComplianceCustomLinkOrderFieldRank)
|
||||
cursorKey = &ck
|
||||
}
|
||||
|
||||
return links, nil
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) ListCommitmentGroupsForPortalID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
cursor *page.Cursor[coredata.CompliancePortalCommitmentGroupOrderField],
|
||||
) (*page.Page[*coredata.CompliancePortalCommitmentGroup, coredata.CompliancePortalCommitmentGroupOrderField], error) {
|
||||
var groups coredata.CompliancePortalCommitmentGroups
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := groups.LoadByTrustCenterID(ctx, conn, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load compliance portal commitment groups: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(groups, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) GetCommitmentGroup(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
groupID gid.GID,
|
||||
) (*coredata.CompliancePortalCommitmentGroup, error) {
|
||||
group := &coredata.CompliancePortalCommitmentGroup{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := group.LoadByID(ctx, conn, scope, groupID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load compliance portal commitment group: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return group, nil
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) ListCommitmentsForGroupID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
groupID gid.GID,
|
||||
cursor *page.Cursor[coredata.CompliancePortalCommitmentOrderField],
|
||||
) (*page.Page[*coredata.CompliancePortalCommitment, coredata.CompliancePortalCommitmentOrderField], error) {
|
||||
var commitments coredata.CompliancePortalCommitments
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := commitments.LoadByGroupID(ctx, conn, scope, groupID, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load compliance portal commitments: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(commitments, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) GetCommitment(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
commitmentID gid.GID,
|
||||
) (*coredata.CompliancePortalCommitment, error) {
|
||||
commitment := &coredata.CompliancePortalCommitment{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := commitment.LoadByID(ctx, conn, scope, commitmentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load compliance portal commitment: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return commitment, nil
|
||||
}
|
||||
57
pkg/complianceportal/visitor/custom_link_service.go
Normal file
57
pkg/complianceportal/visitor/custom_link_service.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) ListCustomLinksForPortalID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
cursor *page.Cursor[coredata.ComplianceCustomLinkOrderField],
|
||||
) (*page.Page[*coredata.ComplianceCustomLink, coredata.ComplianceCustomLinkOrderField], error) {
|
||||
var links coredata.ComplianceCustomLinks
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := links.LoadByTrustCenterID(ctx, conn, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load custom links: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(links, cursor), nil
|
||||
}
|
||||
346
pkg/complianceportal/visitor/document_service.go
Normal file
346
pkg/complianceportal/visitor/document_service.go
Normal file
@@ -0,0 +1,346 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/docgen"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/html2pdf"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.probo.inc/probo/pkg/pdfutils"
|
||||
)
|
||||
|
||||
type ErrDocumentArchived struct{}
|
||||
|
||||
func (e ErrDocumentArchived) Error() string {
|
||||
return "cannot access an archived document"
|
||||
}
|
||||
|
||||
func (s *Service) ListDocumentsForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.DocumentOrderField],
|
||||
filter *coredata.DocumentFilter,
|
||||
) (*page.Page[*coredata.Document, coredata.DocumentOrderField], error) {
|
||||
var documents coredata.Documents
|
||||
|
||||
if filter == nil {
|
||||
filter = coredata.NewDocumentTrustCenterFilter()
|
||||
}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := documents.LoadPublishedByOrganizationID(ctx, conn, scope, organizationID, cursor, filter); err != nil {
|
||||
return fmt.Errorf("cannot load published documents: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(documents, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportDocumentPDF(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
documentID gid.GID,
|
||||
email mail.Addr,
|
||||
) ([]byte, error) {
|
||||
pdfData, err := s.exportDocumentPDFData(ctx, scope, documentID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot export document PDF: %w", err)
|
||||
}
|
||||
|
||||
watermarkedPDF, err := pdfutils.AddConfidentialWithTimestamp(pdfData, email)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot add watermark to PDF: %w", err)
|
||||
}
|
||||
|
||||
return watermarkedPDF, nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportDocumentPDFWithoutWatermark(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
documentID gid.GID,
|
||||
) ([]byte, error) {
|
||||
return s.exportDocumentPDFData(ctx, scope, documentID)
|
||||
}
|
||||
|
||||
func (s *Service) GetDocument(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
documentID gid.GID,
|
||||
) (*coredata.Document, error) {
|
||||
document := &coredata.Document{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := document.LoadByID(ctx, conn, scope, documentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load document: %w", err)
|
||||
}
|
||||
|
||||
if document.ArchivedAt != nil {
|
||||
return &ErrDocumentArchived{}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if document.OrganizationID != organizationID {
|
||||
return nil, ErrDocumentNotFound
|
||||
}
|
||||
|
||||
if document.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
return nil, ErrDocumentNotVisible
|
||||
}
|
||||
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func (s *Service) exportDocumentPDFData(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
documentID gid.GID,
|
||||
) ([]byte, error) {
|
||||
document := &coredata.Document{}
|
||||
version := &coredata.DocumentVersion{}
|
||||
fileRecord := &coredata.File{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := document.LoadByID(ctx, conn, scope, documentID); err != nil {
|
||||
return fmt.Errorf("cannot load document: %w", err)
|
||||
}
|
||||
|
||||
if document.ArchivedAt != nil {
|
||||
return &ErrDocumentArchived{}
|
||||
}
|
||||
|
||||
if document.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
return fmt.Errorf("document not visible on trust center")
|
||||
}
|
||||
|
||||
if err := version.LoadLatestPublishedVersion(ctx, conn, scope, documentID); err != nil {
|
||||
return fmt.Errorf("cannot load latest published document version: %w", err)
|
||||
}
|
||||
|
||||
if version.FileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := fileRecord.LoadByID(ctx, conn, scope, *version.FileID); err != nil {
|
||||
return fmt.Errorf("cannot load document version file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if version.FileID != nil {
|
||||
pdfData, err := s.fileManager.GetFileBytes(ctx, fileRecord)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot fetch document PDF file: %w", err)
|
||||
}
|
||||
|
||||
return pdfData, nil
|
||||
}
|
||||
|
||||
// TODO: remove on-the-fly fallback once all published versions have a stored PDF.
|
||||
pdfData, err := s.generateDocumentPDFOnTheFly(ctx, scope, document, version)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate PDF on the fly: %w", err)
|
||||
}
|
||||
|
||||
return pdfData, nil
|
||||
}
|
||||
|
||||
// generatePDFOnTheFly generates a PDF from scratch for versions that don't have
|
||||
// a stored file yet. Can be removed once all published versions have been
|
||||
// processed by the document PDF worker.
|
||||
func (s *Service) generateDocumentPDFOnTheFly(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
document *coredata.Document,
|
||||
version *coredata.DocumentVersion,
|
||||
) ([]byte, error) {
|
||||
organization := &coredata.Organization{}
|
||||
|
||||
var approverNames []string
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
lastQuorum := &coredata.DocumentVersionApprovalQuorum{}
|
||||
if err := lastQuorum.LoadLastByDocumentVersionID(ctx, conn, scope, version.ID); err != nil {
|
||||
if !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load last approval quorum: %w", err)
|
||||
}
|
||||
} else if lastQuorum.Status == coredata.DocumentVersionApprovalQuorumStatusApproved {
|
||||
approvedDecisions := &coredata.DocumentVersionApprovalDecisions{}
|
||||
|
||||
approvedFilter := coredata.NewDocumentVersionApprovalDecisionFilter(
|
||||
coredata.DocumentVersionApprovalDecisionStates{coredata.DocumentVersionApprovalDecisionStateApproved},
|
||||
)
|
||||
if err := approvedDecisions.LoadByQuorumID(
|
||||
ctx,
|
||||
conn,
|
||||
scope,
|
||||
lastQuorum.ID,
|
||||
page.NewCursor(
|
||||
100,
|
||||
nil,
|
||||
page.Head,
|
||||
page.OrderBy[coredata.DocumentVersionApprovalDecisionOrderField]{
|
||||
Field: coredata.DocumentVersionApprovalDecisionOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
),
|
||||
approvedFilter,
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot load approved decisions: %w", err)
|
||||
}
|
||||
|
||||
approverProfileIDs := make([]gid.GID, 0, len(*approvedDecisions))
|
||||
for _, d := range *approvedDecisions {
|
||||
approverProfileIDs = append(approverProfileIDs, d.ApproverID)
|
||||
}
|
||||
|
||||
if len(approverProfileIDs) > 0 {
|
||||
profiles := coredata.MembershipProfiles{}
|
||||
if err := profiles.LoadByIDs(ctx, conn, scope, approverProfileIDs); err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load approver profiles: %w", err)
|
||||
}
|
||||
|
||||
for _, p := range profiles {
|
||||
approverNames = append(approverNames, p.FullName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := organization.LoadByID(ctx, conn, scope, document.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
classification := docgen.ClassificationSecret
|
||||
|
||||
switch version.Classification {
|
||||
case coredata.DocumentClassificationPublic:
|
||||
classification = docgen.ClassificationPublic
|
||||
case coredata.DocumentClassificationInternal:
|
||||
classification = docgen.ClassificationInternal
|
||||
case coredata.DocumentClassificationConfidential:
|
||||
classification = docgen.ClassificationConfidential
|
||||
}
|
||||
|
||||
horizontalLogoBase64 := ""
|
||||
|
||||
if organization.HorizontalLogoFileID != nil {
|
||||
fileRecord := &coredata.File{}
|
||||
|
||||
fileErr := s.pg.WithConn(
|
||||
|
||||
ctx,
|
||||
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return fileRecord.LoadByID(ctx, conn, scope, *organization.HorizontalLogoFileID)
|
||||
})
|
||||
|
||||
if fileErr == nil {
|
||||
base64Data, mimeType, logoErr := s.fileManager.GetFileBase64(ctx, fileRecord)
|
||||
if logoErr == nil {
|
||||
horizontalLogoBase64 = fmt.Sprintf("data:%s;base64,%s", mimeType, base64Data)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
docData := docgen.DocumentData{
|
||||
Title: version.Title,
|
||||
Content: json.RawMessage([]byte(version.Content)),
|
||||
Major: version.Major,
|
||||
Minor: version.Minor,
|
||||
Classification: classification,
|
||||
Approvers: approverNames,
|
||||
PublishedAt: version.PublishedAt,
|
||||
CompanyHorizontalLogoBase64: horizontalLogoBase64,
|
||||
}
|
||||
|
||||
htmlContent, err := docgen.RenderHTML(docData)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate HTML: %w", err)
|
||||
}
|
||||
|
||||
cfg := html2pdf.RenderConfig{
|
||||
PageFormat: html2pdf.PageFormatA4,
|
||||
Orientation: html2pdf.OrientationPortrait,
|
||||
MarginTop: html2pdf.NewMarginInches(1.0),
|
||||
MarginBottom: html2pdf.NewMarginInches(1.0),
|
||||
MarginLeft: html2pdf.NewMarginInches(1.0),
|
||||
MarginRight: html2pdf.NewMarginInches(1.0),
|
||||
PrintBackground: true,
|
||||
Scale: 1.0,
|
||||
}
|
||||
|
||||
pdfReader, err := s.html2pdfConverter.GeneratePDF(ctx, htmlContent, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate PDF: %w", err)
|
||||
}
|
||||
|
||||
pdfData, err := io.ReadAll(pdfReader)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read PDF data: %w", err)
|
||||
}
|
||||
|
||||
return pdfData, nil
|
||||
}
|
||||
37
pkg/complianceportal/visitor/errors.go
Normal file
37
pkg/complianceportal/visitor/errors.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrPageNotFound = errors.New("page not found")
|
||||
ErrMembershipNotFound = errors.New("membership not found")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
ErrUserInactive = errors.New("user inactive")
|
||||
ErrDocumentAccessNotFound = errors.New("document access not found")
|
||||
ErrNDAFileNotFound = errors.New("NDA file not found")
|
||||
ErrDocumentNotFound = errors.New("document not found")
|
||||
ErrDocumentNotVisible = errors.New("document not visible")
|
||||
ErrReportNotFound = errors.New("report not found")
|
||||
ErrTrustCenterFileNotFound = errors.New("trust center file not found")
|
||||
ErrTrustCenterFileNotVisible = errors.New("trust center file not visible")
|
||||
)
|
||||
142
pkg/complianceportal/visitor/framework_service.go
Normal file
142
pkg/complianceportal/visitor/framework_service.go
Normal file
@@ -0,0 +1,142 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
func (s *Service) GetFramework(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
frameworkID gid.GID,
|
||||
) (*coredata.Framework, error) {
|
||||
framework := &coredata.Framework{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := framework.LoadByID(ctx, conn, scope, frameworkID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load framework: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return framework, nil
|
||||
}
|
||||
|
||||
func (s *Service) GenerateFrameworkLightLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
frameworkID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
file := &coredata.File{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
framework := &coredata.Framework{}
|
||||
if err := framework.LoadByID(ctx, conn, scope, frameworkID); err != nil {
|
||||
return fmt.Errorf("cannot load framework: %w", err)
|
||||
}
|
||||
|
||||
if framework.LightLogoFileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := file.LoadByID(ctx, conn, scope, *framework.LightLogoFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if file.FileKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
presignedURL, err := s.fileManager.GeneratePresignedURL(ctx, file, expiresIn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate file URL: %w", err)
|
||||
}
|
||||
|
||||
return &presignedURL, nil
|
||||
}
|
||||
|
||||
func (s *Service) GenerateFrameworkDarkLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
frameworkID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
file := &coredata.File{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
framework := &coredata.Framework{}
|
||||
if err := framework.LoadByID(ctx, conn, scope, frameworkID); err != nil {
|
||||
return fmt.Errorf("cannot load framework: %w", err)
|
||||
}
|
||||
|
||||
if framework.DarkLogoFileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := file.LoadByID(ctx, conn, scope, *framework.DarkLogoFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if file.FileKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
presignedURL, err := s.fileManager.GeneratePresignedURL(ctx, file, expiresIn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate file URL: %w", err)
|
||||
}
|
||||
|
||||
return &presignedURL, nil
|
||||
}
|
||||
114
pkg/complianceportal/visitor/organization_service.go
Normal file
114
pkg/complianceportal/visitor/organization_service.go
Normal file
@@ -0,0 +1,114 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
func (s *Service) GetOrganization(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
) (*coredata.Organization, error) {
|
||||
organization := &coredata.Organization{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := organization.LoadByID(
|
||||
ctx,
|
||||
conn,
|
||||
scope,
|
||||
organizationID,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return organization, nil
|
||||
}
|
||||
|
||||
func (s *Service) GenerateOrganizationLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
organization, err := s.GetOrganization(ctx, scope, organizationID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get organization: %w", err)
|
||||
}
|
||||
|
||||
if organization.LogoFileID == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
file := &coredata.File{}
|
||||
|
||||
err = s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return file.LoadByID(ctx, conn, scope, *organization.LogoFileID)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
presignClient := s3.NewPresignClient(s.s3)
|
||||
|
||||
encodedFilename := url.QueryEscape(file.FileName)
|
||||
contentDisposition := fmt.Sprintf("attachment; filename=\"%s\"; filename*=UTF-8''%s",
|
||||
encodedFilename, encodedFilename)
|
||||
|
||||
presignedReq, err := presignClient.PresignGetObject(
|
||||
ctx,
|
||||
&s3.GetObjectInput{
|
||||
Bucket: new(s.bucket),
|
||||
Key: new(file.FileKey),
|
||||
ResponseCacheControl: new("max-age=3600, public"),
|
||||
ResponseContentDisposition: new(contentDisposition),
|
||||
}, func(opts *s3.PresignOptions) {
|
||||
opts.Expires = expiresIn
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot presign GetObject request: %w", err)
|
||||
}
|
||||
|
||||
return &presignedReq.URL, nil
|
||||
}
|
||||
830
pkg/complianceportal/visitor/portal_access_service.go
Normal file
830
pkg/complianceportal/visitor/portal_access_service.go
Normal file
@@ -0,0 +1,830 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/log"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/packages/emails"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type PortalAccessRequest struct {
|
||||
TrustCenterID gid.GID
|
||||
IdentityID gid.GID
|
||||
DocumentIDs []gid.GID
|
||||
ReportIDs []gid.GID
|
||||
TrustCenterFileIDs []gid.GID
|
||||
}
|
||||
|
||||
const (
|
||||
PortalAccessURLFormat = "https://%s/organizations/%s/trust-center/access"
|
||||
)
|
||||
|
||||
func (s *Service) RequestPortalAccess(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
req *PortalAccessRequest,
|
||||
) (*coredata.TrustCenterAccess, error) {
|
||||
var (
|
||||
now = time.Now()
|
||||
access *coredata.TrustCenterAccess
|
||||
)
|
||||
|
||||
err := s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, tx, scope, req.TrustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
access = &coredata.TrustCenterAccess{}
|
||||
if err := access.LoadByTrustCenterIDAndIdentityID(ctx, tx, scope, req.TrustCenterID, req.IdentityID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance page membership: %w", err)
|
||||
}
|
||||
|
||||
organizationID := trustCenter.OrganizationID
|
||||
|
||||
documentIDs := req.DocumentIDs
|
||||
if req.DocumentIDs == nil {
|
||||
filter := coredata.NewDocumentTrustCenterFilter()
|
||||
|
||||
allDocuments, err := page.LoadAll(
|
||||
ctx,
|
||||
page.OrderBy[coredata.DocumentOrderField]{
|
||||
Field: coredata.DocumentOrderFieldTitle,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
func(ctx context.Context, cursor *page.Cursor[coredata.DocumentOrderField]) ([]*coredata.Document, error) {
|
||||
var batch coredata.Documents
|
||||
if err := batch.LoadByOrganizationID(ctx, tx, scope, organizationID, cursor, filter); err != nil {
|
||||
return nil, fmt.Errorf("cannot list documents: %w", err)
|
||||
}
|
||||
|
||||
return batch, nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, doc := range allDocuments {
|
||||
documentIDs = append(documentIDs, doc.ID)
|
||||
}
|
||||
}
|
||||
|
||||
reportIDs := req.ReportIDs
|
||||
if req.ReportIDs == nil {
|
||||
auditFilter := coredata.NewAuditTrustCenterFilter()
|
||||
|
||||
allAudits, err := page.LoadAll(
|
||||
ctx,
|
||||
page.OrderBy[coredata.AuditOrderField]{
|
||||
Field: coredata.AuditOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
func(ctx context.Context, cursor *page.Cursor[coredata.AuditOrderField]) ([]*coredata.Audit, error) {
|
||||
var batch coredata.Audits
|
||||
if err := batch.LoadByOrganizationID(ctx, tx, scope, organizationID, cursor, auditFilter); err != nil {
|
||||
return nil, fmt.Errorf("cannot list audits: %w", err)
|
||||
}
|
||||
|
||||
return batch, nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, audit := range allAudits {
|
||||
if audit.ReportFileID != nil {
|
||||
reportIDs = append(reportIDs, *audit.ReportFileID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trustCenterFileIDs := req.TrustCenterFileIDs
|
||||
if req.TrustCenterFileIDs == nil {
|
||||
filter := coredata.NewTrustCenterFileFilter(
|
||||
coredata.WithTrustCenterFileVisibilities(coredata.TrustCenterVisibilityPrivate, coredata.TrustCenterVisibilityNone),
|
||||
)
|
||||
|
||||
allTrustCenterFiles, err := page.LoadAll(
|
||||
ctx,
|
||||
page.OrderBy[coredata.TrustCenterFileOrderField]{
|
||||
Field: coredata.TrustCenterFileOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionDesc,
|
||||
},
|
||||
func(ctx context.Context, cursor *page.Cursor[coredata.TrustCenterFileOrderField]) ([]*coredata.TrustCenterFile, error) {
|
||||
var batch coredata.TrustCenterFiles
|
||||
if err := batch.LoadByOrganizationID(ctx, tx, scope, organizationID, cursor, filter); err != nil {
|
||||
return nil, fmt.Errorf("cannot list trust center files: %w", err)
|
||||
}
|
||||
|
||||
return batch, nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, file := range allTrustCenterFiles {
|
||||
trustCenterFileIDs = append(trustCenterFileIDs, file.ID)
|
||||
}
|
||||
}
|
||||
|
||||
existingAccesses, err := page.LoadAll(
|
||||
ctx,
|
||||
page.OrderBy[coredata.TrustCenterDocumentAccessOrderField]{
|
||||
Field: coredata.TrustCenterDocumentAccessOrderFieldCreatedAt,
|
||||
Direction: page.OrderDirectionAsc,
|
||||
},
|
||||
func(ctx context.Context, cursor *page.Cursor[coredata.TrustCenterDocumentAccessOrderField]) ([]*coredata.TrustCenterDocumentAccess, error) {
|
||||
var batch coredata.TrustCenterDocumentAccesses
|
||||
if err := batch.LoadByTrustCenterAccessID(ctx, tx, scope, access.ID, cursor); err != nil {
|
||||
return nil, fmt.Errorf("cannot load existing access records: %w", err)
|
||||
}
|
||||
|
||||
return batch, nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
existingDocumentIDs, existingReportIDs, existingTrustCenterFileIDs := extractExistingIDs(existingAccesses)
|
||||
newDocumentIDs := filterExistingIDs(documentIDs, existingDocumentIDs)
|
||||
newReportIDs := filterExistingIDs(reportIDs, existingReportIDs)
|
||||
newTrustCenterFileIDs := filterExistingIDs(trustCenterFileIDs, existingTrustCenterFileIDs)
|
||||
|
||||
var accesses coredata.TrustCenterDocumentAccesses
|
||||
|
||||
if err := accesses.BulkInsertDocumentAccesses(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
access.ID,
|
||||
access.OrganizationID,
|
||||
newDocumentIDs,
|
||||
coredata.TrustCenterDocumentAccessStatusRequested,
|
||||
now,
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot bulk insert trust center document accesses: %w", err)
|
||||
}
|
||||
|
||||
if err := accesses.BulkInsertReportFileAccesses(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
access.ID,
|
||||
access.OrganizationID,
|
||||
newReportIDs,
|
||||
coredata.TrustCenterDocumentAccessStatusRequested,
|
||||
now,
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot bulk insert trust center report accesses: %w", err)
|
||||
}
|
||||
|
||||
if err := accesses.BulkInsertTrustCenterFileAccesses(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
access.ID,
|
||||
access.OrganizationID,
|
||||
newTrustCenterFileIDs,
|
||||
coredata.TrustCenterDocumentAccessStatusRequested,
|
||||
now,
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot bulk insert trust center file accesses: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := s.slack.QueueSlackNotification(ctx, scope, req.IdentityID, req.TrustCenterID); err != nil {
|
||||
s.logger.ErrorCtx(ctx, "cannot queue slack notification", log.Error(err))
|
||||
}
|
||||
|
||||
return access, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalAccess(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
identityID gid.GID,
|
||||
) (coredata.TrustCenterAccess, error) {
|
||||
var access coredata.TrustCenterAccess
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return access.LoadByTrustCenterIDAndIdentityID(ctx, conn, scope, trustCenterID, identityID)
|
||||
},
|
||||
)
|
||||
|
||||
return access, err
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalDocumentAccess(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
identityID gid.GID,
|
||||
documentID gid.GID,
|
||||
) (*coredata.TrustCenterDocumentAccess, error) {
|
||||
var documentAccess *coredata.TrustCenterDocumentAccess
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
|
||||
err := access.LoadByTrustCenterIDAndIdentityID(ctx, conn, scope, trustCenterID, identityID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrMembershipNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(ctx, conn, scope, identityID, access.OrganizationID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if profile.State != coredata.ProfileStateActive {
|
||||
return ErrUserInactive
|
||||
}
|
||||
|
||||
documentAccess = &coredata.TrustCenterDocumentAccess{}
|
||||
|
||||
err = documentAccess.LoadByTrustCenterAccessIDAndDocumentID(ctx, conn, scope, access.ID, documentID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrDocumentAccessNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load document access: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return documentAccess, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalReportFileAccess(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
identityID gid.GID,
|
||||
reportFileID gid.GID,
|
||||
) (*coredata.TrustCenterDocumentAccess, error) {
|
||||
var reportAccess *coredata.TrustCenterDocumentAccess
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
|
||||
err := access.LoadByTrustCenterIDAndIdentityID(ctx, conn, scope, trustCenterID, identityID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrMembershipNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(ctx, conn, scope, identityID, access.OrganizationID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if profile.State != coredata.ProfileStateActive {
|
||||
return ErrUserInactive
|
||||
}
|
||||
|
||||
reportAccess = &coredata.TrustCenterDocumentAccess{}
|
||||
|
||||
err = reportAccess.LoadByTrustCenterAccessIDAndReportFileID(ctx, conn, scope, access.ID, reportFileID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrDocumentAccessNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load report access: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reportAccess, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalFileAccess(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
identityID gid.GID,
|
||||
trustCenterFileID gid.GID,
|
||||
) (*coredata.TrustCenterDocumentAccess, error) {
|
||||
var fileAccess *coredata.TrustCenterDocumentAccess
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
|
||||
err := access.LoadByTrustCenterIDAndIdentityID(ctx, conn, scope, trustCenterID, identityID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrMembershipNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(ctx, conn, scope, identityID, access.OrganizationID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if profile.State != coredata.ProfileStateActive {
|
||||
return ErrUserInactive
|
||||
}
|
||||
|
||||
fileAccess = &coredata.TrustCenterDocumentAccess{}
|
||||
|
||||
err = fileAccess.LoadByTrustCenterAccessIDAndTrustCenterFileID(ctx, conn, scope, access.ID, trustCenterFileID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrDocumentAccessNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center file access: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return fileAccess, nil
|
||||
}
|
||||
|
||||
func (s *Service) GrantPortalAccessByIDs(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
email mail.Addr,
|
||||
documentIDs []gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
fileIDs []gid.GID,
|
||||
) error {
|
||||
return s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByOrganizationID(ctx, tx, scope, organizationID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
identity := &coredata.Identity{}
|
||||
if err := identity.LoadByEmail(ctx, tx, email); err != nil {
|
||||
return fmt.Errorf("cannot load identity: %w", err)
|
||||
}
|
||||
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
if err := access.LoadByTrustCenterIDAndIdentityID(ctx, tx, scope, trustCenter.ID, identity.ID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(ctx, tx, scope, identity.ID, access.OrganizationID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrUserNotFound
|
||||
}
|
||||
}
|
||||
|
||||
if profile.State != coredata.ProfileStateActive {
|
||||
return ErrUserInactive
|
||||
}
|
||||
|
||||
shouldSendEmail := profile.State != coredata.ProfileStateActive
|
||||
now := time.Now()
|
||||
|
||||
if len(documentIDs) > 0 {
|
||||
if err := coredata.GrantByDocumentIDs(ctx, tx, scope, access.ID, documentIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot grant document accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(reportIDs) > 0 {
|
||||
if err := coredata.GrantByReportFileIDs(ctx, tx, scope, access.ID, reportIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot grant report accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(fileIDs) > 0 {
|
||||
if err := coredata.GrantByTrustCenterFileIDs(ctx, tx, scope, access.ID, fileIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot grant trust center file accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if shouldSendEmail {
|
||||
profile.State = coredata.ProfileStateActive
|
||||
|
||||
profile.UpdatedAt = now
|
||||
if err := profile.Update(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot update profile: %w", err)
|
||||
}
|
||||
|
||||
if err := s.sendPortalAccessEmail(ctx, tx, scope, access, profile); err != nil {
|
||||
return fmt.Errorf("cannot send access email: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Service) sendPortalAccessEmail(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
scope coredata.Scoper,
|
||||
access *coredata.TrustCenterAccess,
|
||||
profile *coredata.MembershipProfile,
|
||||
) error {
|
||||
organization := &coredata.Organization{}
|
||||
if err := organization.LoadByID(ctx, tx, scope, access.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
access.UpdatedAt = now
|
||||
|
||||
if err := access.Update(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot update trust center access with expiration: %w", err)
|
||||
}
|
||||
|
||||
emailPresenterCfg, err := s.GetPortalEmailPresenterConfig(ctx, scope, access.TrustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot get compliance page email presenter config: %w", err)
|
||||
}
|
||||
|
||||
emailPresenter := emails.NewPresenterFromConfig(emailPresenterCfg, profile.FullName)
|
||||
|
||||
subject, textBody, htmlBody, err := emailPresenter.RenderTrustCenterAccess(ctx, organization.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot render trust center access email: %w", err)
|
||||
}
|
||||
|
||||
accessEmail := coredata.NewEmail(
|
||||
profile.FullName,
|
||||
profile.EmailAddress,
|
||||
subject,
|
||||
textBody,
|
||||
htmlBody,
|
||||
&coredata.EmailOptions{
|
||||
SenderName: new(organization.Name),
|
||||
},
|
||||
)
|
||||
|
||||
if err := accessEmail.Insert(ctx, tx); err != nil {
|
||||
return fmt.Errorf("cannot insert access email: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Service) RejectOrRevokePortalAccessByIDs(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
email mail.Addr,
|
||||
documentIDs []gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
fileIDs []gid.GID,
|
||||
) error {
|
||||
return s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByOrganizationID(ctx, tx, scope, organizationID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
identity := &coredata.Identity{}
|
||||
if err := identity.LoadByEmail(ctx, tx, email); err != nil {
|
||||
return fmt.Errorf("cannot load identity: %w", err)
|
||||
}
|
||||
|
||||
access := &coredata.TrustCenterAccess{}
|
||||
if err := access.LoadByTrustCenterIDAndIdentityID(ctx, tx, scope, trustCenter.ID, identity.ID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(ctx, tx, scope, identity.ID, access.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load profile: %w", err)
|
||||
}
|
||||
|
||||
shouldSendEmail := false
|
||||
now := time.Now()
|
||||
|
||||
if len(documentIDs) > 0 {
|
||||
shouldSendEmail = true
|
||||
|
||||
if err := coredata.RejectOrRevokeByDocumentIDs(ctx, tx, scope, access.ID, documentIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot reject/revoke document accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(reportIDs) > 0 {
|
||||
shouldSendEmail = true
|
||||
|
||||
if err := coredata.RejectOrRevokeByReportFileIDs(ctx, tx, scope, access.ID, reportIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot reject/revoke report accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(fileIDs) > 0 {
|
||||
shouldSendEmail = true
|
||||
|
||||
if err := coredata.RejectOrRevokeByTrustCenterFileIDs(ctx, tx, scope, access.ID, fileIDs, now); err != nil {
|
||||
return fmt.Errorf("cannot reject/revoke trust center file accesses: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if shouldSendEmail {
|
||||
if err := s.sendPortalDocumentAccessRejectedEmail(ctx, tx, scope, access, profile, documentIDs, reportIDs, fileIDs); err != nil {
|
||||
return fmt.Errorf("cannot send access email: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Service) sendPortalDocumentAccessRejectedEmail(
|
||||
ctx context.Context,
|
||||
tx pg.Tx,
|
||||
scope coredata.Scoper,
|
||||
access *coredata.TrustCenterAccess,
|
||||
profile *coredata.MembershipProfile,
|
||||
documentIDs []gid.GID,
|
||||
reportIDs []gid.GID,
|
||||
fileIDs []gid.GID,
|
||||
) error {
|
||||
organization := &coredata.Organization{}
|
||||
if err := organization.LoadByID(ctx, tx, scope, access.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
var (
|
||||
fileNames []string
|
||||
documents coredata.Documents
|
||||
)
|
||||
|
||||
if len(documentIDs) > 0 {
|
||||
if err := documents.LoadByIDs(ctx, tx, scope, documentIDs); err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load documents by IDs: %w", err)
|
||||
}
|
||||
|
||||
for _, d := range documents {
|
||||
fileNames = append(fileNames, d.Title)
|
||||
}
|
||||
}
|
||||
|
||||
if len(reportIDs) > 0 {
|
||||
reportLabels, err := reportAccessLabels(ctx, tx, scope, reportIDs)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot build report access labels: %w", err)
|
||||
}
|
||||
|
||||
fileNames = append(fileNames, reportLabels...)
|
||||
}
|
||||
|
||||
var files coredata.TrustCenterFiles
|
||||
if len(fileIDs) > 0 {
|
||||
if err := files.LoadByIDs(ctx, tx, scope, fileIDs); err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load files by IDs: %w", err)
|
||||
}
|
||||
|
||||
for _, f := range files {
|
||||
fileNames = append(fileNames, f.Name)
|
||||
}
|
||||
}
|
||||
|
||||
emailPresenterCfg, err := s.GetPortalEmailPresenterConfig(ctx, scope, access.TrustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot get compliance page email presenter config: %w", err)
|
||||
}
|
||||
|
||||
emailPresenter := emails.NewPresenterFromConfig(emailPresenterCfg, profile.FullName)
|
||||
|
||||
subject, textBody, htmlBody, err := emailPresenter.RenderTrustCenterDocumentAccessRejected(
|
||||
ctx,
|
||||
fileNames,
|
||||
organization.Name,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot render trust center documents access rejected email: %w", err)
|
||||
}
|
||||
|
||||
accessEmail := coredata.NewEmail(
|
||||
profile.FullName,
|
||||
profile.EmailAddress,
|
||||
subject,
|
||||
textBody,
|
||||
htmlBody,
|
||||
&coredata.EmailOptions{
|
||||
SenderName: new(organization.Name),
|
||||
},
|
||||
)
|
||||
|
||||
if err := accessEmail.Insert(ctx, tx); err != nil {
|
||||
return fmt.Errorf("cannot insert access email: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func extractExistingIDs(accesses coredata.TrustCenterDocumentAccesses) ([]gid.GID, []gid.GID, []gid.GID) {
|
||||
var (
|
||||
documentIDs []gid.GID
|
||||
reportIDs []gid.GID
|
||||
trustCenterFileIDs []gid.GID
|
||||
)
|
||||
|
||||
for _, access := range accesses {
|
||||
if access.DocumentID != nil {
|
||||
documentIDs = append(documentIDs, *access.DocumentID)
|
||||
}
|
||||
|
||||
if access.ReportFileID != nil {
|
||||
reportIDs = append(reportIDs, *access.ReportFileID)
|
||||
}
|
||||
|
||||
if access.TrustCenterFileID != nil {
|
||||
trustCenterFileIDs = append(trustCenterFileIDs, *access.TrustCenterFileID)
|
||||
}
|
||||
}
|
||||
|
||||
return documentIDs, reportIDs, trustCenterFileIDs
|
||||
}
|
||||
|
||||
func filterExistingIDs(allIDs []gid.GID, existingIDs []gid.GID) []gid.GID {
|
||||
existingMap := make(map[gid.GID]bool)
|
||||
for _, id := range existingIDs {
|
||||
existingMap[id] = true
|
||||
}
|
||||
|
||||
var newIDs []gid.GID
|
||||
|
||||
for _, id := range allIDs {
|
||||
if !existingMap[id] {
|
||||
newIDs = append(newIDs, id)
|
||||
}
|
||||
}
|
||||
|
||||
return newIDs
|
||||
}
|
||||
|
||||
func reportAccessLabels(
|
||||
ctx context.Context,
|
||||
conn pg.Querier,
|
||||
scope coredata.Scoper,
|
||||
reportFileIDs []gid.GID,
|
||||
) ([]string, error) {
|
||||
var reportFiles coredata.Files
|
||||
if err := reportFiles.LoadByIDs(ctx, conn, scope, reportFileIDs); err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return nil, fmt.Errorf("cannot load report files by IDs: %w", err)
|
||||
}
|
||||
|
||||
fileByID := make(map[gid.GID]*coredata.File, len(reportFiles))
|
||||
for _, f := range reportFiles {
|
||||
fileByID[f.ID] = f
|
||||
}
|
||||
|
||||
var audits coredata.Audits
|
||||
if err := audits.LoadByReportFileIDs(ctx, conn, scope, reportFileIDs); err != nil {
|
||||
return nil, fmt.Errorf("cannot load audits by report file IDs: %w", err)
|
||||
}
|
||||
|
||||
auditByFileID := make(map[gid.GID]*coredata.Audit, len(audits))
|
||||
frameworkIDSet := make(map[gid.GID]struct{})
|
||||
|
||||
for _, audit := range audits {
|
||||
if audit.ReportFileID == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, exists := auditByFileID[*audit.ReportFileID]; exists {
|
||||
continue
|
||||
}
|
||||
|
||||
auditByFileID[*audit.ReportFileID] = audit
|
||||
frameworkIDSet[audit.FrameworkID] = struct{}{}
|
||||
}
|
||||
|
||||
frameworkIDs := make([]gid.GID, 0, len(frameworkIDSet))
|
||||
for frameworkID := range frameworkIDSet {
|
||||
frameworkIDs = append(frameworkIDs, frameworkID)
|
||||
}
|
||||
|
||||
frameworkByID := make(map[gid.GID]*coredata.Framework, len(frameworkIDs))
|
||||
|
||||
if len(frameworkIDs) > 0 {
|
||||
var frameworks coredata.Frameworks
|
||||
if err := frameworks.LoadByIDs(ctx, conn, scope, frameworkIDs); err != nil && !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return nil, fmt.Errorf("cannot load frameworks by IDs: %w", err)
|
||||
}
|
||||
|
||||
for _, framework := range frameworks {
|
||||
frameworkByID[framework.ID] = framework
|
||||
}
|
||||
}
|
||||
|
||||
labels := make([]string, 0, len(reportFileIDs))
|
||||
|
||||
for _, fileID := range reportFileIDs {
|
||||
file, ok := fileByID[fileID]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("cannot load report file %q: %w", fileID, coredata.ErrResourceNotFound)
|
||||
}
|
||||
|
||||
audit, ok := auditByFileID[fileID]
|
||||
if !ok {
|
||||
labels = append(labels, file.FileName)
|
||||
continue
|
||||
}
|
||||
|
||||
framework, ok := frameworkByID[audit.FrameworkID]
|
||||
if !ok {
|
||||
labels = append(labels, file.FileName)
|
||||
continue
|
||||
}
|
||||
|
||||
if audit.Name != nil && *audit.Name != "" {
|
||||
labels = append(labels, fmt.Sprintf("%s - %s", framework.Name, *audit.Name))
|
||||
continue
|
||||
}
|
||||
|
||||
labels = append(labels, framework.Name)
|
||||
}
|
||||
|
||||
return labels, nil
|
||||
}
|
||||
178
pkg/complianceportal/visitor/portal_file_service.go
Normal file
178
pkg/complianceportal/visitor/portal_file_service.go
Normal file
@@ -0,0 +1,178 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.probo.inc/probo/pkg/pdfutils"
|
||||
)
|
||||
|
||||
func (s *Service) GetPortalFile(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
trustCenterFileID gid.GID,
|
||||
) (*coredata.TrustCenterFile, error) {
|
||||
trustCenterFile := &coredata.TrustCenterFile{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := trustCenterFile.LoadByID(ctx, conn, scope, trustCenterFileID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if trustCenterFile.OrganizationID != organizationID {
|
||||
return nil, ErrTrustCenterFileNotFound
|
||||
}
|
||||
|
||||
if trustCenterFile.TrustCenterVisibility == coredata.TrustCenterVisibilityNone {
|
||||
return nil, ErrTrustCenterFileNotVisible
|
||||
}
|
||||
|
||||
return trustCenterFile, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListPortalFilesForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.TrustCenterFileOrderField],
|
||||
filter *coredata.TrustCenterFileFilter,
|
||||
) (*page.Page[*coredata.TrustCenterFile, coredata.TrustCenterFileOrderField], error) {
|
||||
var trustCenterFiles coredata.TrustCenterFiles
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := trustCenterFiles.LoadByOrganizationID(ctx, conn, scope, organizationID, cursor, filter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center files: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(trustCenterFiles, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportPortalFile(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterFileID gid.GID,
|
||||
email mail.Addr,
|
||||
) ([]byte, string, error) {
|
||||
fileData, mimeType, err := s.exportPortalFileData(ctx, scope, trustCenterFileID)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot export trust center file: %w", err)
|
||||
}
|
||||
|
||||
if mimeType == "application/pdf" {
|
||||
watermarkedPDF, err := pdfutils.AddConfidentialWithTimestamp(fileData, email)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot add watermark to PDF: %w", err)
|
||||
}
|
||||
|
||||
return watermarkedPDF, mimeType, nil
|
||||
}
|
||||
|
||||
return fileData, mimeType, nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportPortalFileWithoutWatermark(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterFileID gid.GID,
|
||||
) ([]byte, string, error) {
|
||||
return s.exportPortalFileData(ctx, scope, trustCenterFileID)
|
||||
}
|
||||
|
||||
func (s *Service) exportPortalFileData(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterFileID gid.GID,
|
||||
) ([]byte, string, error) {
|
||||
var (
|
||||
trustCenterFile *coredata.TrustCenterFile
|
||||
file *coredata.File
|
||||
)
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenterFile = &coredata.TrustCenterFile{}
|
||||
if err := trustCenterFile.LoadByID(ctx, conn, scope, trustCenterFileID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center file: %w", err)
|
||||
}
|
||||
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, scope, trustCenterFile.FileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
result, err := s.s3.GetObject(
|
||||
ctx,
|
||||
&s3.GetObjectInput{
|
||||
Bucket: new(s.bucket),
|
||||
Key: new(file.FileKey),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot download file from S3: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = result.Body.Close() }()
|
||||
|
||||
fileData, err := io.ReadAll(result.Body)
|
||||
if err != nil {
|
||||
return nil, "", fmt.Errorf("cannot read file data: %w", err)
|
||||
}
|
||||
|
||||
return fileData, file.MimeType, nil
|
||||
}
|
||||
109
pkg/complianceportal/visitor/portal_reference_service.go
Normal file
109
pkg/complianceportal/visitor/portal_reference_service.go
Normal file
@@ -0,0 +1,109 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) ListPortalReferencesForPortalID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
cursor *page.Cursor[coredata.TrustCenterReferenceOrderField],
|
||||
) (*page.Page[*coredata.TrustCenterReference, coredata.TrustCenterReferenceOrderField], error) {
|
||||
var references coredata.TrustCenterReferences
|
||||
|
||||
err := s.pg.WithConn(
|
||||
|
||||
ctx,
|
||||
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := references.LoadByTrustCenterID(ctx, conn, scope, trustCenterID, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center references: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(references, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) GeneratePortalReferenceLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
referenceID gid.GID,
|
||||
) (string, error) {
|
||||
reference := &coredata.TrustCenterReference{}
|
||||
|
||||
err := s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
return reference.LoadByID(ctx, tx, scope, referenceID)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot load trust center reference: %w", err)
|
||||
}
|
||||
|
||||
file, err := s.fileManager.GetPublicFile(ctx, reference.LogoFileID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return s.fileManager.GenerateFileURL(file), nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalReference(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
referenceID gid.GID,
|
||||
) (*coredata.TrustCenterReference, error) {
|
||||
reference := &coredata.TrustCenterReference{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := reference.LoadByID(ctx, conn, scope, referenceID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center reference: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return reference, nil
|
||||
}
|
||||
359
pkg/complianceportal/visitor/portal_service.go
Normal file
359
pkg/complianceportal/visitor/portal_service.go
Normal file
@@ -0,0 +1,359 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/packages/emails"
|
||||
"go.probo.inc/probo/pkg/complianceportal"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
)
|
||||
|
||||
func (s *Service) GetPortal(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) (*coredata.TrustCenter, error) {
|
||||
var trustCenter *coredata.TrustCenter
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter = &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return trustCenter, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalByOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
) (*coredata.TrustCenter, error) {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := trustCenter.LoadByOrganizationID(ctx, conn, scope, organizationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trustCenter, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalNDAFile(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) (*coredata.File, error) {
|
||||
var file *coredata.File
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.NonDisclosureAgreementFileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, scope, *trustCenter.NonDisclosureAgreementFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *Service) GeneratePortalNDAFileURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (string, error) {
|
||||
var file *coredata.File
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.NonDisclosureAgreementFileID == nil {
|
||||
return fmt.Errorf("no NDA file found")
|
||||
}
|
||||
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, scope, *trustCenter.NonDisclosureAgreementFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
presignedURL, err := s.fileManager.GeneratePresignedURL(ctx, file, expiresIn)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("cannot generate file URL: %w", err)
|
||||
}
|
||||
|
||||
return presignedURL, nil
|
||||
}
|
||||
|
||||
func (s *Service) GeneratePortalLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
compliancePageID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
file := &coredata.File{}
|
||||
compliancePage := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := compliancePage.LoadByID(ctx, conn, scope, compliancePageID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance page: %w", err)
|
||||
}
|
||||
|
||||
if compliancePage.LogoFileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := file.LoadByID(ctx, conn, scope, *compliancePage.LogoFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if compliancePage.LogoFileID == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if file.FileKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
presignedURL, err := s.fileManager.GeneratePresignedURL(ctx, file, expiresIn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate file URL: %w", err)
|
||||
}
|
||||
|
||||
return &presignedURL, nil
|
||||
}
|
||||
|
||||
func (s *Service) GeneratePortalDarkLogoURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
compliancePageID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
file := &coredata.File{}
|
||||
compliancePage := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := compliancePage.LoadByID(ctx, conn, scope, compliancePageID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance page: %w", err)
|
||||
}
|
||||
|
||||
if compliancePage.DarkLogoFileID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := file.LoadByID(ctx, conn, scope, *compliancePage.DarkLogoFileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if compliancePage.DarkLogoFileID == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if file.FileKey == "" {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
presignedURL, err := s.fileManager.GeneratePresignedURL(ctx, file, expiresIn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot generate file URL: %w", err)
|
||||
}
|
||||
|
||||
return &presignedURL, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalEmailPresenterConfig(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
compliancePageID gid.GID,
|
||||
) (emails.PresenterConfig, error) {
|
||||
var (
|
||||
compliancePage = &coredata.TrustCenter{}
|
||||
organization = &coredata.Organization{}
|
||||
logoFile = &coredata.File{}
|
||||
compliancePageURL string
|
||||
emailPresenterCfg = emails.DefaultPresenterConfig(s.baseURL)
|
||||
)
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := compliancePage.LoadByID(ctx, conn, scope, compliancePageID); err != nil {
|
||||
return fmt.Errorf("cannot load compliance page: %w", err)
|
||||
}
|
||||
|
||||
if compliancePage.LogoFileID != nil {
|
||||
if err := logoFile.LoadByID(ctx, conn, scope, *compliancePage.LogoFileID); err != nil {
|
||||
return fmt.Errorf("cannot load logoFile: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := organization.LoadByID(ctx, conn, scope, compliancePage.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
publicURL, err := complianceportal.PublicURLForTrustCenter(
|
||||
ctx,
|
||||
conn,
|
||||
scope,
|
||||
compliancePage,
|
||||
s.baseDomain,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot resolve compliance page URL: %w", err)
|
||||
}
|
||||
|
||||
compliancePageURL = publicURL
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return emailPresenterCfg, err
|
||||
}
|
||||
|
||||
emailPresenterCfg.BaseURL = compliancePageURL
|
||||
|
||||
if compliancePage.LogoFileID != nil {
|
||||
if logoFile.FileKey == "" {
|
||||
return emailPresenterCfg, nil
|
||||
}
|
||||
|
||||
// If logo exists, then we will brand the emails with the org as a sender
|
||||
emailPresenterCfg.SenderCompanyLogoPath = filepath.Join("/api/files/v1/public/", logoFile.ID.String())
|
||||
emailPresenterCfg.SenderCompanyName = organization.Name
|
||||
|
||||
if compliancePage.WebsiteURL != nil {
|
||||
emailPresenterCfg.SenderCompanyWebsiteURL = *compliancePage.WebsiteURL
|
||||
}
|
||||
|
||||
if compliancePage.HeadquarterAddress != nil {
|
||||
emailPresenterCfg.SenderCompanyHeadquarterAddress = *compliancePage.HeadquarterAddress
|
||||
}
|
||||
}
|
||||
|
||||
return emailPresenterCfg, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalMailingList(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) (*coredata.MailingList, error) {
|
||||
var mailingList *coredata.MailingList
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, scope, trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.MailingListID == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
mailingList = &coredata.MailingList{}
|
||||
if err := mailingList.LoadByID(ctx, conn, scope, *trustCenter.MailingListID); err != nil {
|
||||
return fmt.Errorf("cannot load mailing list: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mailingList, nil
|
||||
}
|
||||
179
pkg/complianceportal/visitor/report_service.go
Normal file
179
pkg/complianceportal/visitor/report_service.go
Normal file
@@ -0,0 +1,179 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/mail"
|
||||
"go.probo.inc/probo/pkg/pdfutils"
|
||||
)
|
||||
|
||||
func (s *Service) GetReport(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
fileID gid.GID,
|
||||
) (*coredata.File, error) {
|
||||
file, err := s.loadReportByID(ctx, scope, fileID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if file.OrganizationID != organizationID {
|
||||
return nil, ErrReportNotFound
|
||||
}
|
||||
|
||||
// check the given report file ID is linked to an audit in order to avoid
|
||||
// being able to get any file from the report request.
|
||||
_, err = s.GetAuditByReportFileID(ctx, scope, fileID)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return nil, ErrReportNotFound
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("cannot verify report file: %w", err)
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *Service) loadReportByID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
fileID gid.GID,
|
||||
) (*coredata.File, error) {
|
||||
file := &coredata.File{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
if err := file.LoadActiveByID(ctx, conn, scope, fileID); err != nil {
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *Service) GenerateReportDownloadURL(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
fileID gid.GID,
|
||||
expiresIn time.Duration,
|
||||
) (*string, error) {
|
||||
file, err := s.loadReportByID(ctx, scope, fileID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get file: %w", err)
|
||||
}
|
||||
|
||||
presignClient := s3.NewPresignClient(s.s3)
|
||||
|
||||
presignedReq, err := presignClient.PresignGetObject(
|
||||
ctx,
|
||||
&s3.GetObjectInput{
|
||||
Bucket: new(s.bucket),
|
||||
Key: new(file.FileKey),
|
||||
ResponseCacheControl: new("max-age=3600, public"),
|
||||
ResponseContentType: new(file.MimeType),
|
||||
ResponseContentDisposition: new(fmt.Sprintf("attachment; filename=\"%s\"", file.FileName)),
|
||||
}, func(opts *s3.PresignOptions) {
|
||||
opts.Expires = expiresIn
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot presign GetObject request: %w", err)
|
||||
}
|
||||
|
||||
return &presignedReq.URL, nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportReportPDF(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
reportID gid.GID,
|
||||
email mail.Addr,
|
||||
) ([]byte, error) {
|
||||
pdfData, err := s.exportReportPDFData(ctx, scope, reportID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot export report PDF: %w", err)
|
||||
}
|
||||
|
||||
watermarkedPDF, err := pdfutils.AddConfidentialWithTimestamp(pdfData, email)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot add watermark to PDF: %w", err)
|
||||
}
|
||||
|
||||
return watermarkedPDF, nil
|
||||
}
|
||||
|
||||
func (s *Service) ExportReportPDFWithoutWatermark(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
reportID gid.GID,
|
||||
) ([]byte, error) {
|
||||
return s.exportReportPDFData(ctx, scope, reportID)
|
||||
}
|
||||
|
||||
func (s *Service) exportReportPDFData(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
fileID gid.GID,
|
||||
) ([]byte, error) {
|
||||
file, err := s.loadReportByID(ctx, scope, fileID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot get file: %w", err)
|
||||
}
|
||||
|
||||
result, err := s.s3.GetObject(
|
||||
ctx,
|
||||
&s3.GetObjectInput{
|
||||
Bucket: new(s.bucket),
|
||||
Key: new(file.FileKey),
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot download PDF from S3: %w", err)
|
||||
}
|
||||
|
||||
defer func() { _ = result.Body.Close() }()
|
||||
|
||||
pdfData, err := io.ReadAll(result.Body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot read PDF data: %w", err)
|
||||
}
|
||||
|
||||
return pdfData, nil
|
||||
}
|
||||
149
pkg/complianceportal/visitor/rights_request_service.go
Normal file
149
pkg/complianceportal/visitor/rights_request_service.go
Normal file
@@ -0,0 +1,149 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"go.probo.inc/probo/pkg/probo"
|
||||
"go.probo.inc/probo/pkg/validator"
|
||||
"go.probo.inc/probo/pkg/webhook"
|
||||
webhooktypes "go.probo.inc/probo/pkg/webhook/types"
|
||||
)
|
||||
|
||||
// RightsRequestDeadlineDays is the number of days a portal-submitted data
|
||||
// subject request is given before its response deadline. Thirty days matches
|
||||
// the GDPR Article 12(3) one-month standard (the shorter of GDPR / CCPA), and
|
||||
// the console can adjust it afterwards.
|
||||
const RightsRequestDeadlineDays = 30
|
||||
|
||||
// CreateRightsRequest is a data subject request submitted from the trust
|
||||
// portal. The organization comes from the current compliance page and the
|
||||
// contact from the verified viewer's identity, so neither is client-supplied.
|
||||
type CreateRightsRequest struct {
|
||||
OrganizationID gid.GID
|
||||
RequestType coredata.RightsRequestType
|
||||
DataSubject *string
|
||||
Contact string
|
||||
Details *string
|
||||
}
|
||||
|
||||
// Validate bounds the free-text fields with the same rules the console applies,
|
||||
// so this public portal mutation can't persist oversized or unsafe input.
|
||||
func (r *CreateRightsRequest) Validate() error {
|
||||
v := validator.New()
|
||||
|
||||
v.Check(r.DataSubject, "data_subject", validator.SafeText(probo.ContentMaxLength))
|
||||
v.Check(r.Details, "details", validator.SafeText(probo.ContentMaxLength))
|
||||
|
||||
return v.Error()
|
||||
}
|
||||
|
||||
func (s *Service) CreateRightsRequest(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
req *CreateRightsRequest,
|
||||
) (*coredata.RightsRequest, error) {
|
||||
if err := req.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
deadline := now.AddDate(0, 0, RightsRequestDeadlineDays)
|
||||
|
||||
request := &coredata.RightsRequest{
|
||||
ID: gid.New(scope.GetTenantID(), coredata.RightsRequestEntityType),
|
||||
OrganizationID: req.OrganizationID,
|
||||
RequestType: req.RequestType,
|
||||
RequestState: coredata.RightsRequestStateTodo,
|
||||
DataSubject: req.DataSubject,
|
||||
Contact: &req.Contact,
|
||||
Details: req.Details,
|
||||
Deadline: &deadline,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
err := s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
organization := &coredata.Organization{}
|
||||
if err := organization.LoadByID(ctx, tx, scope, req.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
if err := request.Insert(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot insert rights request: %w", err)
|
||||
}
|
||||
|
||||
if err := webhook.InsertData(
|
||||
ctx,
|
||||
tx,
|
||||
scope,
|
||||
request.OrganizationID,
|
||||
coredata.WebhookEventTypeRightRequestCreated,
|
||||
webhooktypes.NewRightsRequest(request),
|
||||
); err != nil {
|
||||
return fmt.Errorf("cannot insert webhook event: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListRightsRequestsForOrganizationIDAndContact(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
contact string,
|
||||
cursor *page.Cursor[coredata.RightsRequestOrderField],
|
||||
) (*page.Page[*coredata.RightsRequest, coredata.RightsRequestOrderField], error) {
|
||||
var requests coredata.RightsRequests
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := requests.LoadByOrganizationIDAndContact(ctx, conn, scope, organizationID, contact, cursor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load rights requests: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(requests, cursor), nil
|
||||
}
|
||||
152
pkg/complianceportal/visitor/rights_request_service_test.go
Normal file
152
pkg/complianceportal/visitor/rights_request_service_test.go
Normal file
@@ -0,0 +1,152 @@
|
||||
// Copyright (c) 2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/internal/test"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
webhooktypes "go.probo.inc/probo/pkg/webhook/types"
|
||||
)
|
||||
|
||||
func TestRightsRequestService_CreateEnqueuesWebhook(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
client := test.PGClient(t)
|
||||
organizationID := insertPortalRequestWebhookOrganization(t, client)
|
||||
scope := coredata.NewScope(organizationID.TenantID())
|
||||
insertPortalRequestWebhookSubscription(t, client, scope, organizationID)
|
||||
|
||||
service := Service{pg: client}
|
||||
dataSubject := "Jane Doe"
|
||||
contact := "jane@example.com"
|
||||
rightsRequest, err := service.CreateRightsRequest(
|
||||
t.Context(),
|
||||
scope,
|
||||
&CreateRightsRequest{
|
||||
OrganizationID: organizationID,
|
||||
RequestType: coredata.RightsRequestTypeAccess,
|
||||
DataSubject: &dataSubject,
|
||||
Contact: contact,
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
var data []byte
|
||||
|
||||
err = client.WithConn(
|
||||
t.Context(),
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return conn.QueryRow(
|
||||
ctx,
|
||||
`SELECT data FROM webhook_data WHERE organization_id = $1 AND event_type = $2`,
|
||||
organizationID.String(),
|
||||
coredata.WebhookEventTypeRightRequestCreated.String(),
|
||||
).Scan(&data)
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
payload := &webhooktypes.RightsRequest{}
|
||||
require.NoError(t, json.Unmarshal(data, payload))
|
||||
assert.Equal(t, rightsRequest.ID, payload.ID)
|
||||
assert.Equal(t, coredata.RightsRequestStateTodo, payload.RequestState)
|
||||
assert.Equal(t, dataSubject, *payload.DataSubject)
|
||||
assert.Equal(t, contact, *payload.Contact)
|
||||
assert.NotNil(t, payload.Deadline)
|
||||
}
|
||||
|
||||
func insertPortalRequestWebhookOrganization(t *testing.T, client *pg.Client) gid.GID {
|
||||
t.Helper()
|
||||
|
||||
tenantID := gid.NewTenantID()
|
||||
organizationID := gid.New(tenantID, coredata.OrganizationEntityType)
|
||||
now := time.Now()
|
||||
|
||||
err := client.WithConn(
|
||||
t.Context(),
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
_, err := conn.Exec(
|
||||
ctx,
|
||||
`INSERT INTO organizations (id, tenant_id, name, created_at, updated_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
organizationID.String(),
|
||||
tenantID.String(),
|
||||
"portal-request-webhook-"+organizationID.String(),
|
||||
now,
|
||||
now,
|
||||
)
|
||||
|
||||
return err
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
t.Cleanup(func() {
|
||||
ctx, cancel := context.WithTimeout(t.Context(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_ = client.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
_, err := conn.Exec(ctx, "DELETE FROM organizations WHERE id = $1", organizationID.String())
|
||||
return err
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
return organizationID
|
||||
}
|
||||
|
||||
func insertPortalRequestWebhookSubscription(
|
||||
t *testing.T,
|
||||
client *pg.Client,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
now := time.Now()
|
||||
subscription := coredata.WebhookSubscription{
|
||||
ID: gid.New(organizationID.TenantID(), coredata.WebhookSubscriptionEntityType),
|
||||
OrganizationID: organizationID,
|
||||
EndpointURL: "https://example.test/webhook",
|
||||
SelectedEvents: coredata.WebhookEventTypes{coredata.WebhookEventTypeRightRequestCreated},
|
||||
EncryptedSigningSecret: []byte("test-signing-secret"),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
err := client.WithTx(
|
||||
t.Context(),
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
return subscription.Insert(ctx, tx, scope)
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
9
pkg/complianceportal/visitor/robots.txt.tmpl
Normal file
9
pkg/complianceportal/visitor/robots.txt.tmpl
Normal file
@@ -0,0 +1,9 @@
|
||||
{{- if .Indexable }}
|
||||
User-Agent: *
|
||||
Allow: /
|
||||
|
||||
Sitemap: {{ .BaseURL }}/sitemap.xml
|
||||
{{- else }}
|
||||
User-Agent: *
|
||||
Disallow: /
|
||||
{{- end }}
|
||||
440
pkg/complianceportal/visitor/service.go
Normal file
440
pkg/complianceportal/visitor/service.go
Normal file
@@ -0,0 +1,440 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"go.gearno.de/kit/log"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/packages/emails"
|
||||
"go.probo.inc/probo/pkg/complianceportal"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/esign"
|
||||
"go.probo.inc/probo/pkg/filemanager"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/html2pdf"
|
||||
"go.probo.inc/probo/pkg/iam"
|
||||
"go.probo.inc/probo/pkg/resourcealias"
|
||||
"go.probo.inc/probo/pkg/slack"
|
||||
)
|
||||
|
||||
const NDAConsentText = "By clicking \"Review and sign\", I consent to sign this document electronically and agree that my electronic signature has the same legal validity as a handwritten signature. If you have questions about the NDA, please contact security@probo.com."
|
||||
|
||||
type (
|
||||
// Service is the visitor-facing compliance portal service. It exposes the
|
||||
// public read operations for the trust center and its related resources as
|
||||
// methods on a single type.
|
||||
Service struct {
|
||||
pg *pg.Client
|
||||
s3 *s3.Client
|
||||
bucket string
|
||||
slackSigningSecret string
|
||||
baseURL string
|
||||
baseDomain string
|
||||
iam *iam.Service
|
||||
esign *esign.Service
|
||||
html2pdfConverter *html2pdf.Converter
|
||||
fileManager *filemanager.Service
|
||||
logger *log.Logger
|
||||
slack *slack.Service
|
||||
resourceAlias *resourcealias.Service
|
||||
}
|
||||
)
|
||||
|
||||
func NewService(
|
||||
pgClient *pg.Client,
|
||||
s3Client *s3.Client,
|
||||
bucket string,
|
||||
baseURL string,
|
||||
baseDomain string,
|
||||
slackSigningSecret string,
|
||||
iam *iam.Service,
|
||||
esignSvc *esign.Service,
|
||||
html2pdfConverter *html2pdf.Converter,
|
||||
fileManagerService *filemanager.Service,
|
||||
logger *log.Logger,
|
||||
slack *slack.Service,
|
||||
resourceAliasSvc *resourcealias.Service,
|
||||
) *Service {
|
||||
svc := &Service{
|
||||
pg: pgClient,
|
||||
s3: s3Client,
|
||||
bucket: bucket,
|
||||
slackSigningSecret: slackSigningSecret,
|
||||
baseURL: baseURL,
|
||||
baseDomain: baseDomain,
|
||||
iam: iam,
|
||||
esign: esignSvc,
|
||||
html2pdfConverter: html2pdfConverter,
|
||||
fileManager: fileManagerService,
|
||||
logger: logger,
|
||||
slack: slack,
|
||||
resourceAlias: resourceAliasSvc,
|
||||
}
|
||||
|
||||
return svc
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalByID(
|
||||
ctx context.Context,
|
||||
id gid.GID,
|
||||
) (*coredata.TrustCenter, error) {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := trustCenter.LoadByID(ctx, conn, coredata.NewNoScope(), id)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrPageNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trustCenter, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalBySlug(
|
||||
ctx context.Context,
|
||||
slug string,
|
||||
) (*coredata.TrustCenter, error) {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := trustCenter.LoadBySlug(ctx, conn, slug)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrPageNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trustCenter, nil
|
||||
}
|
||||
|
||||
// GetEffectiveCanonicalHost returns the host a compliance page should be
|
||||
// served under. It prefers the primary domain when its certificate is active,
|
||||
// and otherwise falls back to the managed probopage subdomain. An empty string
|
||||
// is returned when no serving host can be determined.
|
||||
func (s *Service) GetPortalEffectiveCanonicalHost(ctx context.Context, trustCenterID gid.GID) (string, error) {
|
||||
var host string
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, coredata.NewNoScope(), trustCenterID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
domain, err := complianceportal.EffectiveDomainForTrustCenter(ctx, conn, coredata.NewNoScope(), trustCenter)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if domain != nil {
|
||||
host = domain.Domain
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return host, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalByDomainName(ctx context.Context, domain string) (*coredata.TrustCenter, error) {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
var customDomain coredata.CustomDomain
|
||||
if err := customDomain.LoadByDomain(ctx, conn, coredata.NewNoScope(), domain); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrPageNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load custom domain: %w", err)
|
||||
}
|
||||
|
||||
trustCenter = &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByDomainID(ctx, conn, customDomain.ID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrPageNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return trustCenter, err
|
||||
}
|
||||
|
||||
// GetPortalEmailPresenterConfigByOrganizationID resolves the emails.PresenterConfig for
|
||||
// the trust center that belongs to the given organization. This is used by the
|
||||
// esign certificate worker which needs per-org branding at render time.
|
||||
func (s *Service) GetPortalEmailPresenterConfigByOrganizationID(ctx context.Context, orgID gid.GID) (emails.PresenterConfig, error) {
|
||||
var trustCenter coredata.TrustCenter
|
||||
|
||||
scope := coredata.NewScopeFromObjectID(orgID)
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return trustCenter.LoadByOrganizationID(ctx, conn, scope, orgID)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return emails.PresenterConfig{}, fmt.Errorf("cannot load trust center for org %s: %w", orgID, err)
|
||||
}
|
||||
|
||||
return s.GetPortalEmailPresenterConfig(ctx, scope, trustCenter.ID)
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalOrganization(
|
||||
ctx context.Context,
|
||||
trustCenterID gid.GID,
|
||||
) (*coredata.Organization, error) {
|
||||
trustCenter, err := s.GetPortalByID(ctx, trustCenterID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
org := &coredata.Organization{}
|
||||
|
||||
err = s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return org.LoadByID(ctx, conn, coredata.NewNoScope(), trustCenter.OrganizationID)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cannot load organization: %w", err)
|
||||
}
|
||||
|
||||
return org, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalMembership(ctx context.Context, compliancePageID gid.GID, identityID gid.GID) (*coredata.TrustCenterAccess, error) {
|
||||
membership := &coredata.TrustCenterAccess{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
return membership.LoadByTrustCenterIDAndIdentityID(
|
||||
ctx,
|
||||
conn,
|
||||
coredata.NewScopeFromObjectID(compliancePageID),
|
||||
compliancePageID,
|
||||
identityID,
|
||||
)
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return nil, ErrMembershipNotFound
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return membership, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPortalNDAFileByID(
|
||||
ctx context.Context,
|
||||
compliancePageID gid.GID,
|
||||
) (*coredata.File, error) {
|
||||
var (
|
||||
file *coredata.File
|
||||
scope = coredata.NewScopeFromObjectID(compliancePageID)
|
||||
)
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
trustCenter := &coredata.TrustCenter{}
|
||||
if err := trustCenter.LoadByID(ctx, conn, scope, compliancePageID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
if trustCenter.NonDisclosureAgreementFileID == nil {
|
||||
return ErrNDAFileNotFound
|
||||
}
|
||||
|
||||
file = &coredata.File{}
|
||||
if err := file.LoadByID(ctx, conn, scope, *trustCenter.NonDisclosureAgreementFileID); err != nil {
|
||||
if errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return ErrNDAFileNotFound
|
||||
}
|
||||
|
||||
return fmt.Errorf("cannot load file: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return file, nil
|
||||
}
|
||||
|
||||
func (s *Service) ProvisionPortalMember(
|
||||
ctx context.Context,
|
||||
compliancePageID gid.GID,
|
||||
identityID gid.GID,
|
||||
) (*coredata.TrustCenterAccess, error) {
|
||||
var (
|
||||
access *coredata.TrustCenterAccess
|
||||
now = time.Now()
|
||||
scope = coredata.NewScopeFromObjectID(compliancePageID)
|
||||
)
|
||||
|
||||
err := s.pg.WithTx(
|
||||
ctx,
|
||||
func(ctx context.Context, tx pg.Tx) error {
|
||||
compliancePage := &coredata.TrustCenter{}
|
||||
if err := compliancePage.LoadByID(ctx, tx, scope, compliancePageID); err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
identity := &coredata.Identity{}
|
||||
if err := identity.LoadByID(ctx, tx, identityID); err != nil {
|
||||
return fmt.Errorf("cannot load identity: %w", err)
|
||||
}
|
||||
|
||||
access = &coredata.TrustCenterAccess{}
|
||||
if err := access.LoadByTrustCenterIDAndIdentityID(ctx, tx, scope, compliancePageID, identityID); err != nil {
|
||||
if !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load trust center access: %w", err)
|
||||
}
|
||||
|
||||
access = &coredata.TrustCenterAccess{
|
||||
ID: gid.New(scope.GetTenantID(), coredata.TrustCenterAccessEntityType),
|
||||
OrganizationID: compliancePage.OrganizationID,
|
||||
TenantID: scope.GetTenantID(),
|
||||
IdentityID: identityID,
|
||||
TrustCenterID: compliancePageID,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
var sig *coredata.ElectronicSignature
|
||||
|
||||
if compliancePage.NonDisclosureAgreementFileID != nil && s.esign != nil {
|
||||
var err error
|
||||
|
||||
sig, err = s.esign.CreateSignature(
|
||||
ctx,
|
||||
tx,
|
||||
&esign.CreateSignatureRequest{
|
||||
OrganizationID: access.OrganizationID,
|
||||
DocumentType: coredata.ElectronicSignatureDocumentTypeNDA,
|
||||
FileID: *compliancePage.NonDisclosureAgreementFileID,
|
||||
SignerEmail: identity.EmailAddress,
|
||||
ConsentText: NDAConsentText,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot create pending signature: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if sig != nil {
|
||||
access.ElectronicSignatureID = &sig.ID
|
||||
}
|
||||
|
||||
if err := access.Insert(ctx, tx, scope); err != nil {
|
||||
return fmt.Errorf("cannot insert trust center access: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
profile := &coredata.MembershipProfile{}
|
||||
if err := profile.LoadByIdentityIDAndOrganizationID(
|
||||
ctx,
|
||||
tx,
|
||||
coredata.NewScopeFromObjectID(access.ID),
|
||||
identityID,
|
||||
access.OrganizationID,
|
||||
); err != nil {
|
||||
if !errors.Is(err, coredata.ErrResourceNotFound) {
|
||||
return fmt.Errorf("cannot load profile: %w", err)
|
||||
}
|
||||
|
||||
profile = &coredata.MembershipProfile{
|
||||
ID: gid.New(access.TenantID, coredata.MembershipProfileEntityType),
|
||||
IdentityID: identityID,
|
||||
OrganizationID: access.OrganizationID,
|
||||
EmailAddress: identity.EmailAddress,
|
||||
Source: coredata.ProfileSourceManual,
|
||||
State: coredata.ProfileStateActive,
|
||||
FullName: identity.FullName,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
if err := profile.Insert(ctx, tx); err != nil {
|
||||
return fmt.Errorf("cannot insert profile: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
return access, err
|
||||
}
|
||||
18
pkg/complianceportal/visitor/sitemap.xml.tmpl
Normal file
18
pkg/complianceportal/visitor/sitemap.xml.tmpl
Normal file
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
<url>
|
||||
<loc>{{ .BaseURL }}/overview</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>{{ .BaseURL }}/documents</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>{{ .BaseURL }}/subprocessors</loc>
|
||||
</url>
|
||||
<url>
|
||||
<loc>{{ .BaseURL }}/updates</loc>
|
||||
</url>
|
||||
{{ range .Documents }} <url>
|
||||
<loc>{{ $.BaseURL }}/documents/{{ . }}</loc>
|
||||
</url>
|
||||
{{ end }}</urlset>
|
||||
177
pkg/complianceportal/visitor/third_party_service.go
Normal file
177
pkg/complianceportal/visitor/third_party_service.go
Normal file
@@ -0,0 +1,177 @@
|
||||
// Copyright (c) 2025-2026 Probo Inc <hello@probo.com>.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
package visitor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
func (s *Service) GetThirdParty(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
thirdPartyID gid.GID,
|
||||
) (*coredata.ThirdParty, error) {
|
||||
thirdParty := &coredata.ThirdParty{}
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
err := thirdParty.LoadByID(ctx, conn, scope, thirdPartyID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load thirdParty: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return thirdParty, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListThirdPartiesForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[coredata.ThirdPartyOrderField],
|
||||
) (*page.Page[*coredata.ThirdParty, coredata.ThirdPartyOrderField], error) {
|
||||
var thirdParties coredata.ThirdParties
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
showOnTrustCenter := true
|
||||
filter := coredata.NewThirdPartyFilter(&showOnTrustCenter, nil, nil, nil, nil)
|
||||
|
||||
err := thirdParties.LoadByOrganizationID(ctx, conn, scope, organizationID, cursor, filter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load thirdParties: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(thirdParties, cursor), nil
|
||||
}
|
||||
|
||||
func (s *Service) ListDistinctTrustCenterCategoriesForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
) ([]coredata.ThirdPartyCategory, error) {
|
||||
var categories []coredata.ThirdPartyCategory
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
thirdParties := &coredata.ThirdParties{}
|
||||
|
||||
result, err := thirdParties.LoadDistinctTrustCenterCategoriesByOrganizationID(ctx, conn, scope, organizationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load thirdParty categories: %w", err)
|
||||
}
|
||||
|
||||
categories = result
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return categories, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListDistinctTrustCenterCountriesForOrganizationID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
organizationID gid.GID,
|
||||
) ([]coredata.CountryCode, error) {
|
||||
var countries []coredata.CountryCode
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) error {
|
||||
thirdParties := &coredata.ThirdParties{}
|
||||
|
||||
result, err := thirdParties.LoadDistinctTrustCenterCountriesByOrganizationID(ctx, conn, scope, organizationID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load thirdParty countries: %w", err)
|
||||
}
|
||||
|
||||
countries = result
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return countries, nil
|
||||
}
|
||||
|
||||
func (s *Service) CountThirdPartiesForPortalID(
|
||||
ctx context.Context,
|
||||
scope coredata.Scoper,
|
||||
trustCenterID gid.GID,
|
||||
) (int, error) {
|
||||
var count int
|
||||
|
||||
err := s.pg.WithConn(
|
||||
ctx,
|
||||
func(ctx context.Context, conn pg.Querier) (err error) {
|
||||
trustCenter, err := s.GetPortal(ctx, scope, trustCenterID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot load trust center: %w", err)
|
||||
}
|
||||
|
||||
thirdParties := &coredata.ThirdParties{}
|
||||
showOnTrustCenter := true
|
||||
filter := coredata.NewThirdPartyFilter(&showOnTrustCenter, nil, nil, nil, nil)
|
||||
|
||||
count, err = thirdParties.CountByOrganizationID(ctx, conn, scope, trustCenter.OrganizationID, filter)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot count thirdParties: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return count, nil
|
||||
}
|
||||
Reference in New Issue
Block a user