Change :connection to :handle to match upstream nomenclature

This commit is contained in:
Mark Felder 2024-09-17 13:46:56 -04:00
parent 1d123832da
commit ea63533cf2

View file

@ -47,7 +47,7 @@ defmodule Pleroma.LDAP do
def handle_info(:connect, _state), do: do_handle_connect() def handle_info(:connect, _state), do: do_handle_connect()
def handle_info({:bind_after_reconnect, name, password, from}, state) do def handle_info({:bind_after_reconnect, name, password, from}, state) do
result = bind_user(state[:connection], name, password) result = bind_user(state[:handle], name, password)
GenServer.reply(from, result) GenServer.reply(from, result)
@ -57,10 +57,10 @@ defmodule Pleroma.LDAP do
defp do_handle_connect do defp do_handle_connect do
state = state =
case connect() do case connect() do
{:ok, connection} -> {:ok, handle} ->
:eldap.controlling_process(connection, self()) :eldap.controlling_process(handle, self())
Process.link(connection) Process.link(handle)
[connection: connection] [handle: handle]
_ -> _ ->
Logger.error("Failed to connect to LDAP. Retrying in 5000ms") Logger.error("Failed to connect to LDAP. Retrying in 5000ms")
@ -73,7 +73,7 @@ defmodule Pleroma.LDAP do
@impl true @impl true
def handle_call({:bind_user, name, password}, from, state) do def handle_call({:bind_user, name, password}, from, state) do
case bind_user(state[:connection], name, password) do case bind_user(state[:handle], name, password) do
:needs_reconnect -> :needs_reconnect ->
Process.send(self(), {:bind_after_reconnect, name, password, from}, []) Process.send(self(), {:bind_after_reconnect, name, password, from}, [])
{:noreply, state, {:continue, :connect}} {:noreply, state, {:continue, :connect}}
@ -85,10 +85,10 @@ defmodule Pleroma.LDAP do
@impl true @impl true
def terminate(_, state) do def terminate(_, state) do
connection = Keyword.get(state, :connection) handle = Keyword.get(state, :handle)
if not is_nil(connection) do if not is_nil(handle) do
:eldap.close(connection) :eldap.close(handle)
end end
:ok :ok
@ -127,25 +127,25 @@ defmodule Pleroma.LDAP do
end end
case :eldap.open([to_charlist(host)], options) do case :eldap.open([to_charlist(host)], options) do
{:ok, connection} -> {:ok, handle} ->
try do try do
cond do cond do
tls -> tls ->
case :eldap.start_tls( case :eldap.start_tls(
connection, handle,
tlsopts, tlsopts,
@connection_timeout @connection_timeout
) do ) do
:ok -> :ok ->
{:ok, connection} {:ok, handle}
error -> error ->
Logger.error("Could not start TLS: #{inspect(error)}") Logger.error("Could not start TLS: #{inspect(error)}")
:eldap.close(connection) :eldap.close(handle)
end end
true -> true ->
{:ok, connection} {:ok, handle}
end end
after after
:ok :ok
@ -157,24 +157,24 @@ defmodule Pleroma.LDAP do
end end
end end
defp bind_user(connection, name, password) do defp bind_user(handle, name, password) do
uid = Config.get([:ldap, :uid], "cn") uid = Config.get([:ldap, :uid], "cn")
base = Config.get([:ldap, :base]) base = Config.get([:ldap, :base])
case :eldap.simple_bind(connection, "#{uid}=#{name},#{base}", password) do case :eldap.simple_bind(handle, "#{uid}=#{name},#{base}", password) do
:ok -> :ok ->
case fetch_user(name) do case fetch_user(name) do
%User{} = user -> %User{} = user ->
user user
_ -> _ ->
register_user(connection, base, uid, name) register_user(handle, base, uid, name)
end end
# eldap does not inform us of socket closure # eldap does not inform us of socket closure
# until it is used # until it is used
{:error, {:gen_tcp_error, :closed}} -> {:error, {:gen_tcp_error, :closed}} ->
:eldap.close(connection) :eldap.close(handle)
:needs_reconnect :needs_reconnect
error -> error ->
@ -183,8 +183,8 @@ defmodule Pleroma.LDAP do
end end
end end
defp register_user(connection, base, uid, name) do defp register_user(handle, base, uid, name) do
case :eldap.search(connection, [ case :eldap.search(handle, [
{:base, to_charlist(base)}, {:base, to_charlist(base)},
{:filter, :eldap.equalityMatch(to_charlist(uid), to_charlist(name))}, {:filter, :eldap.equalityMatch(to_charlist(uid), to_charlist(name))},
{:scope, :eldap.wholeSubtree()}, {:scope, :eldap.wholeSubtree()},