132 lines
4.9 KiB
Go
132 lines
4.9 KiB
Go
// Package evidence reads the skill evidence projection. The candidate reads
|
|
// their own facts; an organization reads only the permitted projection of a
|
|
// card it is currently allowed to see. The projection holds no answers,
|
|
// failures, or anti-cheat data, and this package has no way to reach them.
|
|
package evidence
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
var (
|
|
// ErrForbidden reports an actor of the wrong role or without a verified
|
|
// email.
|
|
ErrForbidden = errors.New("skill evidence is forbidden")
|
|
// ErrInvalidRequest reports a page request outside the contract bounds.
|
|
ErrInvalidRequest = errors.New("skill evidence request is invalid")
|
|
// ErrServiceMissing reports an incomplete service wiring.
|
|
ErrServiceMissing = errors.New("skill evidence service is unavailable")
|
|
)
|
|
|
|
// AuthorizeCategoriesSection runs the shared organization card checks inside
|
|
// the given transaction: active membership, effective C2 publication, hiding of
|
|
// the organization, the minimum cohort, the card-view quota, and the access
|
|
// audit. It returns the candidate and the categories that the card shows. The
|
|
// candidateview usecase provides it; bootstrap passes it in so this package
|
|
// does not depend on another usecase.
|
|
type AuthorizeCategoriesSection func(
|
|
ctx context.Context,
|
|
unit outbound.UnitOfWork,
|
|
actorID, organizationID uuid.UUID,
|
|
handle string,
|
|
) (uuid.UUID, []domain.CandidateViewCategory, error)
|
|
|
|
// Dependencies configure the read service.
|
|
type Dependencies struct {
|
|
Transactor outbound.Transactor
|
|
AuthorizeCategories AuthorizeCategoriesSection
|
|
}
|
|
|
|
// Service reads the evidence projection.
|
|
type Service struct {
|
|
transactor outbound.Transactor
|
|
authorizeCategories AuthorizeCategoriesSection
|
|
}
|
|
|
|
func NewService(deps Dependencies) (*Service, error) {
|
|
if deps.Transactor == nil || deps.AuthorizeCategories == nil {
|
|
return nil, fmt.Errorf("evidence.NewService - dependencies are missing: %w", ErrServiceMissing)
|
|
}
|
|
return &Service{transactor: deps.Transactor, authorizeCategories: deps.AuthorizeCategories}, nil
|
|
}
|
|
|
|
// ListMine returns one owner-scoped page of the candidate's facts, hidden ones
|
|
// included. The candidate comes from the session and nothing else selects the
|
|
// owner.
|
|
func (service *Service) ListMine(ctx context.Context, query outbound.EvidenceListQuery) (outbound.EvidenceFactPage, error) {
|
|
const location = "evidence.Service.ListMine"
|
|
if query.CandidateID == uuid.Nil {
|
|
return outbound.EvidenceFactPage{}, ErrForbidden
|
|
}
|
|
if query.Limit < 1 || query.Limit > outbound.MaxEvidencePageSize {
|
|
return outbound.EvidenceFactPage{}, ErrInvalidRequest
|
|
}
|
|
var page outbound.EvidenceFactPage
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
if err := authorizeCandidate(ctx, unit, query.CandidateID); err != nil {
|
|
return err
|
|
}
|
|
var readErr error
|
|
page, readErr = unit.Evidence().ListCandidateFacts(ctx, query)
|
|
if readErr != nil {
|
|
return fmt.Errorf("%s - read owner facts: %w", location, readErr)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return outbound.EvidenceFactPage{}, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return page, nil
|
|
}
|
|
|
|
// ListForOrganization returns the permitted facts of the card that the handle
|
|
// names for the organization. The membership, the publication, the hiding, and
|
|
// the cohort are rechecked by the card checks, and the repository rechecks the
|
|
// publication and the hiding again in the statement that reads the facts, so a
|
|
// change between the two cannot widen the answer. Nothing returned belongs to
|
|
// a candidate that the organization may not see now.
|
|
func (service *Service) ListForOrganization(
|
|
ctx context.Context,
|
|
actorID, organizationID uuid.UUID,
|
|
handle string,
|
|
) ([]domain.EvidenceOrganizationFact, error) {
|
|
const location = "evidence.Service.ListForOrganization"
|
|
var facts []domain.EvidenceOrganizationFact
|
|
err := service.transactor.WithinTransaction(ctx, func(ctx context.Context, unit outbound.UnitOfWork) error {
|
|
candidateID, categories, err := service.authorizeCategories(ctx, unit, actorID, organizationID, handle)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
facts, err = unit.Evidence().ListOrganizationFacts(ctx, organizationID, candidateID, categories)
|
|
if err != nil {
|
|
return fmt.Errorf("%s - read permitted facts: %w", location, err)
|
|
}
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s - run transaction: %w", location, err)
|
|
}
|
|
return facts, nil
|
|
}
|
|
|
|
func authorizeCandidate(ctx context.Context, unit outbound.UnitOfWork, actorID uuid.UUID) error {
|
|
user, err := unit.Users().GetByID(ctx, actorID)
|
|
if errors.Is(err, outbound.ErrUserNotFound) {
|
|
return ErrForbidden
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("evidence.authorizeCandidate - read actor account: %w", err)
|
|
}
|
|
if !user.EmailVerified() || user.Scenario != domain.ScenarioCandidate {
|
|
return ErrForbidden
|
|
}
|
|
return nil
|
|
}
|