Haytni (Haytni v0.7.0) View Source

Documentation for Haytni.

Link to this section Summary

Functions

Notifies plugins that the authentication failed for user.

Creates an %Ecto.Changeset{} for a new user/account (at registration from a module) or from a user (when editing account from a struct)

Register user from controller's params.

To delete user.

Function to be called, for the user, to modify its own email address: it actually updates it in the database but also invokes, first, the Haytni.Plugin.on_email_change/4 callback of the plugins registered in the module Haytni stack.

Extracts an Haytni stack (module) from a Plug connection

Injects Ecto.Schema.fields necessary to enabled plugins into your User schema

Used by plug to extract the current user (if any) from the HTTP request (meaning from headers, cookies, etc)

Fetchs a user from its id.

Fetches a user from the Ecto.Repo specified in config :haytni, YourApp.Haytni as repo subkey via the attributes specified by clauses as a map or a keyword-list.

Checks if a user is valid according to plugins.

To be called on (manual) login

Notifies plugins that current user is going to be logged out

Returns true if plugin is enabled in the module Haytni stack.

Injects the necessary routes for enabled plugins into your Router

Returns the name of the assign (in Plug.Conn and templates) of the current user

Returns the name of the session key which carries the user

Get the list of shared (templates/views) or independant (Haytni stack) files to install

Update user's registration, its own registration.

Update user in the same way as update_user_with/3 but as part of a set of operations (Ecto.Multi).

Update the given user from a list of changes as Keyword.

Same as update_user_with/3 but returns the updated user struct or raises if changes are invalid.

This function is a callback to be called from your User.create_registration_changeset/2 so validations and others internal tasks can be done by plugins at user's registration.

Runs any custom password validations from the plugins (via their Haytni.Plugin.validate_password/3 callback) of the module Haytni stack. An %Ecto.Changeset{} is returned with the potential validation errors added by the plugins.

Same than validate_update_registration/3 but at registration's edition.

Link to this section Types

Specs

config() :: any()

Specs

duration() :: pos_integer() | {pos_integer(), duration_unit()}

Specs

duration_unit() :: :second | :minute | :hour | :day | :week | :month | :year

Specs

irrelevant() :: any()

Specs

multi_result() ::
  {:ok, %{required(Ecto.Multi.name()) => any()}}
  | {:error, Ecto.Multi.name(), any(), %{optional(Ecto.Multi.name()) => any()}}

Specs

nilable(type) :: type | nil

Specs

params() :: %{optional(String.t()) => String.t()}
Link to this type

repo_nobang_operation(type)

View Source

Specs

repo_nobang_operation(type) :: {:ok, type} | {:error, Ecto.Changeset.t()}

Specs

user() :: struct()

Link to this section Functions

Link to this function

authentication_failed(module, user)

View Source

Specs

authentication_failed(module :: module(), user :: nilable(user())) ::
  multi_result()

Notifies plugins that the authentication failed for user.

If user is nil, nothing is done.

Link to this function

change_user(user_or_module, params \\ %{})

View Source

Specs

change_user(user_or_module :: module() | user(), params :: params()) ::
  Ecto.Changeset.t()

Creates an %Ecto.Changeset{} for a new user/account (at registration from a module) or from a user (when editing account from a struct)

Link to this function

create_user(module, attrs, options \\ [])

View Source

Specs

create_user(module :: module(), attrs :: map(), options :: Keyword.t()) ::
  multi_result()

Register user from controller's params.

Returned value is one of:

  • {:ok, map} where map is the result of the internals Ecto.Multi.* calls
  • {:error, failed_operation, result_of_failed_operation, changes_so_far} with:
    • failed_operation: the name of the operation which failed
    • result_of_failed_operation: its result/returned value
    • changes_so_far: same as map of the {:ok, map} case

The inserted user will be part of map (or eventualy changes_so_far) under the key :user.

See Ecto.Repo.insert/2 for options.

Link to this function

delete_user(module, user)

View Source

Specs

delete_user(module :: module(), user :: user()) :: multi_result()

To delete user.

This function doesn't actually do nothing except calling the Haytni.Plugin.on_delete_user/4 callbacks from the plugins in the Haytni's module stack. This way you can implement it the way you like and do extra stuffs like deleting files associated to user.

See documentation of Haytni.Plugin.on_delete_user/4 for some examples.

Link to this function

email_changed(module, user, new_email_address)

View Source

Specs

email_changed(
  module :: module(),
  user :: user(),
  new_email_address :: String.t()
) :: multi_result()

Function to be called, for the user, to modify its own email address: it actually updates it in the database but also invokes, first, the Haytni.Plugin.on_email_change/4 callback of the plugins registered in the module Haytni stack.

Note: caller is responsible for validations against new_email_address

Link to this function

fetch_module_from_conn!(conn)

View Source

Specs

fetch_module_from_conn!(conn :: Plug.Conn.t()) :: module()

Extracts an Haytni stack (module) from a Plug connection

Raises if no Haytni's stack was defined (through the router).

Injects Ecto.Schema.fields necessary to enabled plugins into your User schema

Note that this function is invoked at compile time: you'll need to recompile your application to reflect any change related to fields injected in your user schema.

Specs

