Unify trust center file visibility on queries from trust and console and slack
Signed-off-by: Émile Ré <nemile.re@gmail.com>
This commit is contained in:
@@ -520,7 +520,10 @@ all_items AS (
|
||||
tcf.updated_at AS item_updated_at
|
||||
FROM trust_center_files tcf, tenant_organization o
|
||||
WHERE tcf.organization_id = o.organization_id
|
||||
AND tcf.trust_center_visibility = 'PRIVATE'::trust_center_visibility
|
||||
AND (
|
||||
tcf.trust_center_visibility = 'PRIVATE'::trust_center_visibility
|
||||
ORtcf.trust_center_visibility = 'NONE'::trust_center_visibility
|
||||
)
|
||||
),
|
||||
final_items AS (
|
||||
SELECT
|
||||
|
||||
@@ -20,10 +20,10 @@ import (
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
"go.probo.inc/probo/pkg/gid"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -306,6 +306,7 @@ func (t *TrustCenterFiles) LoadAllByOrganizationID(
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organizationID gid.GID,
|
||||
filter *TrustCenterFileFilter,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
@@ -320,16 +321,18 @@ SELECT
|
||||
FROM
|
||||
trust_center_files
|
||||
WHERE
|
||||
%s
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
ORDER BY
|
||||
created_at DESC
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, filter.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
|
||||
65
pkg/coredata/trust_center_file_filter.go
Normal file
65
pkg/coredata/trust_center_file_filter.go
Normal file
@@ -0,0 +1,65 @@
|
||||
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
// PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
package coredata
|
||||
|
||||
import (
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type (
|
||||
TrustCenterFileFilter struct {
|
||||
trustCenterVisibilities []TrustCenterVisibility
|
||||
}
|
||||
)
|
||||
|
||||
type TrustCenterFileOption func(f *TrustCenterFileFilter)
|
||||
|
||||
func NewTrustCenterFileFilter(opts ...TrustCenterFileOption) *TrustCenterFileFilter {
|
||||
f := &TrustCenterFileFilter{}
|
||||
|
||||
for _, opt := range opts {
|
||||
opt(f)
|
||||
}
|
||||
|
||||
return f
|
||||
}
|
||||
|
||||
func WithTrustCenterFileVisibilities(visibilities ...TrustCenterVisibility) TrustCenterFileOption {
|
||||
return func(f *TrustCenterFileFilter) {
|
||||
f.trustCenterVisibilities = visibilities
|
||||
}
|
||||
}
|
||||
|
||||
func (f *TrustCenterFileFilter) SQLArguments() pgx.NamedArgs {
|
||||
var visibilities []string
|
||||
if f.trustCenterVisibilities != nil {
|
||||
visibilities = make([]string, len(f.trustCenterVisibilities))
|
||||
for i, v := range f.trustCenterVisibilities {
|
||||
visibilities[i] = v.String()
|
||||
}
|
||||
}
|
||||
return pgx.NamedArgs{
|
||||
"trust_center_visibilities": visibilities,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *TrustCenterFileFilter) SQLFragment() string {
|
||||
return `CASE
|
||||
WHEN @trust_center_visibilities::trust_center_visibility[] IS NOT NULL THEN
|
||||
trust_center_visibility = ANY(@trust_center_visibilities::trust_center_visibility[])
|
||||
ELSE TRUE
|
||||
END
|
||||
`
|
||||
}
|
||||
@@ -150,8 +150,11 @@ func (s TrustCenterAccessService) Request(
|
||||
trustCenterFileIDs := req.TrustCenterFileIDs
|
||||
if req.TrustCenterFileIDs == nil {
|
||||
var allTrustCenterFiles coredata.TrustCenterFiles
|
||||
filter := coredata.NewTrustCenterFileFilter(
|
||||
coredata.WithTrustCenterFileVisibilities(coredata.TrustCenterVisibilityPrivate, coredata.TrustCenterVisibilityNone),
|
||||
)
|
||||
|
||||
if err := allTrustCenterFiles.LoadAllByOrganizationID(ctx, tx, s.svc.scope, organizationID); err != nil {
|
||||
if err := allTrustCenterFiles.LoadAllByOrganizationID(ctx, tx, s.svc.scope, organizationID, filter); err != nil {
|
||||
return fmt.Errorf("cannot list trust center files: %w", err)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user