View Source Teiserver.Account.UserLib (Teiserver v0.0.4)
Library of user related functions.
Summary
Functions
Tests if a User or user_id has all of the required permissions.
Returns an %Ecto.Changeset{}
for tracking user changes.
Creates a user with no checks, use this for system users or automated processes; for user registration make use of register_user/1
.
Deletes a user.
Generates a strong, though not very human readable, password.
Gets a single user. Can take additional arguments for the query.
Gets a single user.
Gets a single user by their email. If no user is found, returns nil
.
Gets a single user by their user_id. If no user is found, returns nil
.
Gets a single user by their name. If no user is found, returns nil
.
Returns the list of users.
Creates a user specifically via the registration changeset, you should use this for user-registration and create_user/1
for system accounts or automated processes.
Tests if a User or user_id has any of the listed restrictions applied to their account.
Updates a user but only the fields users are allowed to alter themselves.
Updates a user's password.
Updates a user.
Tests is the user name is acceptable. Can be over-ridden using the config fn_user_name_acceptor
Takes a user, a plaintext password and returns a boolean if the password is correct for the user. Note it does this via a secure method to prevent timing attacks, never manually verify the password with standard string comparison.
Functions
@spec allow?( Teiserver.user_id() | Teiserver.Account.User.t(), [String.t()] | String.t() ) :: boolean()
Tests if a User or user_id has all of the required permissions.
If the user doesn't exist you will get back a failure.
Examples
iex> allow?(123, "Permission") true
iex> allow?(123, "NotPermission") false
@spec change_user(Teiserver.Account.User.t(), map()) :: Ecto.Changeset.t()
Returns an %Ecto.Changeset{}
for tracking user changes.
Examples
iex> change_user(user)
%Ecto.Changeset{data: %User{}}
@spec create_user(map()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Creates a user with no checks, use this for system users or automated processes; for user registration make use of register_user/1
.
Examples
iex> create_user(%{field: value})
{:ok, %User{}}
iex> create_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
@spec delete_user(Teiserver.Account.User.t()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Deletes a user.
Examples
iex> delete_user(user)
{:ok, %User{}}
iex> delete_user(user)
{:error, %Ecto.Changeset{}}
@spec generate_password() :: String.t()
Generates a strong, though not very human readable, password.
Examples
iex> generate_password() "d52r8i5BhA6xBtmp7ElHI3Y/U/qztw2jUkgdeoZijWBEYzTf5DSBR5N87283WDiA"
@spec get_user(Teiserver.user_id(), Teiserver.query_args()) :: Teiserver.Account.User.t() | nil
Gets a single user. Can take additional arguments for the query.
Returns nil if the User does not exist.
Examples
iex> get_user(123)
%User{}
iex> get_user(456)
nil
iex> get_user(123, preload: [:extra_user_data])
%User{}
@spec get_user!(Teiserver.user_id(), Teiserver.query_args()) :: Teiserver.Account.User.t()
Gets a single user.
Raises Ecto.NoResultsError
if the User does not exist.
Examples
iex> get_user!(123)
%User{}
iex> get_user!(456)
** (Ecto.NoResultsError)
@spec get_user_by_email(String.t()) :: Teiserver.Account.User.t() | nil
Gets a single user by their email. If no user is found, returns nil
.
Examples
iex> get_user_by_email("noodle@teiserver")
%User{}
iex> get_user_by_email("nobody@nowhere")
nil
@spec get_user_by_id(Teiserver.user_id()) :: Teiserver.Account.User.t() | nil
Gets a single user by their user_id. If no user is found, returns nil
.
Examples
iex> get_user_by_id(123)
%User{}
iex> get_user_by_id(456)
nil
@spec get_user_by_name(String.t()) :: Teiserver.Account.User.t() | nil
Gets a single user by their name. If no user is found, returns nil
.
Examples
iex> get_user_by_name("noodle")
%User{}
iex> get_user_by_name("nobody")
nil
@spec list_users(Teiserver.query_args()) :: [Teiserver.Account.User.t()]
Returns the list of users.
Examples
iex> list_users()
[%User{}, ...]
@spec register_user(map()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Creates a user specifically via the registration changeset, you should use this for user-registration and create_user/1
for system accounts or automated processes.
Examples
iex> register_user(%{field: value})
{:ok, %User{}}
iex> register_user(%{field: bad_value})
{:error, %Ecto.Changeset{}}
@spec restricted?( Teiserver.user_id() | Teiserver.Account.User.t(), [String.t()] | String.t() ) :: boolean()
Tests if a User or user_id has any of the listed restrictions applied to their account.
- Returns
true
to indicate the user is restricted - Returns
false
to indicate the user is not restricted
If the user doesn't exist you will get back a true.
Examples
iex> restricted?(123, "Banned") true
iex> restricted?(123, "NotRestriction") false
@spec update_limited_user(Teiserver.Account.User.t(), map()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Updates a user but only the fields users are allowed to alter themselves.
Examples
iex> update_limited_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_limited_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
@spec update_password(Teiserver.Account.User.t(), map()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Updates a user's password.
Examples
iex> update_password(user, %{field: new_value})
{:ok, %User{}}
iex> update_password(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
@spec update_user(Teiserver.Account.User.t(), map()) :: {:ok, Teiserver.Account.User.t()} | {:error, Ecto.Changeset.t()}
Updates a user.
Examples
iex> update_user(user, %{field: new_value})
{:ok, %User{}}
iex> update_user(user, %{field: bad_value})
{:error, %Ecto.Changeset{}}
Tests is the user name is acceptable. Can be over-ridden using the config fn_user_name_acceptor
@spec valid_password?(Teiserver.Account.User.t(), String.t()) :: boolean()
Takes a user, a plaintext password and returns a boolean if the password is correct for the user. Note it does this via a secure method to prevent timing attacks, never manually verify the password with standard string comparison.