Files
lincodex/backend/internal/usecase/invitation/contacts.go
T

154 lines
6.5 KiB
Go

package invitation
import (
"context"
"errors"
"fmt"
"time"
"github.com/google/uuid"
"lincodex/backend/internal/domain"
"lincodex/backend/internal/port/outbound"
)
const contactsReadLocation = "invitation.Service.ReadContacts"
// ReadContacts returns the contacts that the active grant of one invitation
// allows the organization to read now. Only the owner of the organization
// reads contacts in the MVP. Every other case answers ErrContactGrantNotFound,
// which the caller maps to the same not found: no grant of the invitation, a
// grant of another organization, a revoked grant, a revoked C4 record, and a
// profile that the candidate does not publish or has hidden from the
// organization.
//
// The read checks everything on every call, with no cache. It takes the locks
// in the order of the other candidate-scoped reads: the organization and the
// membership (authorizeOwner), the candidate account, the current C2 record,
// the publication, the C4 record, and the grant. A grant read is the only
// read that records an audit event; it does not consume the card-view quota,
// because the contract does not count contact reads against it.
func (service *Service) ReadContacts(
ctx context.Context,
actorID, organizationID, invitationID uuid.UUID,
) (domain.ContactDisclosure, error) {
if actorID == uuid.Nil || organizationID == uuid.Nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - actor and organization IDs must be set: %w", contactsReadLocation, ErrForbidden)
}
at, err := service.now(contactsReadLocation)
if err != nil {
return domain.ContactDisclosure{}, err
}
var result domain.ContactDisclosure
err = service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
var err error
result, err = service.readContactsInTransaction(ctx, unit, actorID, organizationID, invitationID, at)
return err
})
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - run transaction: %w", contactsReadLocation, err)
}
return result, nil
}
func (service *Service) readContactsInTransaction(
ctx context.Context,
unit outbound.UnitOfWork,
actorID, organizationID, invitationID uuid.UUID,
at time.Time,
) (domain.ContactDisclosure, error) {
if err := authorizeOwner(ctx, unit, actorID, organizationID, contactsReadLocation); err != nil {
return domain.ContactDisclosure{}, err
}
if invitationID == uuid.Nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - the nil identifier names no grant: %w", contactsReadLocation, outbound.ErrContactGrantNotFound)
}
// The unlocked read only names the candidate, which never changes for a
// grant, so readableContactGrant can take the visibility locks before the
// grant lock.
grant, err := unit.ContactGrants().GetByInvitation(ctx, organizationID, invitationID)
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - read contact grant: %w", contactsReadLocation, err)
}
stored, readable, err := readableContactGrant(ctx, unit, grant, organizationID)
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - check contact read: %w", contactsReadLocation, err)
}
if !readable {
return domain.ContactDisclosure{}, fmt.Errorf("%s - the grant does not allow the read now: %w", contactsReadLocation, outbound.ErrContactGrantNotFound)
}
profile, err := unit.CandidateProfiles().Get(ctx, stored.CandidateID())
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - read candidate profile: %w", contactsReadLocation, err)
}
disclosure, err := domain.NewContactDisclosure(stored, profile)
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - build disclosure: %w", contactsReadLocation, err)
}
access, err := domain.NewCandidateCardAccess(domain.CandidateCardAccessInput{
CandidateID: stored.CandidateID(), OrganizationID: organizationID, ActorID: actorID,
Kind: domain.CandidateCardAccessContactRead, ContactFields: stored.Fields().Fields(), OccurredAt: at,
})
if err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - build access audit: %w", contactsReadLocation, err)
}
if err := unit.PrivacyView().RecordCandidateCardAccess(ctx, access); err != nil {
return domain.ContactDisclosure{}, fmt.Errorf("%s - record access audit: %w", contactsReadLocation, err)
}
return disclosure, nil
}
// readableContactGrant applies the checks that decide whether the organization
// may read the contacts of a grant now. It takes the visibility locks first and
// then the C4 record and the grant, the order that the revocations follow. It
// reports true only for an active grant under a current consent, while the
// organization may read the profile, and it returns the grant as stored under
// those locks. The caller holds the locks until its transaction ends.
func readableContactGrant(
ctx context.Context,
unit outbound.UnitOfWork,
grant domain.ContactGrant,
organizationID uuid.UUID,
) (domain.ContactGrant, bool, error) {
visible, err := lockContactVisibility(ctx, unit, grant.CandidateID(), organizationID)
if err != nil {
return domain.ContactGrant{}, false, fmt.Errorf("lock visibility: %w", err)
}
if !visible {
return domain.ContactGrant{}, false, nil
}
stored, consentCurrent, err := unit.ContactGrants().LockForRead(ctx, grant)
if err != nil {
return domain.ContactGrant{}, false, fmt.Errorf("lock contact grant: %w", err)
}
return stored, consentCurrent && stored.Status() == domain.ContactGrantActive, nil
}
// lockContactVisibility share-locks the candidate account, the current C2
// record, and the publication, in the order of the card reads. It reports
// false when the candidate has no active C2 or the publication is not visible
// to the organization, and it holds the locks until the transaction ends, so a
// hiding or a revocation that commits later waits for the read.
func lockContactVisibility(
ctx context.Context,
unit outbound.UnitOfWork,
candidateID, organizationID uuid.UUID,
) (bool, error) {
repository := unit.PrivacyView()
if err := repository.LockCandidateAccountForView(ctx, candidateID); err != nil {
return false, fmt.Errorf("lock candidate account: %w", err)
}
c2RecordID, err := repository.LockCurrentC2ForView(ctx, candidateID)
if errors.Is(err, outbound.ErrCandidateC2Unavailable) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("lock current C2: %w", err)
}
visible, err := repository.LockVisiblePublicationForView(ctx, organizationID, candidateID, c2RecordID)
if err != nil {
return false, fmt.Errorf("lock visible publication: %w", err)
}
return visible, nil
}