1
0
Fork 0
mirror of https://framagit.org/framasoft/mobilizon.git synced 2024-12-23 16:33:28 +00:00
mobilizon/lib/eventos/actors/user.ex

51 lines
1.1 KiB
Elixir
Raw Normal View History

defmodule Eventos.Actors.User do
@moduledoc """
Represents a local user
"""
use Ecto.Schema
import Ecto.Changeset
alias Eventos.Actors.{Actor, User}
schema "users" do
field :email, :string
2017-12-09 13:58:37 +00:00
field :password_hash, :string
field :password, :string, virtual: true
field :role, :integer, default: 0
belongs_to :actor, Actor
timestamps()
end
@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:email, :role, :password_hash, :actor_id])
2017-12-09 13:58:37 +00:00
|> validate_required([:email])
|> unique_constraint(:email)
|> validate_format(:email, ~r/@/)
2017-12-09 13:58:37 +00:00
end
def registration_changeset(struct, params) do
struct
|> changeset(params)
|> cast(params, ~w(password)a, [])
|> validate_length(:password, min: 6, max: 100)
|> hash_password()
2017-12-09 13:58:37 +00:00
end
@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
end
end