# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
-defmodule Pleroma.Web.AdminAPI.InviteTokenController do
+defmodule Pleroma.Web.AdminAPI.InviteController do
use Pleroma.Web, :controller
import Pleroma.Web.ControllerHelper, only: [json_response: 3]
alias Pleroma.Config
alias Pleroma.Plugs.OAuthScopesPlug
alias Pleroma.UserInviteToken
- alias Pleroma.Web.AdminAPI.AccountView
require Logger
action_fallback(Pleroma.Web.AdminAPI.FallbackController)
- defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteTokenOperation
+ defdelegate open_api_operation(action), to: Pleroma.Web.ApiSpec.Admin.InviteOperation
@doc "Get list of created invites"
def index(conn, _params) do
invites = UserInviteToken.list_invites()
- conn
- |> put_view(AccountView)
- |> render("invites.json", %{invites: invites})
+ render(conn, "index.json", invites: invites)
end
@doc "Create an account registration invite token"
def create(%{body_params: params} = conn, _) do
{:ok, invite} = UserInviteToken.create_invite(params)
- json(conn, AccountView.render("invite.json", %{invite: invite}))
+ render(conn, "show.json", invite: invite)
end
@doc "Revokes invite by token"
def revoke(%{body_params: %{token: token}} = conn, _) do
with {:ok, invite} <- UserInviteToken.find_by_token(token),
{:ok, updated_invite} = UserInviteToken.update_invite(invite, %{used: true}) do
- conn
- |> put_view(AccountView)
- |> render("invite.json", %{invite: updated_invite})
+ render(conn, "show.json", invite: updated_invite)
else
nil -> {:error, :not_found}
+ error -> error
end
end
with {_, false} <- {:registrations_open, Config.get([:instance, :registrations_open])},
{_, true} <- {:invites_enabled, Config.get([:instance, :invites_enabled])},
{:ok, invite_token} <- UserInviteToken.create_invite(),
- email <-
- Pleroma.Emails.UserEmail.user_invitation_email(
- user,
+ {:ok, _} <-
+ user
+ |> Pleroma.Emails.UserEmail.user_invitation_email(
invite_token,
email,
params[:name]
- ),
- {:ok, _} <- Pleroma.Emails.Mailer.deliver(email) do
+ )
+ |> Pleroma.Emails.Mailer.deliver() do
json_response(conn, :no_content, "")
else
{:registrations_open, _} ->
{:invites_enabled, _} ->
{:error, "To send invites you need to set the `invites_enabled` option to true."}
+
+ {:error, error} ->
+ {:error, error}
end
end
end
}
end
- def render("invite.json", %{invite: invite}) do
- %{
- "id" => invite.id,
- "token" => invite.token,
- "used" => invite.used,
- "expires_at" => invite.expires_at,
- "uses" => invite.uses,
- "max_use" => invite.max_use,
- "invite_type" => invite.invite_type
- }
- end
-
- def render("invites.json", %{invites: invites}) do
- %{
- invites: render_many(invites, AccountView, "invite.json", as: :invite)
- }
- end
-
def render("created.json", %{user: user}) do
%{
type: "success",
--- /dev/null
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.AdminAPI.InviteView do
+ use Pleroma.Web, :view
+
+ def render("index.json", %{invites: invites}) do
+ %{
+ invites: render_many(invites, __MODULE__, "show.json", as: :invite)
+ }
+ end
+
+ def render("show.json", %{invite: invite}) do
+ %{
+ "id" => invite.id,
+ "token" => invite.token,
+ "used" => invite.used,
+ "expires_at" => invite.expires_at,
+ "uses" => invite.uses,
+ "max_use" => invite.max_use,
+ "invite_type" => invite.invite_type
+ }
+ end
+end
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
-defmodule Pleroma.Web.ApiSpec.Admin.InviteTokenOperation do
+defmodule Pleroma.Web.ApiSpec.Admin.InviteOperation do
alias OpenApiSpex.Operation
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Schemas.ApiError
%Operation{
tags: ["Admin", "Invites"],
summary: "Get a list of generated invites",
- operationId: "AdminAPI.InviteTokenController.index",
+ operationId: "AdminAPI.InviteController.index",
security: [%{"oAuth" => ["read:invites"]}],
responses: %{
200 =>
%Operation{
tags: ["Admin", "Invites"],
summary: "Create an account registration invite token",
- operationId: "AdminAPI.InviteTokenController.create",
+ operationId: "AdminAPI.InviteController.create",
security: [%{"oAuth" => ["write:invites"]}],
requestBody:
request_body("Parameters", %Schema{
%Operation{
tags: ["Admin", "Invites"],
summary: "Revoke invite by token",
- operationId: "AdminAPI.InviteTokenController.revoke",
+ operationId: "AdminAPI.InviteController.revoke",
security: [%{"oAuth" => ["write:invites"]}],
requestBody:
request_body(
%Operation{
tags: ["Admin", "Invites"],
summary: "Sends registration invite via email",
- operationId: "AdminAPI.InviteTokenController.email",
+ operationId: "AdminAPI.InviteController.email",
security: [%{"oAuth" => ["write:invites"]}],
requestBody:
request_body(
post("/relay", AdminAPIController, :relay_follow)
delete("/relay", AdminAPIController, :relay_unfollow)
- post("/users/invite_token", InviteTokenController, :create)
- get("/users/invites", InviteTokenController, :index)
- post("/users/revoke_invite", InviteTokenController, :revoke)
- post("/users/email_invite", InviteTokenController, :email)
+ post("/users/invite_token", InviteController, :create)
+ get("/users/invites", InviteController, :index)
+ post("/users/revoke_invite", InviteController, :revoke)
+ post("/users/email_invite", InviteController, :email)
get("/users/:nickname/password_reset", AdminAPIController, :get_password_reset)
patch("/users/force_password_reset", AdminAPIController, :force_password_reset)
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
-defmodule Pleroma.Web.AdminAPI.InviteTokenControllerTest do
+defmodule Pleroma.Web.AdminAPI.InviteControllerTest do
use Pleroma.Web.ConnCase, async: true
import Pleroma.Factory