// Copyright (c) 2025 Probo Inc . // // 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 ( "context" "errors" "fmt" "maps" "strings" "time" "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgconn" "go.gearno.de/kit/pg" "go.probo.inc/probo/pkg/gid" "go.probo.inc/probo/pkg/mail" "go.probo.inc/probo/pkg/page" ) type ( User struct { ID gid.GID `db:"id"` EmailAddress mail.Addr `db:"email_address"` HashedPassword []byte `db:"hashed_password"` FullName string `db:"fullname"` EmailAddressVerified bool `db:"email_address_verified"` SAMLSubject *string `db:"saml_subject"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` } Users []*User ErrUserNotFound struct { Identifier string } ErrUserAlreadyExists struct { message string } ) func (e ErrUserNotFound) Error() string { return fmt.Sprintf("user not found: %q", e.Identifier) } func (e ErrUserAlreadyExists) Error() string { return e.message } func (u User) CursorKey(orderBy UserOrderField) page.CursorKey { switch orderBy { case UserOrderFieldCreatedAt: return page.NewCursorKey(u.ID, u.CreatedAt) } panic(fmt.Sprintf("unsupported order by: %s", orderBy)) } func (u *Users) LoadByOrganizationID( ctx context.Context, conn pg.Conn, organizationID gid.GID, cursor *page.Cursor[UserOrderField], ) error { q := ` SELECT id, email_address, hashed_password, email_address_verified, fullname, created_at, updated_at FROM users WHERE id IN ( SELECT user_id FROM authz_memberships WHERE organization_id = @organization_id ) AND %s ` q = fmt.Sprintf(q, cursor.SQLFragment()) args := pgx.StrictNamedArgs{"organization_id": organizationID} maps.Copy(args, cursor.SQLArguments()) rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query users: %w", err) } users, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[User]) if err != nil { return fmt.Errorf("cannot collect users: %w", err) } *u = users return nil } func (u *Users) CountByOrganizationID( ctx context.Context, conn pg.Conn, scope Scoper, organizationID gid.GID, ) (int, error) { q := ` SELECT COUNT(*) FROM users WHERE id IN ( SELECT user_id FROM authz_memberships WHERE organization_id = @organization_id AND %s ) ` q = fmt.Sprintf(q, scope.SQLFragment()) args := pgx.StrictNamedArgs{"organization_id": organizationID} maps.Copy(args, scope.SQLArguments()) row := conn.QueryRow(ctx, q, args) var count int err := row.Scan(&count) if err != nil { return 0, fmt.Errorf("cannot count users: %w", err) } return count, nil } // Tenant id scope is not applied because we want to access users across all tenants for authentication purposes. func (u *User) LoadByEmail( ctx context.Context, conn pg.Conn, email mail.Addr, ) error { q := ` SELECT id, email_address, hashed_password, email_address_verified, fullname, saml_subject, created_at, updated_at FROM users WHERE email_address = @user_email LIMIT 1; ` args := pgx.StrictNamedArgs{"user_email": email} rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query user: %w", err) } user, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[User]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return &ErrUserNotFound{Identifier: email.String()} } return fmt.Errorf("cannot collect user: %w", err) } *u = user return nil } // Tenant id scope is not applied because we want to access users across all tenants for authentication purposes. func (u *User) LoadByID( ctx context.Context, conn pg.Conn, userID gid.GID, ) error { q := ` SELECT id, email_address, hashed_password, email_address_verified, fullname, saml_subject, created_at, updated_at FROM users WHERE id = @user_id LIMIT 1; ` args := pgx.StrictNamedArgs{"user_id": userID} rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query user: %w", err) } user, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[User]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return &ErrUserNotFound{Identifier: userID.String()} } return fmt.Errorf("cannot collect user: %w", err) } *u = user return nil } func (u *User) Insert( ctx context.Context, conn pg.Conn, scope Scoper, ) error { q := ` INSERT INTO users (id, email_address, hashed_password, email_address_verified, fullname, saml_subject, created_at, updated_at) VALUES ( @user_id, @email_address, @hashed_password, @email_address_verified, @fullname, @saml_subject, @created_at, @updated_at ) ` args := pgx.StrictNamedArgs{ "user_id": u.ID, "email_address": u.EmailAddress, "hashed_password": u.HashedPassword, "fullname": u.FullName, "saml_subject": u.SAMLSubject, "created_at": u.CreatedAt, "updated_at": u.UpdatedAt, "email_address_verified": u.EmailAddressVerified, } _, err := conn.Exec(ctx, q, args) if err != nil { var pgErr *pgconn.PgError if errors.As(err, &pgErr) { if pgErr.Code == "23505" && strings.Contains(pgErr.ConstraintName, "email_address") { return &ErrUserAlreadyExists{ message: fmt.Sprintf("user with email %s already exists", u.EmailAddress), } } } return err } return nil } func (u *User) UpdateEmailVerification( ctx context.Context, conn pg.Conn, verified bool, ) error { q := ` UPDATE users SET email_address_verified = @email_address_verified, updated_at = @updated_at WHERE id = @user_id ` args := pgx.StrictNamedArgs{ "user_id": u.ID, "email_address_verified": verified, "updated_at": time.Now(), } _, err := conn.Exec(ctx, q, args) if err != nil { return fmt.Errorf("cannot update user email verification: %w", err) } u.EmailAddressVerified = verified u.UpdatedAt = args["updated_at"].(time.Time) return nil } func (u *User) UpdatePassword( ctx context.Context, conn pg.Conn, hashedPassword []byte, ) error { q := ` UPDATE users SET hashed_password = @hashed_password, updated_at = @updated_at WHERE id = @user_id ` now := time.Now() args := pgx.StrictNamedArgs{ "user_id": u.ID, "hashed_password": hashedPassword, "updated_at": now, } _, err := conn.Exec(ctx, q, args) if err != nil { return fmt.Errorf("cannot update user password: %w", err) } u.HashedPassword = hashedPassword u.UpdatedAt = now return nil } func (u *User) Update(ctx context.Context, conn pg.Conn) error { q := ` UPDATE users SET email_address = @email_address, email_address_verified = @email_address_verified, saml_subject = @saml_subject, updated_at = @updated_at WHERE id = @user_id ` args := pgx.StrictNamedArgs{ "user_id": u.ID, "email_address": u.EmailAddress, "email_address_verified": u.EmailAddressVerified, "saml_subject": u.SAMLSubject, "updated_at": u.UpdatedAt, } _, err := conn.Exec(ctx, q, args) if err != nil { return fmt.Errorf("cannot update user: %w", err) } return nil } // LoadBySAMLSubject loads a user by their SAML subject (NameID) func (u *User) LoadBySAMLSubject( ctx context.Context, conn pg.Conn, samlSubject string, ) error { q := ` SELECT id, email_address, hashed_password, email_address_verified, fullname, saml_subject, created_at, updated_at FROM users WHERE saml_subject = @saml_subject LIMIT 1; ` args := pgx.StrictNamedArgs{"saml_subject": samlSubject} rows, err := conn.Query(ctx, q, args) if err != nil { return fmt.Errorf("cannot query user by SAML subject: %w", err) } user, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[User]) if err != nil { if errors.Is(err, pgx.ErrNoRows) { return &ErrUserNotFound{Identifier: samlSubject} } return fmt.Errorf("cannot collect user: %w", err) } *u = user return nil } // LoadByEmailAndTenant, LoadByEmailGlobal, and IsTenantUser methods removed // All users are now global (no tenant_id distinction) // Use LoadByEmail() for all email-based lookups func (u *User) CountMemberships( ctx context.Context, conn pg.Conn, ) (int, error) { q := ` SELECT COUNT(*) FROM authz_memberships WHERE user_id = @user_id ` args := pgx.StrictNamedArgs{"user_id": u.ID} var count int err := conn.QueryRow(ctx, q, args).Scan(&count) if err != nil { return 0, fmt.Errorf("cannot count user memberships: %w", err) } return count, nil } // ConvertToTenantUser method removed // All users are now global (no tenant conversion needed)