Coherence.Controller (Coherence v0.8.0)

Common helper functions for Coherence Controllers.

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.

Types

@type changeset() :: Ecto.Changeset.t()
@type conn() :: Plug.Conn.t()
@type params() :: map()
@type schema() :: Ecto.Schema.t()
Link to this type

schema_or_error()

@type schema_or_error() :: schema() | {:error, changeset()}

Functions

Link to this function

changeset(which, module, model, params \\ %{})

@spec changeset(atom(), module(), schema(), params()) :: changeset()
@spec 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)

@spec deactivate_user(conn()) :: conn()

Deactivate a user.

Removes all logged in sessions for a user.

Link to this function

expired?(datetime, opts)

@spec expired?(
  nil | struct(),
  keyword()
) :: 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)

@spec layout_view(
  Plug.Conn.t(),
  keyword()
) :: Plug.Conn.t()

Put LayoutView

Adds Config.layout if set.

Link to this function

lock!(user, locked_at \\ NaiveDateTime.utc_now())

@spec 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)

@spec lockable_failure(Ecto.Changeset.t()) :: :ok

Log an error message when lockable update fails.

Link to this function

logged_in_url(conn)

@spec logged_in_url(Plug.Conn.t()) :: String.t()

Get the configured logged_in_url.

Link to this function

logged_out_url(conn)

@spec logged_out_url(Plug.Conn.t()) :: String.t()

Get the configured logged_out_url.

Link to this function

login_user(conn, user, params \\ %{})

@spec 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 \\ [])

@spec logout_user(
  conn(),
  keyword()
) :: conn()

Logout a user.

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

Link to this function

permit(params, permitted)

@spec permit(map(), []) :: map()

leaves in params only permitted request parameters.

Same as Rails permit, prevents mass assignment attacks.

Link to this function

random_string(length)

@spec 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)

@spec redirect_logged_in(conn(), any()) :: conn()

Plug to redirect already logged in users.

Link to this function

redirect_to(conn, path, params)

@spec redirect_to(conn(), atom(), params()) :: conn()
Link to this function

redirect_to(conn, path, params, user)

@spec redirect_to(conn(), atom(), params(), schema()) :: conn()
Link to this function

respond_with(conn, atom, opts \\ %{})

Link to this function

router_helpers()

@spec 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)

@spec 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)

@spec 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)

@spec set_view(
  Plug.Conn.t(),
  keyword()
) :: Plug.Conn.t()

Set view plug

Link to this function

shift(datetime, opts)

@spec shift(struct(), keyword()) :: 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"
@spec unlock!(Ecto.Schema.t()) :: schema_or_error()

Unlock a user account.

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