2019-07-23 13:49:22 +02:00
|
|
|
defmodule Mobilizon.Admin do
|
|
|
|
@moduledoc """
|
|
|
|
The Admin context.
|
|
|
|
"""
|
|
|
|
|
2019-09-08 01:49:56 +02:00
|
|
|
import Ecto.Query
|
2020-01-23 21:59:50 +01:00
|
|
|
import EctoEnum
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2020-01-23 21:59:50 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.{Admin, Users}
|
2019-07-23 13:49:22 +02:00
|
|
|
alias Mobilizon.Admin.ActionLog
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Admin.Setting
|
2019-09-08 01:49:56 +02:00
|
|
|
alias Mobilizon.Storage.{Page, Repo}
|
2020-01-23 21:59:50 +01:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
|
|
|
|
defenum(ActionLogAction, [
|
|
|
|
"update",
|
|
|
|
"create",
|
2020-06-11 19:13:21 +02:00
|
|
|
"delete",
|
|
|
|
"suspend",
|
|
|
|
"unsuspend"
|
2020-01-23 21:59:50 +01:00
|
|
|
])
|
2019-07-23 13:49:22 +02:00
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Ecto.Multi
|
|
|
|
|
2019-07-23 13:49:22 +02:00
|
|
|
@doc """
|
|
|
|
Creates a action_log.
|
|
|
|
"""
|
2019-09-08 02:06:28 +02:00
|
|
|
@spec create_action_log(map) :: {:ok, ActionLog.t()} | {:error, Ecto.Changeset.t()}
|
2019-07-23 13:49:22 +02:00
|
|
|
def create_action_log(attrs \\ %{}) do
|
|
|
|
%ActionLog{}
|
|
|
|
|> ActionLog.changeset(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
end
|
2019-09-08 02:06:28 +02:00
|
|
|
|
2019-09-09 00:52:49 +02:00
|
|
|
@doc """
|
|
|
|
Returns the list of action logs.
|
|
|
|
"""
|
2021-04-27 17:53:11 +02:00
|
|
|
@spec list_action_logs(integer | nil, integer | nil) :: Page.t()
|
2019-09-09 00:52:49 +02:00
|
|
|
def list_action_logs(page \\ nil, limit \\ nil) do
|
|
|
|
list_action_logs_query()
|
2021-04-27 17:53:11 +02:00
|
|
|
|> Page.build_page(page, limit)
|
2019-09-09 00:52:49 +02:00
|
|
|
end
|
|
|
|
|
2020-01-23 21:59:50 +01:00
|
|
|
@doc """
|
|
|
|
Log an admin action
|
|
|
|
"""
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec log_action(Actor.t(), String.t(), struct()) ::
|
|
|
|
{:ok, ActionLog.t()} | {:error, Ecto.Changeset.t() | :user_not_moderator}
|
2020-01-23 21:59:50 +01:00
|
|
|
def log_action(%Actor{user_id: user_id, id: actor_id}, action, target) do
|
|
|
|
with %User{role: role} <- Users.get_user!(user_id),
|
|
|
|
{:role, true} <- {:role, role in [:administrator, :moderator]},
|
|
|
|
{:ok, %ActionLog{} = create_action_log} <-
|
|
|
|
Admin.create_action_log(%{
|
|
|
|
"actor_id" => actor_id,
|
|
|
|
"target_type" => to_string(target.__struct__),
|
|
|
|
"target_id" => target.id,
|
|
|
|
"action" => action,
|
|
|
|
"changes" => stringify_struct(target)
|
|
|
|
}) do
|
|
|
|
{:ok, create_action_log}
|
2021-09-24 16:46:42 +02:00
|
|
|
else
|
|
|
|
{:role, false} ->
|
|
|
|
{:error, :user_not_moderator}
|
2020-01-23 21:59:50 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-08 02:06:28 +02:00
|
|
|
@spec list_action_logs_query :: Ecto.Query.t()
|
|
|
|
defp list_action_logs_query do
|
2019-09-21 23:59:07 +02:00
|
|
|
from(r in ActionLog, preload: [:actor], order_by: [desc: :id])
|
2019-09-08 02:06:28 +02:00
|
|
|
end
|
2020-01-23 21:59:50 +01:00
|
|
|
|
|
|
|
defp stringify_struct(%_{} = struct) do
|
|
|
|
association_fields = struct.__struct__.__schema__(:associations)
|
|
|
|
|
|
|
|
struct
|
|
|
|
|> Map.from_struct()
|
|
|
|
|> Map.drop(association_fields ++ [:__meta__])
|
|
|
|
end
|
|
|
|
|
|
|
|
defp stringify_struct(struct), do: struct
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
def get_admin_setting_value(group, name, fallback \\ nil)
|
2021-04-08 16:41:49 +02:00
|
|
|
when is_binary(group) and is_binary(name) do
|
2019-12-20 13:04:34 +01:00
|
|
|
case Repo.get_by(Setting, group: group, name: name) do
|
2020-10-07 15:37:23 +02:00
|
|
|
nil ->
|
|
|
|
fallback
|
|
|
|
|
|
|
|
%Setting{value: ""} ->
|
|
|
|
fallback
|
|
|
|
|
|
|
|
%Setting{value: nil} ->
|
|
|
|
fallback
|
|
|
|
|
|
|
|
%Setting{value: value} ->
|
|
|
|
get_setting_value(value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-08 08:19:59 +02:00
|
|
|
def get_setting_value(nil), do: nil
|
|
|
|
|
2020-10-07 15:37:23 +02:00
|
|
|
def get_setting_value(value) do
|
|
|
|
case Jason.decode(value) do
|
|
|
|
{:ok, val} ->
|
|
|
|
val
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
case value do
|
|
|
|
"true" -> true
|
|
|
|
"false" -> false
|
|
|
|
value -> value
|
|
|
|
end
|
2019-12-20 13:04:34 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec save_settings(String.t(), map()) :: {:ok, any} | {:error, any}
|
2019-12-20 13:04:34 +01:00
|
|
|
def save_settings(group, args) do
|
|
|
|
Multi.new()
|
|
|
|
|> do_save_setting(group, args)
|
|
|
|
|> Repo.transaction()
|
|
|
|
end
|
|
|
|
|
2020-07-07 15:51:42 +02:00
|
|
|
def clear_settings(group) do
|
|
|
|
Setting |> where([s], s.group == ^group) |> Repo.delete_all()
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec do_save_setting(Ecto.Multi.t(), String.t(), map()) :: Ecto.Multi.t()
|
2019-12-20 13:04:34 +01:00
|
|
|
defp do_save_setting(transaction, _group, args) when args == %{}, do: transaction
|
|
|
|
|
|
|
|
defp do_save_setting(transaction, group, args) do
|
|
|
|
key = hd(Map.keys(args))
|
|
|
|
{val, rest} = Map.pop(args, key)
|
|
|
|
|
|
|
|
transaction =
|
|
|
|
Multi.insert(
|
|
|
|
transaction,
|
|
|
|
key,
|
|
|
|
Setting.changeset(%Setting{}, %{
|
|
|
|
group: group,
|
|
|
|
name: Atom.to_string(key),
|
2020-10-07 15:37:23 +02:00
|
|
|
value: convert_to_string(val)
|
2019-12-20 13:04:34 +01:00
|
|
|
}),
|
|
|
|
on_conflict: :replace_all,
|
|
|
|
conflict_target: [:group, :name]
|
|
|
|
)
|
|
|
|
|
|
|
|
do_save_setting(transaction, group, rest)
|
|
|
|
end
|
2020-10-07 15:37:23 +02:00
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec convert_to_string(any()) :: String.t()
|
2020-10-07 15:37:23 +02:00
|
|
|
defp convert_to_string(val) do
|
|
|
|
case val do
|
|
|
|
val when is_list(val) -> Jason.encode!(val)
|
|
|
|
val -> to_string(val)
|
|
|
|
end
|
|
|
|
end
|
2019-07-23 13:49:22 +02:00
|
|
|
end
|