2019-03-14 14:00:34 +00:00
|
|
|
defmodule Mobilizon.Service.Users.Tools do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Common functions for actors services
|
|
|
|
"""
|
2019-03-05 16:23:05 +00:00
|
|
|
alias Mobilizon.Users.User
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2018-11-12 17:17:53 +00:00
|
|
|
@spec we_can_send_email(User.t(), atom()) :: :ok | {:error, :email_too_soon}
|
2018-11-06 09:30:27 +00:00
|
|
|
def we_can_send_email(%User{} = user, key \\ :reset_password_sent_at) do
|
|
|
|
case Map.get(user, key) do
|
|
|
|
nil ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
_ ->
|
2019-02-14 13:19:55 +00:00
|
|
|
case Timex.before?(
|
|
|
|
Timex.shift(Map.get(user, key), hours: 1),
|
|
|
|
DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
) do
|
2018-11-06 09:30:27 +00:00
|
|
|
true ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
false ->
|
|
|
|
{:error, :email_too_soon}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec random_string(integer) :: String.t()
|
|
|
|
def random_string(length) do
|
|
|
|
length
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64()
|
|
|
|
end
|
|
|
|
end
|
2019-03-06 17:45:26 +00:00
|
|
|
|
|
|
|
defmodule Mobilizon.Users.Guards do
|
|
|
|
@moduledoc """
|
|
|
|
Guards for users
|
|
|
|
"""
|
|
|
|
|
|
|
|
defguard is_admin(role) when is_atom(role) and role == :administrator
|
|
|
|
|
|
|
|
defguard is_moderator(role) when is_atom(role) and role in [:administrator, :moderator]
|
|
|
|
end
|