Linkhut.Accounts.User (linkhut v0.1.0)

View Source

The User schema represents a user account in the Linkhut application.

A user can have different account types (unconfirmed, active_free, active_paying) and optional admin roles. Each user has an associated credential for authentication, can own multiple links, and participates in the OAuth system through applications, access grants, and access tokens.

Schema Fields

  • username - Unique identifier for the user; must be alphanumeric and at least 3 characters
  • bio - Optional information about the user
  • unlisted - Boolean flag to hide the user's links from public listings
  • type - Account status enum: :unconfirmed, :active_free, or :active_paying
  • roles - Array of user roles, currently supports :admin

Associations

  • credential - One-to-one association with user authentication credentials
  • links - One-to-many association with user's saved links
  • applications - One-to-many association with OAuth applications owned by the user
  • access_grants - One-to-many association with OAuth access grants
  • access_tokens - One-to-many association with OAuth access tokens

Virtual Fields

  • authenticated_at - Timestamp of last authentication (virtual field)

Examples

iex> changeset = User.changeset(%User{}, %{username: "johndoe", bio: "Hello world"})
iex> changeset.valid?
true

iex> User.confirm_user(%User{credential: %Credential{}}, %{})
%Ecto.Changeset{changes: %{type: :active_free}}

Summary

Functions

Creates a changeset for updating user profile information.

Creates a changeset for updating user roles.

Creates a changeset to confirm a user's email and activate their account.

Types

t()

@type t() :: Ecto.Schema.t()

Functions

ban_user(user)

changeset(user, attrs)

Creates a changeset for updating user profile information.

Validates the username format and ensures it's not reserved.

Parameters

  • user - The user struct or changeset to update
  • attrs - Map of attributes to update (username, bio)

Returns

An Ecto.Changeset with the validated changes.

Examples

iex> User.changeset(%User{}, %{username: "validuser", bio: "My bio"})
%Ecto.Changeset{valid?: true}

iex> User.changeset(%User{}, %{username: "ab"})
%Ecto.Changeset{valid?: false, errors: [username: {"...", [...]}]}

changeset_role(user, attrs)

@spec changeset_role(Ecto.Schema.t() | Ecto.Changeset.t(), map()) ::
  Ecto.Changeset.t()

Creates a changeset for updating user roles.

Parameters

  • user - The user struct or changeset to update
  • attrs - Map containing the roles to set

Returns

An Ecto.Changeset with validated role changes.

Examples

iex> User.changeset_role(%User{}, %{roles: [:admin]})
%Ecto.Changeset{valid?: true}

iex> User.changeset_role(%User{}, %{roles: [:invalid]})
%Ecto.Changeset{valid?: false}

confirm_user(user, attrs)

@spec confirm_user(Ecto.Schema.t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()

Creates a changeset to confirm a user's email and activate their account.

This function confirms the user's email credential and upgrades their account type from :unconfirmed to :active_free.

Parameters

  • user - User struct with associated credential
  • attrs - Attributes for email confirmation

Returns

An Ecto.Changeset that confirms the credential and updates user type.

Examples

iex> user = %User{credential: %Credential{}}
iex> User.confirm_user(user, %{})
%Ecto.Changeset{changes: %{type: :active_free}}

unban_user(user)