Coherence.Schema (Coherence v0.8.0)
Add Coherence support to a User schema module.
Add use Coherence.Schema, opts \ []
to your User module to add a number of
Module functions and helpers.
The optional opt
parameter can be used to disable options enabled in the
global configuration by passing option: false
For example,
defmodule MyProject.User do
use MyProject.Web, :model
use Coherence.Schema, invitable: false
The following functions are added regardless of the options configured:
authenticatable?/0
- Returns true if the option is configured.registerable?/0
- Returns true if the option is configured.confirmable?/0
- Returns true if the option is configured.trackable?/0
- Returns true if the option is configured.trackable_table?/0
- Returns true if the option is configured.recoverable?/0
- Returns true if the option is configured.lockable?/0
- Returns true if the option is configured.invitable?/0
- Returns true if the option is configured.unlockable_with_token?/0
- Returns true if the option is configured.
The following functions are available when authenticatable?/0
returns true:
checkpw/2
- Validate password.encrypt_password/1
- encrypted a password using<password_hashing_alg>.hashpwsalt
validate_coherence/2
- run the coherence password validations.validate_password/2
- Used byvalidate_coherence for password validation
The following functions are available when confirmable?/0
returns true.
confirmed?/1
- Has the given user been confirmed?confirm/1
- Return a changeset to confirm the given user
The following functions are available when lockable?/0
returns true.
locked?/1
- Is the given user locked?lock/1
- Return a changeset to lock the given userunlock/1
- Return a changeset to unlock the given user
The coherence_schema/1
macro is used to add the configured schema fields to the User models schema.
The coherence_fields/0
function is used to return the validation fields appropriate for the selected options.
Examples:
The following is an example User module when the :authenticatable is used:
defmodule MyProject.User do
use MyProject.Web, :model
use Coherence.Schema
schema "users" do
field :name, :string
field :email, :string
coherence_schema
timestamps
end
@required_fields ~w(name email)
@optional_fields ~w() ++ coherence_fields
def changeset(model, params \ %{}) do
model
|> cast(params, @required_fields, @optional_fields)
|> unique_constraint(:email)
|> validate_coherence(params)
end
def changeset(model, params, :password) do
model
|> cast(params, ~w(password password_confirmation reset_password_token reset_password_sent_at))
|> validate_coherence_password_reset(params)
end
end
Configurable confirmable
You may have an application that has its own setting for enabling/disabling confirmable accounts at runtime. To enable this:
defmodule MyProject.User do
use MyProject.Web, :model
use Coherence.Schema
# ...
def confirmable? do
# test if confirmation is enable
end
def changeset(schema, params) do
schema
|> cast(params, @all_params ++ coherence_fields())
# ...
|> prepare_changes(&prepare_confirmation/1)
end
def prepare_confirmation(%{valid?: true} = changeset) do
if local_settings_require_confirmation? do
changeset
else
changeset
|> put_change(:confirmed_at, NaiveDateTime.utc_now())
|> put_change(:confirmation_token, nil)
end
end
def prepare_confirmation(changeset) do
changeset
end
end
Summary
Functions
Get a list of the configured database fields.
Add configure schema fields.
Default permitted attributes used when they are not configured.
Functions
coherence_fields()
Get a list of the configured database fields.
Returns a list of fields that can be appended to your @option_fields used in your models changeset cast.
For example, for Coherence.Config.opts == [:authenticatable, :recoverable]
coherence_fields/0
will return:
~w(password_hash password password_confirmation reset_password_token reset_password_sent_at)a
Add configure schema fields.
Adds the schema fields to the schema block for the options selected. Only the fields for configured options are added.
For example, for Coherence.Config.opts == [:authenticatable, :recoverable]
coherence_schema
used in the following context:
defmodule MyProject.User do
use MyProject.Web, :model
use Coherence.Schema
schema "users" do
field :name, :string
field :email, :string
coherence_schema
timestamps
end
Will compile a schema to the following:
defmodule MyProject.User do
use MyProject.Web, :model
use Coherence.Schema
schema "users" do
field :name, :string
field :email, :string
field :active, :boolean, default: true
# authenticatable
field :password_hash, :string
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
# recoverable
field :reset_password_token, :string
field :reset_password_sent_at, NaiveDateTime
timestamps
end
permitted_attributes_default(atom)
Default permitted attributes used when they are not configured.
Defaults to be used when the entry is not found in configuration. These are mainly used when a pre 0.5.1 project is updated.