1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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}
54 - password: #{if(generated_password?, do: "[generated; a reset link will be created]", else: password)}
57 - moderator: #{if(moderator?, do: "true", else: "false")}
58 - admin: #{if(admin?, do: "true", else: "false")}
61 proceed? = assume_yes? or shell_prompt("Continue?", "n") in ~w(Yn Y y)
70 password_confirmation: password,
75 changeset = User.register_changeset(%User{}, params, is_confirmed: true)
76 {:ok, _user} = User.register(changeset)
78 shell_info("User #{nickname} created")
81 run(["set", nickname, "--moderator"])
85 run(["set", nickname, "--admin"])
88 if generated_password? do
89 run(["reset_password", nickname])
92 shell_info("User will not be created.")
96 def run(["rm", nickname]) do
99 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
100 {:ok, delete_data, _} <- Builder.delete(user, user.ap_id),
101 {:ok, _delete, _} <- Pipeline.common_pipeline(delete_data, local: true) do
102 shell_info("User #{nickname} deleted.")
104 _ -> shell_error("No local user #{nickname}")
108 def run(["reset_password", nickname]) do
111 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
112 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
113 shell_info("Generated password reset token for #{user.nickname}")
116 Pleroma.Web.Router.Helpers.reset_password_url(Pleroma.Web.Endpoint, :reset, token.token)
118 IO.puts("URL: #{url}")
121 shell_error("No local user #{nickname}")
125 def run(["reset_mfa", nickname]) do
128 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
129 {:ok, _token} <- Pleroma.MFA.disable(user) do
130 shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
133 shell_error("No local user #{nickname}")
137 def run(["activate", nickname]) do
140 with %User{} = user <- User.get_cached_by_nickname(nickname),
141 false <- user.is_active do
142 User.set_activation(user, true)
145 shell_info("Successfully activated #{nickname}")
148 shell_info("User #{nickname} already activated")
151 shell_error("No user #{nickname}")
155 def run(["deactivate", nickname]) do
158 with %User{} = user <- User.get_cached_by_nickname(nickname),
159 true <- user.is_active do
160 User.set_activation(user, false)
163 user = User.get_cached_by_id(user.id)
165 if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do
166 shell_info("Successfully deactivated #{nickname} and unsubscribed all local followers")
170 shell_info("User #{nickname} already deactivated")
173 shell_error("No user #{nickname}")
177 def run(["deactivate_all_from_instance", instance]) do
180 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
181 |> Pleroma.Repo.chunk_stream(500, :batches)
182 |> Stream.each(fn users ->
184 |> Enum.each(fn user ->
185 run(["deactivate", user.nickname])
191 def run(["set", nickname | rest]) do
205 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
207 case Keyword.get(options, :admin) do
209 value -> set_admin(user, value)
213 case Keyword.get(options, :confirmed) do
215 value -> set_confirmation(user, value)
219 case Keyword.get(options, :locked) do
221 value -> set_locked(user, value)
225 case Keyword.get(options, :moderator) do
227 value -> set_moderator(user, value)
231 shell_error("No local user #{nickname}")
235 def run(["tag", nickname | tags]) do
238 with %User{} = user <- User.get_cached_by_nickname(nickname) do
239 user = user |> User.tag(tags)
241 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
244 shell_error("Could not change user tags for #{nickname}")
248 def run(["untag", nickname | tags]) do
251 with %User{} = user <- User.get_cached_by_nickname(nickname) do
252 user = user |> User.untag(tags)
254 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
257 shell_error("Could not change user tags for #{nickname}")
261 def run(["invite" | rest]) do
263 OptionParser.parse(rest,
272 |> Keyword.update(:expires_at, {:ok, nil}, fn
274 val -> Date.from_iso8601(val)
280 with {:ok, val} <- options[:expires_at],
281 options = Map.put(options, :expires_at, val),
282 {:ok, invite} <- UserInviteToken.create_invite(options) do
283 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
286 Pleroma.Web.Router.Helpers.redirect_url(
287 Pleroma.Web.Endpoint,
295 shell_error("Could not create invite token: #{inspect(error)}")
299 def run(["invites"]) do
302 shell_info("Invites list:")
304 UserInviteToken.list_invites()
305 |> Enum.each(fn invite ->
307 with expires_at when not is_nil(expires_at) <- invite.expires_at do
308 " | Expires at: #{Date.to_string(expires_at)}"
312 with max_use when not is_nil(max_use) <- invite.max_use do
313 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
317 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{invite.used}#{expire_info}#{using_info}"
322 def run(["revoke_invite", token]) do
325 with {:ok, invite} <- UserInviteToken.find_by_token(token),
326 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
327 shell_info("Invite for token #{token} was revoked.")
329 _ -> shell_error("No invite found with token #{token}")
333 def run(["delete_activities", nickname]) do
336 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
337 User.delete_user_activities(user)
338 shell_info("User #{nickname} statuses deleted.")
341 shell_error("No local user #{nickname}")
345 def run(["confirm", nickname]) do
348 with %User{} = user <- User.get_cached_by_nickname(nickname) do
349 {:ok, user} = User.confirm(user)
351 message = if !user.is_confirmed, do: "needs", else: "doesn't need"
353 shell_info("#{nickname} #{message} confirmation.")
356 shell_error("No local user #{nickname}")
360 def run(["confirm_all"]) do
363 Pleroma.User.Query.build(%{
370 |> Pleroma.Repo.chunk_stream(500, :batches)
371 |> Stream.each(fn users ->
373 |> Enum.each(fn user -> User.set_confirmation(user, true) end)
378 def run(["unconfirm_all"]) do
381 Pleroma.User.Query.build(%{
388 |> Pleroma.Repo.chunk_stream(500, :batches)
389 |> Stream.each(fn users ->
391 |> Enum.each(fn user -> User.set_confirmation(user, false) end)
396 def run(["sign_out", nickname]) do
399 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
400 User.global_sign_out(user)
402 shell_info("#{nickname} signed out from all apps.")
405 shell_error("No local user #{nickname}")
412 Pleroma.User.Query.build(%{local: true})
413 |> Pleroma.Repo.chunk_stream(500, :batches)
414 |> Stream.each(fn users ->
416 |> Enum.each(fn user ->
418 "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{user.is_locked}, is_active: #{user.is_active}"
425 def run(["fix_follow_state", local_user, remote_user]) do
428 with {:local, %User{} = local} <- {:local, User.get_by_nickname(local_user)},
429 {:remote, %User{} = remote} <- {:remote, User.get_by_nickname(remote_user)},
430 {:follow_data, %{data: %{"state" => request_state}}} <-
431 {:follow_data, Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(local, remote)} do
432 calculated_state = User.following?(local, remote)
435 "Request state is #{request_state}, vs calculated state of following=#{calculated_state}"
438 if calculated_state == false && request_state == "accept" do
439 shell_info("Discrepancy found, fixing")
440 Pleroma.Web.CommonAPI.reject_follow_request(local, remote)
441 shell_info("Relationship fixed")
443 shell_info("No discrepancy found")
447 shell_error("No local user #{local_user}")
450 shell_error("No remote user #{remote_user}")
453 shell_error("No follow data for #{local_user} and #{remote_user}")
457 defp set_moderator(user, value) do
460 |> Changeset.change(%{is_moderator: value})
461 |> User.update_and_set_cache()
463 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
467 defp set_admin(user, value) do
468 {:ok, user} = User.admin_api_update(user, %{is_admin: value})
470 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
474 defp set_locked(user, value) do
477 |> Changeset.change(%{is_locked: value})
478 |> User.update_and_set_cache()
480 shell_info("Locked status of #{user.nickname}: #{user.is_locked}")
484 defp set_confirmation(user, value) do
485 {:ok, user} = User.set_confirmation(user, value)
487 shell_info("Confirmation status of #{user.nickname}: #{user.is_confirmed}")