2019-03-05 17:23:05 +01:00
|
|
|
defmodule Mobilizon.Users.User do
|
2018-01-14 17:56:50 +01:00
|
|
|
@moduledoc """
|
2019-09-07 19:54:11 +02:00
|
|
|
Represents a local user.
|
2018-01-14 17:56:50 +01:00
|
|
|
"""
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
use Ecto.Schema
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
import Ecto.Changeset
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
2019-09-07 19:54:11 +02:00
|
|
|
alias Mobilizon.Crypto
|
2019-03-08 12:25:06 +01:00
|
|
|
alias Mobilizon.Events.FeedToken
|
2021-06-03 17:17:13 +02:00
|
|
|
alias Mobilizon.Users.{ActivitySetting, Setting, UserRole}
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Web.Email.Checker
|
2020-09-29 09:53:48 +02:00
|
|
|
import Mobilizon.Web.Gettext
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
email: String.t(),
|
|
|
|
password_hash: String.t(),
|
|
|
|
password: String.t(),
|
|
|
|
role: UserRole.t(),
|
|
|
|
confirmed_at: DateTime.t(),
|
|
|
|
confirmation_sent_at: DateTime.t(),
|
|
|
|
confirmation_token: String.t(),
|
|
|
|
reset_password_sent_at: DateTime.t(),
|
|
|
|
reset_password_token: String.t(),
|
2019-10-01 13:08:09 +02:00
|
|
|
locale: String.t(),
|
2019-09-07 19:54:11 +02:00
|
|
|
default_actor: Actor.t(),
|
2020-06-11 19:13:21 +02:00
|
|
|
disabled: boolean(),
|
2019-09-07 19:54:11 +02:00
|
|
|
actors: [Actor.t()],
|
2020-08-28 09:23:49 +02:00
|
|
|
feed_tokens: [FeedToken.t()],
|
|
|
|
last_sign_in_at: DateTime.t(),
|
|
|
|
last_sign_in_ip: String.t(),
|
|
|
|
current_sign_in_ip: String.t(),
|
|
|
|
current_sign_in_at: DateTime.t()
|
2019-09-07 19:54:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@required_attrs [:email]
|
|
|
|
@optional_attrs [
|
|
|
|
:role,
|
|
|
|
:password,
|
|
|
|
:password_hash,
|
|
|
|
:confirmed_at,
|
|
|
|
:confirmation_sent_at,
|
|
|
|
:confirmation_token,
|
|
|
|
:reset_password_sent_at,
|
2019-10-01 13:08:09 +02:00
|
|
|
:reset_password_token,
|
2020-06-27 19:12:45 +02:00
|
|
|
:default_actor_id,
|
2020-02-13 15:48:12 +01:00
|
|
|
:locale,
|
2020-06-11 19:13:21 +02:00
|
|
|
:unconfirmed_email,
|
2020-06-27 19:12:45 +02:00
|
|
|
:disabled,
|
2020-08-28 09:23:49 +02:00
|
|
|
:provider,
|
|
|
|
:last_sign_in_at,
|
|
|
|
:last_sign_in_ip,
|
|
|
|
:current_sign_in_ip,
|
|
|
|
:current_sign_in_at
|
2019-09-07 19:54:11 +02:00
|
|
|
]
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
2020-02-13 15:48:12 +01:00
|
|
|
@registration_required_attrs @required_attrs ++ [:password]
|
2019-09-07 19:54:11 +02:00
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
@auth_provider_required_attrs @required_attrs ++ [:provider]
|
|
|
|
|
2019-09-24 18:08:33 +02:00
|
|
|
@password_change_required_attrs [:password]
|
|
|
|
@password_reset_required_attrs @password_change_required_attrs ++
|
|
|
|
[:reset_password_token, :reset_password_sent_at]
|
2019-09-07 19:54:11 +02:00
|
|
|
|
|
|
|
@confirmation_token_length 30
|
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-09-07 19:54:11 +02:00
|
|
|
field(:role, UserRole, default: :user)
|
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)
|
2020-02-13 15:48:12 +01:00
|
|
|
field(:unconfirmed_email, :string)
|
2019-10-01 13:08:09 +02:00
|
|
|
field(:locale, :string, default: "en")
|
2020-06-11 19:13:21 +02:00
|
|
|
field(:disabled, :boolean, default: false)
|
2020-06-27 19:12:45 +02:00
|
|
|
field(:provider, :string)
|
2020-08-28 09:23:49 +02:00
|
|
|
field(:last_sign_in_at, :utc_datetime)
|
|
|
|
field(:last_sign_in_ip, :string)
|
|
|
|
field(:current_sign_in_ip, :string)
|
|
|
|
field(:current_sign_in_at, :utc_datetime)
|
2019-09-07 19:54:11 +02:00
|
|
|
|
|
|
|
belongs_to(:default_actor, Actor)
|
|
|
|
has_many(:actors, Actor)
|
2019-03-08 12:25:06 +01:00
|
|
|
has_many(:feed_tokens, FeedToken, foreign_key: :user_id)
|
2020-02-18 08:57:00 +01:00
|
|
|
has_one(:settings, Setting)
|
2021-06-03 17:17:13 +02:00
|
|
|
has_many(:activity_settings, ActivitySetting)
|
2018-01-13 23:33:03 +01:00
|
|
|
|
2017-12-08 09:58:14 +01:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def changeset(%__MODULE__{} = user, attrs) do
|
2018-11-29 17:43:22 +01:00
|
|
|
changeset =
|
|
|
|
user
|
2019-09-07 19:54:11 +02:00
|
|
|
|> cast(attrs, @attrs)
|
|
|
|
|> validate_required(@required_attrs)
|
2020-09-29 09:53:48 +02:00
|
|
|
|> unique_constraint(:email, message: dgettext("errors", "This email is already used."))
|
2019-12-20 13:04:34 +01:00
|
|
|
|> Checker.validate_changeset()
|
2020-09-29 09:53:48 +02:00
|
|
|
|> validate_length(:password,
|
|
|
|
min: 6,
|
|
|
|
max: 200,
|
|
|
|
message: dgettext("errors", "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
|
|
|
|
|
2020-06-11 19:13:21 +02:00
|
|
|
def delete_changeset(%__MODULE__{} = user) do
|
|
|
|
user
|
|
|
|
|> change()
|
|
|
|
|> put_change(:disabled, true)
|
|
|
|
|> put_change(:default_actor_id, nil)
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec registration_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def registration_changeset(%__MODULE__{} = user, attrs) do
|
2019-09-07 19:54:11 +02:00
|
|
|
user
|
|
|
|
|> changeset(attrs)
|
2018-11-29 17:43:22 +01:00
|
|
|
|> cast_assoc(:default_actor)
|
2019-09-07 19:54:11 +02:00
|
|
|
|> validate_required(@registration_required_attrs)
|
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,
|
2020-09-29 09:53:48 +02:00
|
|
|
message:
|
|
|
|
dgettext(
|
|
|
|
"errors",
|
|
|
|
"The registration token 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
|
|
|
|
|
2020-06-27 19:12:45 +02:00
|
|
|
@doc false
|
|
|
|
@spec auth_provider_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def auth_provider_changeset(%__MODULE__{} = user, attrs) do
|
|
|
|
user
|
|
|
|
|> changeset(attrs)
|
|
|
|
|> cast_assoc(:default_actor)
|
|
|
|
|> put_change(:confirmed_at, DateTime.utc_now() |> DateTime.truncate(:second))
|
|
|
|
|> validate_required(@auth_provider_required_attrs)
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec send_password_reset_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def send_password_reset_changeset(%__MODULE__{} = user, attrs) do
|
2019-09-07 19:54:11 +02:00
|
|
|
cast(user, attrs, [:reset_password_token, :reset_password_sent_at])
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc false
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec password_reset_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def password_reset_changeset(%__MODULE__{} = user, attrs) do
|
2019-09-24 18:08:33 +02:00
|
|
|
password_change_changeset(user, attrs, @password_reset_required_attrs)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Changeset to change a password
|
|
|
|
|
|
|
|
It checks the minimum requirements for a password and hashes it.
|
|
|
|
"""
|
|
|
|
@spec password_change_changeset(t, map) :: Ecto.Changeset.t()
|
|
|
|
def password_change_changeset(
|
|
|
|
%__MODULE__{} = user,
|
|
|
|
attrs,
|
|
|
|
required_attrs \\ @password_change_required_attrs
|
|
|
|
) do
|
2018-07-04 14:29:17 +02:00
|
|
|
user
|
2019-09-24 18:08:33 +02:00
|
|
|
|> cast(attrs, required_attrs)
|
2019-09-07 19:54:11 +02:00
|
|
|
|> validate_length(:password,
|
2018-07-27 10:45:35 +02:00
|
|
|
min: 6,
|
2019-10-18 11:21:31 +02:00
|
|
|
max: 200,
|
2018-07-27 10:45:35 +02:00
|
|
|
message: "registration.error.password_too_short"
|
|
|
|
)
|
2018-07-04 14:29:17 +02:00
|
|
|
|> hash_password()
|
|
|
|
end
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@doc """
|
2019-11-21 15:51:13 +01:00
|
|
|
Checks whether an user is confirmed.
|
2019-09-07 19:54:11 +02:00
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec is_confirmed(t) :: boolean
|
|
|
|
def is_confirmed(%__MODULE__{confirmed_at: nil}), do: false
|
|
|
|
def is_confirmed(%__MODULE__{}), do: true
|
2019-09-07 19:54:11 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns whether an user owns an actor.
|
|
|
|
"""
|
2019-09-13 01:55:45 +02:00
|
|
|
@spec owns_actor(t, integer | String.t()) :: {:is_owned, Actor.t() | nil}
|
|
|
|
def owns_actor(%__MODULE__{actors: actors}, actor_id) do
|
2019-09-07 19:54:11 +02:00
|
|
|
user_actor = Enum.find(actors, fn actor -> "#{actor.id}" == "#{actor_id}" end)
|
|
|
|
|
|
|
|
{:is_owned, user_actor}
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec save_confirmation_token(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
2019-11-21 15:51:13 +01:00
|
|
|
defp save_confirmation_token(
|
|
|
|
%Ecto.Changeset{valid?: true, changes: %{email: _email}} = changeset
|
|
|
|
) do
|
|
|
|
case fetch_change(changeset, :confirmed_at) do
|
|
|
|
:error ->
|
2019-09-07 19:54:11 +02:00
|
|
|
changeset
|
|
|
|
|> put_change(:confirmation_token, Crypto.random_string(@confirmation_token_length))
|
2019-11-21 15:51:13 +01:00
|
|
|
|> put_change(: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
|
|
|
|
|
2019-11-21 15:51:13 +01:00
|
|
|
defp save_confirmation_token(%Ecto.Changeset{} = changeset), do: changeset
|
|
|
|
|
2019-09-07 19:54:11 +02:00
|
|
|
@spec hash_password(Ecto.Changeset.t()) :: Ecto.Changeset.t()
|
2019-10-18 11:21:31 +02:00
|
|
|
defp hash_password(%Ecto.Changeset{} = changeset) do
|
2017-12-09 14:58:37 +01:00
|
|
|
case changeset do
|
2018-07-27 10:45:35 +02:00
|
|
|
%Ecto.Changeset{valid?: true, changes: %{password: password}} ->
|
2019-09-07 19:54:11 +02:00
|
|
|
put_change(changeset, :password_hash, 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
|
|
|
|
end
|