Coherence v0.4.0 Coherence.ControllerHelpers

Common helper functions for Coherence Controllers.

Link to this section Summary

Functions

Confirm a user account

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

Logout a user

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

Set view plug

Shift a Ecto.DateTime

Unlock a user account

Link to this section Types

Link to this type changeset()
changeset() :: Ecto.Changeset.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

Confirm a user account.

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

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

Test if a datetime has expired.

Convert the datetime from Ecto.DateTime 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> Ecto.DateTime.utc
...> |> Coherence.ControllerHelpers.expired?(days: 1)
false

iex> Ecto.DateTime.utc
...> |> Coherence.ControllerHelpers.shift(days: -2)
...> |> Ecto.DateTime.cast!
...> |> Coherence.ControllerHelpers.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 \\ Ecto.DateTime.utc())
lock!(Ecto.Schema.t, struct) :: schema_or_error

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)
logout_user(conn) :: conn

Logout a user.

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

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 router_helpers()
router_helpers() :: module

Get the Router.Helpers module for the project..

Returns the projects Router.Helpers module.

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

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

Shift a Ecto.DateTime.

Examples

iex> Ecto.DateTime.cast!("2016-10-10 10:10:10")
...> |> Coherence.ControllerHelpers.shift(days: -2)
...> |> Ecto.DateTime.cast!
...> |> to_string
"2016-10-08 10:10:10"

Unlock a user account.

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