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
Lock a use account.
Log an error message when lockable update fails.
Get the configured logged_in_url.
Get the configured logged_out_url.
Login a user.
Logout a user.
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.
Send a user email.
Set view plug
Shift a NaiveDateTime.
Unlock a user account.
Types
changeset()
@type changeset() :: Ecto.Changeset.t()
conn()
@type conn() :: Plug.Conn.t()
params()
@type params() :: map()
schema()
@type schema() :: Ecto.Schema.t()
schema_or_error()
Functions
changeset(which, module, model, params \\ %{})
confirm!(user)
@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
deactivate_user(conn)
Deactivate a user.
Removes all logged in sessions for a user.
expired?(datetime, opts)
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
layout_view(conn, opts)
@spec layout_view( Plug.Conn.t(), keyword() ) :: Plug.Conn.t()
Put LayoutView
Adds Config.layout if set.
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.
lockable_failure(changeset)
@spec lockable_failure(Ecto.Changeset.t()) :: :ok
Log an error message when lockable update fails.
logged_in_url(conn)
@spec logged_in_url(Plug.Conn.t()) :: String.t()
Get the configured logged_in_url.
logged_out_url(conn)
@spec logged_out_url(Plug.Conn.t()) :: String.t()
Get the configured logged_out_url.
login_user(conn, user, params \\ %{})
Login a user.
Logs in a user and redirects them to the session_create page.
logout_user(conn, opts \\ [])
Logout a user.
Logs out a user and redirects them to the session_delete page.
permit(params, permitted)
leaves in params only permitted request parameters.
Same as Rails permit, prevents mass assignment attacks.
random_string(length)
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.
redirect_logged_in(conn, params)
Plug to redirect already logged in users.
redirect_to(conn, path, params)
redirect_to(conn, path, params, user)
respond_with(conn, atom, opts \\ %{})
router_helpers()
@spec router_helpers() :: module()
Get the Router.Helpers module for the project..
Returns the projects Router.Helpers module.
schema_module(schema)
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.
send_email_if_mailer(conn, info, send_function)
Sends an email if the mailer is properly configured and add the appropriate flash.
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.
set_view(conn, opts)
@spec set_view( Plug.Conn.t(), keyword() ) :: Plug.Conn.t()
Set view plug
shift(datetime, opts)
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!(user)
@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.