2018-05-18 07:56:21 +00:00
|
|
|
defmodule Eventos.Actors.User do
|
2018-01-14 16:56:50 +00:00
|
|
|
@moduledoc """
|
|
|
|
Represents a local user
|
|
|
|
"""
|
2017-12-08 08:58:14 +00:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2018-05-18 07:56:21 +00:00
|
|
|
alias Eventos.Actors.{Actor, User}
|
2017-12-08 08:58:14 +00:00
|
|
|
|
|
|
|
schema "users" do
|
|
|
|
field :email, :string
|
2017-12-09 13:58:37 +00:00
|
|
|
field :password_hash, :string
|
2018-01-13 22:33:03 +00:00
|
|
|
field :password, :string, virtual: true
|
|
|
|
field :role, :integer, default: 0
|
2018-07-10 08:00:07 +00:00
|
|
|
has_many :actors, Actor
|
2018-07-04 12:29:17 +00:00
|
|
|
field :confirmed_at, :utc_datetime
|
|
|
|
field :confirmation_sent_at, :utc_datetime
|
|
|
|
field :confirmation_token, :string
|
|
|
|
field :reset_password_sent_at, :utc_datetime
|
|
|
|
field :reset_password_token, :string
|
2018-01-13 22:33:03 +00:00
|
|
|
|
2017-12-08 08:58:14 +00:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%User{} = user, attrs) do
|
|
|
|
user
|
2018-07-10 08:00:07 +00:00
|
|
|
|> cast(attrs, [:email, :role, :password_hash, :confirmed_at, :confirmation_sent_at, :confirmation_token, :reset_password_sent_at, :reset_password_token])
|
2017-12-09 13:58:37 +00:00
|
|
|
|> validate_required([:email])
|
2018-07-04 12:29:17 +00:00
|
|
|
|> unique_constraint(:email, [message: "registration.error.email_already_used"])
|
2017-12-08 08:58:14 +00:00
|
|
|
|> validate_format(:email, ~r/@/)
|
2018-07-04 12:29:17 +00:00
|
|
|
|> validate_length(:password, min: 6, max: 100, message: "registration.error.password_too_short")
|
2017-12-09 13:58:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def registration_changeset(struct, params) do
|
|
|
|
struct
|
|
|
|
|> changeset(params)
|
|
|
|
|> cast(params, ~w(password)a, [])
|
2018-07-04 12:29:17 +00:00
|
|
|
|> validate_required([:email, :password])
|
|
|
|
|> validate_length(:password, min: 6, max: 100, message: "registration.error.password_too_short")
|
2018-01-26 10:02:11 +00:00
|
|
|
|> hash_password()
|
2018-07-04 12:29:17 +00:00
|
|
|
|> save_confirmation_token()
|
|
|
|
|> unique_constraint(:confirmation_token, [message: "regisration.error.confirmation_token_already_in_use"])
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_password_reset_changeset(%User{} = user, attrs) do
|
|
|
|
user
|
|
|
|
|> cast(attrs, [:reset_password_token, :reset_password_sent_at])
|
|
|
|
end
|
|
|
|
|
|
|
|
def password_reset_changeset(%User{} = user, attrs) do
|
|
|
|
user
|
|
|
|
|> cast(attrs, [:password, :reset_password_token, :reset_password_sent_at])
|
|
|
|
|> validate_length(:password, min: 6, max: 100, message: "registration.error.password_too_short")
|
|
|
|
|> hash_password()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_confirmation_token(changeset) do
|
|
|
|
case changeset do
|
|
|
|
%Ecto.Changeset{valid?: true,
|
|
|
|
changes: %{email: _email}} ->
|
|
|
|
changeset = put_change(changeset, :confirmation_token, random_string(30))
|
|
|
|
put_change(changeset, :confirmation_sent_at, DateTime.utc_now())
|
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp random_string(length) do
|
2018-07-04 15:16:02 +00:00
|
|
|
length
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64
|
2017-12-09 13:58:37 +00:00
|
|
|
end
|
|
|
|
|
2018-01-26 10:02:11 +00:00
|
|
|
@doc """
|
|
|
|
Hash password when it's changed
|
|
|
|
"""
|
2017-12-09 13:58:37 +00:00
|
|
|
defp hash_password(changeset) do
|
|
|
|
case changeset do
|
|
|
|
%Ecto.Changeset{valid?: true,
|
|
|
|
changes: %{password: password}} ->
|
|
|
|
put_change(changeset,
|
|
|
|
:password_hash,
|
|
|
|
Comeonin.Argon2.hashpwsalt(password))
|
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|
2018-07-04 12:29:17 +00:00
|
|
|
|
|
|
|
def is_confirmed(%User{confirmed_at: nil} = _user) do
|
|
|
|
{:error, :unconfirmed}
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_confirmed(%User{} = user) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
2017-12-08 08:58:14 +00:00
|
|
|
end
|