2020-05-26 09:13:39 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-05-26 09:13:39 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-05-26 11:21:33 +00:00
|
|
|
defmodule Pleroma.Web.AdminAPI.InviteController do
|
2020-05-26 09:13:39 +00:00
|
|
|
use Pleroma.Web, :controller
|
|
|
|
|
|
|
|
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
|
|
|
|
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.UserInviteToken
|
2020-06-24 10:07:47 +00:00
|
|
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
2020-05-26 09:13:39 +00:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2024-01-30 22:06:37 +00:00
|
|
|
plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
|
2021-02-17 18:37:23 +00:00
|
|
|
plug(OAuthScopesPlug, %{scopes: ["admin:read:invites"]} when action == :index)
|
2020-05-26 09:13:39 +00:00
|
|
|
|
|
|
|
plug(
|
|
|
|
OAuthScopesPlug,
|
2021-02-17 18:37:23 +00:00
|
|
|
%{scopes: ["admin:write:invites"]} when action in [:create, :revoke, :email]
|
2020-05-26 09:13:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
|
|
|
|
|
2020-05-26 11:21:33 +00:00
|
|
|
defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
|
2020-05-26 11:02:51 +00:00
|
|
|
|
2020-05-26 09:13:39 +00:00
|
|
|
@doc "Get list of created invites"
|
|
|
|
def index(conn, _params) do
|
|
|
|
invites = UserInviteToken.list_invites()
|
|
|
|
|
2020-05-26 11:21:33 +00:00
|
|
|
render(conn, "index.json", invites: invites)
|
2020-05-26 09:13:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "Create an account registration invite token"
|
2024-01-30 22:06:37 +00:00
|
|
|
def create(%{private: %{open_api_spex: %{body_params: params}}} = conn, _) do
|
2020-05-26 11:02:51 +00:00
|
|
|
{:ok, invite} = UserInviteToken.create_invite(params)
|
2020-05-26 09:13:39 +00:00
|
|
|
|
2020-05-26 11:21:33 +00:00
|
|
|
render(conn, "show.json", invite: invite)
|
2020-05-26 09:13:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc "Revokes invite by token"
|
2024-01-30 22:06:37 +00:00
|
|
|
def revoke(%{private: %{open_api_spex: %{body_params: %{token: token}}}} = conn, _) do
|
2020-05-26 09:13:39 +00:00
|
|
|
with {:ok, invite} <- UserInviteToken.find_by_token(token),
|
|
|
|
{:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
|
2020-05-26 11:21:33 +00:00
|
|
|
render(conn, "show.json", invite: updated_invite)
|
2020-05-26 09:13:39 +00:00
|
|
|
else
|
|
|
|
nil -> {:error, :not_found}
|
2020-05-26 11:21:33 +00:00
|
|
|
error -> error
|
2020-05-26 09:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc "Sends registration invite via email"
|
2024-01-30 22:06:37 +00:00
|
|
|
def email(
|
|
|
|
%{
|
|
|
|
assigns: %{user: user},
|
|
|
|
private: %{open_api_spex: %{body_params: %{email: email} = params}}
|
|
|
|
} = conn,
|
|
|
|
_
|
|
|
|
) do
|
2020-05-26 09:13:39 +00:00
|
|
|
with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
|
|
|
|
{_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
|
|
|
|
{:ok, invite_token} <- UserInviteToken.create_invite(),
|
2020-05-26 11:21:33 +00:00
|
|
|
{:ok, _} <-
|
|
|
|
user
|
|
|
|
|> Pleroma.Emails.UserEmail.user_invitation_email(
|
2020-05-26 09:13:39 +00:00
|
|
|
invite_token,
|
|
|
|
email,
|
2024-01-30 19:14:56 +00:00
|
|
|
params[:name]
|
2020-05-26 11:21:33 +00:00
|
|
|
)
|
|
|
|
|> Pleroma.Emails.Mailer.deliver() do
|
2020-05-26 09:13:39 +00:00
|
|
|
json_response(conn, :no_content, "")
|
|
|
|
else
|
|
|
|
{:registrations_open, _} ->
|
|
|
|
{:error, "To send invites you need to set the `registrations_open` option to false."}
|
|
|
|
|
|
|
|
{:invites_enabled, _} ->
|
|
|
|
{:error, "To send invites you need to set the `invites_enabled` option to true."}
|
2020-05-26 11:21:33 +00:00
|
|
|
|
|
|
|
{:error, error} ->
|
|
|
|
{:error, error}
|
2020-05-26 09:13:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|