2019-03-06 18:45:26 +01:00
|
|
|
import EctoEnum
|
|
|
|
|
|
|
|
defenum(Mobilizon.Users.UserRoleEnum, :user_role_type, [
|
|
|
|
:administrator,
|
|
|
|
:moderator,
|
|
|
|
:user
|
|
|
|
])
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
defmodule Mobilizon.Users.User do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
|
|
|
Represents a local user
|
|
|
|
"""
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Users.User
|
2018-11-15 17:35:47 +01:00
|
|
|
alias Mobilizon.Service.EmailChecker
|
2017-12-08 09:58:14 +01:00
|
|
|
|
|
|
|
schema "users" do
|
2018-07-27 10:45:35 +02:00
|
|
|
field(:email, :string)
|
|
|
|
field(:password_hash, :string)
|
|
|
|
field(:password, :string, virtual: true)
|
2019-03-06 18:45:26 +01:00
|
|
|
field(:role, Mobilizon.Users.UserRoleEnum, default: :user)
|
2018-07-27 10:45:35 +02:00
|
|
|
has_many(:actors, Actor)
|
2018-11-29 17:43:22 +01:00
|
|
|
belongs_to(:default_actor, Actor)
|
2018-07-27 10:45:35 +02: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 23:33:03 +01:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(%User{} = user, attrs) do
|
2018-11-29 17:43:22 +01:00
|
|
|
changeset =
|
|
|
|
user
|
|
|
|
|> cast(attrs, [
|
|
|
|
:email,
|
|
|
|
:role,
|
2019-01-29 11:02:32 +01:00
|
|
|
:password,
|
2018-11-29 17:43:22 +01:00
|
|
|
:password_hash,
|
|
|
|
:confirmed_at,
|
|
|
|
:confirmation_sent_at,
|
|
|
|
:confirmation_token,
|
|
|
|
:reset_password_sent_at,
|
|
|
|
:reset_password_token
|
|
|
|
])
|
|
|
|
|> validate_required([:email])
|
2019-01-29 11:02:32 +01:00
|
|
|
|> unique_constraint(:email, message: "This email is already used.")
|
|
|
|
|> validate_email()
|
2018-11-29 17:43:22 +01:00
|
|
|
|> validate_length(
|
|
|
|
:password,
|
|
|
|
min: 6,
|
|
|
|
max: 100,
|
2019-02-21 18:11:49 +01:00
|
|
|
message: "The chosen password is too short."
|
2018-11-29 17:43:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
if Map.has_key?(attrs, :default_actor) do
|
|
|
|
put_assoc(changeset, :default_actor, attrs.default_actor)
|
|
|
|
else
|
|
|
|
changeset
|
|
|
|
end
|
2017-12-09 14:58:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def registration_changeset(struct, params) do
|
|
|
|
struct
|
|
|
|
|> changeset(params)
|
2018-11-29 17:43:22 +01:00
|
|
|
|> cast_assoc(:default_actor)
|
2018-11-28 17:16:23 +01:00
|
|
|
|> validate_required([:email, :password])
|
2018-01-26 11:02:11 +01:00
|
|
|
|> hash_password()
|
2018-07-04 14:29:17 +02:00
|
|
|
|> save_confirmation_token()
|
2018-07-27 10:45:35 +02:00
|
|
|
|> unique_constraint(
|
|
|
|
:confirmation_token,
|
2019-01-29 11:02:32 +01:00
|
|
|
message: "The registration is already in use, this looks like an issue on our side."
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
2018-07-04 14:29:17 +02:00
|
|
|
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])
|
2018-07-27 10:45:35 +02:00
|
|
|
|> validate_length(
|
|
|
|
:password,
|
|
|
|
min: 6,
|
|
|
|
max: 100,
|
|
|
|
message: "registration.error.password_too_short"
|
|
|
|
)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> hash_password()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp save_confirmation_token(changeset) do
|
|
|
|
case changeset do
|
2018-07-27 10:45:35 +02:00
|
|
|
%Ecto.Changeset{valid?: true, changes: %{email: _email}} ->
|
|
|
|
changeset = put_change(changeset, :confirmation_token, random_string(30))
|
2019-02-14 14:19:55 +01:00
|
|
|
|
|
|
|
put_change(
|
|
|
|
changeset,
|
|
|
|
:confirmation_sent_at,
|
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
)
|
2018-07-27 10:45:35 +02:00
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-06 10:30:27 +01:00
|
|
|
defp validate_email(changeset) do
|
|
|
|
case changeset do
|
|
|
|
%Ecto.Changeset{valid?: true, changes: %{email: email}} ->
|
|
|
|
case EmailChecker.valid?(email) do
|
|
|
|
false -> add_error(changeset, :email, "Email doesn't fit required format")
|
|
|
|
_ -> changeset
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-07-04 14:29:17 +02:00
|
|
|
defp random_string(length) do
|
2018-07-04 17:16:02 +02:00
|
|
|
length
|
|
|
|
|> :crypto.strong_rand_bytes()
|
2018-07-27 10:45:35 +02:00
|
|
|
|> Base.url_encode64()
|
2017-12-09 14:58:37 +01:00
|
|
|
end
|
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
# Hash password when it's changed
|
2017-12-09 14:58:37 +01:00
|
|
|
defp hash_password(changeset) do
|
|
|
|
case changeset do
|
2018-07-27 10:45:35 +02:00
|
|
|
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
|
|
|
|
put_change(
|
|
|
|
changeset,
|
2017-12-09 14:58:37 +01:00
|
|
|
:password_hash,
|
2019-02-14 14:19:55 +01:00
|
|
|
Argon2.hash_pwd_salt(password)
|
2018-07-27 10:45:35 +02:00
|
|
|
)
|
|
|
|
|
2017-12-09 14:58:37 +01:00
|
|
|
_ ->
|
|
|
|
changeset
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|
2018-07-04 14:29:17 +02:00
|
|
|
|
|
|
|
def is_confirmed(%User{confirmed_at: nil} = _user) do
|
|
|
|
{:error, :unconfirmed}
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_confirmed(%User{} = user) do
|
|
|
|
{:ok, user}
|
|
|
|
end
|
2019-01-25 09:23:44 +01:00
|
|
|
|
2019-01-25 17:06:57 +01:00
|
|
|
def owns_actor(%User{actors: actors}, actor_id) do
|
2019-02-01 15:38:35 +01:00
|
|
|
case Enum.find(actors, fn a -> a.id == actor_id end) do
|
|
|
|
nil -> {:is_owned, false}
|
|
|
|
actor -> {:is_owned, true, actor}
|
2019-01-25 09:23:44 +01:00
|
|
|
end
|
|
|
|
end
|
2017-12-08 09:58:14 +01:00
|
|
|
end
|