1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Mix.Tasks.Pleroma.User do
9 alias Pleroma.UserInviteToken
10 alias Pleroma.Web.OAuth
12 @shortdoc "Manages Pleroma users"
13 @moduledoc File.read!("docs/administration/CLI_tasks/user.md")
15 def run(["new", nickname, email | rest]) do
32 name = Keyword.get(options, :name, nickname)
33 bio = Keyword.get(options, :bio, "")
35 {password, generated_password?} =
36 case Keyword.get(options, :password) do
38 {:crypto.strong_rand_bytes(16) |> Base.encode64(), true}
44 moderator? = Keyword.get(options, :moderator, false)
45 admin? = Keyword.get(options, :admin, false)
46 assume_yes? = Keyword.get(options, :assume_yes, false)
49 A user will be created with the following information:
50 - nickname: #{nickname}
53 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_yes?("Continue?")
70 password_confirmation: password,
75 changeset = User.register_changeset(%User{}, params, need_confirmation: false)
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) do
100 User.perform(:delete, user)
101 shell_info("User #{nickname} deleted.")
104 shell_error("No local user #{nickname}")
108 def run(["toggle_activated", nickname]) do
111 with %User{} = user <- User.get_cached_by_nickname(nickname) do
112 {:ok, user} = User.deactivate(user, !user.info.deactivated)
115 "Activation status of #{nickname}: #{if(user.info.deactivated, do: "de", else: "")}activated"
119 shell_error("No user #{nickname}")
123 def run(["reset_password", nickname]) do
126 with %User{local: true} = user <- User.get_cached_by_nickname(nickname),
127 {:ok, token} <- Pleroma.PasswordResetToken.create_token(user) do
128 shell_info("Generated password reset token for #{user.nickname}")
132 Pleroma.Web.Router.Helpers.reset_password_url(
133 Pleroma.Web.Endpoint,
141 shell_error("No local user #{nickname}")
145 def run(["unsubscribe", nickname]) do
148 with %User{} = user <- User.get_cached_by_nickname(nickname) do
149 shell_info("Deactivating #{user.nickname}")
150 User.deactivate(user)
153 |> User.get_friends()
154 |> Enum.each(fn friend ->
155 user = User.get_cached_by_id(user.id)
157 shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}")
158 User.unfollow(user, friend)
163 user = User.get_cached_by_id(user.id)
165 if Enum.empty?(user.following) do
166 shell_info("Successfully unsubscribed all followers from #{user.nickname}")
170 shell_error("No user #{nickname}")
174 def run(["unsubscribe_all_from_instance", instance]) do
177 Pleroma.User.Query.build(%{nickname: "@#{instance}"})
178 |> Pleroma.RepoStreamer.chunk_stream(500)
179 |> Stream.each(fn users ->
181 |> Enum.each(fn user ->
182 run(["unsubscribe", user.nickname])
188 def run(["set", nickname | rest]) do
201 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
203 case Keyword.get(options, :moderator) do
205 value -> set_moderator(user, value)
209 case Keyword.get(options, :locked) do
211 value -> set_locked(user, value)
215 case Keyword.get(options, :admin) do
217 value -> set_admin(user, value)
221 shell_error("No local user #{nickname}")
225 def run(["tag", nickname | tags]) do
228 with %User{} = user <- User.get_cached_by_nickname(nickname) do
229 user = user |> User.tag(tags)
231 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
234 shell_error("Could not change user tags for #{nickname}")
238 def run(["untag", nickname | tags]) do
241 with %User{} = user <- User.get_cached_by_nickname(nickname) do
242 user = user |> User.untag(tags)
244 shell_info("Tags of #{user.nickname}: #{inspect(tags)}")
247 shell_error("Could not change user tags for #{nickname}")
251 def run(["invite" | rest]) do
253 OptionParser.parse(rest,
262 |> Keyword.update(:expires_at, {:ok, nil}, fn
264 val -> Date.from_iso8601(val)
270 with {:ok, val} <- options[:expires_at],
271 options = Map.put(options, :expires_at, val),
272 {:ok, invite} <- UserInviteToken.create_invite(options) do
273 shell_info("Generated user invite token " <> String.replace(invite.invite_type, "_", " "))
276 Pleroma.Web.Router.Helpers.redirect_url(
277 Pleroma.Web.Endpoint,
285 shell_error("Could not create invite token: #{inspect(error)}")
289 def run(["invites"]) do
292 shell_info("Invites list:")
294 UserInviteToken.list_invites()
295 |> Enum.each(fn invite ->
297 with expires_at when not is_nil(expires_at) <- invite.expires_at do
298 " | Expires at: #{Date.to_string(expires_at)}"
302 with max_use when not is_nil(max_use) <- invite.max_use do
303 " | Max use: #{max_use} Left use: #{max_use - invite.uses}"
307 "ID: #{invite.id} | Token: #{invite.token} | Token type: #{invite.invite_type} | Used: #{
309 }#{expire_info}#{using_info}"
314 def run(["revoke_invite", token]) do
317 with {:ok, invite} <- UserInviteToken.find_by_token(token),
318 {:ok, _} <- UserInviteToken.update_invite(invite, %{used: true}) do
319 shell_info("Invite for token #{token} was revoked.")
321 _ -> shell_error("No invite found with token #{token}")
325 def run(["delete_activities", nickname]) do
328 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
329 User.delete_user_activities(user)
330 shell_info("User #{nickname} statuses deleted.")
333 shell_error("No local user #{nickname}")
337 def run(["toggle_confirmed", nickname]) do
340 with %User{} = user <- User.get_cached_by_nickname(nickname) do
341 {:ok, user} = User.toggle_confirmation(user)
343 message = if user.info.confirmation_pending, do: "needs", else: "doesn't need"
345 shell_info("#{nickname} #{message} confirmation.")
348 shell_error("No local user #{nickname}")
352 def run(["sign_out", nickname]) do
355 with %User{local: true} = user <- User.get_cached_by_nickname(nickname) do
356 OAuth.Token.delete_user_tokens(user)
357 OAuth.Authorization.delete_user_authorizations(user)
359 shell_info("#{nickname} signed out from all apps.")
362 shell_error("No local user #{nickname}")
366 defp set_moderator(user, value) do
367 {:ok, user} = User.update_info(user, &User.Info.admin_api_update(&1, %{is_moderator: value}))
369 shell_info("Moderator status of #{user.nickname}: #{user.info.is_moderator}")
373 defp set_admin(user, value) do
374 {:ok, user} = User.update_info(user, &User.Info.admin_api_update(&1, %{is_admin: value}))
376 shell_info("Admin status of #{user.nickname}: #{user.info.is_admin}")
380 defp set_locked(user, value) do
381 {:ok, user} = User.update_info(user, &User.Info.user_upgrade(&1, %{locked: value}))
383 shell_info("Locked status of #{user.nickname}: #{user.info.locked}")