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

Link to this function

allow?(user_or_user_id, permissions)

View Source

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

Link to this function

change_user(user, attrs \\ %{})

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

create_user(attrs \\ %{})

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

default_user_name_acceptable?(name)

View Source
@spec default_user_name_acceptable?(String.t()) :: boolean()
@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"

Link to this function

get_user(user_id, query_args \\ [])

View Source

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

get_user!(user_id, query_args \\ [])

View Source

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

get_user_by_email(email)

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

register_user(attrs \\ %{})

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

restricted?(user_or_user_id, permissions)

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

Link to this function

update_limited_user(user, attrs)

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

update_password(user, attrs)

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

update_user(user, attrs)

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

user_name_acceptable?(name)

View Source
@spec user_name_acceptable?(String.t()) :: boolean()

Tests is the user name is acceptable. Can be over-ridden using the config fn_user_name_acceptor

Link to this function

valid_password?(user, plaintext_password)

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