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 Pleroma.Web.AdminAPI.InviteController do
6 use Pleroma.Web, :controller
8 import Pleroma.Web.ControllerHelper, only: [json_response: 3]
11 alias Pleroma.UserInviteToken
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
16 plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
17 plug(OAuthScopesPlug, %{scopes: ["admin:read:invites"]} when action == :index)
21 %{scopes: ["admin:write:invites"]} when action in [:create, :revoke, :email]
24 action_fallback(Pleroma.Web.AdminAPI.FallbackController)
26 defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
28 @doc "Get list of created invites"
29 def index(conn, _params) do
30 invites = UserInviteToken.list_invites()
32 render(conn, "index.json", invites: invites)
35 @doc "Create an account registration invite token"
36 def create(%{private: %{open_api_spex: %{body_params: params}}} = conn, _) do
37 {:ok, invite} = UserInviteToken.create_invite(params)
39 render(conn, "show.json", invite: invite)
42 @doc "Revokes invite by token"
43 def revoke(%{private: %{open_api_spex: %{body_params: %{token: token}}}} = conn, _) do
44 with {:ok, invite} <- UserInviteToken.find_by_token(token),
45 {:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
46 render(conn, "show.json", invite: updated_invite)
48 nil -> {:error, :not_found}
53 @doc "Sends registration invite via email"
56 assigns: %{user: user},
57 private: %{open_api_spex: %{body_params: %{email: email} = params}}
61 with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
62 {_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
63 {:ok, invite_token} <- UserInviteToken.create_invite(),
66 |> Pleroma.Emails.UserEmail.user_invitation_email(
71 |> Pleroma.Emails.Mailer.deliver() do
72 json_response(conn, :no_content, "")
74 {:registrations_open, _} ->
75 {:error, "To send invites you need to set the `registrations_open` option to false."}
77 {:invites_enabled, _} ->
78 {:error, "To send invites you need to set the `invites_enabled` option to true."}