Lightning.Accounts (Lightning v2.13.5-pre)

View Source

The Accounts context.

Summary

Functions

Emulates that the email will change without actually changing it in the database.

Returns an %Ecto.Changeset{} for changing the user scheduled_deletion.

Returns an %Ecto.Changeset{} for tracking superuser changes.

Returns an %Ecto.Changeset{} for changing the user email.

Returns an %Ecto.Changeset{} for changing the user password.

Returns an %Ecto.Changeset{} for tracking user changes.

Confirms a user by the given token.

Deletes the signed token with the given context.

Deletes the signed token with the given context.

Deletes the signed token with the given context.

Deletes a token.

Deletes a user.

Deletes the given user's TOTP

Delivers the confirmation email instructions to the given user.

Delivers the reset password email to the given user.

Exchanges an auth token for a session token.

Generates an API token for a user.

Generates an auth token.

Generates a 2FA session token.

Generates a session token.

Retrieves a specific preference value for a given user.

Gets a single token.

Gets a single user.

Gets a single user.

Gets the user with the given signed token.

Gets the user with the given signed token.

Gets a user by email.

Gets a user by email and password.

Gets the user by reset password token.

Gets the user with the given signed token.

Gets a single UserTOTP if any exists.

Gets all users to alert of workflow failure for a project

Used to determine if there is at least one Superuser in the system. This triggers the setup page on fresh installs.

Lists all user tokens

Lists the user backup codes

Returns the list of users.

Returns the list of users with the given emails

Perform, when called with %{"type" => "purge_deleted"} will find users that are ready for permanent deletion and purge them.

Regenerates the user backup codes

Registers a superuser.

Registers a user.

Delivers the update email instructions to the given user.

Resets the user password.

Given a user and a confirmation email, this function sets a scheduled deletion date based on the PURGE_DELETED_AFTER_DAYS environment variable. If no ENV is set, this date defaults to NOW but the automatic user purge cronjob will never run. (Note that subsequent logins will be blocked for users pending deletion.)

Checks if the given sudo token for the user is valid

Updates the user email using the given token.

Updates the user password.

Updates a specific key in the user's preferences.

Updates the user preferences.

Updates or Inserts the user's TOTP

Validates if the given Backup code is valid.

Validates if the given TOTP code is valid.

Validates the changes for updating a user's email address.

Functions

apply_user_email(user, password, attrs)

Emulates that the email will change without actually changing it in the database.

Examples

iex> apply_user_email(user, "valid password", %{email: ...})
{:ok, %User{}}role: :superuser
iex> apply_user_email(user, "invalid password", %{email: ...})
{:error, %Ecto.Changeset{}}

cancel_scheduled_deletion(user_id)

change_scheduled_deletion(user, attrs \\ %{})

Returns an %Ecto.Changeset{} for changing the user scheduled_deletion.

Examples

iex> change_scheduled_deletion(user)
%Ecto.Changeset{data: %User{}}

change_superuser_registration(attrs \\ %{})

@spec change_superuser_registration(any()) :: Ecto.Changeset.t()

Returns an %Ecto.Changeset{} for tracking superuser changes.

Examples

iex> change_superuser_registration(user)
%Ecto.Changeset{data: %User{}}

change_user(user, attrs)

change_user_email(user, attrs \\ %{})

Returns an %Ecto.Changeset{} for changing the user email.

Examples

iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}

change_user_info(user, attrs \\ %{})

change_user_password(user, attrs \\ %{})

Returns an %Ecto.Changeset{} for changing the user password.

Examples

iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}

change_user_registration(attrs \\ %{})

Returns an %Ecto.Changeset{} for tracking user changes.

Examples

iex> change_user_registration(user)
%Ecto.Changeset{data: %User{}}

confirm_user(token)

Confirms a user by the given token.

If the token matches, the user account is marked as confirmed and the token is deleted.

confirmation_required?(user)

create_user(attrs)

delete_auth_token(token)

Deletes the signed token with the given context.

delete_session_token(token)

Deletes the signed token with the given context.

delete_sudo_session_token(token)

Deletes the signed token with the given context.

delete_token(token)

Deletes a token.

Examples

iex> delete_token(token)
{:ok, %UserToken{}}

