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
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{}}
Returns an %Ecto.Changeset{}
for changing the user scheduled_deletion.
Examples
iex> change_scheduled_deletion(user)
%Ecto.Changeset{data: %User{}}
@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{}}
Returns an %Ecto.Changeset{}
for changing the user email.
Examples
iex> change_user_email(user)
%Ecto.Changeset{data: %User{}}
Returns an %Ecto.Changeset{}
for changing the user password.
Examples
iex> change_user_password(user)
%Ecto.Changeset{data: %User{}}
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.
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.
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
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_reset_password_instructions(user, reset_password_url_fun)
View SourceDelivers 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: ...}}
Exchanges an auth token for a session token.
The auth token is removed from the database if successful.
Generates an API token for a user.
Generates an auth token.
Generates a 2FA session token.
Generates a session token.
@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
.
Gets the user with the given signed token.
Gets the user with the given signed token.
Gets a user by email.
Examples
iex> get_user_by_email("foo@example.com")
%User{}
iex> get_user_by_email("unknown@example.com")
nil
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
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
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
@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
@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{}, ...]
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
@spec regenerate_user_backup_codes(Lightning.Accounts.User.t()) :: {:ok, Lightning.Accounts.User.t()} | {:error, Ecto.Changeset.t()}
Regenerates the user backup codes
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{}}
Delivers the update email instructions to the given user.
Examples
iex> request_email_update(user, new_email)
:ok
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{}}
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.
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.
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{}}
@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{}}
@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"})
@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
@spec valid_user_backup_code?(Lightning.Accounts.User.t(), String.t()) :: true | false
Validates if the given Backup code is valid.
@spec valid_user_totp?(Lightning.Accounts.User.t(), String.t()) :: true | false
Validates if the given TOTP code is valid.
Validates the changes for updating a user's email address.
This function ensures that:
- The
email
andcurrent_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{...}