Files
probo/pkg/coredata/organization.go
Bryan Frimin 55a8e72c17 Rename external URLs and move profile fields
Rename the compliance external URL concept to compliance custom links,
and move the public-facing profile fields (description, website, email,
headquarter address) off the organization onto the trust center. Backfill
managed default domains for pages that lack them.

Signed-off-by: Bryan Frimin <bryan@probo.com>
2026-07-21 15:43:08 +02:00

395 lines
8.6 KiB
Go

// 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 coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/iam/policy"
"go.probo.inc/probo/pkg/page"
)
type (
Organization struct {
ID gid.GID `db:"id"`
TenantID gid.TenantID `db:"tenant_id"`
Name string `db:"name"`
LogoFileID *gid.GID `db:"logo_file_id"`
HorizontalLogoFileID *gid.GID `db:"horizontal_logo_file_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Organizations []*Organization
)
func (o *Organization) AuthorizationAttributes(
ctx context.Context,
conn pg.Querier,
resourceIDs []gid.GID,
) (policy.AttributesByID, error) {
q := `SELECT id FROM organizations WHERE id = ANY(@resource_ids::text[])`
args := pgx.StrictNamedArgs{
"resource_ids": resourceIDs,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query authorization attributes: %w", err)
}
defer rows.Close()
attrsByID := make(policy.AttributesByID)
for rows.Next() {
var id gid.GID
if err := rows.Scan(&id); err != nil {
return nil, fmt.Errorf("cannot scan authorization attributes: %w", err)
}
attrsByID[id] = policy.Attributes{
"organization_id": id.String(),
}
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("cannot iterate authorization attributes: %w", err)
}
return attrsByID, nil
}
func (o Organization) CursorKey(orderBy OrganizationOrderField) page.CursorKey {
switch orderBy {
case OrganizationOrderFieldName:
return page.NewCursorKey(o.ID, o.Name)
case OrganizationOrderFieldCreatedAt:
return page.NewCursorKey(o.ID, o.CreatedAt)
case OrganizationOrderFieldUpdatedAt:
return page.NewCursorKey(o.ID, o.UpdatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (o *Organization) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
) error {
q := `
SELECT
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
created_at,
updated_at
FROM
organizations
WHERE
%s
AND id = @organization_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organizations: %w", err)
}
organization, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Organization])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect organization: %w", err)
}
*o = organization
return nil
}
func (o *Organizations) LoadByIDs(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationIDs []gid.GID,
) error {
q := `
SELECT
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
created_at,
updated_at
FROM
organizations
WHERE
%s
AND id = ANY(@organization_ids)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"organization_ids": organizationIDs}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organizations: %w", err)
}
organizations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Organization])
if err != nil {
return fmt.Errorf("cannot collect organizations: %w", err)
}
*o = organizations
if len(organizations) != len(gid.NewSet(organizationIDs...)) {
return ErrResourceNotFound
}
return nil
}
func (o *Organizations) LoadByIdentityID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
identityID gid.GID,
cursor *page.Cursor[OrganizationOrderField],
) error {
q := `
WITH identity_org AS (
SELECT
organization_id
FROM
iam_memberships
WHERE
identity_id = @identity_id
)
SELECT
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
created_at,
updated_at
FROM
organizations
INNER JOIN
identity_org ON organizations.id = identity_org.organization_id
WHERE
%s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"identity_id": identityID}
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organizations: %w", err)
}
organizations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Organization])
if err != nil {
return fmt.Errorf("cannot collect organizations: %w", err)
}
*o = organizations
return nil
}
func (o *Organizations) LoadByIdentityIDWithPendingInvitation(
ctx context.Context,
conn pg.Querier,
scope Scoper,
identityID gid.GID,
cursor *page.Cursor[OrganizationOrderField],
) error {
q := `
WITH invited_org AS (
SELECT DISTINCT
p.organization_id
FROM
iam_membership_profiles p
INNER JOIN iam_invitations inv ON inv.user_id = p.id
WHERE
p.identity_id = @identity_id
AND inv.accepted_at IS NULL
AND inv.expires_at > NOW()
)
SELECT
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
created_at,
updated_at
FROM
organizations
INNER JOIN
invited_org ON organizations.id = invited_org.organization_id
WHERE
%s
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.StrictNamedArgs{"identity_id": identityID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, cursor.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query organizations: %w", err)
}
organizations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Organization])
if err != nil {
return fmt.Errorf("cannot collect organizations: %w", err)
}
*o = organizations
return nil
}
func (o *Organization) Insert(
ctx context.Context,
conn pg.Tx,
) error {
q := `
INSERT INTO organizations (
tenant_id,
id,
name,
logo_file_id,
horizontal_logo_file_id,
created_at,
updated_at
) VALUES (@tenant_id, @id, @name, @logo_file_id, @horizontal_logo_file_id, @created_at, @updated_at)
`
args := pgx.StrictNamedArgs{
"tenant_id": o.TenantID,
"id": o.ID,
"name": o.Name,
"logo_file_id": o.LogoFileID,
"horizontal_logo_file_id": o.HorizontalLogoFileID,
"created_at": o.CreatedAt,
"updated_at": o.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return err
}
return nil
}
func (o *Organization) Update(
ctx context.Context,
scope Scoper,
conn pg.Tx,
) error {
q := `
UPDATE organizations
SET
name = @name,
logo_file_id = @logo_file_id,
horizontal_logo_file_id = @horizontal_logo_file_id,
updated_at = @updated_at
WHERE
%s
AND id = @id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"id": o.ID,
"name": o.Name,
"logo_file_id": o.LogoFileID,
"horizontal_logo_file_id": o.HorizontalLogoFileID,
"updated_at": o.UpdatedAt,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot update organization: %w", err)
}
return nil
}
func (o *Organization) Delete(
ctx context.Context,
conn pg.Tx,
organizationID gid.GID,
) error {
q := `
DELETE FROM organizations
WHERE id = @id
`
args := pgx.StrictNamedArgs{"id": o.ID}
_, err := conn.Exec(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot delete organization: %w", err)
}
return nil
}