feat(invitations): accept invitations with C4 consent and contact grants; share the E0 quota across holdings
This commit is contained in:
1 parent
a5990d3fc6
commit
676b3c6e24
59 files changed
+4926
-194
No files matched your search
@@ -0,0 +1,339 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
// ErrInvalidContactGrant reports a grant that breaks a rule of the contact
|
||||
// grant or does not agree with the accepted invitation and its C4 record.
|
||||
ErrInvalidContactGrant = errors.New("invalid contact grant")
|
||||
)
|
||||
|
||||
// ContactRevocationReason says why a contact grant was revoked. It is shown
|
||||
// only to the candidate. The revocation itself belongs to a later operation.
|
||||
type ContactRevocationReason string
|
||||
|
||||
const (
|
||||
// ContactRevocationConsentRevoked closes every grant under a revoked C4.
|
||||
ContactRevocationConsentRevoked ContactRevocationReason = "consent_revoked"
|
||||
// ContactRevocationParticipationCancelled closes the grant of one
|
||||
// invitation that the candidate cancelled.
|
||||
ContactRevocationParticipationCancelled ContactRevocationReason = "participation_cancelled"
|
||||
// ContactRevocationVisibilityClosed closes a grant after the publication
|
||||
// was closed or the organization was hidden.
|
||||
ContactRevocationVisibilityClosed ContactRevocationReason = "visibility_closed"
|
||||
)
|
||||
|
||||
func (reason ContactRevocationReason) known() bool {
|
||||
switch reason {
|
||||
case ContactRevocationConsentRevoked, ContactRevocationParticipationCancelled, ContactRevocationVisibilityClosed:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// ContactAccessBlock says why an active grant does not allow a read now.
|
||||
type ContactAccessBlock string
|
||||
|
||||
// ContactAccessBlockVisibility means the profile is not published (C2) or is
|
||||
// hidden from the organization. The acceptance never publishes the profile or
|
||||
// lifts the hiding, so the grant becomes readable only when the candidate does.
|
||||
const ContactAccessBlockVisibility ContactAccessBlock = "visibility"
|
||||
|
||||
// ContactGrantStatus is the state of a contact grant.
|
||||
type ContactGrantStatus string
|
||||
|
||||
const (
|
||||
ContactGrantActive ContactGrantStatus = "active"
|
||||
ContactGrantRevoked ContactGrantStatus = "revoked"
|
||||
)
|
||||
|
||||
const redactedContactGrant = "[redacted contact grant]"
|
||||
|
||||
// ContactGrant is the permission of one organization to read the selected
|
||||
// contact kinds of one candidate, created by the acceptance of one invitation.
|
||||
// It names the C4 consent record that gave it, so the grant and its consent
|
||||
// always agree. The candidate identifier is internal and never logged.
|
||||
type ContactGrant struct {
|
||||
id uuid.UUID
|
||||
organizationID uuid.UUID
|
||||
candidateID uuid.UUID
|
||||
invitationID uuid.UUID
|
||||
consentRecordID uuid.UUID
|
||||
fields ContactFieldSet
|
||||
termsVersion int64
|
||||
consentTextVersion ConsentTextVersion
|
||||
grantedAt time.Time
|
||||
revokedAt time.Time
|
||||
revocationReason ContactRevocationReason
|
||||
}
|
||||
|
||||
// StoredContactGrant carries the columns of a stored grant. A nil RevokedAt
|
||||
// marks an active grant, and an empty RevocationReason goes with it.
|
||||
type StoredContactGrant struct {
|
||||
ID uuid.UUID
|
||||
OrganizationID uuid.UUID
|
||||
CandidateID uuid.UUID
|
||||
InvitationID uuid.UUID
|
||||
ConsentRecordID uuid.UUID
|
||||
TermsVersion int64
|
||||
Fields []string
|
||||
ConsentTextVersion string
|
||||
GrantedAt time.Time
|
||||
RevokedAt *time.Time
|
||||
RevocationReason string
|
||||
}
|
||||
|
||||
// NewContactGrant builds the grant of an accepted invitation from its C4
|
||||
// consent record. The record and the invitation must name the same subject,
|
||||
// organization, invitation, and terms version, and the invitation must be
|
||||
// accepted. The grant takes the fields, the text version, and the grant time
|
||||
// of the record, so it covers exactly what the candidate consented to.
|
||||
func NewContactGrant(record ConsentRecord, invitation Invitation) (ContactGrant, error) {
|
||||
const location = "domain.NewContactGrant"
|
||||
if record.Purpose() != ConsentPurposeContactDisclosure || record.Source() != ConsentSourceInvitationAcceptance {
|
||||
return ContactGrant{}, fmt.Errorf("%s - the grant needs the C4 record of an acceptance: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
if _, revoked := record.RevokedAt(); revoked {
|
||||
return ContactGrant{}, fmt.Errorf("%s - a revoked C4 record gives no grant: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
if invitation.State() != InvitationAccepted {
|
||||
return ContactGrant{}, fmt.Errorf("%s - only an accepted invitation has a grant: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
switch {
|
||||
case record.OrganizationID() != invitation.OrganizationID(),
|
||||
record.InvitationID() != invitation.ID(),
|
||||
record.SubjectID() != invitation.CandidateID(),
|
||||
record.InvitationTermsVersion() != invitation.TermsVersion():
|
||||
return ContactGrant{}, fmt.Errorf("%s - the C4 record must name the accepted invitation: %w", location, ErrInvalidContactGrant)
|
||||
case record.Fields().IsZero():
|
||||
return ContactGrant{}, fmt.Errorf("%s - the C4 record must name its fields: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return ContactGrant{}, fmt.Errorf("%s - generate grant ID: %w", location, err)
|
||||
}
|
||||
return ContactGrant{
|
||||
id: id,
|
||||
organizationID: invitation.OrganizationID(),
|
||||
candidateID: invitation.CandidateID(),
|
||||
invitationID: invitation.ID(),
|
||||
consentRecordID: record.ID(),
|
||||
fields: record.Fields(),
|
||||
termsVersion: invitation.TermsVersion(),
|
||||
consentTextVersion: record.TextVersion(),
|
||||
grantedAt: record.GrantedAt(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// RestoreContactGrant validates a stored grant, so a row changed outside the
|
||||
// application is not served.
|
||||
func RestoreContactGrant(stored StoredContactGrant) (ContactGrant, error) {
|
||||
const location = "domain.RestoreContactGrant"
|
||||
switch {
|
||||
case stored.ID == uuid.Nil, stored.OrganizationID == uuid.Nil, stored.CandidateID == uuid.Nil,
|
||||
stored.InvitationID == uuid.Nil, stored.ConsentRecordID == uuid.Nil:
|
||||
return ContactGrant{}, fmt.Errorf("%s - identifiers must be set: %w", location, ErrInvalidContactGrant)
|
||||
case stored.TermsVersion < 1, stored.GrantedAt.IsZero():
|
||||
return ContactGrant{}, fmt.Errorf("%s - terms version and grant time must be set: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
fields, err := restoreGrantFields(stored.Fields)
|
||||
if err != nil {
|
||||
return ContactGrant{}, fmt.Errorf("%s - validate fields: %w", location, err)
|
||||
}
|
||||
version, err := ParseConsentTextVersion(stored.ConsentTextVersion)
|
||||
if err != nil {
|
||||
return ContactGrant{}, fmt.Errorf("%s - validate consent text version: %w", location, err)
|
||||
}
|
||||
grant := ContactGrant{
|
||||
id: stored.ID, organizationID: stored.OrganizationID, candidateID: stored.CandidateID,
|
||||
invitationID: stored.InvitationID, consentRecordID: stored.ConsentRecordID, fields: fields,
|
||||
termsVersion: stored.TermsVersion, consentTextVersion: version, grantedAt: stored.GrantedAt,
|
||||
}
|
||||
switch {
|
||||
case stored.RevokedAt == nil && stored.RevocationReason == "":
|
||||
case stored.RevokedAt != nil && ContactRevocationReason(stored.RevocationReason).known() &&
|
||||
!stored.RevokedAt.Before(stored.GrantedAt):
|
||||
grant.revokedAt = *stored.RevokedAt
|
||||
grant.revocationReason = ContactRevocationReason(stored.RevocationReason)
|
||||
default:
|
||||
return ContactGrant{}, fmt.Errorf("%s - the revocation stamp is inconsistent: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
return grant, nil
|
||||
}
|
||||
|
||||
// restoreGrantFields accepts the stored kinds only in the canonical form the
|
||||
// database stores: distinct, known, and ascending.
|
||||
func restoreGrantFields(stored []string) (ContactFieldSet, error) {
|
||||
fields := make([]ContactField, 0, len(stored))
|
||||
for _, value := range stored {
|
||||
field, err := ParseContactField(value)
|
||||
if err != nil {
|
||||
return ContactFieldSet{}, err
|
||||
}
|
||||
fields = append(fields, field)
|
||||
}
|
||||
set, err := NewContactFieldSet(fields...)
|
||||
if err != nil {
|
||||
return ContactFieldSet{}, err
|
||||
}
|
||||
if !slices.Equal(contactFieldCodes(set), stored) {
|
||||
return ContactFieldSet{}, fmt.Errorf("stored fields are not in canonical order: %w", ErrInvalidContactGrant)
|
||||
}
|
||||
return set, nil
|
||||
}
|
||||
|
||||
func (grant ContactGrant) ID() uuid.UUID { return grant.id }
|
||||
func (grant ContactGrant) OrganizationID() uuid.UUID { return grant.organizationID }
|
||||
func (grant ContactGrant) CandidateID() uuid.UUID { return grant.candidateID }
|
||||
func (grant ContactGrant) InvitationID() uuid.UUID { return grant.invitationID }
|
||||
func (grant ContactGrant) ConsentRecordID() uuid.UUID { return grant.consentRecordID }
|
||||
func (grant ContactGrant) Fields() ContactFieldSet { return grant.fields }
|
||||
func (grant ContactGrant) TermsVersion() int64 { return grant.termsVersion }
|
||||
func (grant ContactGrant) ConsentTextVersion() ConsentTextVersion { return grant.consentTextVersion }
|
||||
func (grant ContactGrant) GrantedAt() time.Time { return grant.grantedAt }
|
||||
|
||||
// RevokedAt returns the revocation time, and false while the grant is active.
|
||||
func (grant ContactGrant) RevokedAt() (time.Time, bool) {
|
||||
return grant.revokedAt, !grant.revokedAt.IsZero()
|
||||
}
|
||||
|
||||
// RevocationReason is empty while the grant is active.
|
||||
func (grant ContactGrant) RevocationReason() ContactRevocationReason { return grant.revocationReason }
|
||||
|
||||
// Status is revoked once a revocation stamp is set; a grant never becomes
|
||||
// active again.
|
||||
func (grant ContactGrant) Status() ContactGrantStatus {
|
||||
if grant.revokedAt.IsZero() {
|
||||
return ContactGrantActive
|
||||
}
|
||||
return ContactGrantRevoked
|
||||
}
|
||||
|
||||
// IsZero reports a grant not built by NewContactGrant or RestoreContactGrant.
|
||||
func (grant ContactGrant) IsZero() bool { return grant.id == uuid.Nil }
|
||||
|
||||
// Stored returns the persisted form of the grant.
|
||||
func (grant ContactGrant) Stored() StoredContactGrant {
|
||||
stored := StoredContactGrant{
|
||||
ID: grant.id, OrganizationID: grant.organizationID, CandidateID: grant.candidateID,
|
||||
InvitationID: grant.invitationID, ConsentRecordID: grant.consentRecordID,
|
||||
TermsVersion: grant.termsVersion, Fields: contactFieldCodes(grant.fields),
|
||||
ConsentTextVersion: string(grant.consentTextVersion), GrantedAt: grant.grantedAt,
|
||||
RevocationReason: string(grant.revocationReason),
|
||||
}
|
||||
if !grant.revokedAt.IsZero() {
|
||||
revokedAt := grant.revokedAt
|
||||
stored.RevokedAt = &revokedAt
|
||||
}
|
||||
return stored
|
||||
}
|
||||
|
||||
// ContactGrantView is the grant as the candidate reads it. It holds kinds
|
||||
// only, never contact values, and it names no organization identifier. The
|
||||
// organization reads it through its own view.
|
||||
type ContactGrantView struct {
|
||||
ID uuid.UUID
|
||||
Fields []ContactField
|
||||
Status ContactGrantStatus
|
||||
Readable bool
|
||||
BlockedBy *ContactAccessBlock
|
||||
TermsVersion int64
|
||||
ConsentTextVersion ConsentTextVersion
|
||||
GrantedAt time.Time
|
||||
RevokedAt *time.Time
|
||||
RevocationReason *ContactRevocationReason
|
||||
}
|
||||
|
||||
// CandidateView returns the grant for the candidate. visible reports whether
|
||||
// the organization may read the profile now, which is the publication and the
|
||||
// hiding of the organization. An active grant is readable only when it is
|
||||
// visible; a revoked grant is never readable and shows its reason instead.
|
||||
func (grant ContactGrant) CandidateView(visible bool) ContactGrantView {
|
||||
view := ContactGrantView{
|
||||
ID: grant.id, Fields: grant.fields.Fields(), Status: grant.Status(),
|
||||
TermsVersion: grant.termsVersion, ConsentTextVersion: grant.consentTextVersion, GrantedAt: grant.grantedAt,
|
||||
}
|
||||
if view.Status == ContactGrantRevoked {
|
||||
revokedAt, reason := grant.revokedAt, grant.revocationReason
|
||||
view.RevokedAt, view.RevocationReason = &revokedAt, &reason
|
||||
return view
|
||||
}
|
||||
if visible {
|
||||
view.Readable = true
|
||||
return view
|
||||
}
|
||||
block := ContactAccessBlockVisibility
|
||||
view.BlockedBy = &block
|
||||
return view
|
||||
}
|
||||
|
||||
func (grant ContactGrant) String() string { return redactedContactGrant }
|
||||
func (grant ContactGrant) GoString() string { return redactedContactGrant }
|
||||
func (grant ContactGrant) Format(state fmt.State, _ rune) {
|
||||
writeRedacted(state, redactedContactGrant)
|
||||
}
|
||||
|
||||
// LogValue carries the grant identifier and its status only.
|
||||
func (grant ContactGrant) LogValue() slog.Value {
|
||||
return slog.GroupValue(slog.String("grant_id", grant.id.String()), slog.String("status", string(grant.Status())))
|
||||
}
|
||||
|
||||
// ContactConsentText is the published C4 text that the candidate sees, with
|
||||
// the organization name in it.
|
||||
type ContactConsentText struct {
|
||||
TextVersion ConsentTextVersion
|
||||
Text string
|
||||
}
|
||||
|
||||
// ContactConsentOffer is what the candidate can consent to while an
|
||||
// acceptance is possible: the rendered text and the kinds the profile holds.
|
||||
// Nothing is preselected, and no contact value is part of it.
|
||||
type ContactConsentOffer struct {
|
||||
Text ContactConsentText
|
||||
AvailableFields []ContactField
|
||||
}
|
||||
|
||||
// NewContactConsentOffer renders the C4 text for the organization name and
|
||||
// lists the kinds that the profile holds, in the order given. It needs at least
|
||||
// one kind, because a grant covers at least one field.
|
||||
func NewContactConsentOffer(text ConsentText, organizationName string, available []ContactField) (ContactConsentOffer, error) {
|
||||
const location = "domain.NewContactConsentOffer"
|
||||
if len(available) == 0 || len(available) > maxContactFields {
|
||||
return ContactConsentOffer{}, fmt.Errorf("%s - the offer needs 1 to %d kinds: %w", location, maxContactFields, ErrInvalidContactGrant)
|
||||
}
|
||||
for index, field := range available {
|
||||
if _, err := ParseContactField(string(field)); err != nil {
|
||||
return ContactConsentOffer{}, fmt.Errorf("%s - validate kind: %w", location, err)
|
||||
}
|
||||
if slices.Contains(available[:index], field) {
|
||||
return ContactConsentOffer{}, fmt.Errorf("%s - kinds must be distinct: %w", location, ErrInvalidContactGrant)
|
||||
}
|
||||
}
|
||||
rendered, err := text.RenderForOrganization(organizationName)
|
||||
if err != nil {
|
||||
return ContactConsentOffer{}, fmt.Errorf("%s - render consent text: %w", location, err)
|
||||
}
|
||||
return ContactConsentOffer{
|
||||
Text: ContactConsentText{TextVersion: text.Version(), Text: rendered},
|
||||
AvailableFields: slices.Clone(available),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// contactFieldCodes returns the kinds of a set as the stored strings, in the
|
||||
// set's canonical order.
|
||||
func contactFieldCodes(set ContactFieldSet) []string {
|
||||
codes := make([]string, 0, len(set.Fields()))
|
||||
for _, field := range set.Fields() {
|
||||
codes = append(codes, string(field))
|
||||
}
|
||||
return codes
|
||||
}
|
||||
Reference in new issue
Block a user