Packages

This is a CMS written in Elixir. Aims to be the great open-source ecommerce and/or startup solution for those who are searching for a performance and stability on top of modern technologies like React and Elixir.

Current section

Files

Jump to
lyn web models user.ex
Raw

web/models/user.ex

defmodule Lyn.User do
use Lyn.Web, :model
import Comeonin.Bcrypt, only: [hashpwsalt: 1]
schema "users" do
field :username, :string
field :email, :string
field :password_digest, :string
field :avatar, :string
belongs_to :role, Lyn.Role
timestamps
# Virtual Fields
field :password, :string, virtual: true
field :password_confirmation, :string, virtual: true
end
def admin_fields do
[
id: %{
label: "id",
type: :integer
},
username: %{
label: "username",
type: :string
},
avatar: %{
label: "avatar",
type: :string
},
email: %{
label: "email",
type: :string
},
role_id: %{
label: "role_id",
type: :integer
}
]
end
@required_fields ~w(username email password password_confirmation role_id)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> hash_password
|> validate_confirmation(:password, message: "does not match password!")
|> unique_constraint(:username)
|> unique_constraint(:email)
|> validate_length(:password, min: 4)
end
defp hash_password(changeset) do
if password = get_change(changeset, :password) do
changeset
|> put_change(:password_digest, hashpwsalt(password))
else
changeset
end
end
end