View Source Lightning.Accounts (Lightning v2.10.4)

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

Link to this function

apply_user_email(user, password, attrs)

View Source

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{}}
Link to this function

cancel_scheduled_deletion(user_id)

View Source
Link to this function

change_scheduled_deletion(user, attrs \\ %{})

View Source

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

Examples

iex> change_scheduled_deletion(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_superuser_registration(attrs \\ %{})

View Source
@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{}}
Link to this function

change_user(user, attrs)

View Source
Link to this function

change_user_email(user, attrs \\ %{})

View Source

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

Examples

iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_user_info(user, attrs \\ %{})

View Source
Link to this function

change_user_password(user, attrs \\ %{})

View Source

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

Examples

iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}
Link to this function

change_user_registration(attrs \\ %{})

View Source

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

Examples

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

Confirms a user by the given token.

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

Link to this function

confirmation_required?(user)

View Source
Link to this function

delete_auth_token(token)

View Source

Deletes the signed token with the given context.

Link to this function

delete_session_token(token)

View Source

Deletes the signed token with the given context.

Link to this function

delete_sudo_session_token(token)

View Source

Deletes the signed token with the given context.

Deletes a token.

Examples

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

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

Deletes a user.

Examples

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

iex> delete_user(user)
{:error, %Ecto.Changeset{}}
@spec delete_user_totp(Lightning.Accounts.UserTOTP.t()) ::
  {:ok, Lightning.Accounts.UserTOTP.t()} | {:error, Ecto.Changeset.t()}

Deletes the given user's TOTP

Link to this function

deliver_user_confirmation_instructions(user)

View Source

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}
Link to this function

deliver_user_confirmation_instructions(registerer, user)

View Source
Link to this function

deliver_user_reset_password_instructions(user, reset_password_url_fun)

View Source

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: ...}}
Link to this function

exchange_auth_token(auth_token)

View Source

Exchanges an auth token for a session token.

The auth token is removed from the database if successful.

Link to this function

generate_api_token(user)

View Source

Generates an API token for a user.

Link to this function

generate_auth_token(user)

View Source

Generates an auth token.

Link to this function

generate_sudo_session_token(user)

View Source

Generates a 2FA session token.

Link to this function

generate_user_session_token(user)

View Source

Generates a session token.

Link to this function

get_preference(user, key)

View Source
@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

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)

Gets a single user.

Examples

iex> get_user(123)
%User{}

iex> get_user!(456)
nil

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

See get_user/1.

Link to this function

get_user_by_api_token(claims)

View Source

Gets the user with the given signed token.

Link to this function

get_user_by_auth_token(token)

View Source

Gets the user with the given signed token.

Link to this function

get_user_by_email(email)

View Source

Gets a user by email.

Examples

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

iex> get_user_by_email("unknown@example.com")
nil
Link to this function

get_user_by_email_and_password(email, password)

View Source

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
Link to this function

get_user_by_reset_password_token(token)

View Source

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
Link to this function

get_user_by_session_token(token)

View Source

Gets the user with the given signed token.

Gets a single UserTOTP if any exists.

Link to this function

get_users_to_alert_for_project(map)

View Source

Gets all users to alert of workflow failure for a project

Link to this function

has_activity_in_projects?(user)

View Source
@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.

Lists all user tokens

Link to this function

list_user_backup_codes(user)

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

Lists the user backup codes

Returns the list of users.

Examples

iex> list_users()
[%User{}, ...]
Link to this function

list_users_by_emails(emails)

View Source

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.

@spec purge_user(id :: Ecto.UUID.t()) :: :ok
Link to this function

regenerate_user_backup_codes(user)

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

Regenerates the user backup codes

Link to this function

register_superuser(attrs)

View Source

Registers a superuser.

Examples

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

iex> register_superuser(%{field: bad_value})
{:error, %Ecto.Changeset{}}
@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{}}
Link to this function

remind_account_confirmation(user)

View Source
Link to this function

request_email_update(user, new_email)

View Source

Delivers the update email instructions to the given user.

Examples

iex> request_email_update(user, new_email)
:ok
Link to this function

reset_user_password(user, attrs)

View Source

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{}}
Link to this function

schedule_user_deletion(user, email)

View Source

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.)

See Lightning.Accounts.Events.subscribe/0.

Link to this function

sudo_session_token_valid?(user, token)

View Source

Checks if the given sudo token for the user is valid

Link to this function

update_user_details(user, attrs \\ %{})

View Source
Link to this function

update_user_email(user, token)

View Source

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.

Link to this function

update_user_info(user, attrs)

View Source
Link to this function

update_user_password(user, password, attrs)

View Source

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{}}
Link to this function

update_user_preference(user, key, value)

View Source
@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{}}
Link to this function

update_user_preferences(user, preferences)

View Source
@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"})
Link to this function

upsert_user_totp(totp, attrs)

View Source
@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

Link to this function

valid_user_backup_code?(user, code)

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

Validates if the given Backup code is valid.

Link to this function

valid_user_totp?(user, code)

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

Validates if the given TOTP code is valid.

Link to this function

validate_change_user_email(user, params \\ %{})

View Source

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{...}