Files
warmbly/internal/models/user.go

130 lines
5.2 KiB
Go

package models
import (
"strings"
"time"
"github.com/google/uuid"
)
type User struct {
ID uuid.UUID `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
// Email is the sign-in address, and where platform mail goes. Shown to people only through VisibleEmail.
Email string `json:"email"`
AvatarURL *string `json:"avatar_url,omitempty"`
Roles []uuid.UUID `json:"roles"`
// ContactEmail is the address a partner gave for a person it signs in; never a sign-in or a mail destination.
ContactEmail string `json:"contact_email,omitempty"`
// PartnerAccount marks an account that signs in only through the partner managing its workspace (set by ForDisplay).
PartnerAccount bool `json:"partner_account,omitempty"`
ReferralSource *string `json:"referral_source"`
OnboardingCompletedAt *time.Time `json:"onboarding_completed_at"`
// When the dashboard's product tour was finished or skipped; nil shows it.
ProductTourCompletedAt *time.Time `json:"product_tour_completed_at"`
MaxOrganizations int `json:"max_organizations"`
FreeTrialUsed bool `json:"free_trial_used"`
// Seconds an instant send is held before it actually leaves, so
// the user can still cancel it. Bounds live in config
// (UndoSendSecondsMin/Max); default 30.
UndoSendSeconds int `json:"undo_send_seconds"`
// Platform admin access, populated on the /auth/me load. AdminPermissions
// is the raw bitmask from users.admin_permissions; IsAdmin is the derived
// "has any admin permission" flag the admin app gates on.
AdminPermissions AdminPermission `json:"admin_permissions"`
IsAdmin bool `json:"is_admin"`
// SessionMFAVerified reports whether the session making this request
// presented a second factor. The admin panel needs it to explain why an
// admin account is being refused, rather than showing a bare 403 on the
// first data call.
SessionMFAVerified bool `json:"session_mfa_verified"`
// Set when the user has scheduled their own account for deletion.
// While these are populated the account is "pending deletion" and
// gets hard-deleted at DeletionScheduledFor unless cancelled.
DeletionScheduledAt *time.Time `json:"deletion_scheduled_at,omitempty"`
DeletionScheduledFor *time.Time `json:"deletion_scheduled_for,omitempty"`
// The workspace's label registries, for the organization the session
// currently has selected. Always serialized as arrays (never null) so the
// frontend can iterate without optional-chaining every access. Populated
// by the /auth/me handler after the base user load.
Folders []Group `json:"folders"`
Tags []Group `json:"tags"`
Categories []Group `json:"categories"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// PasswordExpiresAt is set only by CreateExemptUser, to the stored expiry of the issued password.
PasswordExpiresAt *time.Time `json:"-"`
}
// VisibleEmail is the address the user is shown under (see the package function).
func (u *User) VisibleEmail() string {
return VisibleEmail(u.Email, u.ContactEmail)
}
// ForDisplay rewrites the user for a response read by people: Email becomes the visible address.
func (u *User) ForDisplay() {
if u == nil {
return
}
u.PartnerAccount = InternalAddress(u.Email)
u.Email, u.ContactEmail = u.VisibleEmail(), ""
}
// InternalAddress reports a sign-in address under the reserved .invalid domain (RFC 2606): a partner's
// service account or a person it signs in. Such an address receives no mail and is never shown.
func InternalAddress(addr string) bool {
a := strings.TrimSuffix(strings.ToLower(strings.TrimSpace(addr)), ">")
return strings.HasSuffix(a, ".invalid")
}
// VisibleEmail is the address a user is shown under: the sign-in address, or for an internal one the
// contact email its partner gave, else nothing (the name alone is shown).
func VisibleEmail(login, contact string) string {
if !InternalAddress(login) {
return login
}
if InternalAddress(contact) {
return ""
}
return contact
}
// IsPendingDeletion reports whether the user has a pending account deletion.
func (u *User) IsPendingDeletion() bool {
return u.DeletionScheduledFor != nil
}
// LoginCodeExemption is one account excused from the emailed login code, as
// the instance check and the operator CLI report it.
type LoginCodeExemption struct {
UserID uuid.UUID `json:"user_id"`
Email string `json:"email"`
Reason *string `json:"reason,omitempty"`
GrantedAt *time.Time `json:"granted_at,omitempty"`
// PasswordExpiresAt is set on a tester whose password was handed out.
PasswordExpiresAt *time.Time `json:"password_expires_at,omitempty"`
TestWorkspaceID *uuid.UUID `json:"test_workspace_id,omitempty"`
SampleDataSeededAt *time.Time `json:"sample_data_seeded_at,omitempty"`
// SampleDataEligible is the seeding check itself: an active tester with an unexpired Test grant.
SampleDataEligible bool `json:"sample_data_eligible"`
// CreatedAsTester marks an account the tester flow created, which alone can be issued a new password.
CreatedAsTester bool `json:"created_as_tester"`
}
type TesterSampleData struct {
OrganizationID uuid.UUID `json:"organization_id"`
Created bool `json:"created"`
SeededAt time.Time `json:"seeded_at"`
}