Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

79 рядки
2.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.AdminAPI.InviteController do
  5. use Pleroma.Web, :controller
  6. import Pleroma.Web.ControllerHelper, only: [json_response: 3]
  7. alias Pleroma.Config
  8. alias Pleroma.UserInviteToken
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. require Logger
  11. plug(Pleroma.Web.ApiSpec.CastAndValidate)
  12. plug(OAuthScopesPlug, %{scopes: ["read:invites"], admin: true} when action == :index)
  13. plug(
  14. OAuthScopesPlug,
  15. %{scopes: ["write:invites"], admin: true} when action in [:create, :revoke, :email]
  16. )
  17. action_fallback(Pleroma.Web.AdminAPI.FallbackController)
  18. defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
  19. @doc "Get list of created invites"
  20. def index(conn, _params) do
  21. invites = UserInviteToken.list_invites()
  22. render(conn, "index.json", invites: invites)
  23. end
  24. @doc "Create an account registration invite token"
  25. def create(%{body_params: params} = conn, _) do
  26. {:ok, invite} = UserInviteToken.create_invite(params)
  27. render(conn, "show.json", invite: invite)
  28. end
  29. @doc "Revokes invite by token"
  30. def revoke(%{body_params: %{token: token}} = conn, _) do
  31. with {:ok, invite} <- UserInviteToken.find_by_token(token),
  32. {:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
  33. render(conn, "show.json", invite: updated_invite)
  34. else
  35. nil -> {:error, :not_found}
  36. error -> error
  37. end
  38. end
  39. @doc "Sends registration invite via email"
  40. def email(%{assigns: %{user: user}, body_params: %{email: email} = params} = conn, _) do
  41. with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
  42. {_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
  43. {:ok, invite_token} <- UserInviteToken.create_invite(),
  44. {:ok, _} <-
  45. user
  46. |> Pleroma.Emails.UserEmail.user_invitation_email(
  47. invite_token,
  48. email,
  49. params[:name]
  50. )
  51. |> Pleroma.Emails.Mailer.deliver() do
  52. json_response(conn, :no_content, "")
  53. else
  54. {:registrations_open, _} ->
  55. {:error, "To send invites you need to set the `registrations_open` option to false."}
  56. {:invites_enabled, _} ->
  57. {:error, "To send invites you need to set the `invites_enabled` option to true."}
  58. {:error, error} ->
  59. {:error, error}
  60. end
  61. end
  62. end