iex> delete_token(token)
{:error, %Ecto.Changeset{}}

delete_user(user)

Deletes a user.

Examples

iex> delete_user(user)
{:ok, %User{}}

iex> delete_user(user)
{:error, %Ecto.Changeset{}}

delete_user_totp(totp)

@spec delete_user_totp(Lightning.Accounts.UserTOTP.t()) ::
  {:ok, Lightning.Accounts.UserTOTP.t()} | {:error, Ecto.Changeset.t()}

Deletes the given user's TOTP

deliver_user_confirmation_instructions(user)

Delivers the confirmation email instructions to the given user.

Examples

iex> deliver_user_confirmation_instructions(user)
{:ok, %{to: ..., body: ...}}

iex> deliver_user_confirmation_instructions(confirmed_user)
{:error, :already_confirmed}

deliver_user_confirmation_instructions(registerer, user)

deliver_user_reset_password_instructions(user, reset_password_url_fun)

Delivers the reset password email to the given user.

Examples

iex> deliver_user_reset_password_instructions(user, &Routes.user_reset_password_url(conn, :edit, &1))
{:ok, %{to: ..., body: ...}}

exchange_auth_token(auth_token)

Exchanges an auth token for a session token.

The auth token is removed from the database if successful.

generate_api_token(user)

Generates an API token for a user.

generate_auth_token(user)

Generates an auth token.

generate_sudo_session_token(user)

Generates a 2FA session token.

generate_user_session_token(user)

Generates a session token.

get_preference(user, key)

@spec get_preference(Lightning.Accounts.User.t(), String.t()) :: any()

Retrieves a specific preference value for a given user.

Returns the value of the specified key from the user's preferences. If the value is the string "true" or "false", it is converted to a boolean.

Examples

iex> get_preference(user, "editor.orientation")
"vertical"

iex> get_preference(user, "notifications.enabled")
true

get_token!(id)

Gets a single token.

Raises Ecto.NoResultsError if the UserToken does not exist.

Examples

iex> get_token!(123)
%UserToken{}

iex> get_token!(456)
** (Ecto.NoResultsError)

get_user(id)

Gets a single user.

Examples

iex> get_user(123)
%User{}

iex> get_user!(456)
nil

get_user!(id)

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

See get_user/1.

get_user_by_api_token(claims)

Gets the user with the given signed token.

get_user_by_auth_token(token)

Gets the user with the given signed token.

get_user_by_email(email)

Gets a user by email.

Examples

iex> get_user_by_email("foo@example.com")
%User{}

iex> get_user_by_email("unknown@example.com")
nil

get_user_by_email_and_password(email, password)

Gets a user by email and password.

Examples

iex> get_user_by_email_and_password("foo@example.com", "correct_password")
%User{}

iex> get_user_by_email_and_password("foo@example.com", "invalid_password")
nil

get_user_by_reset_password_token(token)

Gets the user by reset password token.

Examples

iex> get_user_by_reset_password_token("validtoken")
%User{}

iex> get_user_by_reset_password_token("invalidtoken")
nil

get_user_by_session_token(token)

Gets the user with the given signed token.

get_user_totp(user)

Gets a single UserTOTP if any exists.

get_users_to_alert_for_project(map)

Gets all users to alert of workflow failure for a project

has_activity_in_projects?(user)

has_one_superuser?()

@spec has_one_superuser?() :: boolean()

Used to determine if there is at least one Superuser in the system. This triggers the setup page on fresh installs.

list_api_tokens(user)

Lists all user tokens

list_user_backup_codes(user)

@spec list_user_backup_codes(Lightning.Accounts.User.t()) ::
  [Lightning.Accounts.UserBackupCode.t(), ...] | []

Lists the user backup codes

list_users()

Returns the list of users.

Examples

iex> list_users()
[%User{}, ...]

list_users_by_emails(emails)

Returns the list of users with the given emails

perform(job)

Perform, when called with %{"type" => "purge_deleted"} will find users that are ready for permanent deletion and purge them.

purge_user(id)

@spec purge_user(id :: Ecto.UUID.t()) :: :ok

regenerate_user_backup_codes(user)

