4b4ecec693
Signed-off-by: Thomas Citharel <tcit@tcit.fr> typo Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rename avatar to avatar_url, same with header. Add a comment to explain why the tweak with HTTPoison and TLS1.2 Signed-off-by: Thomas Citharel <tcit@tcit.fr> Rename avatar to avatar_url Signed-off-by: Thomas Citharel <tcit@tcit.fr> rename old avatar properties in front-end to avatar_url Signed-off-by: Thomas Citharel <tcit@tcit.fr> fix change gravatar from ?d= to ?default= Signed-off-by: Thomas Citharel <tcit@tcit.fr> reorganize aliases and imports Signed-off-by: Thomas Citharel <tcit@tcit.fr> set avatar url only when gravatar exists, add a test for that case Signed-off-by: Thomas Citharel <tcit@tcit.fr>
51 lines
1.1 KiB
Elixir
51 lines
1.1 KiB
Elixir
defmodule Eventos.Accounts.User do
|
|
@moduledoc """
|
|
Represents a local user
|
|
"""
|
|
use Ecto.Schema
|
|
import Ecto.Changeset
|
|
alias Eventos.Accounts.{Account, User}
|
|
|
|
schema "users" do
|
|
field :email, :string
|
|
field :password_hash, :string
|
|
field :password, :string, virtual: true
|
|
field :role, :integer, default: 0
|
|
belongs_to :account, Account
|
|
|
|
timestamps()
|
|
end
|
|
|
|
@doc false
|
|
def changeset(%User{} = user, attrs) do
|
|
user
|
|
|> cast(attrs, [:email, :role, :password_hash, :account_id])
|
|
|> validate_required([:email])
|
|
|> unique_constraint(:email)
|
|
|> validate_format(:email, ~r/@/)
|
|
end
|
|
|
|
def registration_changeset(struct, params) do
|
|
struct
|
|
|> changeset(params)
|
|
|> cast(params, ~w(password)a, [])
|
|
|> validate_length(:password, min: 6, max: 100)
|
|
|> hash_password()
|
|
end
|
|
|
|
@doc """
|
|
Hash password when it's changed
|
|
"""
|
|
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
|
|
end
|
|
end
|