Coherence v0.5.2 Coherence.Controller

Common helper functions for Coherence Controllers.

Link to this section Summary

Functions

Confirm a user account

Deactivate a user

Test if a datetime has expired

Put LayoutView

Log an error message when lockable update fails

Get the configured logged_in_url

Get the configured logged_out_url

leaves in params only permitted request parameters

Get a random string of given length

Plug to redirect already logged in users

Get the Router.Helpers module for the project

Send confirmation email with token

Sends an email if the mailer is properly configured and add the appropriate flash

Set view plug

Shift a NaiveDateTime

Unlock a user account

Link to this section Types

Link to this type changeset()
changeset() :: Ecto.Changeset.t()
Link to this type conn()
conn() :: Plug.Conn.t()
Link to this type params()
params() :: Map.t()
Link to this type schema()
schema() :: Ecto.Schema.t()
Link to this type schema_or_error()
schema_or_error() :: schema() | {:error, changeset()}

Link to this section Functions

Link to this function changeset(which, module, model, params \\ %{})
changeset(atom(), module(), schema(), params()) :: changeset()
Link to this function confirm!(user)
confirm!(Ecto.Schema.t()) :: schema_or_error()

Confirm a user account.

Adds the :confirmed_at datetime field on the user model and updates the database

Link to this function deactivate_user(conn)
deactivate_user(conn()) :: conn()

Deactivate a user.

Removes all logged in sessions for a user.

Link to this function expired?(datetime, opts)
expired?(nil | struct(), Keyword.t()) :: boolean()

Test if a datetime has expired.

Convert the datetime from NaiveDateTime format to Timex format to do the comparison given the time during in opts.

Examples

expired?(user.expire_at, days: 5)
expired?(user.expire_at, minutes: 10)

iex> NaiveDateTime.utc_now()
...> |> Coherence.Controller.expired?(days: 1)
false

iex> NaiveDateTime.utc_now()
...> |> Coherence.Controller.shift(days: -2)
...> |> Coherence.Controller.expired?(days: 1)
true
Link to this function layout_view(conn, opts)
layout_view(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()

Put LayoutView

Adds Config.layout if set.

Link to this function lock!(user, locked_at \\ NaiveDateTime.utc_now())

Lock a use account.

Sets the :locked_at field on the user model to the current date and time unless provided a value for the optional parameter.

You can provide a date in the future to override the configured lock expiry time. You can set this data far in the future to do a pseudo permanent lock.

Link to this function lockable_failure(changeset)
lockable_failure(Ecto.Changeset.t()) :: :ok

Log an error message when lockable update fails.

Link to this function logged_in_url(conn)
logged_in_url(Plug.Conn.t()) :: String.t()

Get the configured logged_in_url.

Link to this function logged_out_url(conn)
logged_out_url(Plug.Conn.t()) :: String.t()

Get the configured logged_out_url.

Link to this function login_user(conn, user, params \\ %{})
login_user(conn(), schema(), params()) :: conn()

Login a user.

Logs in a user and redirects them to the session_create page.

Link to this function logout_user(conn, opts \\ [])
logout_user(conn(), Keyword.t()) :: conn()

Logout a user.

Logs out a user and redirects them to the session_delete page.

Link to this function permit(params, permitted)
permit(Map.t(), List.t()) :: Map.t()

leaves in params only permitted request parameters.

Same as Rails permit, prevents mass assignment attacks.

Link to this function random_string(length)
random_string(integer()) :: binary()

Get a random string of given length.

Returns a random url safe encoded64 string of the given length. Used to generate tokens for the various modules that require unique tokens.

Link to this function redirect_logged_in(conn, params)
redirect_logged_in(conn(), params()) :: conn()

Plug to redirect already logged in users.

Link to this function redirect_to(conn, path, params)
redirect_to(conn(), atom(), params()) :: conn()
Link to this function redirect_to(conn, path, params, user)
redirect_to(conn(), atom(), params(), schema()) :: conn()
Link to this function respond_with(conn, atom, opts \\ %{})
Link to this function router_helpers()
router_helpers() :: module()

Get the Router.Helpers module for the project..

Returns the projects Router.Helpers module.

Link to this function schema_module(schema)
Link to this function send_confirmation(conn, user, user_schema)
send_confirmation(Plug.Conn.t(), Ecto.Schema.t(), module()) :: Plug.Conn.t()

Send confirmation email with token.

If the user supports confirmable, generate a token and send the email.

Link to this function send_email_if_mailer(conn, info, send_function)

Sends an email if the mailer is properly configured and add the appropriate flash.

Link to this function send_user_email(fun, model, url)
send_user_email(atom(), Ecto.Schema.t(), String.t()) :: any()

Send a user email.

Sends a user email given the module, model, and url. Logs the email for debug purposes.

Note: This function uses an apply to avoid compile warnings if the mailer is not selected as an option.

Link to this function set_view(conn, opts)
set_view(Plug.Conn.t(), Keyword.t()) :: Plug.Conn.t()

Set view plug

Link to this function shift(datetime, opts)
shift(struct(), Keyword.t()) :: struct()

Shift a NaiveDateTime.

Examples

iex> ~N(2016-10-10 10:10:10)
...> |> Coherence.Controller.shift(days: -2)
...> |> to_string
"2016-10-08 10:10:10Z"

Unlock a user account.

Clears the :locked_at field on the user model and updates the database.