@spec regenerate_user_backup_codes(Lightning.Accounts.User.t()) ::
  {:ok, Lightning.Accounts.User.t()} | {:error, Ecto.Changeset.t()}

Regenerates the user backup codes

register_superuser(attrs)

Registers a superuser.

Examples

iex> register_superuser(%{field: value})
{:ok, %User{}}

iex> register_superuser(%{field: bad_value})
{:error, %Ecto.Changeset{}}

register_user(attrs)

@spec register_user(
  :invalid
  | %{optional(:__struct__) => none(), optional(atom() | binary()) => any()}
) ::
  any()

Registers a user.

Examples

iex> register_user(%{field: value})
{:ok, %User{}}

iex> register_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}

remind_account_confirmation(user)

request_email_update(user, new_email)

Delivers the update email instructions to the given user.

Examples

iex> request_email_update(user, new_email)
:ok

reset_user_password(user, attrs)

Resets the user password.

Examples

iex> reset_user_password(user, %{password: "new long password", password_confirmation: "new long password"})
{:ok, %User{}}

iex> reset_user_password(user, %{password: "valid", password_confirmation: "not the same"})
{:error, %Ecto.Changeset{}}

schedule_user_deletion(user, email)

Given a user and a confirmation email, this function sets a scheduled deletion date based on the PURGE_DELETED_AFTER_DAYS environment variable. If no ENV is set, this date defaults to NOW but the automatic user purge cronjob will never run. (Note that subsequent logins will be blocked for users pending deletion.)

subscribe()

See Lightning.Accounts.Events.subscribe/0.

sudo_session_token_valid?(user, token)

Checks if the given sudo token for the user is valid

update_user_details(user, attrs \\ %{})

update_user_email(user, token)

Updates the user email using the given token.

If the token matches, the user email is updated and the token is deleted. The confirmed_at date is also updated to the current time.

update_user_info(user, attrs)

update_user_password(user, password, attrs)

Updates the user password.

Examples

iex> update_user_password(user, "valid password", %{password: ...})
{:ok, %User{}}

iex> update_user_password(user, "invalid password", %{password: ...})
{:error, %Ecto.Changeset{}}

update_user_preference(user, key, value)

@spec update_user_preference(Lightning.Accounts.User.t(), String.t(), any()) ::
  {:ok, Lightning.Accounts.User.t()} | {:error, Ecto.Changeset.t()}

Updates a specific key in the user's preferences.

Merges the new key-value pair into the user's existing preferences and updates the database.

Examples

iex> update_user_preference(user, "editor.orientation", "vertical")
{:ok, %User{}}

iex> update_user_preference(user, "notifications.enabled", true)
{:ok, %User{}}

update_user_preferences(user, preferences)

@spec update_user_preferences(Lightning.Accounts.User.t(), map()) ::
  {:ok, Lightning.Accounts.User.t()} | {:error, Ecto.Changeset.t()}

Updates the user preferences.

Examples

iex> update_user_preferences(%User{}, %{"editor.orientaion" => "vertical"})

upsert_user_totp(totp, attrs)

@spec upsert_user_totp(Lightning.Accounts.UserTOTP.t(), map()) ::
  {:ok, Lightning.Accounts.UserTOTP.t()} | {:error, Ecto.Changeset.t()}

Updates or Inserts the user's TOTP

valid_user_backup_code?(user, code)

@spec valid_user_backup_code?(Lightning.Accounts.User.t(), String.t()) :: true | false

Validates if the given Backup code is valid.

valid_user_totp?(user, code)

@spec valid_user_totp?(Lightning.Accounts.User.t(), String.t()) :: true | false

Validates if the given TOTP code is valid.

validate_change_user_email(user, params \\ %{})

Validates the changes for updating a user's email address.

This function ensures that:

  • The email and current_password fields are present.
  • The new email is in a valid format.
  • The new email is different from the current one.
  • The provided current_password matches the user's password.

Parameters

  • user: The %User{} struct representing the current user.
  • params: A map of parameters containing the new email and current password.

Returns

An Ecto.Changeset containing any validation errors.

Examples

iex> validate_change_user_email(user, %{"email" => "new@example.com", "current_password" => "secret"})
%Ecto.Changeset{...}