From ae82852330105edb681d131fd33cf35557c8614c Mon Sep 17 00:00:00 2001 From: rinpatch Date: Sat, 1 Dec 2018 18:55:52 +0300 Subject: [PATCH] Move set_admin task to lib/mix/tasks/pleroma/user.ex --- lib/mix/tasks/pleroma/user.ex | 26 ++++++++++++++++++++++++++ lib/mix/tasks/set_admin.ex | 32 -------------------------------- 2 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 lib/mix/tasks/set_admin.ex diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index c7c69ed01..9aa569b2a 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -15,6 +15,7 @@ defmodule Mix.Tasks.Pleroma.User do - `--bio BIO` - the user's bio - `--password PASSWORD` - the user's password - `--moderator`/`--no-moderator` - whether the user is a moderator + - `--admin`/`--no-admin` - whether the user is an admin ## Delete the user's account. @@ -35,6 +36,7 @@ defmodule Mix.Tasks.Pleroma.User do Options: - `--locked`/`--no-locked` - whether the user's account is locked - `--moderator`/`--no-moderator` - whether the user is a moderator + - `--admin`/`--no-admin` - whether the user is an admin """ def run(["new", nickname, email | rest]) do @@ -154,6 +156,7 @@ def run(["set", nickname | rest]) do rest, strict: [ moderator: :boolean, + admin: :boolean, locked: :boolean ] ) @@ -167,6 +170,11 @@ def run(["set", nickname | rest]) do nil -> nil value -> set_locked(nickname, value) end + + case Keyword.get(options, :admin) do + nil -> nil + value -> set_admin(nickname, value) + end end defp set_moderator(nickname, value) do @@ -187,6 +195,24 @@ defp set_moderator(nickname, value) do end end + defp set_admin(nickname, value) do + Application.ensure_all_started(:pleroma) + + with %User{local: true} = user <- User.get_by_nickname(nickname) do + info = + user.info + |> Map.put("is_admin", value) + + cng = User.info_changeset(user, %{info: info}) + {:ok, user} = User.update_and_set_cache(cng) + + Mix.shell().info("Admin status of #{nickname}: #{user.info["is_admin"]}") + else + _ -> + Mix.shell().error("No local user #{nickname}") + end + end + defp set_locked(nickname, value) do Mix.Ecto.ensure_started(Repo, []) diff --git a/lib/mix/tasks/set_admin.ex b/lib/mix/tasks/set_admin.ex deleted file mode 100644 index d5ccf261b..000000000 --- a/lib/mix/tasks/set_admin.ex +++ /dev/null @@ -1,32 +0,0 @@ -defmodule Mix.Tasks.SetAdmin do - use Mix.Task - alias Pleroma.User - - @doc """ - Sets admin status - Usage: set_admin nickname [true|false] - """ - def run([nickname | rest]) do - Application.ensure_all_started(:pleroma) - - status = - case rest do - [status] -> status == "true" - _ -> true - end - - with %User{local: true} = user <- User.get_by_nickname(nickname) do - info = - user.info - |> Map.put("is_admin", !!status) - - cng = User.info_changeset(user, %{info: info}) - {:ok, user} = User.update_and_set_cache(cng) - - IO.puts("Admin status of #{nickname}: #{user.info["is_admin"]}") - else - _ -> - IO.puts("No local user #{nickname}") - end - end -end