1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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, is_confirmed: true)
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(["reset_password", nickname]) do
113 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
114 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
115 shell_info("Generated password reset token for #{user.nickname}")
119 Pleroma.Web.Router.Helpers.reset_password_url(
120 Pleroma.Web.Endpoint,
128 shell_error("No local user #{nickname}")
132 def run(["reset_mfa", nickname]) do
135 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
136 {:ok, _token} <- Pleroma.MFA.disable(user) do
137 shell_info("Multi-Factor Authentication disabled for #{user.nickname}")
140 shell_error("No local user #{nickname}")
144 def run(["deactivate", nickname]) do
147 with %User{} = user <- User.get_cached_by_nickname(nickname) do
148 shell_info("Deactivating #{user.nickname}")
149 User.set_activation(user, false)
152 user = User.get_cached_by_id(user.id)
154 if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do
155 shell_info("Successfully unsubscribed all local followers from #{user.nickname}")
159 shell_error("No user #{nickname}")
163 def run(["deactivate_all_from_instance", instance]) do
166 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
167 |> Pleroma.Repo.chunk_stream(500, :batches)
168 |> Stream.each(fn users ->
170 |> Enum.each(fn user ->
171 run(["deactivate", user.nickname])
177 def run(["set", nickname | rest]) do
191 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
193 case Keyword.get(options, :admin) do
195 value -> set_admin(user, value)
199 case Keyword.get(options, :confirmed) do
201 value -> set_confirmation(user, value)
205 case Keyword.get(options, :locked) do
207 value -> set_locked(user, value)
211 case Keyword.get(options, :moderator) do
213 value -> set_moderator(user, value)
217 shell_error("No local user #{nickname}")
221 def run(["tag", nickname | tags]) do
224 with %User{} = user <- User.get_cached_by_nickname(nickname) do
225 user = user |> User.tag(tags)
227 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
230 shell_error("Could not change user tags for #{nickname}")
234 def run(["untag", nickname | tags]) do
237 with %User{} = user <- User.get_cached_by_nickname(nickname) do
238 user = user |> User.untag(tags)
240 shell_info("Tags of #{user.nickname}: #{inspect(user.tags)}")
243 shell_error("Could not change user tags for #{nickname}")
247 def run(["invite" | rest]) do
249 OptionParser.parse(rest,
258 |> Keyword.update(:expires_at, {:ok, nil}, fn
260 val -> Date.from_iso8601(val)
266 with {:ok, val} <- options[:expires_at],
267 options = Map.put(options, :expires_at, val),
268 {:ok, invite} <- UserInviteToken.create_invite(options) do
269 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
272 Pleroma.Web.Router.Helpers.redirect_url(
273 Pleroma.Web.Endpoint,
281 shell_error("Could not create invite token: #{inspect(error)}")
285 def run(["invites"]) do
288 shell_info("Invites list:")
290 UserInviteToken.list_invites()
291 |> Enum.each(fn invite ->
293 with expires_at when not is_nil(expires_at) <- invite.expires_at do
294 " | Expires at: #{Date.to_string(expires_at)}"
298 with max_use when not is_nil(max_use) <- invite.max_use do
299 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
303 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
305 }#{expire_info}#{using_info}"
310 def run(["revoke_invite", token]) do
313 with {:ok, invite} <- UserInviteToken.find_by_token(token),
314 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
315 shell_info("Invite for token #{token} was revoked.")
317 _ -> shell_error("No invite found with token #{token}")
321 def run(["delete_activities", nickname]) do
324 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
325 User.delete_user_activities(user)
326 shell_info("User #{nickname} statuses deleted.")
329 shell_error("No local user #{nickname}")
333 def run(["confirm", nickname]) do
336 with %User{} = user <- User.get_cached_by_nickname(nickname) do
337 {:ok, user} = User.confirm(user)
339 message = if !user.is_confirmed, do: "needs", else: "doesn't need"
341 shell_info("#{nickname} #{message} confirmation.")
344 shell_error("No local user #{nickname}")
348 def run(["confirm_all"]) do
351 Pleroma.User.Query.build(%{
358 |> Pleroma.Repo.chunk_stream(500, :batches)
359 |> Stream.each(fn users ->
361 |> Enum.each(fn user -> User.set_confirmation(user, true) end)
366 def run(["unconfirm_all"]) do
369 Pleroma.User.Query.build(%{
376 |> Pleroma.Repo.chunk_stream(500, :batches)
377 |> Stream.each(fn users ->
379 |> Enum.each(fn user -> User.set_confirmation(user, false) end)
384 def run(["sign_out", nickname]) do
387 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
388 User.global_sign_out(user)
390 shell_info("#{nickname} signed out from all apps.")
393 shell_error("No local user #{nickname}")
400 Pleroma.User.Query.build(%{local: true})
401 |> Pleroma.Repo.chunk_stream(500, :batches)
402 |> Stream.each(fn users ->
404 |> Enum.each(fn user ->
406 "#{user.nickname} moderator: #{user.is_moderator}, admin: #{user.is_admin}, locked: #{
408 }, is_active: #{user.is_active}"
415 defp set_moderator(user, value) do
418 |> Changeset.change(%{is_moderator: value})
419 |> User.update_and_set_cache()
421 shell_info("Moderator status of #{user.nickname}: #{user.is_moderator}")
425 defp set_admin(user, value) do
426 {:ok, user} = User.admin_api_update(user, %{is_admin: value})
428 shell_info("Admin status of #{user.nickname}: #{user.is_admin}")
432 defp set_locked(user, value) do
435 |> Changeset.change(%{is_locked: value})
436 |> User.update_and_set_cache()
438 shell_info("Locked status of #{user.nickname}: #{user.is_locked}")
442 defp set_confirmation(user, value) do
443 {:ok, user} = User.set_confirmation(user, value)
445 shell_info("Confirmation status of #{user.nickname}: #{user.is_confirmed}")