find_user(module :: module(), conn :: Plug.Conn.t()) ::
  {Plug.Conn.t(), user() | nil}

Used by plug to extract the current user (if any) from the HTTP request (meaning from headers, cookies, etc)

Specs

get_user(module :: module(), id :: any()) :: nilable(user())

Fetchs a user from its id.

Returns nil if no user matches.

Example:

case Haytni.get_user_by(YourApp.Haytni, params["id"]) do
  nil ->
    # not found
  user = %User{} ->
    # do something of user
end
Link to this function

get_user_by(module, clauses)

View Source

Specs

get_user_by(module :: module(), clauses :: Keyword.t() | map()) ::
  nilable(user())

Fetches a user from the Ecto.Repo specified in config :haytni, YourApp.Haytni as repo subkey via the attributes specified by clauses as a map or a keyword-list.

Returns nil if no user matches.

Example:

hulk = Haytni.get_user_by(YourApp.Haytni, first_name: "Robert", last_name: "Banner")
Link to this function

invalid_user?(module, user)

View Source

Specs

invalid_user?(module :: module(), user :: user()) ::
  {:error, String.t()} | false

Checks if a user is valid according to plugins.

Returns false if the user is valid else {:error, reason}.

Link to this function

login(conn, module, user, changes \\ [])

View Source

Specs

login(
  conn :: Plug.Conn.t(),
  module :: module(),
  user :: user(),
  changes :: Keyword.t()
) :: {:ok, Plug.Conn.t()} | {:error, String.t()}

To be called on (manual) login

Link to this function

logout(conn, module, options \\ [])

View Source

Specs

logout(conn :: Plug.Conn.t(), module :: module(), options :: Keyword.t()) ::
  Plug.Conn.t()

Notifies plugins that current user is going to be logged out

Link to this function

plugin_enabled?(module, plugin)

View Source

Specs

plugin_enabled?(module :: module(), plugin :: module()) :: boolean()

Returns true if plugin is enabled in the module Haytni stack.

Link to this function

routes(module, options \\ [])

View Source

Injects the necessary routes for enabled plugins into your Router

Note that this function is invoked at compile time: you'll need to recompile your application to reflect any change in your router.

Specs

scoped_assign(module :: module()) :: atom()

Returns the name of the assign (in Plug.Conn and templates) of the current user

Link to this function

scoped_session_key(module)

View Source

Specs

scoped_session_key(module :: module()) :: String.t()

Returns the name of the session key which carries the user

Link to this function

shared_files_to_install(base_path, web_path, scope, timestamp)

View Source

Specs

shared_files_to_install(
  base_path :: String.t(),
  web_path :: String.t(),
  scope :: String.t(),
  timestamp :: String.t()
) :: [{:eex | :text, String.t(), String.t()}]

Get the list of shared (templates/views) or independant (Haytni stack) files to install

Link to this macro

stack(module, options \\ [])

View Source (macro)
Link to this function

update_registration(module, user, attrs, options \\ [])

View Source

Specs

update_registration(
  module :: module(),
  user :: user(),
  attrs :: map(),
  options :: Keyword.t()
) :: repo_nobang_operation(user())

Update user's registration, its own registration.

Link to this function

update_user_in_multi_with(multi, name, user, changes)

View Source

Specs

update_user_in_multi_with(
  multi :: Ecto.Multi.t(),
  name :: Ecto.Multi.name(),
  user :: user(),
  changes :: Keyword.t()
) :: Ecto.Multi.t()

Update user in the same way as update_user_with/3 but as part of a set of operations (Ecto.Multi).

Link to this function

update_user_with(module, user, changes)

View Source

Specs

update_user_with(module :: module(), user :: user(), changes :: Keyword.t()) ::
  repo_nobang_operation(user())

Update the given user from a list of changes as Keyword.

Returns {:error, changeset} if there was a validation or a known constraint error else {:ok, struct} where struct is the updated user.

NOTE: for internal use, there isn't any validation. Do NOT inject values from controller's params!

Link to this function

update_user_with!(module, user, changes)

View Source

Specs

update_user_with!(module :: module(), user :: user(), changes :: Keyword.t()) ::
  user() | no_return()

Same as update_user_with/3 but returns the updated user struct or raises if changes are invalid.

Link to this function

validate_create_registration(module, changeset)

View Source

Specs

validate_create_registration(
  module :: module(),
  changeset :: Ecto.Changeset.t()
) :: Ecto.Changeset.t()

This function is a callback to be called from your User.create_registration_changeset/2 so validations and others internal tasks can be done by plugins at user's registration.

Link to this function

validate_password(module, changeset)

View Source

Specs

validate_password(module :: module(), changeset :: Ecto.Changeset.t()) ::
  Ecto.Changeset.t()

Runs any custom password validations from the plugins (via their Haytni.Plugin.validate_password/3 callback) of the module Haytni stack. An %Ecto.Changeset{} is returned with the potential validation errors added by the plugins.

Link to this function

validate_update_registration(module, changeset)

View Source

Specs

validate_update_registration(
  module :: module(),
  changeset :: Ecto.Changeset.t()
) :: Ecto.Changeset.t()

Same than validate_update_registration/3 but at registration's edition.