1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.User do
10 alias Pleroma.UserInviteToken
11 alias Pleroma.Web.ActivityPub.Builder
12 alias Pleroma.Web.ActivityPub.Pipeline
14 @shortdoc "Manages Pleroma users"
15 @moduledoc File.read!("docs/administration/CLI_tasks/user.md")
17 def run(["new", nickname, email | rest]) do
34 name = Keyword.get(options, :name, nickname)
35 bio = Keyword.get(options, :bio, "")
37 {password, generated_password?} =
38 case Keyword.get(options, :password) do
40 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
46 moderator? = Keyword.get(options, :moderator, false)
47 admin? = Keyword.get(options, :admin, false)
48 assume_yes? = Keyword.get(options, :assume_yes, false)
51 A user will be created with the following information:
52 - nickname: #{nickname}
55 if(generated_password?, do: "[generated; a reset link will be created]", else: password)
59 - moderator: #{if(moderator?, do: "true", else: "false")}
60 - admin: #{if(admin?, do: "true", else: "false")}
63 proceed? = assume_yes? or shell_prompt("Continue?", "n") in ~w(Yn Y y)
72 password_confirmation: password,
77 changeset = User.register_changeset(%User{}, params, need_confirmation: false)
78 {:ok, _user} = User.register(changeset)
80 shell_info("User #{nickname} created")
83 run(["set", nickname, "--moderator"])
87 run(["set", nickname, "--admin"])
90 if generated_password? do
91 run(["reset_password", nickname])
94 shell_info("User will not be created.")
98 def run(["rm", nickname]) do
101 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
102 {:ok, delete_data, _} <- Builder.delete(user, user.ap_id),
103 {:ok, _delete, _} <- Pipeline.common_pipeline(delete_data, local: true) do
104 shell_info("User #{nickname} deleted.")
106 _ -> shell_error("No local user #{nickname}")
110 def run(["toggle_activated", nickname]) do
113 with %User{} = user <- User.get_cached_by_nickname(nickname) do
114 {:ok, user} = User.deactivate(user, !user.deactivated)
117 "Activation status of #{nickname}: #{if(user.deactivated, do: "de", else: "")}activated"
121 shell_error("No user #{nickname}")
125 def run(["reset_password", nickname]) do
128 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
129 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
130 shell_info("Generated password reset token for #{user.nickname}")
134 Pleroma.Web.Router.Helpers.reset_password_url(
135 Pleroma.Web.Endpoint,
143 shell_error("No local user #{nickname}")
147 def run(["reset_mfa", nickname]) do
150 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
151 {:ok, _token} <- Pleroma.MFA.disable(user) do
152 shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
155 shell_error("No local user #{nickname}")
159 def run(["deactivate", nickname]) do
162 with %User{} = user <- User.get_cached_by_nickname(nickname) do
163 shell_info("Deactivating #{user.nickname}")
164 User.deactivate(user)
167 user = User.get_cached_by_id(user.id)
169 if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do
170 shell_info("Successfully unsubscribed all local followers from #{user.nickname}")
174 shell_error("No user #{nickname}")
178 def run(["deactivate_all_from_instance", instance]) do
181 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
182 |> Pleroma.Repo.chunk_stream(500, :batches)
183 |> Stream.each(fn users ->
185 |> Enum.each(fn user ->
186 run(["deactivate", user.nickname])
192 def run(["set", nickname | rest]) do
206 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
208 case Keyword.get(options, :admin) do
210 value -> set_admin(user, value)
214 case Keyword.get(options, :confirmed) do
216 value -> set_confirmed(user, value)
220 case Keyword.get(options, :locked) do
222 value -> set_locked(user, value)
226 case Keyword.get(options, :moderator) do
228 value -> set_moderator(user, value)
232 shell_error("No local user #{nickname}")
236 def run(["tag", nickname | tags]) do
239 with %User{} = user <- User.get_cached_by_nickname(nickname) do
240 user = user |> User.tag(tags)
242 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
245 shell_error("Could not change user tags for #{nickname}")
249 def run(["untag", nickname | tags]) do
252 with %User{} = user <- User.get_cached_by_nickname(nickname) do
253 user = user |> User.untag(tags)
255 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
258 shell_error("Could not change user tags for #{nickname}")
262 def run(["invite" | rest]) do
264 OptionParser.parse(rest,
273 |> Keyword.update(:expires_at, {:ok, nil}, fn
275 val -> Date.from_iso8601(val)
281 with {:ok, val} <- options[:expires_at],
282 options = Map.put(options, :expires_at, val),
283 {:ok, invite} <- UserInviteToken.create_invite(options) do
284 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
287 Pleroma.Web.Router.Helpers.redirect_url(
288 Pleroma.Web.Endpoint,
296 shell_error("Could not create invite token: #{inspect(error)}")
300 def run(["invites"]) do
303 shell_info("Invites list:")
305 UserInviteToken.list_invites()
306 |> Enum.each(fn invite ->
308 with expires_at when not is_nil(expires_at) <- invite.expires_at do
309 " | Expires at: #{Date.to_string(expires_at)}"
313 with max_use when not is_nil(max_use) <- invite.max_use do
314 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
318 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
320 }#{expire_info}#{using_info}"
325 def run(["revoke_invite", token]) do
328 with {:ok, invite} <- UserInviteToken.find_by_token(token),
329 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
330 shell_info("Invite for token #{token} was revoked.")
332 _ -> shell_error("No invite found with token #{token}")
336 def run(["delete_activities", nickname]) do
339 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
340 User.delete_user_activities(user)
341 shell_info("User #{nickname} statuses deleted.")
344 shell_error("No local user #{nickname}")
348 def run(["confirm", nickname]) do
351 with %User{} = user <- User.get_cached_by_nickname(nickname) do
352 {:ok, user} = User.confirm(user)
354 message = if user.confirmation_pending, do: "needs", else: "doesn't need"
356 shell_info("#{nickname} #{message} confirmation.")
359 shell_error("No local user #{nickname}")
363 def run(["confirm_all"]) do
366 Pleroma.User.Query.build(%{
373 |> Pleroma.Repo.chunk_stream(500, :batches)
374 |> Stream.each(fn users ->
376 |> Enum.each(fn user -> User.need_confirmation(user, false) end)
381 def run(["unconfirm_all"]) do
384 Pleroma.User.Query.build(%{
391 |> Pleroma.Repo.chunk_stream(500, :batches)
392 |> Stream.each(fn users ->
394 |> Enum.each(fn user -> User.need_confirmation(user, true) end)
399 def run(["sign_out", nickname]) do
402 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
403 User.global_sign_out(user)
405 shell_info("#{nickname} signed out from all apps.")
408 shell_error("No local user #{nickname}")
415 Pleroma.User.Query.build(%{local: true})
416 |> Pleroma.Repo.chunk_stream(500, :batches)
417 |> Stream.each(fn users ->
419 |> Enum.each(fn user ->
421 "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{
423 }, deactivated: #{user.deactivated}"
430 defp set_moderator(user, value) do
433 |> Changeset.change(%{is_moderator: value})
434 |> User.update_and_set_cache()
436 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
440 defp set_admin(user, value) do
441 {:ok, user} = User.admin_api_update(user, %{is_admin: value})
443 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
447 defp set_locked(user, value) do
450 |> Changeset.change(%{is_locked: value})
451 |> User.update_and_set_cache()
453 shell_info("Locked status of #{user.nickname}: #{user.is_locked}")
457 defp set_confirmed(user, value) do
458 {:ok, user} = User.need_confirmation(user, !value)
460 shell_info("Confirmation pending status of #{user.nickname}: #{user.confirmation_pending}")