Move set_admin task to lib/mix/tasks/pleroma/user.ex

This commit is contained in:
rinpatch 2018-12-01 18:55:52 +03:00
parent 6f174cbb71
commit ae82852330
2 changed files with 26 additions and 32 deletions

View file

@ -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, [])

View file

@ -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