spc-pleroma/lib/pleroma/web/admin_api/controllers/invite_controller.ex

79 lines
2.5 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
2020-05-26 11:21:33 +00:00
defmodule Pleroma.Web.AdminAPI.InviteController do
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
require Logger
plug(Pleroma.Web.ApiSpec.CastAndValidate)
plug(OAuthScopesPlug, %{scopes: ["admin:read:invites"]} when action == :index)
plug(
OAuthScopesPlug,
%{scopes: ["admin:write:invites"]} when action in [:create, :revoke, :email]
)
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
@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)
end
@doc "Create an account registration invite token"
def create(%{body_params: params} = conn, _) do
{:ok, invite} = UserInviteToken.create_invite(params)
2020-05-26 11:21:33 +00:00
render(conn, "show.json", invite: invite)
end
@doc "Revokes invite by token"
Pleroma.Web.AdminAPI.InviteController: dialyzer errors lib/pleroma/web/admin_api/controllers/invite_controller.ex:46:call The function call will not succeed. Phoenix.Controller.render( _conn :: %{:body_params => %{:token => _, _ => _}, _ => _}, <<115, 104, 111, 119, 46, 106, 115, 111, 110>>, [ {:invite, %Pleroma.UserInviteToken{ :__meta__ => _, :expires_at => _, :id => _, :inserted_at => _, :invite_type => _, :max_use => _, :token => _, :updated_at => _, :used => _, :uses => _ }}, ... ] ) will never return since the success typing is: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | binary(), atom() | binary() | [{_, _}] | map() ) :: %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :sent, :status => nil | non_neg_integer() } and the contract is (Plug.Conn.t(), binary() | atom(), Keyword.t() | map()) :: Plug.Conn.t() ________________________________________________________________________________ lib/pleroma/web/admin_api/controllers/invite_controller.ex:66:call The function call will not succeed. Pleroma.Web.ControllerHelper.json_response( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:email => _, _ => _}, _ => _ }, :no_content, <<>> ) will never return since the 1st arguments differ from the success typing arguments: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{ binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{binary(), binary()}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{binary(), binary()}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | 1..1_114_111, any() )
2024-01-28 20:48:49 +00:00
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
2020-05-26 11:21:33 +00:00
render(conn, "show.json", invite: updated_invite)
else
nil -> {:error, :not_found}
2020-05-26 11:21:33 +00:00
error -> error
end
end
@doc "Sends registration invite via email"
Pleroma.Web.AdminAPI.InviteController: dialyzer errors lib/pleroma/web/admin_api/controllers/invite_controller.ex:46:call The function call will not succeed. Phoenix.Controller.render( _conn :: %{:body_params => %{:token => _, _ => _}, _ => _}, <<115, 104, 111, 119, 46, 106, 115, 111, 110>>, [ {:invite, %Pleroma.UserInviteToken{ :__meta__ => _, :expires_at => _, :id => _, :inserted_at => _, :invite_type => _, :max_use => _, :token => _, :updated_at => _, :used => _, :uses => _ }}, ... ] ) will never return since the success typing is: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | binary(), atom() | binary() | [{_, _}] | map() ) :: %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :sent, :status => nil | non_neg_integer() } and the contract is (Plug.Conn.t(), binary() | atom(), Keyword.t() | map()) :: Plug.Conn.t() ________________________________________________________________________________ lib/pleroma/web/admin_api/controllers/invite_controller.ex:66:call The function call will not succeed. Pleroma.Web.ControllerHelper.json_response( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:email => _, _ => _}, _ => _ }, :no_content, <<>> ) will never return since the 1st arguments differ from the success typing arguments: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{ binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{binary(), binary()}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{binary(), binary()}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | 1..1_114_111, any() )
2024-01-28 20:48:49 +00:00
def email(%{assigns: %{user: user}, body_params: %{"email" => email} = params} = conn, _) do
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(
invite_token,
email,
Pleroma.Web.AdminAPI.InviteController: dialyzer errors lib/pleroma/web/admin_api/controllers/invite_controller.ex:46:call The function call will not succeed. Phoenix.Controller.render( _conn :: %{:body_params => %{:token => _, _ => _}, _ => _}, <<115, 104, 111, 119, 46, 106, 115, 111, 110>>, [ {:invite, %Pleroma.UserInviteToken{ :__meta__ => _, :expires_at => _, :id => _, :inserted_at => _, :invite_type => _, :max_use => _, :token => _, :updated_at => _, :used => _, :uses => _ }}, ... ] ) will never return since the success typing is: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | binary(), atom() | binary() | [{_, _}] | map() ) :: %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{binary() => binary() | [any()] | map()}, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [any()] | map() }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{_, _}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{_, _}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :sent, :status => nil | non_neg_integer() } and the contract is (Plug.Conn.t(), binary() | atom(), Keyword.t() | map()) :: Plug.Conn.t() ________________________________________________________________________________ lib/pleroma/web/admin_api/controllers/invite_controller.ex:66:call The function call will not succeed. Pleroma.Web.ControllerHelper.json_response( _conn :: %{ :assigns => %{:user => _, _ => _}, :body_params => %{:email => _, _ => _}, _ => _ }, :no_content, <<>> ) will never return since the 1st arguments differ from the success typing arguments: ( %Plug.Conn{ :adapter => {atom(), _}, :assigns => %{atom() => _}, :body_params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :halted => boolean(), :host => binary(), :method => binary(), :owner => pid(), :params => %Plug.Conn.Unfetched{:aspect => atom(), binary() => _}, :path_info => [binary()], :path_params => %{ binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :port => char(), :private => %{atom() => _}, :query_params => %Plug.Conn.Unfetched{ :aspect => atom(), binary() => binary() | [binary() | [any()] | map()] | %{binary() => binary() | [any()] | map()} }, :query_string => binary(), :remote_ip => {byte(), byte(), byte(), byte()} | {char(), char(), char(), char(), char(), char(), char(), char()}, :req_cookies => %Plug.Conn.Unfetched{:aspect => atom(), binary() => binary()}, :req_headers => [{binary(), binary()}], :request_path => binary(), :resp_body => nil | binary() | maybe_improper_list( binary() | maybe_improper_list(any(), binary() | []) | byte(), binary() | [] ), :resp_cookies => %{binary() => map()}, :resp_headers => [{binary(), binary()}], :scheme => :http | :https, :script_name => [binary()], :secret_key_base => nil | binary(), :state => :chunked | :file | :sent | :set | :set_chunked | :set_file | :unset | :upgraded, :status => nil | non_neg_integer() }, atom() | 1..1_114_111, any() )
2024-01-28 20:48:49 +00:00
params["name"]
2020-05-26 11:21:33 +00:00
)
|> Pleroma.Emails.Mailer.deliver() do
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}
end
end
end