logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

invite_controller.ex (2727B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 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, replace_params: false)
  12. plug(OAuthScopesPlug, %{scopes: ["admin:read:invites"]} when action == :index)
  13. plug(
  14. OAuthScopesPlug,
  15. %{scopes: ["admin:write:invites"]} 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(%{private: %{open_api_spex: %{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(%{private: %{open_api_spex: %{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(
  41. %{
  42. assigns: %{user: user},
  43. private: %{open_api_spex: %{body_params: %{email: email} = params}}
  44. } = conn,
  45. _
  46. ) do
  47. with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
  48. {_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
  49. {:ok, invite_token} <- UserInviteToken.create_invite(),
  50. {:ok, _} <-
  51. user
  52. |> Pleroma.Emails.UserEmail.user_invitation_email(
  53. invite_token,
  54. email,
  55. params[:name]
  56. )
  57. |> Pleroma.Emails.Mailer.deliver() do
  58. json_response(conn, :no_content, "")
  59. else
  60. {:registrations_open, _} ->
  61. {:error, "To send invites you need to set the `registrations_open` option to false."}
  62. {:invites_enabled, _} ->
  63. {:error, "To send invites you need to set the `invites_enabled` option to true."}
  64. {:error, error} ->
  65. {:error, error}
  66. end
  67. end
  68. end